Repository: kivy/python-for-android

Issue:
tarfile failure with long user ID
Note this is a follow-on from the same effort which produced #1012, but I think the bugs are independent.  I switched to Python 3 and the CrystaxNDK in an attempt to avoid NDK build errors, and got far enough to run into this.

While the dist's build.py is building its tar archive, I get `ValueError("overflow in number field")`:

```
Traceback (most recent call last):
  File "/usr/local/bin/p4a", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/site-packages/pythonforandroid/toolchain.py", line 754, in main
    ToolchainCL()
  File "/usr/local/lib/python3.5/site-packages/pythonforandroid/toolchain.py", line 343, in __init__
    getattr(self, command_method_name)(unknown)
  File "/usr/local/lib/python3.5/site-packages/pythonforandroid/toolchain.py", line 94, in wrapper_func
    func(self, args)
  File "/usr/local/lib/python3.5/site-packages/pythonforandroid/toolchain.py", line 589, in apk
    build_args = build.parse_args(args)
  File "/Users/testuser/.python-for-android/dists/unnamed_dist_1/build.py", line 488, in parse_args
    make_package(args)
  File "/Users/testuser/.python-for-android/dists/unnamed_dist_1/build.py", line 248, in make_package
    make_tar('assets/private.mp3', tar_dirs, args.ignore_path)
  File "/Users/testuser/.python-for-android/dists/unnamed_dist_1/build.py", line 197, in make_tar
    tf.add(fn, afn)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tarfile.py", line 1938, in add
    self.addfile(tarinfo, f)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tarfile.py", line 1960, in addfile
    buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tarfile.py", line 802, in tobuf
    return self.create_ustar_header(info, encoding, errors)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tarfile.py", line 821, in create_ustar_header
    return self._create_header(info, USTAR_FORMAT, encoding, errors)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tarfile.py", line 916, in _create_header
    itn(info.get("uid", 0), 8, format),
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tarfile.py", line 211, in itn
    raise ValueError("overflow in number field")
ValueError: overflow in number field
```

