diff --git a/controller/app/src/main/java/org/iiab/controller/env/EnvironmentLock.java b/controller/app/src/main/java/org/iiab/controller/env/EnvironmentLock.java new file mode 100644 index 000000000..18c1244a2 --- /dev/null +++ b/controller/app/src/main/java/org/iiab/controller/env/EnvironmentLock.java @@ -0,0 +1,159 @@ +/* + * ============================================================================ + * Name : EnvironmentLock.java + * Author : AppDevForAll + * Copyright : Copyright (c) 2026 AppDevForAll + * Description : ADFA-4951. One app-wide coordination lock for DEEP-ENVIRONMENT operations — the ones + * that own the Debian rootfs/services exclusively and must never overlap: a proot module + * runrole (install), backup, restore, and clone (send/receive). See + * controller/docs/ENVIRONMENT_LOCK_AND_BACKUP_RESTORE.md. + * + * WHY a positive lock (do not replace with "is the server alive?"): + * each op used to guard itself by checking ServerStateRepository.alive and refusing if + * alive. That is unsafe — an op that STOPS the server makes alive=false, so another op + * reads that as "free to go" and collides over the same rootfs. Ask isHeld() instead: + * it is TRUE while a deep-env op is in progress, regardless of the server state. + * + * Generalizes the older, fragmented pieces: InstallGuard (durable "install in progress" + * marker) + InstallJobs.isBusy() (process-scoped "a runrole/download is running"). Adds a + * durable OWNER marker for ops that have no process-scoped repository of their own + * (backup/restore/clone). + * + * COORDINATION vs DAMAGE-RECOVERY (important — do not conflate): + * the owner marker is a *coordination* lock and is SESSION-SCOPED. It carries a token + * unique to this process launch; a marker left by a process that was later killed reads + * as stale and is self-healed (cleared) here — because after a kill NO op is actually + * running, so the lock must not stay held forever. Recovering from *damage* left by an + * interrupted WRITE op (a half-applied restore/runrole) is a SEPARATE concern owned by the + * DURABLE InstallGuard + its recovery (LibraryActivity). So a write op should set BOTH: + * EnvironmentLock (coordination) AND InstallGuard (damage recovery). Read-only ops + * (backup, clone-send) leave no damage, so the session-scoped lock alone is enough. + * + * Content download (ZIM/Books, REST) is NOT a deep-env op — it runs on the live server — + * but a deep-env op would kill it (by stopping the server), so isBusyNow() still reports + * an active download as "busy" to keep a new deep-env op from starting on top of it. + * ============================================================================ + */ +package org.iiab.controller.env; + +import android.content.Context; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.util.UUID; + +public final class EnvironmentLock { + + /** Who holds (or would hold) the lock. */ + public enum Owner { INSTALL, MODULE, BACKUP, RESTORE, CLONE } + + // Owner marker: line 1 = Owner.name(), line 2 = epoch millis, line 3 = session token. + private static final String MARKER = ".env_lock"; + // Unique per process launch: re-generated when the class is (re)loaded in a fresh process, so a + // marker written by a process that was later killed no longer matches → treated as stale. + private static final String SESSION = UUID.randomUUID().toString(); + + private EnvironmentLock() {} + + private static File marker(Context ctx) { + return new File(ctx.getApplicationContext().getFilesDir(), MARKER); + } + + /** + * Process-scoped signal: a runrole is in flight, or a REST download is running (a deep-env op would + * kill it by stopping the server). Matches the legacy {@code InstallJobs.isBusy()} exactly, so its + * callers keep the same behavior when they delegate here. + */ + public static boolean isBusyNow() { + if (org.iiab.controller.install.presentation.ModuleQueueRepository.get().isRunning()) return true; + if (org.iiab.controller.redesign.ZimDownloadService.hasSession() + && !org.iiab.controller.redesign.ZimDownloadService.isComplete()) return true; + if (org.iiab.controller.redesign.BooksDownloadService.hasSession() + && !org.iiab.controller.redesign.BooksDownloadService.isComplete()) return true; + return false; + } + + /** + * The single question every deep-env operation asks before starting: is a deep-environment + * operation already in progress (or is it unsafe to start one)? Combines the process-scoped signal, + * the durable install guard, and this-process's owner marker. Ask THIS, not + * {@code ServerStateRepository.alive}. + */ + public static boolean isHeld(Context ctx) { + return isBusyNow() + || org.iiab.controller.InstallGuard.inProgress(ctx) + || ownerHeld(ctx); + } + + /** + * True only when THIS process's owner marker is present. A marker left by a now-dead process is + * stale (its session token won't match) and is cleared here so the lock can never stay held forever + * after a kill. Any damage from an interrupted write op is InstallGuard's concern, not this. + */ + public static synchronized boolean ownerHeld(Context ctx) { + String[] rec = read(ctx); + if (rec == null) return false; + if (SESSION.equals(rec[2])) return true; + //noinspection ResultOfMethodCallIgnored + marker(ctx).delete(); // stale (written by a process that is no longer alive) → self-heal + return false; + } + + /** Claim the lock for {@code owner}. Durable file, session-scoped validity; call {@link #release} on + * the terminal state. Callers must have already confirmed {@code !isHeld()} (this records intent, + * it does not enforce exclusion). A write op should also set InstallGuard for damage recovery. */ + public static synchronized void acquire(Context ctx, Owner owner) { + try (FileWriter w = new FileWriter(marker(ctx), false)) { + w.write(owner.name() + "\n" + System.currentTimeMillis() + "\n" + SESSION); + } catch (Exception ignored) { + // Best-effort: if we can't write the marker, isHeld() degrades to the process-scoped signal. + } + } + + /** Release the owner marker (success / failure / cancel). Idempotent. */ + public static synchronized void release(Context ctx) { + //noinspection ResultOfMethodCallIgnored + marker(ctx).delete(); + } + + /** The current owner if THIS process holds the lock, else null (not held, or stale/self-healed). */ + public static synchronized Owner currentOwner(Context ctx) { + if (!ownerHeld(ctx)) return null; + String[] rec = read(ctx); + if (rec == null) return null; + try { + return Owner.valueOf(rec[0].trim()); + } catch (Exception ignored) { + return null; // garbled owner line + } + } + + /** Epoch millis when this-process's owner acquired the lock, or 0 if not held (or stale). */ + public static synchronized long heldSince(Context ctx) { + if (!ownerHeld(ctx)) return 0L; + String[] rec = read(ctx); + if (rec == null) return 0L; + try { + return Long.parseLong(rec[1].trim()); + } catch (Exception ignored) { + return 0L; + } + } + + /** Read the 3 marker lines {owner, millis, session}, or null if absent/unreadable. Callers hold the + * class monitor (via the synchronized public methods), so this never races a write. */ + private static String[] read(Context ctx) { + File f = marker(ctx); + if (!f.exists()) return null; + try (BufferedReader r = new BufferedReader(new FileReader(f))) { + String o = r.readLine(); + String t = r.readLine(); + String s = r.readLine(); + return new String[]{o == null ? "" : o, t == null ? "" : t, s == null ? "" : s}; + } catch (Exception ignored) { + return null; + } + } +} diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/InstallJobs.java b/controller/app/src/main/java/org/iiab/controller/redesign/InstallJobs.java index 853c39883..a59cebea6 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/InstallJobs.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/InstallJobs.java @@ -17,17 +17,15 @@ */ package org.iiab.controller.redesign; -import org.iiab.controller.install.presentation.ModuleQueueRepository; - public final class InstallJobs { private InstallJobs() {} - /** True while a proot runrole is in flight OR a REST download session is still going. */ + /** True while a proot runrole is in flight OR a REST download session is still going. + * ADFA-4951: now the process-scoped half of {@link org.iiab.controller.env.EnvironmentLock}; kept + * as a thin alias so existing callers are unchanged. New code should prefer + * {@code EnvironmentLock.isHeld(ctx)} (which also covers the durable install guard + owner marker). */ public static boolean isBusy() { - if (ModuleQueueRepository.get().isRunning()) return true; // proot module runrole - if (ZimDownloadService.hasSession() && !ZimDownloadService.isComplete()) return true; - if (BooksDownloadService.hasSession() && !BooksDownloadService.isComplete()) return true; - return false; + return org.iiab.controller.env.EnvironmentLock.isBusyNow(); } } diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/MapsConfirmFragment.java b/controller/app/src/main/java/org/iiab/controller/redesign/MapsConfirmFragment.java index a6a785db1..6b6032f01 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/MapsConfirmFragment.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/MapsConfirmFragment.java @@ -87,9 +87,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c SetupLibraryActivity act = (SetupLibraryActivity) getActivity(); if (act.isMapsWizard()) { act.mapsWizardConfirm(levels, totalMb); - } else if (InstallJobs.isBusy()) { - // ADFA-4919: proot (Maps) acts on the live system, so it must not overlap a - // running proot runrole or a REST download. Refuse and tell the user to wait. + } else if (org.iiab.controller.env.EnvironmentLock.isHeld(requireContext())) { + // ADFA-4919/4951: proot (Maps) acts on the live system, so it must not overlap ANY + // deep-env op (runrole, download, backup/restore/clone). Refuse and tell the user to wait. Snackbars.make(v, R.string.k2go_install_busy).show(); } else { // ADFA-4919: route Get More Maps through the install index (gated), same as the diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/ModuleDetailFragment.java b/controller/app/src/main/java/org/iiab/controller/redesign/ModuleDetailFragment.java index 6cb09d43c..fc64aba96 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/ModuleDetailFragment.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/ModuleDetailFragment.java @@ -73,7 +73,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c Button installNow = root.findViewById(R.id.k2go_moddet_install_now); installNow.setOnClickListener(v -> { - if (InstallJobs.isBusy()) { Snackbars.make(v, R.string.k2go_install_busy).show(); return; } + if (org.iiab.controller.env.EnvironmentLock.isHeld(requireContext())) { Snackbars.make(v, R.string.k2go_install_busy).show(); return; } ModuleWishlist.add(requireContext(), c.key()); if (getActivity() instanceof SetupLibraryActivity) { ((SetupLibraryActivity) getActivity()).openModuleIndex(); diff --git a/controller/app/src/main/java/org/iiab/controller/redesign/ModuleHubFragment.java b/controller/app/src/main/java/org/iiab/controller/redesign/ModuleHubFragment.java index 1bf513022..b77785883 100644 --- a/controller/app/src/main/java/org/iiab/controller/redesign/ModuleHubFragment.java +++ b/controller/app/src/main/java/org/iiab/controller/redesign/ModuleHubFragment.java @@ -71,7 +71,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c back.setOnClickListener(v -> requireActivity().getOnBackPressedDispatcher().onBackPressed()); proceed.setOnClickListener(v -> { - if (InstallJobs.isBusy()) { Snackbars.make(v, R.string.k2go_install_busy).show(); return; } + if (org.iiab.controller.env.EnvironmentLock.isHeld(requireContext())) { Snackbars.make(v, R.string.k2go_install_busy).show(); return; } if (getActivity() instanceof SetupLibraryActivity) { ((SetupLibraryActivity) getActivity()).openModuleIndex(); } diff --git a/controller/docs/ENVIRONMENT_LOCK_AND_BACKUP_RESTORE.md b/controller/docs/ENVIRONMENT_LOCK_AND_BACKUP_RESTORE.md new file mode 100644 index 000000000..1f938c7ec --- /dev/null +++ b/controller/docs/ENVIRONMENT_LOCK_AND_BACKUP_RESTORE.md @@ -0,0 +1,116 @@ +# Environment Lock, Backup & Restore — Design + +**Epic:** ADFA-1028 (Knowledge2Go) +**Tickets:** ADFA-4951 (EnvironmentLock, foundational) → blocks → ADFA-4952 (Backup & restore, one-step) +**Builds on:** ADFA-4842 (module install: stop the server before runroles, `ServerController.startEnvironment()` to boot after) + +This document records the decisions and the *why* so the mechanism is not broken lightly during later work. Repo docs are in English (per project convention). + +--- + +## 1. The problem + +Several operations touch the Debian **rootfs and its services exclusively** — they cannot run at the same time or the rootfs is read/written by two writers → inconsistency / corruption. Today each guards itself independently, and only by asking **"is the server alive?"**: + +- Module install — stops the server itself and runs `./runrole` in a proot (ADFA-4842). +- Backup / restore (old, `BackupController`) — *refuse if the server is alive*. +- Clone / share — `ShareController.startShareFlow()` refuses if the server is alive; **but the redesign `CloneFragment` has no gate at all and clones LIVE today** (verified: no `isServerAlive`/stop anywhere in it). Send reads the rootfs while services write it (inconsistent snapshot); receive overwrites the running rootfs (corruption). + +**Why "is the server alive?" is not enough:** an operation that *stops* the server makes `alive = false`. Another operation then reads `alive = false` as "free to go" and starts — colliding with the first over the same rootfs. We need a **positive** signal ("an environment operation is in progress"), not the *absence* of the server. + +--- + +## 2. The primitive: one app-wide `EnvironmentLock` + +A single coordination lock that every deep-environment operation acquires before touching the rootfs/services and releases on a terminal state (success / failure / cancel). + +- **Owner + since:** records which job holds it — `INSTALL | MODULE | BACKUP | RESTORE | CLONE` — and when it started. +- **Session-scoped coordination** (not damage recovery): the owner marker carries a token unique to the process launch. A marker left by a process that was later killed reads as *stale* and self-heals (clears) — after a kill no op is actually running, so the lock must never stay held forever. Recovering from *damage* left by an interrupted **write** op is a separate concern owned by the durable `InstallGuard` + its recovery. A write op sets **both** (EnvironmentLock for coordination, InstallGuard for damage); read-only ops (backup, clone-send) set only the lock. +- **`isHeld()` / `owner()`:** every deep-env op checks this **first** and refuses to start while it is held. +- Generalizes and replaces the fragmented `InstallGuard` (durable "install in progress") + `InstallJobs.isBusy()`. + +The lock **owner governs the server** (stop-before / start-after), reusing the ADFA-4842 mechanism (`ServerController.startEnvironment()` — an unconditional boot, never the start/stop toggle). The server being down is a *consequence* of the lock, not the coordination signal. + +--- + +## 3. Rules (best practices — do not break lightly) + +1. **Single writer.** At most one owner at a time; all other deep-env ops refuse to *start* while the lock is held. (Planning/queueing to run in series is a future enhancement, not v1.) +2. **Ask the lock, not the server.** Ops check `EnvironmentLock.isHeld()`, never just `ServerStateRepository.alive`. +3. **The owner governs the server.** Stop-before / start-after via `startEnvironment()`; the app never relies on the alive heuristic to decide safety. +4. **Confirm at the action, not on entry.** Opening a screen is read-only and safe. Only pressing **Start** warns ("this stops the server and blocks other operations") and then acquires the lock + stops the server. +5. **The screen is the gate for hard ops** (see §4): while a write op runs, its screen traps the user (Back → background; reopening returns to it), like the module install index (`SetupProgressActivity`). +6. **Stale-marker self-heal + damage recovery are separate.** The coordination lock is session-scoped: a marker from a dead process is stale and cleared automatically (the lock never stays held after a kill). Damage from an interrupted **write** op (a half-applied restore/runrole) is recovered via the durable `InstallGuard` + its existing recovery — so write ops set **both**. +7. **REST content download is exempt.** ZIM/Books downloads run *on the live server* (in-process, no proot, no rootfs takeover) — they do **not** touch the lock and are never blocked by it. + +--- + +## 4. Rigidity by read vs write (soft vs hard gate) + +Not every op is equally rigid. It depends on whether it **reads** or **writes** the rootfs: + +| Op | Touches rootfs | Server | Abort mid-run? | Gate | +|----|----------------|--------|----------------|------| +| Module install (runrole) | writes | stop → runroles → start | No — leaves a half-built system | **Hard** (screen-gate until done/recovery) | +| Restore | writes (overwrites) | stop → extract → start | No — damaged rootfs | **Hard** | +| Clone **receive** | writes (overwrites) | stop → pull → start | No — damaged rootfs | **Hard** | +| Backup | reads | stop → tar\|gzip → start | **Yes** — discard the partial, no local damage | **Soft** | +| Clone **send** | reads | stop → serve → start | **Yes** — resumable (rsync) | **Soft** | +| Content download (ZIM/Books) | live server, in-process | none | Yes | **None** (exempt) | + +- **Soft gate (read-only: backup, clone-send):** rigid *while transferring*, but the user may stop/cancel with just a warning ("you'll stop the transfer"); on stop the server **auto-restarts** with a loading animation. No local damage — a partial backup is discarded; a partial clone-send resumes later. rsync makes clone **resilient/resumable**. +- **Hard gate (write: restore, clone-receive, module install):** cannot be abandoned mid-run without leaving a damaged rootfs, so the screen is a barrier until the op completes (or recovery runs). The server (re)starts only when done. + +--- + +## 5. "Pretty wait" UX (do not turn this into walls) + +The goal is coordination that *feels like waiting*, not a wall of "can't do that": + +- When a deep-env op is attempted while the lock is held, show a clear, friendly state — *"K2Go is busy: in progress"* — with a way to reach the running op, **not** a dead error dialog. +- v1: inform + wait. Future: queue the requested op to run automatically when the lock frees (run-in-series, never in parallel). + +--- + +## 6. Backup & Restore — one step each (ADFA-4952) + +The old flow was two steps per direction (backup→internal, then export; import→internal, then restore) plus an internal-backup list. Simplify: + +- **Back up → straight to an external file (SAF), streamed.** `tar | gzip` piped directly to the SAF `OutputStream` — **no internal temp copy**. A multi-GB rootfs must never need 2–3× space (50 GB stays 50, not 150). Server down for a **consistent snapshot**. Stamp the identity manifest (`origin=device-backup`, no checksum — the phone is not a builder). +- **Restore → straight from an external file (SAF).** **Validate before touching the rootfs** (`RootfsArchiveValidator`: right arch / valid rootfs / manifest; reject wrong-arch/corrupt/not-rootfs). Then extract over the rootfs (`TarExtractor`). Destructive; a mid-restore crash → damaged rootfs → existing recovery. Ends by booting the restored system (`startEnvironment()`). + +Backup and restore are **the same thing in opposite directions** (rootfs ↔ archive) — much of the logic is symmetric and reusable. + +**UI intro (step "zero"):** Settings → Advanced → **Backup & restore**, mirroring the **Clone** screen — title + description + "what do you want to do?" with **two cards** (Back up / Restore), icon + bold title + description. Replaces the current `SettingsUi.preview` placeholder. + +**Corruption safeguards:** +- Backup consistency comes from the server being down (no writer during `tar`). +- A truncated/corrupt backup is caught **at restore time** by `RootfsArchiveValidator` (→ CORRUPT), so we don't need to police the partial external file (SAF makes deleting it awkward anyway). +- Restore validates *before* any destructive write; the durable lock + damaged-install recovery cover a mid-restore kill. +- The foreground `WatchdogService` holds CPU/Wi-Fi locks so Android's phantom-process killer doesn't kill the operation. + +--- + +## 7. Reuse map (>50% already exists) + +**Reuse as-is / re-host:** +- `BackupController` — `tar|gzip` backup pipe + identity-manifest stamping (add streaming to the SAF `OutputStream`). +- `RootfsArchiveValidator` + `RootfsManifest` — import/restore validation (arch/rootfs/manifest). +- `TarExtractor` — restore extraction (already used by install/reset). +- `ProcessRunner` — the shell pipe. +- `ServerController.startEnvironment()` + the stop-before pattern (ADFA-4842) — server lifecycle for all deep-env jobs. +- `SetupProgressActivity` — the screen-gate pattern for hard ops. +- `BackupNameResolver` — default export filename / collision (minor). + +**To build:** +- `EnvironmentLock` (ADFA-4951): the durable, owner-aware lock; migrate module install, install/reset, and `CloneFragment` onto it. +- Fix `CloneFragment` to stop the server for a consistent rootfs (it clones live today), with a graceful stop + auto-restart. +- Backup/restore redesign UI (ADFA-4952), streaming backup, one-step restore. + +--- + +## 8. Future / hardening (explicitly out of v1) + +- **Resumable backup.** A `tar` stream is not resumable. True resume would need chunking + per-chunk hashing + verify, or a different archive format. Clone (rsync) already resumes; backup does not — for now a failed backup is simply re-run (restore-time validation rejects a truncated one). +- **Atomic-swap restore.** Extract to a staging rootfs and swap, so a mid-restore failure never leaves the live rootfs half-written (costs 2× space during restore). v1 does validate-then-extract-in-place + rely on recovery. +- **Job queue.** Plan several deep-env ops and run them automatically in series when the lock frees (never in parallel).