Modern Apps git

← Modern-Apps.git

files: permanent delete no longer follows directory symlinks out of the tree

Commit
5a665502eee1b544f13b936d2496a9908d4e0b9b
Author
Vayun Mathur <[email protected]>
When
2026-12-01 14:10 UTC (4 days ago)
Parents
d50bbb3
The browser lists a directory symlink as a folder
(isDirectory follows links) and permanent delete used
deleteRecursively โ€” walkBottomUp also follows isDirectory,
so deleting a folder containing a symlink to an outside
directory deleted the TARGET's contents too (verified
against the project's stdlib: keep.txt outside the tree was
gone after the delete). The user confirmed deleting one
tree; everything the link pointed at vanished with it.

confirmPendingPermanentDelete now uses
deleteTreeWithoutFollowingLinks: a symlink is deleted as a
link at every level, real directories recurse, targets are
never entered. Five regression tests (plain tree, dir-link
inside a folder, link-as-item, file-link, end-to-end through
the ViewModel). Copy/move, search and archive paths were
audited and cleared. Verified: :files:testDevUnitTest 130
tests pass; lint errors are pre-existing on untouched files.

Diff

files/src/main/java/com/vayunmathur/files/platform/FilesTrash.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesTrash.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesTrash.ktindex 3e9d0ecec9..41a07a2a5d 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesTrash.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesTrash.kt@@ -5,6 +5,7 @@ import android.content.Context import android.net.Uri import android.provider.MediaStore import java.io.File+import java.nio.file.Files  /**  * System-trash (MediaStore) helpers behind [FilesViewModel]'s trash-first delete.@@ -14,13 +15,38 @@ import java.io.File  * Only MediaStore-indexed files resolve to trashable Uris; directories and unindexed files  * cannot be trashed and fall back to a permanent-delete confirmation.  *- * Implementation lives in `FilesTrashModel.kt`; this file keeps only the stateless queries- * so the behaviour layer stays under the function cap.+ * Implementation lives in `FilesTrashModel.kt`; this file keeps only the stateless+ * helpers so the behaviour layer stays under the function cap.  */  private const val TRASH_QUERY_LIMIT = 2000 private const val MILLIS_PER_SECOND = 1000L +/**+ * Recursively deletes this file or directory tree without following symlinks.+ *+ * [File.deleteRecursively] walks with `isDirectory`, which follows a symlink to+ * a directory, so permanently deleting a folder that merely contains such a+ * link โ€” or the link itself, which the browser also shows as a folder โ€” would+ * delete the link target's contents outside the deleted tree. A symlink is+ * therefore deleted as a link only, at every level of the tree; everything the+ * link points at is left exactly as it was. Returns true only when every entry+ * in the tree was deleted.+ */+internal fun File.deleteTreeWithoutFollowingLinks(): Boolean {+    if (Files.isSymbolicLink(toPath()) || !isDirectory) return delete()+    var allDeleted = true+    val children = listFiles()+    if (children == null) {+        allDeleted = false+    } else {+        for (child in children) {+            if (!child.deleteTreeWithoutFollowingLinks()) allDeleted = false+        }+    }+    return delete() && allDeleted+}+ /** MediaStore Uris for the subset of [files] that the platform can trash. */ internal fun resolveTrashableUris(context: Context, files: List<File>): Map<File, Uri> {     if (files.isEmpty()) return emptyMap()
files/src/main/java/com/vayunmathur/files/platform/FilesTrashModel.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesTrashModel.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesTrashModel.ktindex 0a1ed6700f..396dc92dd3 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesTrashModel.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesTrashModel.kt@@ -48,7 +48,7 @@ abstract class FilesTrashModel(application: Application) :         val pending = pendingPermanentDeleteState.value ?: return         pendingPermanentDeleteState.value = null         viewModelScope.launch(Dispatchers.IO) {-            pending.mapNotNull { it.realFile }.forEach { it.deleteRecursively() }+            pending.mapNotNull { it.realFile }.forEach { it.deleteTreeWithoutFollowingLinks() }             selectedPathsState.value = emptySet()             loadDirectory()         }
files/src/test/java/com/vayunmathur/files/platform/FilesDeleteTreeTest.kt
diff --git a/files/src/test/java/com/vayunmathur/files/platform/FilesDeleteTreeTest.kt b/files/src/test/java/com/vayunmathur/files/platform/FilesDeleteTreeTest.ktnew file mode 100644index 0000000000..03395ccdd4--- /dev/null+++ b/files/src/test/java/com/vayunmathur/files/platform/FilesDeleteTreeTest.kt@@ -0,0 +1,88 @@+package com.vayunmathur.files.platform++import java.io.File+import java.nio.file.Files+import java.nio.file.LinkOption+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++class FilesDeleteTreeTest {++    private fun tempDir(name: String): File =+        Files.createTempDirectory("delete-tree-$name-").toFile()++    private fun linkExists(link: File): Boolean =+        Files.exists(link.toPath(), LinkOption.NOFOLLOW_LINKS)++    private fun tryLink(link: File, target: File): Boolean = runCatching {+        Files.createSymbolicLink(link.toPath(), target.toPath())+    }.isSuccess++    @Test+    fun `plain tree is fully deleted`() {+        val root = tempDir("plain")+        val folder = File(root, "folder").apply { mkdirs() }+        File(folder, "own.txt").writeText("own")+        val sub = File(folder, "sub").apply { mkdirs() }+        File(sub, "inner.txt").writeText("inner")++        assertTrue(folder.deleteTreeWithoutFollowingLinks())+        assertFalse(folder.exists())+        root.deleteRecursively()+    }++    @Test+    fun `deleting a folder preserves the target of a symlink inside it`() {+        val root = tempDir("nested-link")+        val outside = File(root, "outside").apply { mkdirs() }+        File(outside, "keep.txt").writeText("keep")+        val folder = File(root, "folder").apply { mkdirs() }+        File(folder, "own.txt").writeText("own")+        if (!tryLink(File(folder, "innerlink"), outside)) {+            root.deleteRecursively()+            return+        }++        assertTrue(folder.deleteTreeWithoutFollowingLinks())+        assertFalse(folder.exists())+        assertEquals("keep", File(outside, "keep.txt").readText())+        root.deleteRecursively()+    }++    @Test+    fun `deleting a symlink to a directory removes only the link`() {+        val root = tempDir("root-link")+        val outside = File(root, "outside").apply { mkdirs() }+        File(outside, "keep.txt").writeText("keep")+        val browse = File(root, "browse").apply { mkdirs() }+        val link = File(browse, "link")+        if (!tryLink(link, outside)) {+            root.deleteRecursively()+            return+        }++        assertTrue(link.deleteTreeWithoutFollowingLinks())+        assertFalse(linkExists(link))+        assertEquals("keep", File(outside, "keep.txt").readText())+        root.deleteRecursively()+    }++    @Test+    fun `deleting a folder preserves the target of a symlink to a file`() {+        val root = tempDir("file-link")+        val outsideFile = File(root, "outside.txt").apply { writeText("keep") }+        val folder = File(root, "folder").apply { mkdirs() }+        File(folder, "own.txt").writeText("own")+        if (!tryLink(File(folder, "innerlink"), outsideFile)) {+            root.deleteRecursively()+            return+        }++        assertTrue(folder.deleteTreeWithoutFollowingLinks())+        assertFalse(folder.exists())+        assertEquals("keep", outsideFile.readText())+        root.deleteRecursively()+    }+}
files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt
diff --git a/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt b/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.ktindex 2d8962f056..22a2d656a0 100644--- a/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt+++ b/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt@@ -475,6 +475,26 @@ class FilesViewModelTest {         assertNull(vm.pendingPermanentDelete.value)     } +    @Test+    fun `permanent delete of a folder preserves its symlink target outside it`() {+        val outside = File(base, "outside").apply { mkdirs() }+        File(outside, "keep.txt").writeText("keep")+        val folder = File(dirA, "linked").apply { mkdirs() }+        File(folder, "own.txt").writeText("own")+        val linked = runCatching {+            java.nio.file.Files.createSymbolicLink(File(folder, "innerlink").toPath(), outside.toPath())+        }.isSuccess+        if (!linked) return+        vm.navigateTo(dirA)+        await { vm.entries.value.first.any { it.name == "linked" } }+        vm.addToSelection(itemOf("linked"))+        vm.deleteSelection()+        await { vm.pendingPermanentDelete.value?.isNotEmpty() == true }+        vm.confirmPermanentDelete()+        await { !folder.exists() }+        assertEquals("keep", File(outside, "keep.txt").readText())+    }+     @Test     fun `trash screen selection and consent result`() {         vm.openTrash()