Skip to content

Commit

Permalink
[Fix] Allow direct access to external app data/obb directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed May 8, 2022
1 parent cd84d5a commit e356a9c
Showing 1 changed file with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import java8.nio.file.ProviderMismatchException
import java8.nio.file.WatchEvent
import java8.nio.file.WatchKey
import java8.nio.file.WatchService
import me.zhanghai.android.files.app.application
import me.zhanghai.android.files.compat.isPrimaryCompat
import me.zhanghai.android.files.compat.pathFileCompat
import me.zhanghai.android.files.provider.common.ByteString
Expand Down Expand Up @@ -82,27 +83,43 @@ internal class LinuxPath : ByteStringListPath<LinuxPath>, RootablePath {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R && !it.isPrimaryCompat) {
return@none false
}
val storageVolumeFile = it.pathFileCompat
if (!file.startsWith(storageVolumeFile)) {
val storageVolumeDirectory = it.pathFileCompat
if (!file.startsWith(storageVolumeDirectory)) {
return@none false
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
fun File.startsWithAndroidDataOrObb() =
startsWith(storageVolumeFile.resolve(FILE_ANDROID_DATA))
|| startsWith(storageVolumeFile.resolve(FILE_ANDROID_OBB))
if (isAttributeAccess) {
val parentFile = file.parentFile
if (parentFile != null && parentFile.startsWithAndroidDataOrObb()) {
return@none false
}
} else {
if (file.startsWithAndroidDataOrObb()) {
return@none false
}
}
return@none file.isAccessibleInStorageVolume(storageVolumeDirectory, isAttributeAccess)
}
}

private fun File.isAccessibleInStorageVolume(
storageVolumeDirectory: File,
isAttributeAccess: Boolean
): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val parentDirectory = parentFile
val androidDataDirectory = storageVolumeDirectory.resolve(FILE_ANDROID_DATA)
val isInAndroidDataDirectory = if (isAttributeAccess && parentDirectory != null) {
parentDirectory.startsWith(androidDataDirectory)
} else {
startsWith(androidDataDirectory)
}
val appPackageName = application.packageName
if (isInAndroidDataDirectory) {
val appDataDirectory = androidDataDirectory.resolve(appPackageName)
return startsWith(appDataDirectory)
}
val androidObbDirectory = storageVolumeDirectory.resolve(FILE_ANDROID_OBB)
val isInAndroidObbDirectory = if (isAttributeAccess && parentDirectory != null) {
parentDirectory.startsWith(androidObbDirectory)
} else {
startsWith(androidObbDirectory)
}
if (isInAndroidObbDirectory) {
val appObbDirectory = androidObbDirectory.resolve(appPackageName)
return startsWith(appObbDirectory)
}
return@none true
}
return true
}

private constructor(source: Parcel) : super(source) {
Expand Down

0 comments on commit e356a9c

Please sign in to comment.