mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 22:05:59 +00:00
perf(storage): prefer verified direct SD library access
This commit is contained in:
1 parent
4fc2162203
commit
7f141314c6
6 files changed
+65
-457
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
|
||||
|
||||
/** Android shared storage cannot create the links in Wine prefixes or native Steam depots. */
|
||||
object SecondaryLibrary {
|
||||
@@ -17,6 +18,30 @@ object SecondaryLibrary {
|
||||
"3127680" to "FEX-Emu",
|
||||
)
|
||||
|
||||
/** Some devices permit direct access to the backing volume; others require Android's FUSE path. */
|
||||
fun contentPath(library: File): File {
|
||||
val path = library.absolutePath
|
||||
if (!path.startsWith("/storage/")) return library
|
||||
return verifiedAlias(library, File("/mnt/media_rw/" + path.removePrefix("/storage/")))
|
||||
}
|
||||
|
||||
internal fun verifiedAlias(library: File, candidate: File): File {
|
||||
if (!candidate.isDirectory || !candidate.canRead() || !candidate.canWrite()) 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 +49,7 @@ object SecondaryLibrary {
|
||||
}
|
||||
|
||||
/** Parent binds come first; the more specific private directories override them in PRoot. */
|
||||
fun binds(files: File, library: File): List<String> {
|
||||
fun binds(files: File, library: File, content: File = library): List<String> {
|
||||
val private = privateRoot(files, library)
|
||||
val apps = File(library, "steamapps")
|
||||
val tools = LinkedHashMap(bootstrapTools)
|
||||
@@ -58,7 +83,7 @@ object SecondaryLibrary {
|
||||
}
|
||||
}
|
||||
return listOf("/mnt/droiddeck-sd", "/mnt/bannerlator-sd").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" }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -799,9 +799,10 @@ class SessionService : Service() {
|
||||
// Links, prefixes and Steam entries made before the rename still name the old path.
|
||||
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 content = SecondaryLibrary.contentPath(File(library.path))
|
||||
binds.addAll(SecondaryLibrary.binds(filesDir, File(library.path), content))
|
||||
storageDiagnosticLibrary = content
|
||||
Log.i(TAG, "game storage: ${content.path} -> /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)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,34 @@ import org.junit.rules.TemporaryFolder
|
||||
class SecondaryLibraryTest {
|
||||
@get:Rule val tmp = TemporaryFolder()
|
||||
|
||||
@Test fun directContentKeepsTheConfiguredLibrarysPrivateDirectories() {
|
||||
val files = tmp.newFolder("files")
|
||||
val library = tmp.newFolder("shared")
|
||||
val content = tmp.newFolder("direct")
|
||||
val private = SecondaryLibrary.privateRoot(files, library)
|
||||
val binds = SecondaryLibrary.binds(files, library, content)
|
||||
assertTrue(binds.contains("${content.path}:/mnt/droiddeck-sd"))
|
||||
assertTrue(binds.contains("$private/steamapps/compatdata:/mnt/droiddeck-sd/steamapps/compatdata"))
|
||||
assertFalse(SecondaryLibrary.privateRoot(files, content).exists())
|
||||
}
|
||||
|
||||
@Test fun aWritableAliasIsSelectedAndItsProbeIsRemoved() {
|
||||
val library = tmp.newFolder("shared")
|
||||
val candidate = File(tmp.root, "direct")
|
||||
Files.createSymbolicLink(candidate.toPath(), library.toPath())
|
||||
assertEquals(candidate, SecondaryLibrary.verifiedAlias(library, candidate))
|
||||
assertTrue(library.listFiles()!!.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun anUnrelatedOrMissingDirectoryCannotReplaceTheLibrary() {
|
||||
val library = tmp.newFolder("shared")
|
||||
val candidate = tmp.newFolder("other")
|
||||
assertEquals(library, SecondaryLibrary.verifiedAlias(library, candidate))
|
||||
assertTrue(candidate.listFiles()!!.isEmpty())
|
||||
assertTrue(library.listFiles()!!.isEmpty())
|
||||
assertEquals(library, SecondaryLibrary.verifiedAlias(library, File(tmp.root, "missing")))
|
||||
}
|
||||
|
||||
@Test fun gamesStaySharedButPrefixesAndNativeDownloadsArePrivate() {
|
||||
val files = tmp.newFolder("files")
|
||||
val library = tmp.newFolder("card")
|
||||
|
||||
Reference in new issue
Block a user