diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/export/ExportWriter.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/export/ExportWriter.kt index 515430b..a592fd7 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/export/ExportWriter.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/export/ExportWriter.kt @@ -72,8 +72,8 @@ class ExportWriter @Inject constructor( */ suspend fun writeToTree(treeUri: Uri, documents: List): ExportResult = withContext(io) { + val tree = openTree(treeUri) runCatching { - val tree = openWritableTree(treeUri) val existing = tree.listFiles().associateBy { it.name } documents.forEach { document -> @@ -82,7 +82,7 @@ class ExportWriter @Inject constructor( ?: throw ExportFailedException(ExportFailure.CANNOT_CREATE_FILE) write(file.uri, document.content) } - }.getOrElse { throw asExportFailure(it) } + }.getOrElse { throw failureIn(tree, it) } ExportResult(documents.size, documents.map { it.fileName }) } @@ -115,12 +115,12 @@ class ExportWriter @Inject constructor( documents: List, prune: (Set) -> Collection = { emptyList() }, ): ExportResult = withContext(io) { + val tree = openTree(treeUri) val children = runCatching { - val tree = openWritableTree(treeUri) val children = childDocuments(treeUri) if (fileName !in children) writeViaPartial(tree, fileName, partialName, documents) children - partialName - }.getOrElse { throw asExportFailure(it) } + }.getOrElse { throw failureIn(tree, it) } prune(children.keys + fileName).forEach { name -> val uri = children[name] ?: return@forEach runCatching { DocumentsContract.deleteDocument(context.contentResolver, uri) } @@ -195,13 +195,24 @@ class ExportWriter @Inject constructor( }.orEmpty() } - private fun openWritableTree(treeUri: Uri): DocumentFile { - val tree = DocumentFile.fromTreeUri(context, treeUri) - ?: throw ExportFailedException(ExportFailure.FOLDER_UNAVAILABLE) - // An unmounted volume or a dead provider answers neither query. - if (!tree.exists()) throw ExportFailedException(ExportFailure.FOLDER_UNAVAILABLE) - if (!tree.canWrite()) throw ExportFailedException(ExportFailure.FOLDER_NOT_WRITABLE) - return tree + private fun openTree(treeUri: Uri): DocumentFile = + DocumentFile.fromTreeUri(context, treeUri) ?: throw ExportFailedException(ExportFailure.FOLDER_UNAVAILABLE) + + /** + * [cause], a write into [tree] that failed, told apart by asking the folder + * only now: some providers answer no query on the folder itself yet take its + * files. An unmounted volume or a dead provider answers none. + */ + private fun failureIn(tree: DocumentFile, cause: Throwable): Throwable { + val failure = asExportFailure(cause) + // A revoked grant answers no query either, and stays lost access. + val exists = tree.exists() + return when { + exists && !tree.canWrite() -> ExportFailedException(ExportFailure.FOLDER_NOT_WRITABLE, cause) + failure is ExportFailedException && failure.failure == ExportFailure.LOST_ACCESS -> failure + !exists -> ExportFailedException(ExportFailure.FOLDER_UNAVAILABLE, cause) + else -> failure + } } private fun zipInto(target: Uri, documents: List) {