[Bug]: Written file corrupted via Storage Access Framework on Android 11
Problem description
I'm trying to access files in termux's private storage via SAF. Currently I'm using an Oreo(8.1) device, and can read or write files in termux using my Hyperpad(just another notepad clone using SAF) with no problem. But things got tricky on Android 11. I tried to do the same thing in an Android 11 AVD, at first I created a file successfully, but when I removed some words from that file, it got corrupted(see reproduction). I not only reproduced this, but also found that several DocumentProviders including External Storage (shipped with system), Nutstore, ianhanniballake/LocalStorage have the same problem, except Google Drive. I wonder why there's not so many people noticed this problem since Android 11 came out -- but there's some people created issues about malfunctions of my apps, which corrupts files on Android 11.
Steps to reproduce the behavior.
- Get Hyperpad or any other text editor with SAF support
- Create a file in termux's home directory via SAF
- type
%%%%%%#####@@@@@@&&and save then close the file - Reopen the file. It has correct content.
- Delete all '#'s in the file, save and close
- Reopen the file. The content is expected to be
%%%%%%@@@@@@&&, which is what happened on my Android 8.1 phone, but on Android 11, I got%%%%%%@@@@@@&&@@@&&. Obviously the file is not shrinked as expected.
What is the expected behavior?
No response
System information
- Termux application version: Latest on F-Droid
- Android OS version: 11
- Device model: AVD of Android Studio
i had, in my termux time on this phone in bash gawk multifiles to bash code projects twice one longer script was mangled to one line
.. i dunno, just reporting..
I'm currently looking into something that might be related if not the same. The storage changes on Android 11+ are not integrated in Termux and that could yield various issues since those changes are drastic.
I didn't spend a lot of development time though on testing older SDK targets that way but those storage changes offer problems for the older storage code as Android changed to other things on 11+. Even the older permission check style gave me problems.
It wouldn't surprise me at all if its related to the older code and the newer storage API changes based on what I've seen.
Edit - Unless I find something happens on Android 8 - as logcat entries were already gone by the time I got to it on Android 12 - then at this time I'm moving on as I'm not spending further time on it.
I have similar issues, with git repos on internal 'sdcard' storage, similar to this person: https://www.reddit.com/r/termux/comments/l6lsm6/why_does_my_git_repo_keep_messing_up_its_hash/
It happens pretty often, mostly during a commit, sometimes simply when pulling.