#####################################################################
# This file demos Termux permissions, before and after storage setup.
# It was run on Android Oreo, using a Samsung Galaxy Note 9 device.
# In this test:
#     /storage/6C2A-1618 is the phone's removable SD card
#     /sdcard is the phone's internal storage 
#     /data/data is the app's private storage
#####################################################################

 
#====================================================================
# BEFORE termux-setup-storage is run
#
# With existing Android/data/com.termux in both internal storage
# and SD card, and existing text files at various locations.
#====================================================================

Welcome to Termux!


# SHELL access

$ ls /data/data/com.termux
cache         code_cache    files         shared_prefs
$ ls /sdcard/work
ls: can't open '/sdcard/work': Permission denied
$ ls /storage/6C2A-1618
ls: can't open '/storage/6C2A-1618': Permission denied

$ ls /storage/6C2A-1618/Android/data/com.termux
MY-STUFF      TEST          files
Sheets-temp   _experiments  test.txt
$ ls /sdcard/Android/data/com.termux
test.txt

$ cat /sdcard/test.txt
cat: can't open '/sdcard/test.txt': Permission denied
$ cat /storage/6C2A-1618/test.txt
cat: can't open '/storage/6C2A-1618/test.txt': Permission denied

$ cat /sdcard/Android/data/com.termux/test.txt
At Termux nested app folder in internal storage
$ cat /storage/6C2A-1618/Android/data/com.termux/test.txt
xxx


# PYTHON access

$ pkg install python
$ python
Python 3.7.2 (default, Jan 16 2019, 21:05:09)
...
>>> open('/data/data/com.termux/xxx.txt', 'w').write('1234')
4
>>> open('/sdcard/xxx.txt', 'w').write('1234')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/sdcard/xxx.txt'
>>>
>>> open('/storage/6C2A-1618/xxx.txt', 'w').write('1234')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/6C2A-1618/xxx.txt'
>>>
>>> open('/sdcard/Android/data/com.termux/xxx.txt', 'w').write('1234')
4
>>> open('/storage/6C2A-1618/Android/data/com.termux/xxx.txt', 'w').write('1234')
4


# WHEN the app-specific folder on SD has been removed (or doesn't yet exist)

>>> open('/storage/6C2A-1618/Android/data/com.termux/xxx.txt', 'w').write('1234')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/storage/6C2A-1618/Android/data/com.termux/xxx.txt'


# READ access has the same constraints

>>> open('/storage/6C2A-1618/test.txt', 'r').read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/6C2A-1618/test.txt'
>>>
>>> open('/storage/6C2A-1618/Android/data/com.termux/test.txt', 'r').read()
'xxx'

>>> open('/sdcard/test.txt', 'r').read() 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/sdcard/test.txt'
>>>
>>> open('/sdcard/Android/data/com.termux/test.txt', 'r').read()
'At Termux nested app folder in internal storage'


#====================================================================
# AFTER termux-setup-storage is run
#
# Nearly everything is available _except_ updates outside the 
# app-specific folder on removable drives (hence required nesting).
#====================================================================

$ termux-setup-storage


# SHELL access

$ ls /data/data/com.termux
cache         files         xxx.txt
code_cache    shared_prefs

$ ls /sdcard/work
Admin-Mergeall
Mergeall-source
Mergeall-source.zip
ad-hoc
code
mergeall-android-scripts
mergeall-android-scripts.zip

$ ls /storage/6C2A-1618
Admin-Mergeall
Android
DCIM
LOST.DIR
MY-STUFF
TEST
alsoalso-added-on-android.xlsx
my-OTHER-STUFF
test.txt

$ ls /storage/6C2A-1618/Android/data/com.termux
MY-STUFF      TEST          files         xxx.txt
Sheets-temp   _experiments  test.txt

$ ls /sdcard/Android/data/com.termux
files     test.txt  xxx.txt

$ cat /sdcard/test.txt
On Internal Storage root
$ cat /storage/6C2A-1618/test.txt
On SD root
$ cat /storage/6C2A-1618/Android/data/com.termux/test.txt
xxx
$ cat /sdcard/Android/data/com.termux/test.txt
At Termux nested app folder in internal storage


# PYTHON access

$ python 
Python 3.7.2 (default, Jan 16 2019, 21:05:09)
...
>>> open('/data/data/com.termux/xxx.txt', 'w').write('1234')
4
>>> open('/sdcard/test.txt', 'w').write('1234') 
4
>>> open('/sdcard/work/test.txt', 'w').write('1234')
4

>>> open('/storage/6C2A-1618/test.txt', 'r').read()      
'On SD root'
>>> open('/storage/6C2A-1618/xxx.txt', 'w').write('1234')          # <== THE KILLER JOKE...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/6C2A-1618/xxx.txt'

>>> open('/storage/6C2A-1618/Android/data/com.termux/xxx.txt', 'w').write('1234')
4
>>> open('/sdcard/Android/data/com.termux/xxx.txt', 'w').write('1234')
4
