diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/GameFileSync.kt b/app/src/main/java/com/droiddeck/launcher/frontend/GameFileSync.kt index e31b86dd09..275c0e95a8 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/GameFileSync.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/GameFileSync.kt @@ -43,11 +43,14 @@ object GameFileSync { val storageSetting = SessionPrefs.gameStorage(context) val previousStorage = prefs(context).getString("storagePath", null) if (storageSetting.isEmpty() && prefs(context).getString("storageSetting", null) == storageSetting && previousStorage != null) { - check(File(previousStorage).isDirectory) { context.getString(R.string.game_sync_err_previous_sd) } + check(libraryReadable(File(previousStorage))) { context.getString(R.string.game_sync_err_previous_sd) } } - storage?.let { check(File(it.path).listFiles() != null) { context.getString(R.string.game_sync_err_game_storage) } } + storage?.let { check(libraryReadable(File(it.path))) { context.getString(R.string.game_sync_err_game_storage) } } AddedGames.roots(context).forEach { check(it.host.listFiles() != null) { context.getString(R.string.game_sync_err_added_games) } } GameFiles.sync(context, folder, Library.launchableGames(context, strictRead = true)) prefs(context).edit().putString("storageSetting", storageSetting).putString("storagePath", storage?.path).apply() } + + internal fun libraryReadable(dir: File): Boolean = + dir.listFiles() != null || (!dir.exists() && dir.parentFile?.isDirectory == true) } diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/GameFileSyncTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/GameFileSyncTest.kt index a4e3b00283..93a7c352f3 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/GameFileSyncTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/GameFileSyncTest.kt @@ -47,4 +47,52 @@ class GameFileSyncTest { assertFalse(File(folder, "Geometry Wars.steam").exists()) assertEquals(0, folder.listFiles()!!.count { it.extension == GameFiles.EXTENSION }) } + + @Test fun libraryNotCreatedYetOnPresentStorageIsEmptyNotUnavailable() { + val context = RuntimeEnvironment.getApplication() + val root = LinuxRuntime.rootDir(context) + listOf("usr/bin/gamescope", LinuxRuntime.SESSION_SCRIPT.removePrefix("/"), "opt/android-host/proot", "opt/android-host/loader").forEach { path -> + File(root, path).apply { parentFile!!.mkdirs(); writeText("") } + } + SessionPrefs.setAddedGamesDirs(context, emptyList()) + File(root, "root/.local/share/Steam/steamapps").mkdirs() + val volume = File(context.filesDir, "card").apply { mkdirs() } + val library = File(volume, "steam") + SessionPrefs.setGameStorage(context, library.path, "Card") + val folder = File(context.filesDir, "exports-card").apply { mkdirs() } + GameFileSync.enable(context, folder) + assertEquals(folder.path, GameFileSync.folder(context)) + assertFalse(library.exists()) + library.writeText("") + assertThrows(IllegalStateException::class.java) { GameFileSync.syncConfigured(context) } + assertTrue(library.delete()) + GameFileSync.disable(context) + } + + @Test fun libraryReadableOnlyWhenListableOrNotCreatedOnPresentParent() { + val base = File(RuntimeEnvironment.getApplication().filesDir, "readable").apply { mkdirs() } + assertTrue(GameFileSync.libraryReadable(File(base, "dir").apply { mkdirs() })) + assertTrue(GameFileSync.libraryReadable(File(base, "missing"))) + assertFalse(GameFileSync.libraryReadable(File(base, "gone/steam"))) + assertFalse(GameFileSync.libraryReadable(File(base, "file").apply { writeText("") })) + } + + @Test fun previousAutomaticLibraryNotCreatedYetKeepsSyncingButRemovedDriveStops() { + val context = RuntimeEnvironment.getApplication() + val root = LinuxRuntime.rootDir(context) + listOf("usr/bin/gamescope", LinuxRuntime.SESSION_SCRIPT.removePrefix("/"), "opt/android-host/proot", "opt/android-host/loader").forEach { path -> + File(root, path).apply { parentFile!!.mkdirs(); writeText("") } + } + SessionPrefs.setAddedGamesDirs(context, emptyList()) + SessionPrefs.setGameStorage(context, "", "") + File(root, "root/.local/share/Steam/steamapps").mkdirs() + val folder = File(context.filesDir, "exports-auto").apply { mkdirs() } + val prefs = context.getSharedPreferences("game_files", android.content.Context.MODE_PRIVATE) + val volume = File(context.filesDir, "auto-card").apply { mkdirs() } + prefs.edit().putString("storageSetting", "").putString("storagePath", File(volume, "steam").path).apply() + GameFileSync.enable(context, folder) + prefs.edit().putString("storageSetting", "").putString("storagePath", File(context.filesDir, "removed-card/steam").path).apply() + assertThrows(IllegalStateException::class.java) { GameFileSync.syncConfigured(context) } + GameFileSync.disable(context) + } }