Skip to content

fix(select_device.ps1): make DTB/logo update atomic with rollback - #192

Open
bhoot1234567890 wants to merge 1 commit into
southoz:mainfrom
bhoot1234567890:fix/dtb-selector-atomic-update
Open

fix(select_device.ps1): make DTB/logo update atomic with rollback#192
bhoot1234567890 wants to merge 1 commit into
southoz:mainfrom
bhoot1234567890:fix/dtb-selector-atomic-update

Conversation

@bhoot1234567890

Copy link
Copy Markdown

Problem

select_device.ps1 updates the boot partition's DTB + logo files with a delete-then-copy sequence:

Get-ChildItem $rootDir -Filter "*.dtb" | ForEach-Object { Remove-Item $_.FullName -Force }   # delete all .dtb
Remove-Item $oldLogo -Force                                                                  # delete logo
Copy-Item "$sourceFolder\*" -Destination $rootDir ...                                        # then copy
Copy-Item $logoSrc -Destination logo.bmp ...

If the copy fails partway — disk full, card pulled mid-write, unreadable source, or a locked file — the boot partition is left with no DTB files and the device won't boot. For a tool whose whole purpose is keeping the handheld bootable, that's the one gap worth closing.

Fix

Make the apply step atomic with rollback:

  1. Stage the incoming .dtb + logo into a temp dir first — a bad/unreadable source fails here, before anything on the boot volume is touched.
  2. Back up the current root .dtb files and logo.bmp.
  3. Install the staged files into root.
  4. Only then remove the superseded old .dtb files.

Wrapped in try/catch: on any failure the backup is restored so the device returns to its prior bootable state, then the script exits non-zero. The rollback is best-effort per file (a single locked/unwritable file can't abort it) and also discards any new files the failed install partially wrote, so root ends up matching its pre-update state. Temp dirs are cleaned up on both success and failure paths.

Scope

Only the "Apply changes" section. No change to INI parsing, the menu, or device-selection logic. Behaviour is identical on the happy path; only the failure mode changes (safe rollback instead of a bricked partition).

Testing

Parsed clean with the PowerShell AST parser, then ran the extracted apply block against a mock filesystem on PowerShell 7.6:

Scenario Result
Happy path new DTBs installed, superseded files removed, logo updated, temp dirs cleaned, exit 0
Empty source folder fails at stage before touching root, exit 1, root untouched
Partial install failure (dest unwritable mid-copy) rollback restores exact prior file set, discards leftover new files, exit 1

The Windows selector deleted all root .dtb files and logo.bmp *before*
copying the new set in. If the copy failed partway (disk full, card
pulled, unreadable source, locked file), the boot partition was left
with no DTB files and the device would not boot.

Restructure the apply step to:
  1. stage the incoming .dtb + logo into a temp dir (fails fast on a bad
     source, before anything on the boot volume is touched);
  2. back up the current root .dtb files and logo.bmp;
  3. install the staged files over the ones in root;
  4. only then remove the superseded old .dtb files.

Wrap the apply in try/catch: on any failure, restore the backup so the
device is returned to its prior bootable state, then exit non-zero. The
rollback is best-effort per file (a single locked/unwritable file cannot
abort it) and also discards any new files the failed install partially
wrote, so root ends up matching its pre-update state. Temp dirs are
cleaned up on both the success and failure paths.

No change to the menu / device-selection logic.

Verified on PowerShell 7.6 with a mock filesystem:
  - happy path: new files installed, old superseded files removed, logo
    updated, temp dirs cleaned;
  - empty source: fails at stage before touching root, exit 1;
  - partial install failure (dest unwritable mid-copy): rollback restores
    the exact prior file set, discards leftover new files, exit 1.
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