Avoid being killed by SELinux when retrieving volume path

This commit is contained in:
Matéo Duparc 2024-02-11 17:55:24 +01:00
parent c26ab661c2
commit 6f43bc7417
Signed by untrusted user: hardcoresushi
GPG Key ID: AFE384344A45E13A

View File

@ -3,6 +3,7 @@ package sushi.hardcore.droidfs.util
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Environment import android.os.Environment
import android.os.storage.StorageManager import android.os.storage.StorageManager
import android.provider.DocumentsContract import android.provider.DocumentsContract
@ -111,24 +112,27 @@ object PathUtils {
} }
} }
Log.d(PATH_RESOLVER_TAG, "getExternalFilesDirs failed") Log.d(PATH_RESOLVER_TAG, "getExternalFilesDirs failed")
try { // Don't risk to be killed by SELinux on newer Android versions
val process = ProcessBuilder("mount").redirectErrorStream(true).start().apply { waitFor() } if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
process.inputStream.readBytes().decodeToString().split("\n").forEach { line -> try {
if (line.startsWith("/dev/block/vold")) { val process = ProcessBuilder("mount").redirectErrorStream(true).start().apply { waitFor() }
Log.d(PATH_RESOLVER_TAG, "mount: $line") process.inputStream.readBytes().decodeToString().split("\n").forEach { line ->
val fields = line.split(" ") if (line.startsWith("/dev/block/vold")) {
if (fields.size >= 3) { Log.d(PATH_RESOLVER_TAG, "mount: $line")
val path = fields[2] val fields = line.split(" ")
if (File(path).name == name) { if (fields.size >= 3) {
return path val path = fields[2]
if (File(path).name == name) {
return path
}
} }
} }
} }
} catch (e: Exception) {
e.printStackTrace()
} }
} catch (e: Exception) { Log.d(PATH_RESOLVER_TAG, "mount processing failed")
e.printStackTrace()
} }
Log.d(PATH_RESOLVER_TAG, "mount processing failed")
return null return null
} }