Skip to content

ADFA-5105: guard free space before a destructive run - #391

Merged
luisguzman-adfa merged 2 commits into
mainfrom
feat/ADFA-5105-free-space-guard
Aug 12, 2026
Merged

luisguzman-adfa merged 2 commits into
mainfrom
feat/ADFA-5105-free-space-guard

Conversation

@luisguzman-adfa

Copy link
Copy Markdown
Collaborator

Sixteen hand-rolled free-space readers, four APIs, two magic margins and only two of them gated anything. The wizard install, reinstall, restore and reset had no check: a wipe followed by an extract that runs out of disk left an unbootable rootfs. One reader, one margin, on the filesystem actually written to.

Core (pure JVM, unit-tested):

  • storage/StorageGuard: the single rule. Hybrid margin max(2 GiB floor, 10%); FITS / DOES_NOT_FIT / UNKNOWN verdict; shortfall; overflow-safe.
  • storage/SpaceEstimate: compressed -> uncompressed estimate (restore only).

Android:

  • storage/StorageProbe: the one free-space reader, on getFilesDir() (where the rootfs is written) — not /data. Null on failure so destructive callers refuse.
  • storage/FreeSpacePreflight: reads free + asks the rule; proceeds only on FITS.

Gated the destructive routes before the wipe (UNKNOWN refuses, fail-safe):

  • InstallService.runPipeline / runResetPipeline (reinstall, reset, fresh install).
  • DeepOpService.runRestore (restore). The rootfs "needed" is the UNCOMPRESSED size from RootfsCatalog.installedBytes (measured, published by ADFA-5110 as latest_*.installed and captured into rootfs_sizes.csv's installed_bytes column; measured constants as the offline fallback). Restore estimates from the backup archive.

Consolidated the rest onto the same reader/rule:

  • PlannerController and SyncStateViewModel now use StorageGuard/StorageProbe instead of a second -5 GB copy.
  • The advisory picker labels (Maps, ZIM x3, Kolibri x3) read StorageProbe, not a per-screen StatFs. No hand-rolled new StatFs(...) remains outside StorageProbe.
  • Dropped the stale "5GB buffer" wording from the two error strings.

Verification: unit tests (StorageGuardTest, SpaceEstimateTest) + javalang/XML. End-to-end refusal-before-wipe needs a device (StatFs + real rm/extract).

…— and only

two of them gated anything. The wizard install, reinstall, restore and reset had
no check: a wipe followed by an extract that runs out of disk left an unbootable
rootfs. One reader, one margin, on the filesystem actually written to.

Core (pure JVM, unit-tested):
- storage/StorageGuard: the single rule. Hybrid margin max(2 GiB floor, 10%);
  FITS / DOES_NOT_FIT / UNKNOWN verdict; shortfall; overflow-safe.
- storage/SpaceEstimate: compressed -> uncompressed estimate (restore only).

Android:
- storage/StorageProbe: the one free-space reader, on getFilesDir() (where the
  rootfs is written) — not /data. Null on failure so destructive callers refuse.
- storage/FreeSpacePreflight: reads free + asks the rule; proceeds only on FITS.

Gated the destructive routes before the wipe (UNKNOWN refuses, fail-safe):
- InstallService.runPipeline / runResetPipeline (reinstall, reset, fresh install).
- DeepOpService.runRestore (restore).
The rootfs "needed" is the UNCOMPRESSED size from RootfsCatalog.installedBytes
(measured, published by ADFA-5110 as latest_*.installed and captured into
rootfs_sizes.csv's installed_bytes column; measured constants as the offline
fallback). Restore estimates from the backup archive.

Consolidated the rest onto the same reader/rule:
- PlannerController and SyncStateViewModel now use StorageGuard/StorageProbe
  instead of a second -5 GB copy.
- The advisory picker labels (Maps, ZIM x3, Kolibri x3) read StorageProbe, not a
  per-screen StatFs. No hand-rolled `new StatFs(...)` remains outside StorageProbe.
- Dropped the stale "5GB buffer" wording from the two error strings.

Verification: unit tests (StorageGuardTest, SpaceEstimateTest) + javalang/XML.
End-to-end refusal-before-wipe needs a device (StatFs + real rm/extract).
Post-review fixes:

- Peak, not final footprint: the destructive install gate runs before the
  download, and the compressed .tar.gz and the uncompressed tree coexist during
  extraction, so the "needed" is the peak. RootfsCatalog.peakInstallBytes()
  (uncompressed + compressed); InstallService uses it. Full arm64 is ~7.4 GiB
  peak vs ~4.75 GiB final — the old figure could pass an install that then ran
  out of disk mid-extract.

- Advisory matches the hard gate: PlannerController sizes the OS by its
  uncompressed footprint (the legend, and the "fits" decision on the same peak),
  instead of the compressed download — so it no longer says "fits" then the
  install refuses.

- Restore estimate: SpaceEstimate factor 2.3 -> 2.2 (rootfs-shaped archives
  expand at most ~1.80x across tiers; 2.2x plus the StorageGuard margin keeps it
  safe). This factor now only backs restore, since the rootfs uses the measured
  size; a principled replacement (read the archive's real size) is a follow-up.

- Disk hygiene: delete the downloaded .tar.gz on a failed extract too, not only
  on success (a failure used to keep ~2-3 GB until the next attempt).

SpaceEstimateTest updated for 2.2x.
@luisguzman-adfa
luisguzman-adfa merged commit afdd054 into main Aug 12, 2026
3 checks passed
@luisguzman-adfa
luisguzman-adfa deleted the feat/ADFA-5105-free-space-guard branch August 12, 2026 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant