files: fallback move no longer deletes symlink targets
- Commit
0e0bff75e6a4d5626eb7088a4cb2689e6e70e72a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 18:52 UTC (4 days ago)
- Parents
d9cd7b9
A cross-volume move falls back to copy-then-delete when the atomic move cannot be done. copySourceTo deleted the copied source tree with File.deleteRecursively, which follows symlinks (isDirectory on a link resolves to its target). Moving a folder that contained a symlink to a folder outside it therefore copied the outside contents into the destination and then deleted the originals outside the moved tree -- data loss outside the folder the user asked to move. FilesTrash already documents this hazard and provides deleteTreeWithoutFollowingLinks for permanent delete; the move paths missed it. Use deleteTreeWithoutFollowingLinks both for deleting the source after a successful fallback copy and for cleaning up a partial destination after a failed one, so a link is always removed as a link only and everything it points at is left untouched. Regression test in FilesListingTest forces the copy fallback (the existing read-only-parent technique), moves a folder containing a symlink to an outside folder holding a file, and asserts the outside file survives with its contents. It fails pre-fix and passes post-fix. Verified: targeted FilesListingTest, then the full :files:testDevUnitTest suite (131 tests, 0 failures). :files:lint reports one pre-existing ScaffoldSearch error in untouched ui/DirectoryBreadcrumbs.kt:102 and no findings in the touched files.
Diff
files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.ktindex 2da1e894b0..5b2b864521 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesCoreModel.kt@@ -336,7 +336,7 @@ private fun File.copyMoveTo(target: File) { } catch (e: IOException) { // A failed copy must not leave a partial destination behind to // block a retry with "already exists".- target.deleteRecursively()+ target.deleteTreeWithoutFollowingLinks() throw e } }@@ -346,7 +346,9 @@ private fun File.copySourceTo(target: File) { if (!copyRecursively(target, overwrite = false)) { throw IOException("Could not move $absolutePath to ${target.absolutePath}") }- deleteRecursively()+ // Not deleteRecursively(): that follows a symlink inside the tree and+ // would delete the link target's contents outside the moved folder.+ deleteTreeWithoutFollowingLinks() } else { copyTo(target, overwrite = false) delete()
files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.kt
diff --git a/files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.kt b/files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.ktindex 89c5e871c8..e612c30e62 100644--- a/files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.kt+++ b/files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.kt@@ -235,6 +235,38 @@ class FilesListingTest { } } + @Test+ fun `atomic move fallback does not delete contents behind a symlink in the source`() {+ val outside = tempDir("move-link-outside")+ val precious = File(outside, "precious.txt").apply { writeText("precious") }+ val srcParent = tempDir("move-link-src")+ val src = File(srcParent, "srcdir").apply { mkdirs() }+ File(src, "good.txt").writeText("good")+ // A link inside the moved folder that points at a folder outside it.+ // The copy fallback may copy what it points at, but deleting the+ // source afterwards must remove the link only, never the target.+ java.nio.file.Files.createSymbolicLink(File(src, "link").toPath(), outside.toPath())+ val destParent = tempDir("move-link-dest")+ val dest = File(destParent, "destdir")+ // Force the NIO atomic move onto the copy fallback: renaming a directory+ // out of a read-only parent fails, so the fallback copy runs instead.+ srcParent.setWritable(false)+ try {+ // Running as root ignores permission bits, leaving no way to force+ // the fallback; skip rather than assert a move that went another way.+ if (srcParent.canWrite()) return+ src.atomicMoveTo(dest)+ assertTrue(precious.isFile, "move must not delete the symlink target outside the source")+ assertEquals("precious", precious.readText())+ assertEquals("good", File(dest, "good.txt").readText())+ } finally {+ srcParent.setWritable(true)+ srcParent.deleteRecursively()+ destParent.deleteRecursively()+ outside.deleteRecursively()+ }+ }+ @Test fun `sanitized file name drops any directory part`() { assertEquals("report.txt", sanitizedFileName("report.txt"))