feat(saf): implement renameDocument and fix SAF capability flags #4867
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Many external editors and file managers (such as ZArchiver, QuickEdit, Acode, etc.) fail to save modifications to files stored in Termux when accessing them via SAF (Storage Access Framework).
This happens because many of these apps rely on an "atomic write" strategy (write to temp file -> delete original -> rename temp to original). Since
TermuxDocumentsProviderwas not advertisingFLAG_SUPPORTS_RENAMEnor implementing therenameDocumentmethod, these operations failed silently or were disabled in the client apps.Changes
renameDocument: Added the required logic to rename files usingFile.renameTo, including checks for destination existence.includeFileto correctly advertiseFLAG_SUPPORTS_RENAMEandFLAG_SUPPORTS_DELETEwhen the parent directory is writable.file.getParentFile()inincludeFileto prevent a potential crash when querying root directories.createDocument: Refactored file creation to correctly usenew File(parent, name)instead of potentially unsafe path concatenation.Testing
I verified the logic against Android
DocumentsContractspecifications.Note: Due to current environment limitations (coding directly on mobile), I was unable to build the full Termux APK locally. However, these changes are standard implementation requirements for fully compliant DocumentProviders.