|
|
|
@@ -15,18 +15,29 @@ import java.security.MessageDigest
|
|
|
|
|
* - `rollback <version>`: copies the schedules aside as schedule-rollback-<version>, as every install does.
|
|
|
|
|
* - `catalogue`: a read-only copy of the library catalogue beside it, for checking new channels' titles against the library.
|
|
|
|
|
* - `clear`: removes the copy once it's been pulled.
|
|
|
|
|
* - `restore` (8 November 2026, audit U12): puts a pulled snapshot back. See [InstallRestore]: only with `confirm RESTORE`,
|
|
|
|
|
* only while Orbit's screen isn't open, only files a snapshot takes, each checked against its checksum first, and the
|
|
|
|
|
* current data is kept aside before anything is replaced.
|
|
|
|
|
*
|
|
|
|
|
* It only ever copies; it never changes the schedules, the library or the sign-in.
|
|
|
|
|
* Apart from `restore`, it only ever copies; it never changes the schedules, the library or the sign-in.
|
|
|
|
|
*/
|
|
|
|
|
class InstallSnapshotReceiver : BroadcastReceiver() {
|
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
|
|
|
val result = runCatching { InstallSnapshot.handle(context, intent.getStringExtra("do").orEmpty(), intent.getStringExtra("version")) }
|
|
|
|
|
val result = runCatching {
|
|
|
|
|
if (intent.getStringExtra("do") == "restore") {
|
|
|
|
|
if (InstallSnapshot.screenOpen) "refused: Orbit is open; force-stop it first"
|
|
|
|
|
else InstallRestore.apply(context.filesDir.parentFile!!, File(context.getExternalFilesDir(null), "install-restore"),
|
|
|
|
|
intent.getStringExtra("confirm"), System.currentTimeMillis())
|
|
|
|
|
} else InstallSnapshot.handle(context, intent.getStringExtra("do").orEmpty(), intent.getStringExtra("version"))
|
|
|
|
|
}
|
|
|
|
|
resultData = result.fold({ it }, { "failed" })
|
|
|
|
|
android.util.Log.i("OrbitInstall", "${intent.getStringExtra("do")}: $resultData")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal object InstallSnapshot {
|
|
|
|
|
/** Orbit's screen is open in this process (set by MainActivity); a restore waits until it isn't. */
|
|
|
|
|
@Volatile var screenOpen = false
|
|
|
|
|
private val versionShape = Regex("""\d+\.\d+\.\d+""")
|
|
|
|
|
|
|
|
|
|
fun handle(context: Context, action: String, version: String?): String {
|
|
|
|
@@ -68,3 +79,57 @@ internal object InstallSnapshot {
|
|
|
|
|
digest.digest().joinToString("") { "%02x".format(it) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Putting a snapshot back (8 November 2026, audit U12: Android won't install an older Orbit over a newer one, so without
|
|
|
|
|
* this a bad install could only be fixed forward). adb pushes a pulled snapshot to Orbit's shared folder as
|
|
|
|
|
* install-restore/, with restore.txt listing each file's SHA-256 (`sha256sum` format). Then, with Orbit force-stopped:
|
|
|
|
|
*
|
|
|
|
|
* - nothing happens without `confirm RESTORE`;
|
|
|
|
|
* - only what a snapshot takes can come back: no_backup/schedule/... and the settings and profiles prefs. Never the
|
|
|
|
|
* library catalogue, the sign-in or the PIN;
|
|
|
|
|
* - every listed file is checked against its checksum before anything is touched, and any problem refuses the lot;
|
|
|
|
|
* - the current schedules and prefs are kept aside as no_backup/before-restore-<time> (the last two are kept);
|
|
|
|
|
* - the schedules are copied in beside the old ones and swapped in by renaming, so a failure leaves the old ones in place.
|
|
|
|
|
*/
|
|
|
|
|
internal object InstallRestore {
|
|
|
|
|
private val allowed = Regex("""no_backup/schedule/[A-Za-z0-9._-]+(/[A-Za-z0-9._-]+)?|shared_prefs/orbit_(settings|profiles)\.xml""")
|
|
|
|
|
|
|
|
|
|
fun apply(data: File, source: File, confirm: String?, nowMs: Long): String {
|
|
|
|
|
if (confirm != "RESTORE") return "refused: needs confirm RESTORE"
|
|
|
|
|
val manifest = File(source, "restore.txt").takeIf { it.isFile } ?: return "refused: no restore.txt"
|
|
|
|
|
val listed = manifest.readLines().filter { it.isNotBlank() }.map { line ->
|
|
|
|
|
val hash = line.substringBefore(' '); val path = line.substringAfter(' ').trimStart(' ', '*')
|
|
|
|
|
if (!Regex("[0-9a-f]{64}").matches(hash) || !allowed.matches(path) || path.split('/').any { it == ".." || it == "." || it.isEmpty() })
|
|
|
|
|
return "refused: not allowed: ${path.take(80)}"
|
|
|
|
|
path to hash
|
|
|
|
|
}
|
|
|
|
|
if (listed.none { it.first.startsWith("no_backup/schedule/") }) return "refused: no schedules listed"
|
|
|
|
|
for ((path, hash) in listed) {
|
|
|
|
|
val f = File(source, path)
|
|
|
|
|
if (!f.isFile || sha256(f) != hash) return "refused: checksum: ${path.take(80)}"
|
|
|
|
|
}
|
|
|
|
|
// Keep what's there now, before anything changes.
|
|
|
|
|
val aside = File(data, "no_backup/before-restore-$nowMs")
|
|
|
|
|
File(data, "no_backup/schedule").takeIf { it.isDirectory }?.copyRecursively(File(aside, "schedule"), overwrite = true)
|
|
|
|
|
listOf("orbit_settings.xml", "orbit_profiles.xml").forEach { n -> File(data, "shared_prefs/$n").takeIf { it.isFile }?.copyTo(File(aside, "shared_prefs/$n"), overwrite = true) }
|
|
|
|
|
File(data, "no_backup").listFiles { f -> f.name.startsWith("before-restore-") }.orEmpty().sortedBy { it.name }.dropLast(2).forEach { it.deleteRecursively() }
|
|
|
|
|
// The schedules: built beside, then swapped in.
|
|
|
|
|
val incoming = File(data, "no_backup/schedule.restoring").apply { deleteRecursively(); mkdirs() }
|
|
|
|
|
for ((path, _) in listed.filter { it.first.startsWith("no_backup/schedule/") })
|
|
|
|
|
File(source, path).copyTo(File(incoming, path.removePrefix("no_backup/schedule/")), overwrite = true)
|
|
|
|
|
val live = File(data, "no_backup/schedule"); val old = File(data, "no_backup/schedule.replaced")
|
|
|
|
|
old.deleteRecursively()
|
|
|
|
|
if (live.exists() && !live.renameTo(old)) { incoming.deleteRecursively(); return "failed: couldn't move the current schedules" }
|
|
|
|
|
if (!incoming.renameTo(live)) { old.renameTo(live); incoming.deleteRecursively(); return "failed: couldn't swap in; current schedules kept" }
|
|
|
|
|
old.deleteRecursively()
|
|
|
|
|
for ((path, _) in listed.filter { it.first.startsWith("shared_prefs/") }) writeSafely(File(data, path)) { out -> File(source, path).inputStream().use { it.copyTo(out) } }
|
|
|
|
|
return "restored ${listed.size}; aside before-restore-$nowMs"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun sha256(file: File): String = file.inputStream().use { input ->
|
|
|
|
|
val digest = MessageDigest.getInstance("SHA-256"); val buffer = ByteArray(64 * 1024)
|
|
|
|
|
while (true) { val n = input.read(buffer); if (n < 0) break; digest.update(buffer, 0, n) }
|
|
|
|
|
digest.digest().joinToString("") { "%02x".format(it) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|