Verify native SD-card library paths before binding them (#757)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-14 00:59:38 -04:00
1 parent f9b58837d0
commit 9f2e364157
3 files changed
+81 -8

No files matched your search

@@ -6,6 +6,7 @@ import java.nio.file.LinkOption.NOFOLLOW_LINKS
import java.nio.file.StandardCopyOption.COPY_ATTRIBUTES
import java.security.MessageDigest
import java.util.Locale
import java.util.UUID
object SecondaryLibrary {
private val bootstrapTools = linkedMapOf(
@@ -16,6 +17,34 @@ object SecondaryLibrary {
"3127680" to "FEX-Emu",
)
fun contentPath(library: File): File {
val path = library.absolutePath
if (!path.startsWith("/storage/")) return library
val candidate = File("/mnt/media_rw/" + path.removePrefix("/storage/"))
return verifiedAlias(library, candidate)
}
internal fun verifiedAlias(
library: File,
candidate: File,
readableAndWritable: (File) -> Boolean = { it.canRead() && it.canWrite() },
): File {
if (!candidate.isDirectory || !readableAndWritable(candidate)) return library
var probe: File? = null
return try {
val created = File.createTempFile(".droiddeck-path-", ".probe", candidate)
probe = created
val token = UUID.randomUUID().toString()
created.writeText(token)
val visible = File(library, created.name)
if (visible.readText() == token && visible.delete() && !created.exists()) candidate else library
} catch (_: Exception) {
library
} finally {
probe?.delete()
}
}
fun privateRoot(files: File, library: File): File {
val digest = MessageDigest.getInstance("SHA-256").digest(library.canonicalPath.toByteArray())
.joinToString("") { "%02x".format(it.toInt() and 255) }
@@ -24,7 +53,7 @@ object SecondaryLibrary {
val CARD_GUESTS = listOf("/mnt/droiddeck-sd", "/mnt/bannerlator-sd")
fun binds(files: File, library: File, guests: List<String> = CARD_GUESTS): List<String> {
fun binds(files: File, library: File, guests: List<String> = CARD_GUESTS, content: File = library): List<String> {
val private = privateRoot(files, library)
val apps = File(library, "steamapps")
val tools = LinkedHashMap(bootstrapTools)
@@ -52,7 +81,7 @@ object SecondaryLibrary {
}
}
return guests.flatMap { guest ->
listOf("${library.path}:$guest") + overrides.map { (host, relative) -> "${host.path}:$guest/$relative" }
listOf("${content.path}:$guest") + overrides.map { (host, relative) -> "${host.path}:$guest/$relative" }
}
}
@@ -687,9 +687,12 @@ class SessionService : Service() {
File(LinuxRuntime.rootDir(this), "mnt/droiddeck-sd").mkdirs()
File(LinuxRuntime.rootDir(this), "mnt/bannerlator-sd").mkdirs()
try {
binds.addAll(SecondaryLibrary.binds(filesDir, File(library.path)))
storageDiagnosticLibrary = File(library.path)
Log.i(TAG, "game storage: ${library.path} -> /mnt/droiddeck-sd (\"${library.label}\"); prefixes and native tools private")
val configured = File(library.path)
val content = SecondaryLibrary.contentPath(configured)
binds.addAll(SecondaryLibrary.binds(filesDir, configured, content = content))
storageDiagnosticLibrary = content
val route = if (content === configured) "configured" else "verified-native"
Log.i(TAG, "game storage: ${content.path} ($route) -> /mnt/droiddeck-sd (\"${library.label}\"); prefixes and native tools private")
} catch (e: Exception) {
Log.w(TAG, "game storage: private directories could not be prepared; internal only this session", e)
}
@@ -704,9 +707,9 @@ class SessionService : Service() {
val device = StorageDiagnostics.writeSnapshot(
sessionDir, target,
selectedLibrary = if (storageDiagnosticLibrary != null) "secondary" else "internal",
removable = storageDiagnosticLibrary?.let {
runCatching { Environment.isExternalStorageRemovable(it) }.getOrDefault(false)
} ?: false,
removable = storageDiagnosticLibrary != null && library?.let {
runCatching { Environment.isExternalStorageRemovable(File(it.path)) }.getOrDefault(false)
} == true,
)
if (device != null) {
val envAt = guest.indexOf(LinuxRuntime.SESSION_SCRIPT).takeIf { it >= 0 } ?: guest.size