Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isChildDocument: Return true for nested children #3

Merged
merged 1 commit into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions app/src/androidTest/java/com/chiller3/rsaf/RcloneProviderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ class RcloneProviderTest {
}

assertTrue(isChild(docUriFromRoot(), docUriFromRoot("child")))
assertFalse(isChild(docUriFromRoot(), docUriFromRoot("nested", "child")))
assertTrue(isChild(docUriFromRoot(), docUriFromRoot("nested", "child")))
assertTrue(isChild(docUriFromRoot("dir"), docUriFromRoot("dir", "child")))
assertTrue(isChild(docUriFromRoot("dir"), docUriFromRoot("dir", "nested", "child")))
assertFalse(isChild(docUriFromRoot(), docUriFromRoot()))
assertFalse(isChild(docUriFromRoot("dir"), docUriFromRoot("dir")))
}
Expand Down Expand Up @@ -492,17 +493,22 @@ class RcloneProviderTest {
testDelete { DocumentsContract.removeDocument(appContext.contentResolver, it, rootDocUri) }

// Try deleting a file from the wrong parent
File(rootDir, "dir").apply {
File(rootDir, "dir1").apply {
assertTrue(mkdir())
File(this, "file").apply {
assertTrue(createNewFile())
}
}

val uri = docUriFromRoot("dir", "file")
File(rootDir, "dir2").apply {
assertTrue(mkdir())
}

assertThrows(IllegalArgumentException::class.java) {
DocumentsContract.removeDocument(appContext.contentResolver, uri, rootDocUri)
DocumentsContract.removeDocument(
appContext.contentResolver,
docUriFromRoot("dir1", "file"),
docUriFromRoot("dir2"),
)
}
}

Expand Down
25 changes: 23 additions & 2 deletions app/src/main/java/com/chiller3/rsaf/RcloneProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,30 @@ class RcloneProvider : DocumentsProvider(), SharedPreferences.OnSharedPreference
override fun isChildDocument(parentDocumentId: String, documentId: String): Boolean {
debugLog("isChildDocument($parentDocumentId, $documentId)")

val (splitParent, splitName) = splitPath(documentId)
// AOSP's FileSystemProvider [1] returns true [2] if parentDocumentId and documentId refer
// to the same path, but this conflicts with the documented behavior of isChildDocument(),
// which is supposed to test "if a document is descendant (child, grandchild, etc) from the
// given parent". We'll go with the documented behavior instead of matching AOSP's
// FileSystemProvider.
//
// [1] https://cs.android.com/android/platform/superproject/+/android-13.0.0_r49:frameworks/base/core/java/com/android/internal/content/FileSystemProvider.java;l=141
// [2] https://cs.android.com/android/platform/superproject/+/android-13.0.0_r49:frameworks/base/core/java/android/os/FileUtils.java;l=917

var currentDoc = documentId

while (true) {
val (splitParent, splitName) = splitPath(currentDoc)
if (splitName.isEmpty()) {
// Root of the remote
break
} else if (splitParent == parentDocumentId) {
return true
}

currentDoc = splitParent
}

return splitName.isNotEmpty() && splitParent == parentDocumentId
return false
}

/**
Expand Down