From 4f1747caf182987dd1d2c33870d00e6ce67966a9 Mon Sep 17 00:00:00 2001 From: Hai Zhang Date: Sat, 18 May 2024 16:12:39 -0700 Subject: [PATCH] Fix: Fix crash when opening archive files with content path By fixing Path.getParent() which shouldn't depend on Path.getRoot(). Fixes: #1225 --- .../android/files/provider/common/AbstractPath.kt | 7 ------- .../files/provider/common/ByteStringListPath.kt | 3 +++ .../android/files/provider/content/ContentPath.kt | 13 ++++++++----- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/me/zhanghai/android/files/provider/common/AbstractPath.kt b/app/src/main/java/me/zhanghai/android/files/provider/common/AbstractPath.kt index 8d73bd2f1..b2b6151c8 100644 --- a/app/src/main/java/me/zhanghai/android/files/provider/common/AbstractPath.kt +++ b/app/src/main/java/me/zhanghai/android/files/provider/common/AbstractPath.kt @@ -17,13 +17,6 @@ abstract class AbstractPath> : CovariantPath { 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)) diff --git a/app/src/main/java/me/zhanghai/android/files/provider/common/ByteStringListPath.kt b/app/src/main/java/me/zhanghai/android/files/provider/common/ByteStringListPath.kt index c6b9e9d1d..d31cf30c4 100644 --- a/app/src/main/java/me/zhanghai/android/files/provider/common/ByteStringListPath.kt +++ b/app/src/main/java/me/zhanghai/android/files/provider/common/ByteStringListPath.kt @@ -76,6 +76,9 @@ abstract class ByteStringListPath> : AbstractPath, 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))) diff --git a/app/src/main/java/me/zhanghai/android/files/provider/content/ContentPath.kt b/app/src/main/java/me/zhanghai/android/files/provider/content/ContentPath.kt index 363263f30..ab1480fca 100644 --- a/app/src/main/java/me/zhanghai/android/files/provider/content/ContentPath.kt +++ b/app/src/main/java/me/zhanghai/android/files/provider/content/ContentPath.kt @@ -53,13 +53,14 @@ internal class ContentPath : ByteStringListPath { override fun createPath(path: ByteString): ContentPath = ContentPath(fileSystem, path.toString().toUri()) - override fun createPath(absolute: Boolean, segments: List): ContentPath = + override fun createPath(absolute: Boolean, segments: List): 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() { @@ -90,6 +91,8 @@ internal class ContentPath : ByteStringListPath { override fun getRoot(): ContentPath? = null + override fun getParent(): ContentPath? = null + override fun normalize(): ContentPath = this override fun toUri(): URI = URI.create(uri!!.toString())