files: a cut-paste that moves nothing keeps the clipboard
- Commit
ffcf686ae96f559f1bd8d32d5ca7203342dba5a8- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 19:59 UTC (5 days ago)
- Parents
558a25a
Cutting a folder and pasting it into its own subtree silently moved nothing — pasteOne returned without error — yet the cut clipboard was then cleared unconditionally, so Paste vanished and the user had to re-find and re-cut to retry. The same loss hit any failed paste (destination IOException): the sources were untouched on disk but the cut was discarded. pasteSources now tracks unmoved sources: a cut clears the clipboard only when everything moved, otherwise exactly the unmoved sources are retained for retry. Subtree paste throws the 'cannot paste into itself' IOException so the existing move_failed feedback fires, and directory copy checks copyRecursively's result instead of silently reporting a partial copy as success. Regression tests cover subtree paste (plus a successful retry) and failed paste; the existing successful-cut-clears test still passes. Text editor, picker, trash and properties were audited and cleared. Verified: :files:testDevUnitTest passes; lint errors are pre-existing on untouched files.
Diff
files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.ktindex 40c5ab17ca..0ab6fa768d 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.kt@@ -67,16 +67,31 @@ abstract class FilesShareModel(application: Application) : viewModelScope.launch(Dispatchers.IO) { var lastError: IOException? = null var lastSecurityError: SecurityException? = null+ // A cut source that was not actually moved (paste blocked or+ // failed) must stay on the clipboard: its file is untouched, so+ // clearing here would silently discard the user's cut and the+ // paste action would disappear, leaving no way to retry in a+ // valid folder. Only sources that were moved are dropped.+ val unmoved = mutableListOf<File>() sources.forEach { source -> try { pasteOne(source, target, isCut) } catch (e: IOException) {+ unmoved.add(source) lastError = e } catch (e: SecurityException) {+ unmoved.add(source) lastSecurityError = e } }- if (isCut) clearClipboard()+ if (isCut) {+ if (unmoved.isEmpty()) {+ clearClipboard()+ } else {+ clipboardState.value = unmoved+ clipboardIsCutState.value = true+ }+ } loadDirectory() lastError?.let { emitMoveFailed(it) } lastSecurityError?.let { emitMoveFailedMessage(it.localizedMessage) }@@ -89,12 +104,14 @@ abstract class FilesShareModel(application: Application) : val targetPath = target.absolutePath if (targetPath == source.absolutePath || targetPath.startsWith(source.absolutePath + "/")- ) return+ ) throw IOException("Cannot paste a folder into itself") val dest = uniqueDestination(target, source.name) if (isCut) { source.atomicMoveTo(dest) } else if (source.isDirectory) {- source.copyRecursively(dest, overwrite = false)+ if (!source.copyRecursively(dest, overwrite = false)) {+ throw IOException("Could not copy ${source.absolutePath}")+ } } else { source.copyTo(dest, overwrite = false) }
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 45f1e39a3b..2d8962f056 100644--- a/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt+++ b/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt@@ -291,6 +291,61 @@ class FilesViewModelTest { assertTrue(vm.clipboard.value.isEmpty()) } + @Test+ fun `cut paste into own subtree keeps clipboard and source`() {+ val outer = File(dirA, "outer").apply { mkdirs() }+ File(outer, "inner").mkdirs()+ File(outer, "keep.txt").writeText("keep")+ vm.navigateTo(dirA)+ await { vm.entries.value.first.any { it.name == "outer" } }+ vm.addToSelection(itemOf("outer"))+ vm.cutSelection()+ assertTrue(vm.clipboardIsCut.value)+ assertEquals(listOf(outer), vm.clipboard.value)+ // Paste while standing inside the cut folder itself: nothing can+ // move, so the cut must survive for a retry elsewhere and the+ // source must be untouched.+ vm.navigateTo(File(outer, "inner"))+ vm.pasteHere()+ Thread.sleep(500); ShadowLooper.idleMainLooper()+ assertEquals(listOf(outer), vm.clipboard.value)+ assertTrue(vm.clipboardIsCut.value)+ assertTrue(File(outer, "keep.txt").isFile)+ assertFalse(File(outer, "inner/outer").exists())+ // The retained cut still pastes normally into a valid folder.+ vm.navigateTo(dirB)+ vm.pasteHere()+ await { File(dirB, "outer/keep.txt").isFile && !outer.exists() }+ assertTrue(vm.clipboard.value.isEmpty())+ assertFalse(vm.clipboardIsCut.value)+ }++ @Test+ fun `cut paste that fails keeps clipboard and source`() {+ val readonly = File(base, "readonly").apply { mkdirs() }+ vm.navigateTo(dirA)+ await { vm.entries.value.second.isNotEmpty() }+ vm.addToSelection(itemOf("one.txt"))+ vm.cutSelection()+ assertTrue(vm.clipboardIsCut.value)+ vm.navigateTo(readonly)+ readonly.setWritable(false)+ try {+ vm.pasteHere()+ Thread.sleep(500); ShadowLooper.idleMainLooper()+ // As root a read-only directory does not block writes; only+ // assert the retained-cut invariant where the paste really+ // could not create the destination.+ if (!File(readonly, "one.txt").exists()) {+ assertEquals(listOf(File(dirA, "one.txt")), vm.clipboard.value)+ assertTrue(vm.clipboardIsCut.value)+ assertEquals("one", File(dirA, "one.txt").readText())+ }+ } finally {+ readonly.setWritable(true)+ }+ }+ @Test fun `moveInto and moveToBreadcrumb move files with guards`() { val target = File(dirA, "sub")