Skip to content

Commit

Permalink
Fix: Fix ContentPath segments
Browse files Browse the repository at this point in the history
So that its startsWith() etc work correctly for the same file
name (either in the same provider or not) despite having the same
ContentFileSystem.
  • Loading branch information
zhanghai committed Apr 16, 2024
1 parent ea8c4a5 commit 1169cb6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ internal class ContentFileSystem(private val provider: ContentFileSystemProvider

override fun isReadOnly(): Boolean = false

override fun getSeparator(): String {
throw UnsupportedOperationException()
}
override fun getSeparator(): String = SEPARATOR_STRING

override fun getRootDirectories(): Iterable<Path> = emptyList()

Expand Down Expand Up @@ -80,6 +78,9 @@ internal class ContentFileSystem(private val provider: ContentFileSystemProvider
override fun writeToParcel(dest: Parcel, flags: Int) {}

companion object {
const val SEPARATOR = '/'.code.toByte()
private const val SEPARATOR_STRING = SEPARATOR.toInt().toChar().toString()

@JvmField
val CREATOR = object : Parcelable.Creator<ContentFileSystem> {
override fun createFromParcel(source: Parcel): ContentFileSystem =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ internal class ContentPath : ByteStringListPath<ContentPath> {
val uri: Uri?

constructor(fileSystem: ContentFileSystem, uri: Uri) : super(
0.toByte(), true, listOf(uri.displayNameOrUri)
ContentFileSystem.SEPARATOR, true,
listOf(Uri.encode(uri.toString()).toByteString(), uri.bestFileName)
) {
this.fileSystem = fileSystem
this.uri = uri
}

private constructor(fileSystem: ContentFileSystem, segments: List<ByteString>) : super(
0.toByte(), false, segments
ContentFileSystem.SEPARATOR, false, segments
) {
this.fileSystem = fileSystem
uri = null
Expand Down Expand Up @@ -149,14 +150,14 @@ internal class ContentPath : ByteStringListPath<ContentPath> {
}

companion object {
private val Uri.displayNameOrUri: ByteString
private val Uri.bestFileName: ByteString
get() =
(try {
Resolver.getDisplayName(this)
} catch (e: ResolverException) {
e.printStackTrace()
null
} ?: lastPathSegment ?: toString()).toByteString()
} ?: lastPathSegment ?: "file").toByteString()

@JvmField
val CREATOR = object : Parcelable.Creator<ContentPath> {
Expand Down

0 comments on commit 1169cb6

Please sign in to comment.