mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
fix(flatpak): pin app data to the storage it moved to, refuse read-only cards and symlinked destination folders
This commit is contained in:
1 parent
a1486bbcf4
commit
b74e168e58
3 files changed
+141
-22
No files matched your search
@@ -28,26 +28,43 @@ object FlatpakData {
|
||||
private const val SEP = ">"
|
||||
private val lock = Any()
|
||||
|
||||
fun enabled(context: Context): Set<String> = SessionPrefs.flatpakCardData(context)
|
||||
fun enabled(context: Context): Set<String> = SessionPrefs.flatpakCardData(context).map { it.substringBefore(SEP) }.toSet()
|
||||
|
||||
fun available(context: Context): Boolean = GameStorage.effective(context) != null
|
||||
|
||||
internal fun cardDir(context: Context, id: String): File? =
|
||||
GameStorage.effective(context)?.let { File(File(SecondaryLibrary.contentPath(File(it.path)), DIR), id) }
|
||||
private fun pins(context: Context): Map<String, File> =
|
||||
SessionPrefs.flatpakCardData(context).mapNotNull { entry ->
|
||||
val id = entry.substringBefore(SEP)
|
||||
val path = entry.substringAfter(SEP, "")
|
||||
if (path.isEmpty() || !ID.matches(id)) null else id to File(path)
|
||||
}.toMap()
|
||||
|
||||
private fun storage(context: Context, id: String): File? {
|
||||
val pinned = pins(context)[id] ?: return GameStorage.effective(context)?.let { File(it.path) }
|
||||
return pinned.takeIf { GameStorage.attached(it) }
|
||||
}
|
||||
|
||||
private fun cardIn(storage: File, id: String) = File(File(SecondaryLibrary.contentPath(storage), DIR), id)
|
||||
|
||||
internal fun cardDir(context: Context, id: String): File? = storage(context, id)?.let { cardIn(it, id) }
|
||||
|
||||
internal fun internalDir(context: Context, id: String) = File(LinuxRuntime.rootDir(context), "$HOST/$id")
|
||||
|
||||
fun binds(context: Context): List<String> = synchronized(lock) {
|
||||
val ids = enabled(context).filter { ID.matches(it) }.sorted()
|
||||
if (ids.isEmpty()) return@synchronized emptyList()
|
||||
if (!available(context)) {
|
||||
Log.w(TAG, "game storage is not attached; ${ids.size} app(s) use their internal data this session")
|
||||
return@synchronized emptyList()
|
||||
}
|
||||
ids.flatMap { id ->
|
||||
val card = cardDir(context, id) ?: return@flatMap emptyList()
|
||||
fun binds(context: Context, writable: (File) -> Boolean = { it.canWrite() }): List<String> = synchronized(lock) {
|
||||
val pinned = pins(context).toSortedMap()
|
||||
if (pinned.isEmpty()) return@synchronized emptyList()
|
||||
pinned.flatMap { (id, storage) ->
|
||||
if (!GameStorage.attached(storage)) {
|
||||
Log.w(TAG, "$id: its game storage is not attached; internal data this session")
|
||||
return@flatMap emptyList()
|
||||
}
|
||||
val card = cardIn(storage, id)
|
||||
val guest = internalDir(context, id)
|
||||
val kept = File(guest, KEPT)
|
||||
if (isLink(card) || isLink(guest)) {
|
||||
Log.w(TAG, "$id: a data folder is a symbolic link; internal this session")
|
||||
return@flatMap emptyList()
|
||||
}
|
||||
try {
|
||||
purge(guest)
|
||||
purge(card)
|
||||
@@ -58,6 +75,10 @@ object FlatpakData {
|
||||
Log.w(TAG, "$id: data folder could not be prepared; internal this session")
|
||||
return@flatMap emptyList()
|
||||
}
|
||||
if (!writable(card)) {
|
||||
Log.w(TAG, "$id: game storage is read-only; internal this session")
|
||||
return@flatMap emptyList()
|
||||
}
|
||||
listOf("${card.path}:$GUEST/$id", "${kept.path}:$GUEST/$id/$KEPT")
|
||||
}
|
||||
}
|
||||
@@ -65,10 +86,13 @@ object FlatpakData {
|
||||
fun move(
|
||||
context: Context, id: String, toCard: Boolean, onProgress: (String, Int) -> Unit,
|
||||
freeBytes: (File) -> Long = { runCatching { StatFs(it.path).availableBytes }.getOrDefault(0L) },
|
||||
writable: (File) -> Boolean = { it.canWrite() },
|
||||
): String? = synchronized(lock) {
|
||||
if (!ID.matches(id)) return@synchronized context.getString(R.string.store_data_failed)
|
||||
if (SessionState.occupied) return@synchronized context.getString(R.string.store_data_session)
|
||||
val card = cardDir(context, id) ?: return@synchronized context.getString(R.string.store_data_no_storage)
|
||||
val storage = storage(context, id) ?: return@synchronized context.getString(R.string.store_data_no_storage)
|
||||
val card = cardIn(storage, id)
|
||||
val pin = marker(id, storage)
|
||||
val internal = internalDir(context, id)
|
||||
val from = if (toCard) internal else card
|
||||
val to = if (toCard) card else internal
|
||||
@@ -77,8 +101,9 @@ object FlatpakData {
|
||||
val marker = marker(id, to)
|
||||
val placed = ArrayList<File>()
|
||||
try {
|
||||
if (hasSymlink(from)) return@synchronized context.getString(R.string.store_data_symlinks)
|
||||
if (isLink(to) || hasSymlink(from)) return@synchronized context.getString(R.string.store_data_symlinks)
|
||||
if (!to.isDirectory && !to.mkdirs()) throw IOException("could not create ${to.path}")
|
||||
if (card.exists() && !writable(card)) throw IOException("${card.path} is read-only")
|
||||
purge(from)
|
||||
purge(to)
|
||||
if (entries(to).isNotEmpty()) {
|
||||
@@ -100,8 +125,8 @@ object FlatpakData {
|
||||
placed.add(target)
|
||||
}
|
||||
remove(staging)
|
||||
if (!SessionPrefs.setFlatpakCardData(context, id, toCard)) {
|
||||
SessionPrefs.setFlatpakCardData(context, id, !toCard)
|
||||
if (!SessionPrefs.setFlatpakCardData(context, pin, toCard)) {
|
||||
SessionPrefs.setFlatpakCardData(context, pin, !toCard)
|
||||
throw IOException("could not save the data location")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
@@ -175,8 +200,10 @@ object FlatpakData {
|
||||
|
||||
private fun walk(top: File) = top.walkTopDown().onFail { _, e -> throw e }
|
||||
|
||||
private fun isLink(dir: File) = Files.isSymbolicLink(dir.toPath())
|
||||
|
||||
internal fun hasSymlink(dir: File): Boolean =
|
||||
Files.isSymbolicLink(dir.toPath()) || entries(dir).any { top -> walk(top).any { Files.isSymbolicLink(it.toPath()) } }
|
||||
isLink(dir) || entries(dir).any { top -> walk(top).any { Files.isSymbolicLink(it.toPath()) } }
|
||||
|
||||
internal fun size(dir: File): Long =
|
||||
entries(dir).sumOf { top -> walk(top).filter { Files.isRegularFile(it.toPath(), LinkOption.NOFOLLOW_LINKS) }.sumOf { it.length() } }
|
||||
|
||||
@@ -49,6 +49,13 @@ class FlatpakDataTest {
|
||||
|
||||
private fun move(app: String, toCard: Boolean) = FlatpakData.move(this.app, app, toCard, { _, _ -> }, { free })
|
||||
|
||||
private val gone = "/storage/0000-0000/Android/data/x/files/steam"
|
||||
|
||||
private fun pinTo(storage: String) {
|
||||
SessionPrefs.flatpakCardData(app).filter { it.startsWith("$id>") }.forEach { SessionPrefs.setFlatpakCardData(app, it, false) }
|
||||
SessionPrefs.setFlatpakCardData(app, "$id>$storage", true)
|
||||
}
|
||||
|
||||
@Test fun nothingIsBoundUntilAnAppOptsIn() {
|
||||
seed()
|
||||
assertTrue(FlatpakData.binds(app).isEmpty())
|
||||
@@ -120,7 +127,7 @@ class FlatpakDataTest {
|
||||
@Test fun anUnattachedCardFallsBackToInternalWithoutForgettingTheChoice() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
SessionPrefs.setGameStorage(app, "/storage/0000-0000/Android/data/x/files/steam", "gone")
|
||||
pinTo(gone)
|
||||
assertTrue(FlatpakData.binds(app).isEmpty())
|
||||
assertEquals(setOf(id), FlatpakData.enabled(app))
|
||||
assertEquals(app.getString(R.string.store_data_no_storage), move(id, false))
|
||||
@@ -244,10 +251,10 @@ class FlatpakDataTest {
|
||||
@Test fun dataWrittenWhileTheCardWasDetachedSurvivesItsReturn() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
SessionPrefs.setGameStorage(app, "/storage/0000-0000/Android/data/x/files/steam", "gone")
|
||||
pinTo(gone)
|
||||
assertTrue(FlatpakData.binds(app).isEmpty())
|
||||
val save = File(FlatpakData.internalDir(app, id), "save.dat").apply { writeText("new") }
|
||||
SessionPrefs.setGameStorage(app, card.path, "SD")
|
||||
pinTo(card.path)
|
||||
assertEquals(2, FlatpakData.binds(app).size)
|
||||
assertEquals("new", save.readText())
|
||||
assertEquals(app.getString(R.string.store_data_failed), move(id, false))
|
||||
@@ -324,4 +331,86 @@ class FlatpakDataTest {
|
||||
assertNull(move(id, true))
|
||||
assertEquals(setOf(id), FlatpakData.enabled(app))
|
||||
}
|
||||
|
||||
private fun otherWithOnlyKept(): File {
|
||||
val other = FlatpakData.internalDir(app, "org.other.App")
|
||||
File(other, ".ld.so/real").apply { mkdirs() }
|
||||
return other
|
||||
}
|
||||
|
||||
@Test fun aSymlinkAtTheInternalFolderRootRefusesTheMoveBack() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
val other = otherWithOnlyKept()
|
||||
val internal = FlatpakData.internalDir(app, id)
|
||||
assertTrue(internal.deleteRecursively())
|
||||
Files.createSymbolicLink(internal.toPath(), other.toPath())
|
||||
assertEquals(app.getString(R.string.store_data_symlinks), move(id, false))
|
||||
assertEquals(setOf(id), FlatpakData.enabled(app))
|
||||
assertEquals(1000, File(cardFolder(), ".srb2kart/addons/map.pk3").length())
|
||||
assertTrue(File(other, ".ld.so/real").exists())
|
||||
assertEquals(listOf(".ld.so"), other.list()!!.toList())
|
||||
assertTrue(Files.isSymbolicLink(internal.toPath()))
|
||||
}
|
||||
|
||||
@Test fun aSymlinkAtTheInternalFolderRootIsNeverBoundOrCleaned() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
val other = otherWithOnlyKept()
|
||||
val internal = FlatpakData.internalDir(app, id)
|
||||
assertTrue(internal.deleteRecursively())
|
||||
Files.createSymbolicLink(internal.toPath(), other.toPath())
|
||||
assertTrue(FlatpakData.binds(app).isEmpty())
|
||||
assertEquals(1000, File(cardFolder(), ".srb2kart/addons/map.pk3").length())
|
||||
assertEquals(listOf(".ld.so"), other.list()!!.toList())
|
||||
}
|
||||
|
||||
@Test fun aSymlinkAtTheCardFolderRootIsNeverBound() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
val other = otherWithOnlyKept()
|
||||
assertTrue(cardFolder().deleteRecursively())
|
||||
Files.createSymbolicLink(cardFolder().toPath(), other.toPath())
|
||||
assertTrue(FlatpakData.binds(app).isEmpty())
|
||||
assertEquals(listOf(".ld.so"), other.list()!!.toList())
|
||||
}
|
||||
|
||||
@Test fun dataStaysBoundToTheCardItWasMovedTo() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
val second = tmp.newFolder("second")
|
||||
SessionPrefs.setGameStorage(app, second.path, "SD2")
|
||||
assertEquals(listOf("${cardFolder().path}:/root/.var/app/$id", "${FlatpakData.internalDir(app, id).path}/.ld.so:/root/.var/app/$id/.ld.so"), FlatpakData.binds(app))
|
||||
assertFalse(File(second, "flatpak-data").exists())
|
||||
assertNull(move(id, false))
|
||||
assertEquals(1000, File(FlatpakData.internalDir(app, id), ".srb2kart/addons/map.pk3").length())
|
||||
assertFalse(cardFolder().exists())
|
||||
assertTrue(FlatpakData.enabled(app).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun aReadOnlyCardRefusesTheMoveAndKeepsTheData() {
|
||||
seed()
|
||||
assertEquals(app.getString(R.string.store_data_failed), FlatpakData.move(app, id, true, { _, _ -> }, { free }, { false }))
|
||||
assertEquals(1000, File(FlatpakData.internalDir(app, id), ".srb2kart/addons/map.pk3").length())
|
||||
assertTrue(FlatpakData.enabled(app).isEmpty())
|
||||
assertTrue(FlatpakData.entries(cardFolder()).isEmpty())
|
||||
assertTrue(SessionPrefs.flatpakDataMoving(app).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun aReadOnlyCardRefusesTheMoveBackAndKeepsTheChoice() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
assertEquals(app.getString(R.string.store_data_failed), FlatpakData.move(app, id, false, { _, _ -> }, { free }, { false }))
|
||||
assertEquals(setOf(id), FlatpakData.enabled(app))
|
||||
assertEquals(1000, File(cardFolder(), ".srb2kart/addons/map.pk3").length())
|
||||
assertTrue(FlatpakData.entries(FlatpakData.internalDir(app, id)).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun aReadOnlyCardFallsBackToInternalDataWithoutForgettingTheChoice() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
assertTrue(FlatpakData.binds(app) { false }.isEmpty())
|
||||
assertEquals(setOf(id), FlatpakData.enabled(app))
|
||||
assertEquals(1000, File(cardFolder(), ".srb2kart/addons/map.pk3").length())
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,8 @@ internal one. `FlatpakData.binds` then binds the card folder over `/root/.var/ap
|
||||
and `BwrapSpawner` translates the sandbox's binds through it like any other session bind.
|
||||
|
||||
- The move is per app and refused from session start until its teardown finishes, when the data holds symlinks in either location (exFAT and FAT
|
||||
cannot store them), or when the card lacks the size plus 64 MB.
|
||||
cannot store them) or either data folder itself is a symlink, when the card is read-only, or when the
|
||||
card lacks the size plus 64 MB.
|
||||
- `.ld.so` is not moved. Flatpak fills it with a symlink to the runtime's linker cache, so it stays
|
||||
internal and is bound over the card folder.
|
||||
- The destination and its path are journaled (`flatpakDataMoving`) before anything is written. A later
|
||||
@@ -91,7 +92,9 @@ and `BwrapSpawner` translates the sandbox's binds through it like any other sess
|
||||
refused. The old copy is renamed to `.<id>.old` beside it and then deleted, so leftovers are always
|
||||
recognisable and the next move or session start removes them. Live data is never deleted to tidy up. A
|
||||
folder that cannot be listed fails the move instead of counting as empty.
|
||||
- With the card detached, the app uses its internal folder for that session; the choice is kept. Those
|
||||
- The choice records the game storage the data was moved to (`flatpakCardData`, `<id>><storage path>`).
|
||||
Sessions and the move back keep using that volume when the game storage setting later points elsewhere.
|
||||
- With that card detached or read-only, the app uses its internal folder for that session; the choice is kept. Those
|
||||
files stay untouched when the card returns, and a move back is refused while they are there.
|
||||
- "Move data to internal storage" reverses it.
|
||||
|
||||
|
||||
Reference in new issue
Block a user