From 6f43bc7417893a441aba73b1319018e7efc23e3a Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sun, 11 Feb 2024 17:55:24 +0100 Subject: [PATCH] Avoid being killed by SELinux when retrieving volume path --- .../sushi/hardcore/droidfs/util/PathUtils.kt | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt b/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt index 173b826..562cb62 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt @@ -3,6 +3,7 @@ package sushi.hardcore.droidfs.util import android.content.ActivityNotFoundException import android.content.Context import android.net.Uri +import android.os.Build import android.os.Environment import android.os.storage.StorageManager import android.provider.DocumentsContract @@ -111,24 +112,27 @@ object PathUtils { } } Log.d(PATH_RESOLVER_TAG, "getExternalFilesDirs failed") - try { - val process = ProcessBuilder("mount").redirectErrorStream(true).start().apply { waitFor() } - process.inputStream.readBytes().decodeToString().split("\n").forEach { line -> - if (line.startsWith("/dev/block/vold")) { - Log.d(PATH_RESOLVER_TAG, "mount: $line") - val fields = line.split(" ") - if (fields.size >= 3) { - val path = fields[2] - if (File(path).name == name) { - return path + // Don't risk to be killed by SELinux on newer Android versions + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { + try { + val process = ProcessBuilder("mount").redirectErrorStream(true).start().apply { waitFor() } + process.inputStream.readBytes().decodeToString().split("\n").forEach { line -> + if (line.startsWith("/dev/block/vold")) { + Log.d(PATH_RESOLVER_TAG, "mount: $line") + val fields = line.split(" ") + if (fields.size >= 3) { + val path = fields[2] + if (File(path).name == name) { + return path + } } } } + } catch (e: Exception) { + e.printStackTrace() } - } catch (e: Exception) { - e.printStackTrace() + Log.d(PATH_RESOLVER_TAG, "mount processing failed") } - Log.d(PATH_RESOLVER_TAG, "mount processing failed") return null }