mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
fix: file manager copy/move keeps symlinks (#1027)
This commit is contained in:
1 parent
a74ff3d67a
commit
b4c56096a4
3 files changed
+115
-4
No files matched your search
@@ -228,7 +228,7 @@ fun FileManagerScreen(
|
||||
|
||||
fun uniqueDestination(dir: File, name: String): File {
|
||||
var candidate = File(dir, name)
|
||||
if (!candidate.exists()) return candidate
|
||||
if (!java.nio.file.Files.exists(candidate.toPath(), java.nio.file.LinkOption.NOFOLLOW_LINKS)) return candidate
|
||||
val dot = name.lastIndexOf('.')
|
||||
val base = if (dot > 0) name.substring(0, dot) else name
|
||||
val ext = if (dot > 0) name.substring(dot) else ""
|
||||
@@ -236,7 +236,7 @@ fun FileManagerScreen(
|
||||
do {
|
||||
candidate = File(dir, "$base ($i)$ext")
|
||||
i++
|
||||
} while (candidate.exists())
|
||||
} while (java.nio.file.Files.exists(candidate.toPath(), java.nio.file.LinkOption.NOFOLLOW_LINKS))
|
||||
return candidate
|
||||
}
|
||||
|
||||
@@ -287,7 +287,7 @@ fun FileManagerScreen(
|
||||
}
|
||||
|
||||
var dst = File(dstDir, src.name)
|
||||
if (dst.exists()) {
|
||||
if (java.nio.file.Files.exists(dst.toPath(), java.nio.file.LinkOption.NOFOLLOW_LINKS)) {
|
||||
val choice = applyToAll ?: askConflict(src)?.also {
|
||||
if (conflictApplyToAll) applyToAll = it
|
||||
} ?: run { skipped++; null } ?: continue
|
||||
|
||||
@@ -64,7 +64,7 @@ object FileOps {
|
||||
|
||||
private fun copyWithProgress(src: File, dst: File, done: LongArray, total: Long, progress: ProgressCallback?, isCancelled: () -> Boolean): Boolean {
|
||||
if (isCancelled()) throw java.util.concurrent.CancellationException("Copy cancelled: $src")
|
||||
if (isSymlink(src)) return true
|
||||
if (isSymlink(src)) return copyLink(src, dst)
|
||||
if (sameFile(src, dst)) return true
|
||||
if (src.isDirectory && isWithin(dst, src)) {
|
||||
Log.e(TAG, "Refusing to copy directory into itself: $src -> $dst")
|
||||
@@ -152,6 +152,32 @@ object FileOps {
|
||||
}
|
||||
}
|
||||
|
||||
private fun copyLink(src: File, dst: File): Boolean {
|
||||
if (src.absoluteFile == dst.absoluteFile) return true
|
||||
return try {
|
||||
val target = Files.readSymbolicLink(src.toPath())
|
||||
val parent = dst.parentFile
|
||||
if (parent != null && !parent.exists() && !parent.mkdirs()) return false
|
||||
try {
|
||||
Files.createSymbolicLink(dst.toPath(), target)
|
||||
} catch (e: java.nio.file.FileAlreadyExistsException) {
|
||||
val staging = Files.createTempDirectory(checkNotNull(dst.absoluteFile.parentFile).toPath(), ".droiddeck-link-")
|
||||
val tmp = staging.resolve("link")
|
||||
try {
|
||||
Files.createSymbolicLink(tmp, target)
|
||||
Files.move(tmp, dst.toPath(), java.nio.file.StandardCopyOption.ATOMIC_MOVE)
|
||||
} finally {
|
||||
Files.deleteIfExists(tmp)
|
||||
Files.deleteIfExists(staging)
|
||||
}
|
||||
}
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to copy link: $src to $dst", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private fun sameFile(a: File, b: File): Boolean =
|
||||
try { a.canonicalPath == b.canonicalPath } catch (e: IOException) { a.absolutePath == b.absolutePath }
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.droiddeck.launcher.files
|
||||
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assume.assumeTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class FileOpsLinksTest {
|
||||
@get:Rule val tmp = TemporaryFolder()
|
||||
|
||||
private fun link(at: File, target: String): Boolean =
|
||||
runCatching { Files.createSymbolicLink(at.toPath(), File(target).toPath()) }.isSuccess
|
||||
|
||||
@Test fun aMovedFolderKeepsItsLinks() {
|
||||
val src = tmp.newFolder("game")
|
||||
File(src, "data.bin").writeText("x")
|
||||
assumeTrue("symlinks unavailable here", link(File(src, "lib.so"), "data.bin"))
|
||||
val dst = File(tmp.root, "other/game")
|
||||
File(dst, "keep").mkdirs()
|
||||
assertTrue(FileOps.moveWithProgress(src, dst, null))
|
||||
assertFalse(src.exists())
|
||||
assertTrue(Files.isSymbolicLink(File(dst, "lib.so").toPath()))
|
||||
assertEquals("data.bin", Files.readSymbolicLink(File(dst, "lib.so").toPath()).toString())
|
||||
}
|
||||
|
||||
@Test fun aCopiedLinkPointsWhereTheOriginalDoes() {
|
||||
val src = tmp.newFolder("a")
|
||||
assumeTrue("symlinks unavailable here", link(File(src, "l"), "/nowhere/target"))
|
||||
val dst = File(tmp.root, "b")
|
||||
assertTrue(FileOps.copyWithProgress(src, dst, null))
|
||||
assertTrue(Files.isSymbolicLink(File(src, "l").toPath()))
|
||||
assertTrue(Files.isSymbolicLink(File(dst, "l").toPath()))
|
||||
}
|
||||
|
||||
@Test fun copyingALinkPreservesAnUnrelatedStagingName() {
|
||||
val src = File(tmp.root, "source")
|
||||
assumeTrue("symlinks unavailable here", link(src, "missing-target"))
|
||||
val dst = File(tmp.root, "dest")
|
||||
dst.writeText("old destination")
|
||||
val sibling = File(tmp.root, ".dest.link-tmp")
|
||||
sibling.writeText("unrelated user data")
|
||||
|
||||
assertTrue(FileOps.copyWithProgress(src, dst, null))
|
||||
assertEquals("unrelated user data", sibling.readText())
|
||||
assertEquals("missing-target", Files.readSymbolicLink(dst.toPath()).toString())
|
||||
assertEquals(setOf("source", "dest", ".dest.link-tmp"), tmp.root.list()!!.toSet())
|
||||
}
|
||||
|
||||
@Test fun anUnrelatedStagingDirectoryDoesNotPreventCopyingALink() {
|
||||
val src = File(tmp.root, "source")
|
||||
assumeTrue("symlinks unavailable here", link(src, "missing-target"))
|
||||
val sibling = tmp.newFolder(".dest.link-tmp")
|
||||
File(sibling, "keep").writeText("unrelated user data")
|
||||
|
||||
assertTrue(FileOps.copyWithProgress(src, File(tmp.root, "dest"), null))
|
||||
assertEquals("unrelated user data", File(sibling, "keep").readText())
|
||||
}
|
||||
|
||||
@Test fun aLongDestinationNameDoesNotOverflowTheStagingName() {
|
||||
val src = File(tmp.root, "source")
|
||||
assumeTrue("symlinks unavailable here", link(src, "missing-target"))
|
||||
val dst = File(tmp.root, "d".repeat(240))
|
||||
assertTrue(FileOps.copyWithProgress(src, dst, null))
|
||||
assertEquals("missing-target", Files.readSymbolicLink(dst.toPath()).toString())
|
||||
}
|
||||
|
||||
@Test fun aLinkThatCannotReplaceTheDestinationLeavesItAlone() {
|
||||
val src = tmp.newFolder("c")
|
||||
assumeTrue("symlinks unavailable here", link(File(src, "l"), "x"))
|
||||
val dst = File(tmp.root, "d")
|
||||
File(dst, "l").mkdirs()
|
||||
File(dst, "l/keep.txt").writeText("kept")
|
||||
assertFalse(FileOps.copyWithProgress(File(src, "l"), File(dst, "l"), null))
|
||||
assertEquals("kept", File(dst, "l/keep.txt").readText())
|
||||
assertFalse(File(dst, ".l.link-tmp").exists())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user