mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
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:
1 parent
7432a4f1a3
commit
cb153ece87
4 files changed
+119
-36
No files matched your search
@@ -128,7 +128,7 @@ fun FileManagerScreen(
|
||||
initialDir?.takeIf { it.isDirectory } ?: File("/storage/emulated/0")
|
||||
}
|
||||
val launchDir = remember(initialDir, pickMode) {
|
||||
initialDir?.takeIf { it.isDirectory && !pickMode }
|
||||
initialDir?.takeIf { !pickMode }
|
||||
}
|
||||
var currentDir by remember { mutableStateOf(rootDir) }
|
||||
val initialRoot = remember(rootDir) {
|
||||
@@ -236,7 +236,8 @@ fun FileManagerScreen(
|
||||
LaunchedEffect(Unit) { loadDirectory(rootDir) }
|
||||
|
||||
val canGoUp = currentDir != currentRoot && currentDir.parentFile?.exists() == true
|
||||
val operationActive = isOperationRunning || operationJob?.isCompleted == false || cancelledJob?.isCompleted == false
|
||||
fun isOperationActive() = isOperationRunning || operationJob?.isCompleted == false || cancelledJob?.isCompleted == false
|
||||
val operationActive = isOperationActive()
|
||||
|
||||
fun navigate(action: FileManagerNavigation) {
|
||||
when (action) {
|
||||
@@ -252,7 +253,7 @@ fun FileManagerScreen(
|
||||
|
||||
fun requestNavigation(action: FileManagerNavigation) {
|
||||
if (cancellingNavigation) return
|
||||
if (operationActive) pendingNavigation = action else navigate(action)
|
||||
if (isOperationActive()) pendingNavigation = action else navigate(action)
|
||||
}
|
||||
|
||||
BackHandler(enabled = operationActive || cancellingNavigation || showFavorites || currentDir != currentRoot) {
|
||||
@@ -313,14 +314,14 @@ fun FileManagerScreen(
|
||||
}
|
||||
|
||||
fun performDelete(file: File) {
|
||||
if (operationActive) return
|
||||
if (isOperationActive()) return
|
||||
operationJob = scope.launch {
|
||||
isOperationRunning = true
|
||||
operationLabel = context.getString(R.string.fm_op_deleting)
|
||||
val job = coroutineContext[Job]
|
||||
val ok = withContext(Dispatchers.IO) { FileOps.delete(file) { job?.isActive == false } }
|
||||
isOperationRunning = false
|
||||
operationJob = null
|
||||
if (operationJob === job) operationJob = null
|
||||
loadDirectory(currentDir, resetScroll = false)
|
||||
if (!ok) Toast.makeText(context, R.string.fm_delete_failed, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
@@ -335,7 +336,7 @@ fun FileManagerScreen(
|
||||
|
||||
fun performPaste() {
|
||||
val sources = clipboardFiles
|
||||
if (sources.isEmpty() || isOperationRunning || operationJob?.isActive == true || cancelledJob?.isCompleted == false) return
|
||||
if (sources.isEmpty() || isOperationActive()) return
|
||||
val dstDir = currentDir
|
||||
val cut = isCutOperation
|
||||
|
||||
@@ -397,7 +398,7 @@ fun FileManagerScreen(
|
||||
|
||||
isOperationRunning = false
|
||||
operationDeterminate = false
|
||||
operationJob = null
|
||||
if (operationJob === job) operationJob = null
|
||||
clipboardFiles = emptyList()
|
||||
isCutOperation = false
|
||||
selectionMode = false
|
||||
@@ -415,36 +416,38 @@ fun FileManagerScreen(
|
||||
}
|
||||
|
||||
fun performRename(file: File, newName: String) {
|
||||
if (operationActive) return
|
||||
if (isOperationActive()) return
|
||||
val target = File(file.parentFile, newName)
|
||||
if (target.exists()) {
|
||||
Toast.makeText(context, context.getString(R.string.fm_already_exists, newName), Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
operationJob = scope.launch {
|
||||
val job = coroutineContext[Job]
|
||||
isOperationRunning = true
|
||||
operationLabel = context.getString(R.string.fm_op_renaming)
|
||||
val ok = withContext(Dispatchers.IO) { file.renameTo(target) }
|
||||
isOperationRunning = false
|
||||
operationJob = null
|
||||
if (operationJob === job) operationJob = null
|
||||
loadDirectory(currentDir, resetScroll = false)
|
||||
if (!ok) Toast.makeText(context, R.string.fm_rename_failed, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
fun createFolder(parent: File, name: String) {
|
||||
if (operationActive) return
|
||||
if (isOperationActive()) return
|
||||
val target = File(parent, name)
|
||||
if (target.exists()) {
|
||||
Toast.makeText(context, context.getString(R.string.fm_already_exists, name), Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
operationJob = scope.launch {
|
||||
val job = coroutineContext[Job]
|
||||
isOperationRunning = true
|
||||
operationLabel = context.getString(R.string.fm_op_creating_folder)
|
||||
val ok = withContext(Dispatchers.IO) { target.mkdirs() }
|
||||
isOperationRunning = false
|
||||
operationJob = null
|
||||
if (operationJob === job) operationJob = null
|
||||
loadDirectory(currentDir, resetScroll = false)
|
||||
if (!ok) Toast.makeText(context, R.string.fm_create_folder_failed, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
@@ -560,6 +563,7 @@ fun FileManagerScreen(
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
if (isOperationActive()) return@TextButton
|
||||
pendingBulkDelete = emptyList()
|
||||
selectionMode = false
|
||||
selectedPaths = emptySet()
|
||||
@@ -572,7 +576,7 @@ fun FileManagerScreen(
|
||||
if (!withContext(Dispatchers.IO) { FileOps.delete(f) { job?.isActive == false } }) failed++
|
||||
}
|
||||
isOperationRunning = false
|
||||
operationJob = null
|
||||
if (operationJob === job) operationJob = null
|
||||
loadDirectory(currentDir, resetScroll = false)
|
||||
if (failed > 0) {
|
||||
Toast.makeText(context, context.resources.getQuantityString(R.plurals.fm_delete_some_failed, failed, failed), Toast.LENGTH_SHORT).show()
|
||||
|
||||
@@ -42,9 +42,10 @@ object FileOps {
|
||||
return ok
|
||||
}
|
||||
|
||||
fun totalSize(f: File?): Long {
|
||||
fun totalSize(f: File?, isCancelled: () -> Boolean = { false }): Long {
|
||||
if (isCancelled()) throw CancellationException("Size scan cancelled: $f")
|
||||
if (f == null || !f.exists() || isSymlink(f)) return 0
|
||||
if (f.isDirectory) return f.listFiles()?.sumOf { totalSize(it) } ?: 0
|
||||
if (f.isDirectory) return f.listFiles()?.sumOf { totalSize(it, isCancelled) } ?: 0
|
||||
return f.length()
|
||||
}
|
||||
|
||||
@@ -56,12 +57,12 @@ object FileOps {
|
||||
return true
|
||||
}
|
||||
if (!copyWithProgress(src, dst, progress, isCancelled)) return false
|
||||
if (isCancelled()) return false
|
||||
return delete(src, isCancelled)
|
||||
// Once the destination is committed, finish deleting the source.
|
||||
return delete(src)
|
||||
}
|
||||
|
||||
fun copyWithProgress(src: File, dst: File, progress: ProgressCallback?, isCancelled: () -> Boolean = { false }): Boolean {
|
||||
val total = maxOf(totalSize(src), 1L)
|
||||
val total = maxOf(totalSize(src, isCancelled), 1L)
|
||||
return copyWithProgress(src, dst, longArrayOf(0), total, progress, isCancelled)
|
||||
}
|
||||
|
||||
@@ -74,15 +75,23 @@ object FileOps {
|
||||
return false
|
||||
}
|
||||
if (src.isDirectory) {
|
||||
if (!dst.exists() && !dst.mkdirs()) return false
|
||||
var allOk = true
|
||||
src.list()?.forEach { name ->
|
||||
if (!copyWithProgress(File(src, name), File(dst, name), done, total, progress, isCancelled)) {
|
||||
Log.e(TAG, "Failed to copy: ${File(src, name)}")
|
||||
allOk = false
|
||||
val created = if (dst.exists()) false else {
|
||||
if (!dst.mkdirs()) return false
|
||||
true
|
||||
}
|
||||
try {
|
||||
var allOk = true
|
||||
src.list()?.forEach { name ->
|
||||
if (!copyWithProgress(File(src, name), File(dst, name), done, total, progress, isCancelled)) {
|
||||
Log.e(TAG, "Failed to copy: ${File(src, name)}")
|
||||
allOk = false
|
||||
}
|
||||
}
|
||||
return allOk
|
||||
} catch (e: CancellationException) {
|
||||
if (created) delete(dst)
|
||||
throw e
|
||||
}
|
||||
return allOk
|
||||
}
|
||||
val parent = dst.parentFile
|
||||
if (!src.exists() || (parent != null && !parent.exists() && !parent.mkdirs())) return false
|
||||
@@ -134,8 +143,14 @@ object FileOps {
|
||||
}
|
||||
staging?.let { directory ->
|
||||
val copied = File(directory, "file").toPath()
|
||||
if (permissions != null && Files.getPosixFilePermissions(copied) != permissions) {
|
||||
Files.setPosixFilePermissions(copied, permissions)
|
||||
if (permissions != null) {
|
||||
try {
|
||||
if (Files.getPosixFilePermissions(copied) != permissions) {
|
||||
Files.setPosixFilePermissions(copied, permissions)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Failed to restore destination permissions: $target", e)
|
||||
}
|
||||
}
|
||||
if (isCancelled()) throw java.util.concurrent.CancellationException("Copy cancelled: $src")
|
||||
Files.move(copied, target.toPath(), java.nio.file.StandardCopyOption.ATOMIC_MOVE)
|
||||
|
||||
Reference in new issue
Block a user