It seems the overflow is in the "uid" field, and I notice that my UID on my Mac (part of a corporate domain) is pretty high at 1730938684.  Looking at the package in [`tarfile.py`](https://github.com/python/cpython/blob/master/Lib/tarfile.py#L218) it seems this is a limitation of the USTAR format which the python-4-android builder is selecting specifically.  The default GNU format can support much larger numbers, though I presume the unpacking code used in the Android package might not support it?

I was able to work around this problem by running as a different local user account, so long as that account owns both the .python-for-android directory and the source input directory.

Since UID information isn't relevant when unpacking on Android it would be nice to discard it and make this not matter, if using the GNU tar format isn't an option.  It doesn't look like the tarfile package gives the ability to do this directly, but you could chmod all the files to root (in a temporary location) before compressing them.



Discussion:
Similar to the OP, I ran into the same issue.  Also running in an user account (managed via corporate AD system), so I have a very high uid.  I can confirm the same issue occurs under Linux (64-bit Ubuntu 16.04) for both Python 2.7.12 and 3.5.2: 

```
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/build/dists/weather/build.py", line 543, in parse_args
    make_package(args)
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/build/dists/weather/build.py", line 255, in make_package
    make_tar('assets/private.mp3', tar_dirs, args.ignore_path)
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/build/dists/weather/build.py", line 201, in make_tar
    tf.add(fn, afn)
  File "/usr/lib/python2.7/tarfile.py", line 2023, in add
    self.addfile(tarinfo, f)
  File "/usr/lib/python2.7/tarfile.py", line 2046, in addfile
    buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
  File "/usr/lib/python2.7/tarfile.py", line 1006, in tobuf
    return self.create_ustar_header(info)
  File "/usr/lib/python2.7/tarfile.py", line 1025, in create_ustar_header
    return self._create_header(info, USTAR_FORMAT)
  File "/usr/lib/python2.7/tarfile.py", line 1121, in _create_header
    itn(info.get("uid", 0), 8, format),
  File "/usr/lib/python2.7/tarfile.py", line 212, in itn
    raise ValueError("overflow in number field")
ValueError: overflow in number field
# Command failed: python -m pythonforandroid.toolchain apk --debug --bootstrap=sdl2 --dist_name weather --name Weather --version 0.1 --package org.test.weather --android_api 19 --minsdk 9 --private /home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/app --permission INTERNET --orientation landscape --copy-libs --arch armeabi-v7a --color=always --storage-dir=/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/build
```
And Python 3.5.2:
```
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/python-for-android-master/pythonforandroid/toolchain.py", line 903, in <module>
    main()
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/python-for-android-master/pythonforandroid/toolchain.py", line 900, in main
    ToolchainCL()
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/python-for-android-master/pythonforandroid/toolchain.py", line 508, in __init__
    getattr(self, args.subparser_name.replace('-', '_'))(args)
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/python-for-android-master/pythonforandroid/toolchain.py", line 148, in wrapper_func
    func(self, args)
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/python-for-android-master/pythonforandroid/toolchain.py", line 744, in apk
    build_args = build.parse_args(args.unknown_args)
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/build/dists/weather/build.py", line 543, in parse_args
    make_package(args)
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/build/dists/weather/build.py", line 255, in make_package
    make_tar('assets/private.mp3', tar_dirs, args.ignore_path)
  File "/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/build/dists/weather/build.py", line 201, in make_tar
    tf.add(fn, afn)
  File "/home/dorian.pula/.virtualenvs/doric/lib/python3.5/tarfile.py", line 1938, in add
    self.addfile(tarinfo, f)
  File "/home/dorian.pula/.virtualenvs/doric/lib/python3.5/tarfile.py", line 1960, in addfile
    buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
  File "/home/dorian.pula/.virtualenvs/doric/lib/python3.5/tarfile.py", line 802, in tobuf
    return self.create_ustar_header(info, encoding, errors)
  File "/home/dorian.pula/.virtualenvs/doric/lib/python3.5/tarfile.py", line 821, in create_ustar_header
    return self._create_header(info, USTAR_FORMAT, encoding, errors)
  File "/home/dorian.pula/.virtualenvs/doric/lib/python3.5/tarfile.py", line 916, in _create_header
    itn(info.get("uid", 0), 8, format),
  File "/home/dorian.pula/.virtualenvs/doric/lib/python3.5/tarfile.py", line 211, in itn
    raise ValueError("overflow in number field")
ValueError: overflow in number field
# Command failed: python -m pythonforandroid.toolchain apk --bootstrap=sdl2 --dist_name weather --name Weather --version 0.1 --package org.test.weather --android_api 19 --minsdk 9 --private /home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/app --permission INTERNET --orientation landscape --copy-libs --arch armeabi-v7a --color=always --storage-dir=/home/dorian.pula/colonia/hackers-academy/python/kivy/weather_app/.buildozer/android/platform/build
```
I did a bit of research and [Android having busybox should handle GNU style tar files](https://forum.xda-developers.com/showthread.php?t=872438)

I manually changed the generated `build.py` to use the GNU style format on line 181

```
- tf = tarfile.open(tfn, 'w:gz', format=tarfile.USTAR_FORMAT)
+ tf = tarfile.open(tfn, 'w:gz', format=tarfile.GNU_FORMAT)
``` 
and I was able to build and run the package using `buildozer android_new debug deploy run`.
Should be fixed by #1015
Reopening as I found another issue with the above PR and had to revert it.
Looking forward solving this issue, as i suffered also becouse of it.  Manual modification as [suggested](https://github.com/kivy/python-for-android/issues/1013#issuecomment-282204357) helped.

Resolving patch:
diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py
--- a/pythonforandroid/bootstraps/common/build/build.py
+++ b/pythonforandroid/bootstraps/common/build/build.py
@@ -201,6 +201,7 @@ def clean(tinfo):
                 dirs.append(d)
                 tinfo = tarfile.TarInfo(d)
                 tinfo.type = tarfile.DIRTYPE
+                clean(tinfo)
                 tf.addfile(tinfo)
 
         # put the file


Test patch: