Guard File Manager jobs and clean up cancelled copies

Description:
- Reject bulk delete while another operation is active. Check current operation state at action time, and clear operationJob on completion only when the finishing coroutine still owns it.
- Retain the requested Return directory even when it is unavailable, so Return can display the existing unavailable-directory message while browsing starts from the valid fallback.
- Observe cancellation during the recursive size scan before copying. Remove destination directories created by a cancelled copy, while preserving existing destination directories and ordinary copy-failure behavior.
- After a move's copy commits, finish deleting the source instead of reporting cancellation with both copies left behind. Preserve cancellation before copying and the existing copy-failure path.
- Log permission-restoration failures in staged copies and continue publishing the copied file.
- Exercise cancellation signals during transfers and size scans, directory cleanup, preservation of existing directories, and committed move completion without leftover staging entries.

Why it was needed:
- Overlapping operations could replace the tracked job, and an older completion could clear that reference, allowing navigation before the remaining work stopped.
- Cancellation could leave new partial directory trees or wait through the size scan. Late move cancellation could leave the source behind after publishing its replacement.
- A missing launch directory suppressed Return entirely, and a permission-restoration error could prevent an otherwise successful overwrite.

Validation:
- Debug Kotlin compilation passed with skipRust=true.
- Focused FileOps cancellation, copy, and link suites: 17 passed, 10 platform-dependent cases skipped on Windows, no failures.
- git diff --check passed.
This commit is contained in:
K11MCH1 committed 2026-10-22 21:29:18 +09:00
1 parent 7432a4f1a3
commit cb153ece87
4 files changed
+119 -36

No files matched your search

@@ -19,9 +19,10 @@ class FileOpsCancellationTest {
@Test fun cancellingCopyRemovesAnIncompleteNewFile() {
val source = tmp.newFile("source").apply { writeBytes(ByteArray(5 * 1024 * 1024)) }
val destination = File(tmp.root, "destination")
var cancelled = false
assertThrows(CancellationException::class.java) {
FileOps.copyWithProgress(source, destination, { _, _ -> throw CancellationException("cancel") })
FileOps.copyWithProgress(source, destination, { _, _ -> cancelled = true }) { cancelled }
}
assertTrue(source.exists())
@@ -31,15 +32,80 @@ class FileOpsCancellationTest {
@Test fun cancellingMoveFallbackPreservesTheSourceAndExistingDestination() {
val source = tmp.newFile("source").apply { writeText("replacement") }
val destination = tmp.newFile("destination").apply { writeText("original") }
var cancelled = false
assertThrows(CancellationException::class.java) {
FileOps.moveWithProgress(source, destination, { _, _ -> throw CancellationException("cancel") })
FileOps.moveWithProgress(source, destination, { _, _ -> cancelled = true }) { cancelled }
}
assertEquals("replacement", source.readText())
assertEquals("original", destination.readText())
}
@Test fun aCommittedMoveFinishesRemovingTheSourceDespiteCancellation() {
val source = tmp.newFile("source").apply { writeText("replacement") }
val destination = tmp.newFile("destination").apply { writeText("original") }
val isCancelled = { destination.readText() == "replacement" }
assertTrue(FileOps.moveWithProgress(source, destination, null, isCancelled))
assertTrue(isCancelled())
assertEquals("replacement", destination.readText())
assertFalse(source.exists())
assertEquals(setOf("destination"), tmp.root.list()!!.toSet())
}
@Test fun cancellingTheSizeScanStopsBeforeCreatingTheDestination() {
val source = tmp.newFolder("source")
repeat(3) { File(source, "file-$it").writeText("content") }
val destination = File(tmp.root, "destination")
var checks = 0
assertThrows(CancellationException::class.java) {
FileOps.copyWithProgress(source, destination, null) {
assertFalse(destination.exists())
++checks > 2
}
}
assertEquals(3, checks)
assertFalse(destination.exists())
assertEquals(3, source.listFiles()?.size)
}
@Test fun cancellingCopyRemovesANewDestinationDirectory() {
val source = tmp.newFolder("source")
val payload = File(source, "nested/payload").apply {
parentFile!!.mkdirs()
writeText("content")
}
val destination = File(tmp.root, "destination")
assertThrows(CancellationException::class.java) {
FileOps.copyWithProgress(source, destination, { _, _ -> throw CancellationException("cancel") })
}
assertEquals("content", payload.readText())
assertFalse(destination.exists())
}
@Test fun cancellingCopyPreservesAnExistingDestinationDirectory() {
val source = tmp.newFolder("source")
File(source, "nested/payload").apply {
parentFile!!.mkdirs()
writeText("replacement")
}
val destination = tmp.newFolder("destination")
val existing = File(destination, "keep").apply { writeText("original") }
assertThrows(CancellationException::class.java) {
FileOps.copyWithProgress(source, destination, { _, _ -> throw CancellationException("cancel") })
}
assertEquals("original", existing.readText())
assertEquals(setOf("keep"), destination.list()!!.toSet())
}
@Test fun cancellingRecursiveDeleteStopsBeforeTheNextFile() {
val directory = tmp.newFolder("directory")
repeat(3) { File(directory, "file-$it").writeText("content") }
@@ -53,27 +53,25 @@ class FileOpsCopyTest {
assertEquals("replacement", src.readText())
}
@Test fun cancellingStopsATreeOfEmptyFiles() {
@Test fun cancellingRemovesANewTreeOfEmptyFiles() {
val src = tmp.newFolder("source")
for (name in listOf("a", "b", "c")) File(src, name).createNewFile()
val dst = File(tmp.root, "dest")
var checks = 0
assertThrows(CancellationException::class.java) {
FileOps.copyWithProgress(src, dst, null) { checks++ > 1 }
FileOps.copyWithProgress(src, dst, null) { dst.list()?.isNotEmpty() == true }
}
assertEquals(1, dst.list()!!.size)
assertFalse(dst.exists())
assertEquals(3, src.list()!!.size)
}
@Test fun cancellingStopsATreeOfEmptyFolders() {
@Test fun cancellingRemovesANewTreeOfEmptyFolders() {
val src = tmp.newFolder("source")
for (name in listOf("a", "b", "c")) File(src, name).mkdir()
val dst = File(tmp.root, "dest")
var checks = 0
assertThrows(CancellationException::class.java) {
FileOps.copyWithProgress(src, dst, null) { checks++ > 1 }
FileOps.copyWithProgress(src, dst, null) { dst.list()?.isNotEmpty() == true }
}
assertEquals(1, dst.list()!!.size)
assertFalse(dst.exists())
}
@Test fun aCompletedCopyReplacesTheDestination() {