diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2024-10-14 21:41:06 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2024-10-14 22:45:57 +0200 |
| commit | ea9621389bfa62cb4e63688249c52ac0e41ff282 (patch) | |
| tree | 4932900d0c058aa7e187fc02b214a024f801db18 /libs/documents/src/main/java/org/the_jk | |
| parent | 2be5a5171de2ecd51973862c243aecc0be4a0876 (diff) | |
Add tests for create dir/file/link that already exists
Fix implementations to work as expected
(that createDirectory/File/Link fails if an entry with that name
already exists).
Diffstat (limited to 'libs/documents/src/main/java/org/the_jk')
| -rw-r--r-- | libs/documents/src/main/java/org/the_jk/cleversync/io/documents/DocumentDirectory.kt | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/libs/documents/src/main/java/org/the_jk/cleversync/io/documents/DocumentDirectory.kt b/libs/documents/src/main/java/org/the_jk/cleversync/io/documents/DocumentDirectory.kt index 258d5b6..e8b94aa 100644 --- a/libs/documents/src/main/java/org/the_jk/cleversync/io/documents/DocumentDirectory.kt +++ b/libs/documents/src/main/java/org/the_jk/cleversync/io/documents/DocumentDirectory.kt @@ -17,6 +17,7 @@ import org.the_jk.cleversync.io.ModifiableDirectory import org.the_jk.cleversync.io.ModifiableFile import org.the_jk.cleversync.io.ModifiableLink import java.io.IOException +import java.nio.file.FileAlreadyExistsException import kotlin.time.Duration internal open class DocumentDirectory( @@ -103,6 +104,7 @@ internal open class DocumentDirectory( override fun createDirectory(name: String): ModifiableDirectory { if (!metadata.supportsCreate()) throw IOException("Directory doesn't support creation") + if (findChild(name) != null) throw FileAlreadyExistsException(name) val documentUri = DocumentsContract.createDocument( contentResolver, DocumentsContract.buildDocumentUriUsingTree(treeUri, metadata.documentId), @@ -118,6 +120,7 @@ internal open class DocumentDirectory( override fun createFile(name: String): ModifiableFile { if (!metadata.supportsCreate()) throw IOException("Directory doesn't support creation") + if (findChild(name) != null) throw FileAlreadyExistsException(name) // TODO: Handle .tar.gz and such? Is it worth it (will android find them anyway) val dotIndex = name.lastIndexOf('.') val mimeType = if (dotIndex > 0) { @@ -284,9 +287,16 @@ internal open class DocumentDirectory( val queryArgs = Bundle() queryArgs.putString(DocumentsContract.QUERY_ARG_DISPLAY_NAME, displayName) return contentResolver.getAllMetadata( - DocumentsContract.buildChildDocumentsUriUsingTree(treeUri, metadata.documentId), + DocumentsContract.buildChildDocumentsUriUsingTree( + treeUri, + metadata.documentId + ), queryArgs = queryArgs, - ).singleOrNull() + ).singleOrNull { + // Not all document providers use QUERY_ARG_DISPLAY_NAME and the ones + // that do can still return partial matches and case insensitive matches + it.displayName == displayName + } } private companion object { |
