termux / termux-app

Termux - a terminal emulator application for Android OS extendible by variety of packages.
https://f-droid.org/en/packages/com.termux
Other
35.34k stars 3.72k forks source link

Termux can't write to external SD card #1886

Closed rptb1 closed 3 years ago

rptb1 commented 3 years ago

Problem description

I have a Fairphone 3 with an SD card slot, and I have installed an SD card with a single FAT partition formatted by the phone. termux-setup-storage asks for permission to access /storage/emulated/0, and that all works, but it does not ask for permission to access /storage/ID and Termux can't write to any files there.

By contrast, Resilio Sync (also on SDK 28) explicitly asks to be granted permission to the SD card and after doing so, is able to write there.

Screenshot_20210114-110456_Sync

Note that my Termux has been granted the "Storage" permission in the apps settings. This is a separate permission.

I have read Internal and external storage but this is not about /storage/emulated/0, which is on my phone's internal flash memory.

Steps to reproduce

  1. termux-setup-storage
  2. cd /storage/ID/Download (substitute the id of your SD card's FAT FS)
  3. echo foo > bar gets a permission denied message

Expected behavior I want to be able to work on files on the SD card using Termux.

Perhaps termux-setup-storage needs to ask for more permission.

Additional information

rptb1 commented 3 years ago

This may not be an app permissions issue, but something to do with filesystem permissions or the way bind mounts and sdcardfs mounts are set up. Here's some evidence:

.../FB03-5D01/Download $ sudo sh -c "echo foo > bar"
.../FB03-5D01/Download $ cat bar
foo
.../FB03-5D01/Download $ df .
Filesystem              1K-blocks    Used Available Use% Mounted on
/mnt/media_rw/FB03-5D01 104831968 1041216 103790752   1% /storage/FB03-5D01
.../FB03-5D01/Download $ mount | grep FB03
/dev/block/vold/public:179,65 on /mnt/media_rw/FB03-5D01 type vfat (rw,dirsync,nosuid,nodev,noexec,noatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro)
/mnt/media_rw/FB03-5D01 on /mnt/runtime/default/FB03-5D01 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=1015,mask=6)
/mnt/media_rw/FB03-5D01 on /mnt/runtime/read/FB03-5D01 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=9997,mask=18)
/mnt/media_rw/FB03-5D01 on /mnt/runtime/write/FB03-5D01 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=9997,mask=18)
/mnt/media_rw/FB03-5D01 on /mnt/runtime/full/FB03-5D01 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=9997,mask=7)
/mnt/media_rw/FB03-5D01 on /storage/FB03-5D01 type sdcardfs (rw,nosuid,nodev,noexec,noatime,fsuid=1023,fsgid=1023,gid=9997,mask=18)
.../FB03-5D01/Download $ ls -ld .
drwxr-xr-x 2 root everybody 32768 Jan 14 11:41 .
.../FB03-5D01/Download $ su
:/storage/FB03-5D01/Download # ls -ld /mnt/runtime/*/FB03-5D01/Download
drwxrwx--x 2 root sdcard_rw 32768 2021-01-14 11:41 /mnt/runtime/default/FB03-5D01/Download
drwxrwx--- 2 root everybody 32768 2021-01-14 11:41 /mnt/runtime/full/FB03-5D01/Download
drwxr-xr-x 2 root everybody 32768 2021-01-14 11:41 /mnt/runtime/read/FB03-5D01/Download
drwxr-xr-x 2 root everybody 32768 2021-01-14 11:41 /mnt/runtime/write/FB03-5D01/Download
Grimler91 commented 3 years ago

Duplicate of https://github.com/termux/termux-app/issues/20, https://github.com/termux/termux-app/issues/1683 (and some other issues).

It is currently only possible to write to termux's folder on the external storage, /storage/FB03-5D01/Android/data/com.termux/files/ (i.e. ~/storage/external-1). Might be possible to make it work for android <= 10, but requires some work, discussed in https://github.com/termux/termux-app/issues/1617, https://github.com/termux/termux-packages/issues/5486