mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
fix(flatpak): keep the move journal until the destination is verified empty and never clear saves written while the card was detached
This commit is contained in:
1 parent
b74e168e58
commit
9f310d672d
2 files changed
+78
-29
No files matched your search
@@ -51,38 +51,42 @@ object FlatpakData {
|
||||
internal fun internalDir(context: Context, id: String) = File(LinuxRuntime.rootDir(context), "$HOST/$id")
|
||||
|
||||
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()
|
||||
pins(context).toSortedMap().flatMap { (id, storage) ->
|
||||
bind(context, id, storage, writable).also {
|
||||
if (it.isEmpty()) SessionPrefs.setFlatpakDataMoving(context, marker(id, internalDir(context, id)), false)
|
||||
}
|
||||
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)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "$id: the old copy of a moved folder could not be removed", e)
|
||||
}
|
||||
if (!card.isDirectory && !card.mkdirs() || !kept.isDirectory && !kept.mkdirs() || !File(card, KEPT).isDirectory && !File(card, KEPT).mkdirs()) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
private fun bind(context: Context, id: String, storage: File, writable: (File) -> Boolean): List<String> {
|
||||
if (!GameStorage.attached(storage)) {
|
||||
Log.w(TAG, "$id: its game storage is not attached; internal data this session")
|
||||
return 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 emptyList()
|
||||
}
|
||||
try {
|
||||
purge(guest)
|
||||
purge(card)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "$id: the old copy of a moved folder could not be removed", e)
|
||||
}
|
||||
if (!card.isDirectory && !card.mkdirs() || !kept.isDirectory && !kept.mkdirs() || !File(card, KEPT).isDirectory && !File(card, KEPT).mkdirs()) {
|
||||
Log.w(TAG, "$id: data folder could not be prepared; internal this session")
|
||||
return emptyList()
|
||||
}
|
||||
if (!writable(card)) {
|
||||
Log.w(TAG, "$id: game storage is read-only; internal this session")
|
||||
return emptyList()
|
||||
}
|
||||
return listOf("${card.path}:$GUEST/$id", "${kept.path}:$GUEST/$id/$KEPT")
|
||||
}
|
||||
|
||||
fun move(
|
||||
context: Context, id: String, toCard: Boolean, onProgress: (String, Int) -> Unit,
|
||||
freeBytes: (File) -> Long = { runCatching { StatFs(it.path).availableBytes }.getOrDefault(0L) },
|
||||
@@ -131,7 +135,7 @@ object FlatpakData {
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "$id: move to ${if (toCard) "game storage" else "internal"} failed", e)
|
||||
val cleaned = placed.map { remove(it) }.all { it } && remove(staging)
|
||||
val cleaned = placed.map { remove(it) }.all { it } && remove(staging) && runCatching { entries(to).isEmpty() }.getOrDefault(false)
|
||||
if (cleaned) SessionPrefs.setFlatpakDataMoving(context, marker, false)
|
||||
return@synchronized context.getString(R.string.store_data_failed)
|
||||
}
|
||||
|
||||
@@ -238,6 +238,51 @@ class FlatpakDataTest {
|
||||
assertEquals(1000, File(internal, ".srb2kart/addons/map.pk3").length())
|
||||
}
|
||||
|
||||
@Test fun aFailedRetryKeepsTheJournalUntilTheDestinationIsVerifiedEmpty() {
|
||||
seed()
|
||||
val marker = FlatpakData.marker(id, cardFolder())
|
||||
assertTrue(SessionPrefs.setFlatpakDataMoving(app, marker, true))
|
||||
File(card, "flatpak-data").mkdirs()
|
||||
cardFolder().writeText("blocked")
|
||||
assertEquals(app.getString(R.string.store_data_failed), move(id, true))
|
||||
assertEquals(setOf(marker), SessionPrefs.flatpakDataMoving(app))
|
||||
assertTrue(cardFolder().delete())
|
||||
File(cardFolder(), ".srb2kart").mkdirs()
|
||||
File(cardFolder(), "partial.bin").writeText("half")
|
||||
assertNull(move(id, true))
|
||||
assertFalse(File(cardFolder(), "partial.bin").exists())
|
||||
assertEquals(1000, File(cardFolder(), ".srb2kart/addons/map.pk3").length())
|
||||
assertTrue(SessionPrefs.flatpakDataMoving(app).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun savesWrittenWhileDetachedAreNotClearedByAnInterruptedMoveBackRetry() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
val internal = FlatpakData.internalDir(app, id)
|
||||
assertTrue(SessionPrefs.setFlatpakDataMoving(app, FlatpakData.marker(id, internal), true))
|
||||
File(internal, "partial.bin").writeText("half")
|
||||
pinTo(gone)
|
||||
assertTrue(FlatpakData.binds(app).isEmpty())
|
||||
val save = File(internal, "save.dat").apply { writeText("new") }
|
||||
pinTo(card.path)
|
||||
assertEquals(app.getString(R.string.store_data_failed), move(id, false))
|
||||
assertEquals("new", save.readText())
|
||||
assertEquals(1000, File(cardFolder(), ".srb2kart/addons/map.pk3").length())
|
||||
assertEquals(setOf(id), FlatpakData.enabled(app))
|
||||
}
|
||||
|
||||
@Test fun anInterruptedMoveBackStaysRetryableWhenTheCardSessionRanAttached() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
val internal = FlatpakData.internalDir(app, id)
|
||||
assertTrue(SessionPrefs.setFlatpakDataMoving(app, FlatpakData.marker(id, internal), true))
|
||||
File(internal, "partial.bin").writeText("half")
|
||||
assertEquals(2, FlatpakData.binds(app).size)
|
||||
assertNull(move(id, false))
|
||||
assertFalse(File(internal, "partial.bin").exists())
|
||||
assertEquals(1000, File(internal, ".srb2kart/addons/map.pk3").length())
|
||||
}
|
||||
|
||||
@Test fun oldDataLeftAtTheSourceIsDroppedAtSessionStart() {
|
||||
seed()
|
||||
assertNull(move(id, true))
|
||||
|
||||
Reference in new issue
Block a user