Skip to content

Commit

Permalink
Fix: Fix crash when opening archive files with content path
Browse files Browse the repository at this point in the history
By fixing Path.getParent() which shouldn't depend on Path.getRoot().

Fixes: #1225
  • Loading branch information
zhanghai committed May 18, 2024
1 parent 4cd7710 commit 4f1747c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ abstract class AbstractPath<T : AbstractPath<T>> : CovariantPath<T> {
return if (nameCount != 0) getName(nameCount - 1) else null
}

override fun getParent(): T? =
when (val nameCount = nameCount) {
0 -> null
1 -> root
else -> root!!.resolve(subpath(0, nameCount - 1))
}

override fun startsWith(other: String): Boolean = startsWith(fileSystem.getPath(other))

override fun endsWith(other: String): Boolean = endsWith(fileSystem.getPath(other))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ abstract class ByteStringListPath<T : ByteStringListPath<T>> : AbstractPath<T>,
val fileNameByteString: ByteString?
get() = segments.lastOrNull()

override fun getParent(): T? =
if (segments.isNotEmpty()) createPath(isAbsolute, segments.dropLast(1)) else null

override fun getNameCount(): Int = segments.size

override fun getName(index: Int): T = createPath(false, listOf(getNameByteString(index)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ internal class ContentPath : ByteStringListPath<ContentPath> {
override fun createPath(path: ByteString): ContentPath =
ContentPath(fileSystem, path.toString().toUri())

override fun createPath(absolute: Boolean, segments: List<ByteString>): ContentPath =
override fun createPath(absolute: Boolean, segments: List<ByteString>): ContentPath {
if (absolute) {
require(segments.size == 1) { segments.toString() }
createPath(segments.single())
} else {
ContentPath(fileSystem, segments)
require(segments.size == 2) {
"Cannot create absolute ContentPath with segments $segments"
}
}
return ContentPath(fileSystem, segments)
}

override val uriScheme: String
get() {
Expand Down Expand Up @@ -90,6 +91,8 @@ internal class ContentPath : ByteStringListPath<ContentPath> {

override fun getRoot(): ContentPath? = null

override fun getParent(): ContentPath? = null

override fun normalize(): ContentPath = this

override fun toUri(): URI = URI.create(uri!!.toString())
Expand Down

0 comments on commit 4f1747c

Please sign in to comment.