refactor: split removal into plan, run and backups

This commit is contained in:
Abdessamad Derraz committed 2026-11-24 06:21:13 +01:00
1 parent ace6b3d451
commit 42074158fc
14 files changed
+1278 -1234

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@
# and Invoke-WebRequest throws on a cut transfer before the hash is read:
# the site's build rewrites this line there (scripts/sitebootstrap.py).
$mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
$defaultInstallSha256 = "073ff9ca1de78d482d760a6d7ac4376ffce3f31d85010223b47cebe5a8311289"
$defaultInstallSha256 = "e7fa25e09d30911c4d6bcd90d5730f6273e04f0570159537f3019dd33cfb2f56"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
$maximumInstallerBytes = 8MB
+602 -598
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins
# curl or wget stop on a cut transfer before the hash is read: the site's
# build rewrites this line there (scripts/sitebootstrap.py).
MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
DEFAULT_INSTALL_SHA256="073ff9ca1de78d482d760a6d7ac4376ffce3f31d85010223b47cebe5a8311289"
DEFAULT_INSTALL_SHA256="e7fa25e09d30911c4d6bcd90d5730f6273e04f0570159537f3019dd33cfb2f56"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
MAX_INSTALLER_BYTES=8388608
+8 -8
View File
@@ -27,8 +27,8 @@
{
"dest": "roms/ports/retrobios/install.py",
"repo_path": "install.py",
"size": 1502290,
"sha256": "073ff9ca1de78d482d760a6d7ac4376ffce3f31d85010223b47cebe5a8311289",
"size": 1502452,
"sha256": "e7fa25e09d30911c4d6bcd90d5730f6273e04f0570159537f3019dd33cfb2f56",
"executable": false
},
{
@@ -71,8 +71,8 @@
"muos": {
"archive": {
"dest": "ARCHIVE/RetroBIOS.muxapp",
"size": 1555915,
"sha256": "7fb468b321d8808217e60702988ff0aa7020c44909781969e1d65b7a9ca8e19f"
"size": 1556077,
"sha256": "5cfb883ef333755daec30a28077d10bcf80472407d3de01ec6df195952b5aaa4"
},
"members": [
{
@@ -92,8 +92,8 @@
{
"name": "RetroBIOS/install.py",
"repo_path": "install.py",
"size": 1502290,
"sha256": "073ff9ca1de78d482d760a6d7ac4376ffce3f31d85010223b47cebe5a8311289",
"size": 1502452,
"sha256": "e7fa25e09d30911c4d6bcd90d5730f6273e04f0570159537f3019dd33cfb2f56",
"executable": false
},
{
@@ -166,8 +166,8 @@
{
"dest": ".config/modules/retrobios/install.py",
"repo_path": "install.py",
"size": 1502290,
"sha256": "073ff9ca1de78d482d760a6d7ac4376ffce3f31d85010223b47cebe5a8311289",
"size": 1502452,
"sha256": "e7fa25e09d30911c4d6bcd90d5730f6273e04f0570159537f3019dd33cfb2f56",
"executable": false
},
{
+182
View File
@@ -0,0 +1,182 @@
"""The backups a run leaves: listing them, deleting one, and the setup an action names."""
from __future__ import annotations
import argparse
import datetime
import os
import re
import stat
import sys
from pathlib import Path
from installer.console import QUIT_CODE, _answer_yes
from installer.destination import card_dest
from installer.detect_frontends import detect_platforms
from installer.detect_os import detect_os
from installer.events import EVENTS
from installer.host_fs import _clear_read_only
from installer.install_record import _backup_note, day_words, load_record
from installer.invocation import _INVOCATION
from installer.paths import shown_path, spelled
from installer.platforms import normalize_platform
from installer.refusal import Refusal
from installer.safe_replace import KeptFiles, _backup_files, _is_backup_run, _tree
from installer.text import _plural, format_size
def _backup_date(run: Path) -> str:
"""When a run folder was made, read from its name: 2026-11-03 12:00:00."""
match = re.match(r"(\d{4})(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d)", run.name)
if not match:
return ""
year, month, day, hour, minute, second = match.groups()
return f"{year}-{month}-{day} {hour}:{minute}:{second}"
def backup_words(date: str) -> str:
"""A backup's date as a list says it: 3 Nov 12:00."""
try:
when = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
except ValueError:
return date or "unknown date"
return f"{day_words(when)} {when.hour:02d}:{when.minute:02d}"
def list_backups(folder: Path) -> list[dict]:
"""The backups of a BIOS folder, newest first: {folder, date, files,
bytes, source}, source remove for a removal's and install for the files
an install replaced. Only dated folders holding the marker count."""
folder = Path(os.path.abspath(folder))
found = []
for holder, prefix in KeptFiles._holders(folder):
try:
if not stat.S_ISDIR(os.lstat(holder).st_mode):
continue
runs = sorted(holder.iterdir())
except OSError:
continue
for run in runs:
if not _is_backup_run(run):
continue
under = run / prefix if prefix else run
files = [path for path in _backup_files(run) if under in path.parents]
if files:
found.append({"folder": run, "date": _backup_date(run), "files": len(files),
"bytes": sum(path.stat().st_size for path in files),
"source": "remove" if _backup_note(run) is not None
else "install"})
return sorted(found, key=lambda item: item["folder"].name, reverse=True)
def run_backups(args: argparse.Namespace) -> int:
"""--backups: the backups of the BIOS folder, newest first."""
folder = _action_folder(args)
backups = list_backups(folder)
if not backups:
print(f"No backup of {shown_path(folder)}.")
return 0
print(f"Backups of {shown_path(folder)}, newest first:")
for item in backups:
made = "removed" if item["source"] == "remove" else "replaced by an install"
print(f" {backup_words(item['date'])} {_plural(item['files'], 'file')}, "
f"{format_size(item['bytes'])}, {made}")
print(f" {shown_path(item['folder'])}")
EVENTS.emit("backup", folder=str(spelled(item["folder"])), date=item["date"],
files=item["files"], bytes=item["bytes"], source=item["source"],
status="listed", message="")
print(f"To put one back: {_INVOCATION.launcher()} --restore FOLDER")
return 0
def run_delete_backup(args: argparse.Namespace) -> int:
"""--delete-backup: one backup folder deleted after one confirmation.
0 done, 1 a file failed, 2 refused or declined."""
run = Path(os.path.abspath(os.path.expanduser(str(args.delete_backup))))
if not _is_backup_run(run):
raise Refusal("backup", "not_a_backup", "",
f"Error: {run} is not a folder the installer kept files in.",
" Name one of the dated folders --backups lists.", code=2)
files = _backup_files(run)
size = sum(path.stat().st_size for path in files)
date = _backup_date(run)
source = "remove" if _backup_note(run) is not None else "install"
print(f"Delete the backup of {backup_words(date)}: {_plural(len(files), 'file')}, "
f"{format_size(size)}.")
if args.dry_run:
print("Nothing was deleted (--dry-run).")
return 0
if not args.yes:
if args.no_input or not sys.stdin.isatty():
raise Refusal("option", "needs_yes", "",
"Error: deleting a backup needs a yes: add --yes to delete it "
"without a question.", code=2)
if not _answer_yes("Delete it for good? [y/N] "):
print("Nothing was deleted.")
return QUIT_CODE
failed = 0
# Bottom up, never through a link: a link goes, not what it names. A
# junction is removed as a folder, which leaves its target alone.
for path, info in _tree(run):
try:
if stat.S_ISDIR(info.st_mode):
path.rmdir()
else:
_clear_read_only(path)
path.unlink()
except OSError as exc:
failed += 1
print(f" failed: {path} ({exc.strerror or exc})", file=sys.stderr)
try:
run.rmdir()
except OSError as exc:
failed += 1
print(f" failed: {run} ({exc.strerror or exc})", file=sys.stderr)
EVENTS.emit("backup", folder=str(spelled(run)), date=date, files=len(files), bytes=size,
source=source, status="not_deleted" if failed else "deleted",
message=f"{_plural(failed, 'file')} could not be deleted." if failed else "")
if failed:
return 1
print(f"Deleted, {format_size(size)} freed.")
return 0
def _action_folder(args: argparse.Namespace) -> Path:
"""The BIOS folder a listing of backups is for: --dest, else the one
setup found here."""
if args.dest and not args.platform:
return Path(os.path.abspath(os.path.expanduser(str(args.dest))))
return _action_setup(args)[1]
def _action_setup(args: argparse.Namespace) -> tuple[str, Path]:
"""The setup and BIOS folder a removal is for: --platform and --dest,
the one setup a record names or the search here finds, never a guess.
A choice left open exits 2, naming the option that settles it."""
named = args.platform or ""
plat = normalize_platform(args.platform, say=False) if args.platform else ""
if args.dest:
dest = Path(args.dest)
if plat:
plat, dest = card_dest(named, plat, dest)
folder = Path(os.path.abspath(os.path.expanduser(str(dest))))
if plat:
return plat, folder
setups = sorted((load_record(folder).data or {}).get("setups") or {})
if len(setups) == 1:
return setups[0], folder
raise Refusal("detect", "conflict" if setups else "not_found", "",
f"Error: name the setup of {shown_path(folder)} with --platform"
+ (f": {', '.join(setups)}." if setups else "."), code=2)
found = [(name, path) for name, path in detect_platforms(detect_os())
if not plat or name == plat]
if len(found) == 1:
return found[0][0], Path(os.path.abspath(found[0][1]))
if not found:
raise Refusal("detect", "not_found", plat,
"Error: no setup found here: name its BIOS folder with --dest"
+ ("." if plat else " and its setup with --platform."), code=2)
raise Refusal("detect", "conflict", plat,
"Error: several setups found here: name one with --platform and --dest.",
*(f" --platform {name} --dest {_INVOCATION.word(str(path))}"
for name, path in found), code=2)
+2 -1
View File
@@ -34,7 +34,8 @@ from installer.platforms import AVAILABLE_PLATFORMS, TARGET_PLATFORMS, _name_key
from installer.refusal import Refusal
from installer.regions import resolve_regions
from installer.remote import remote_from_env, remote_in_use
from installer.removal import run_backups, run_delete_backup, run_removal
from installer.backups import run_backups, run_delete_backup
from installer.removal import run_removal
from installer.run import _install
from installer.tui_routes import (
TUI_LIST_ENV,
+15 -619
View File
@@ -1,495 +1,53 @@
"""Removing what a run installed, and the backups it leaves."""
"""Removing what a run installed: the files, the record after, and what the run says."""
from __future__ import annotations
import argparse
import datetime
import errno
import json
import os
import re
import stat
import sys
import urllib.error
from collections.abc import Collection, Iterable, Mapping
from collections.abc import Iterable
from pathlib import Path, PurePosixPath
from typing import NamedTuple
from installer.choices import available_choices, emulator_names, resolve_choices, resolve_systems, system_ids
from installer.choices import available_choices, emulator_names, resolve_choices, resolve_systems
from installer.console import QUIT_CODE, _answer_yes
from installer.destination import card_dest
from installer.detect_frontends import detect_platforms
from installer.detect_os import detect_os
from installer.events import EVENTS
from installer.filesystems import keeps_case
from installer.host_fs import _clear_read_only, _is_link
from installer.install_record import (
_backup_note,
_below,
_check_ours,
_record_now,
_update_record,
day_words,
load_record,
record_argv,
record_options,
setup_record,
)
from installer.invocation import _INVOCATION, offline_requested
from installer.lists import fetch_manifest
from installer.local_check import _digest_file
from installer.manifest import OTHER_SYSTEMS, _validate_manifest, entry_copies
from installer.narrowing import designated_cores
from installer.manifest import _validate_manifest
from installer.network import _fetch_json
from installer.paths import shown_path, spelled
from installer.platforms import display_name, entry_systems, normalize_platform, refuse_name, system_title
from installer.platforms import display_name, refuse_name
from installer.refusal import Refusal
from installer.remote import MAX_MANIFEST_BYTES, active_remote
from installer.safe_replace import (
BACKUP_DIR,
KEPT,
REMOVED_NOTE,
KeptFiles,
_backup_files,
_is_backup_run,
_move_aside,
_tree,
)
from installer.text import _plural, agree, format_size
# Removal deletes for good, or moves into a backup with --backup, only what
# the install record proves the installer placed: a file it lists, still
# its recorded bytes, the collection able to serve it again. Anything else
# is named and left. Why each file stays, as removal_plan's kept says it.
REMOVAL_REASONS = {
"shared": "still needed",
"changed": "changed since it was installed",
"link": "a link, never followed",
"not_a_file": "not a regular file",
"outside": "outside the BIOS folder",
"not_servable": "the collection cannot give it again",
"unreadable": "could not be read",
"not_recorded": "not installed by RetroBIOS",
}
# Why a planned file stays when it changed between the plan and its removal.
CHANGED_SINCE_PLAN = "changed since the plan"
class Removal(NamedTuple):
"""What a removal takes and leaves. remove holds {dest, root, sha1,
size, systems, path, inode} per file to remove, kept {dest, root, sha1,
size, systems, reason, what} per file left, gone the record's entries
no longer on disk, choice the setup's choice once the removal is done
(None: the setup goes from the record), systems those losing a file."""
plat: str
folder: Path
remove: list
kept: list
gone: list
choice: list | None
systems: list
def _drive_key(dest: str, fold: bool) -> str:
"""dest as the drive compares it, folded where it folds case."""
return dest.casefold() if fold else dest
def _entry_sha1s(entry: dict) -> set[str]:
return {str(copy.get("sha1")) for copy in entry_copies(entry)
if copy.get("sha1")}
def _entry_readers(entry: dict) -> set[str] | None:
"""The cores that read an entry, None for a file of the platform's own list."""
if entry.get("cores") is None:
return None
return {core for copy in entry_copies(entry)
for core in copy.get("cores") or ()}
def _served(manifest: dict, fold: bool) -> dict[tuple[str, str], tuple]:
"""What the list serves, by (root, dest): (the sha1s it serves there,
its systems, the cores that read it, whether the platform's own list
names it). A place listed twice keeps both: a core's copy beside the
platform's own never makes the file a core's alone. A data tree's member
counts under root data, as the record keeps it."""
served: dict[tuple[str, str], tuple] = {}
for entry in [*(manifest.get("files") or []), *(manifest.get("case_variants") or [])]:
dest = str(entry.get("dest"))
place = (str(entry.get("root") or ""), _drive_key(dest, fold))
sha1s, systems, readers, own = served.get(place, (set(), [], set(), False))
mine = _entry_readers(entry)
served[place] = (sha1s | _entry_sha1s(entry),
sorted(set(systems) | set(entry_systems(entry))),
readers | (mine or set()), own or mine is None)
for tree in manifest.get("data_directories") or []:
for rel, meta in (tree.get("files") or {}).items():
dest = f"{tree['dest']}/{rel}" if tree.get("dest") else rel
served[("data", _drive_key(dest, fold))] = (
{str(meta.get("sha1"))}, list(tree.get("systems") or []), set(), True)
return served
def _recorded_served(setup: dict, manifest: dict, fold: bool
) -> list[tuple[dict, tuple[str, str], tuple]]:
"""Each file a setup's record holds, with its place as the drive opens
it and what the list serves there, by the one rule removal reads."""
served = _served(manifest, fold)
places = [(item, (str(item.get("root") or ""), _drive_key(str(item["dest"]), fold)))
for item in setup.get("files") or []]
return [(item, place, served.get(place, (set(), [], set(), False)))
for item, place in places]
def recorded_systems(setup: dict, manifest: dict, folder: Path) -> list[tuple[dict, list[str]]]:
"""Each file a setup's record holds, with the list's systems of it: a
drive that folds case finds a recased dest under the list's spelling."""
return [(item, list(found[1]))
for item, _place, found in _recorded_served(setup, manifest, not keeps_case(folder))]
def _choice_names(options: list[tuple[str, str | None]], flag: str) -> list[str]:
return [name.strip() for option, value in options if option == flag
for name in (value or "").split(",") if name.strip()]
def _choice_without(choice: list[str], flag: str, names: Collection[str],
left: list[str], ids: Mapping[str, str] | None = None) -> list[str]:
"""A recorded choice once names of flag are taken out: those it named
go, and a choice that named none gets the names left. A flag left with
no name is dropped. ids gives a recorded name the id it stands for."""
options = [option for option in record_options(choice) if option[0] != "--all-files"]
gone = {name.casefold() for name in names}
same = ids or {}
remaining = [name for name in (_choice_names(options, flag) or left)
if same.get(name, name).casefold() not in gone]
kept = [option for option in options if option[0] != flag]
if remaining:
kept.append((flag, ",".join(remaining)))
return record_argv(kept)
class _RemovalScope(NamedTuple):
"""What a removal takes out, and what stays chosen once it is done."""
fold: bool
known: dict[str, list[str]]
options: list[tuple[str, str | None]]
chosen_systems: set[str]
chosen_cores: set[str]
staying_systems: set[str]
staying_cores: set[str]
named_cores: list[str]
others: dict[tuple[str, str], str]
wanted: set[str]
system_ids: dict[str, str]
def norm(self, dest: str) -> str:
"""dest as the drive compares it, folded where it folds case."""
return _drive_key(dest, self.fold)
def _removal_scope(
plat: str, record: dict, manifest: dict, fold: bool, *,
systems: Collection[str], cores: Collection[str],
emulators: Collection[str], files: Collection[str],
) -> _RemovalScope:
"""The scope of a removal of plat's setup from record."""
setup = record["setups"][plat]
known = available_choices(list(manifest.get("files") or []),
list(manifest.get("omitted_files") or []))
options = record_options(setup.get("choice") or [])
chosen_systems = set(systems)
chosen_cores = designated_cores(manifest, [*cores, *emulators]) if (
cores or emulators) else set()
others = {(str(item.get("root") or ""), _drive_key(item["dest"], fold)): name
for name, other in record["setups"].items() if name != plat
for item in other.get("files") or []}
wanted = {_drive_key(dest, fold) for dest in files}
# What stays chosen once the removal is done, as an install names it:
# the systems or cores the record named, else every one of the list.
recorded_systems = _choice_names(options, "--system")
ids = system_ids(recorded_systems, manifest, list(manifest.get("files") or []),
list(manifest.get("omitted_files") or []))
named_systems = {ids.get(name, name).casefold() for name in recorded_systems}
staying_systems = {name for name in known["systems"] if not named_systems
or name.casefold() in named_systems} - chosen_systems
named_cores = _choice_names(options, "--core")
staying_cores = (designated_cores(manifest, named_cores) if named_cores
else set(known["cores"]) | set(manifest.get("core_names") or {})
) - chosen_cores
return _RemovalScope(fold, known, options, chosen_systems, chosen_cores,
staying_systems, staying_cores, named_cores, others, wanted, ids)
def _removal_takes(scope: _RemovalScope, found: tuple, dest: str) -> bool:
"""Whether the removal's choice reaches a recorded file at dest."""
_sha1s, item_systems, readers, own = found
if scope.chosen_systems and not (scope.chosen_systems & set(item_systems) or (
not item_systems and OTHER_SYSTEMS in scope.chosen_systems)):
return False
# The platform's own list keeps its files whatever core is removed.
if scope.chosen_cores and (own or not readers & scope.chosen_cores):
return False
return not (scope.wanted and scope.norm(dest) not in scope.wanted)
def _removal_needed(scope: _RemovalScope, manifest: dict, item: dict, key: tuple[str, str],
found: tuple) -> tuple[str, str]:
"""Why a file of ours on disk stays, and what still needs it: ("", "") when it goes."""
sha1s, item_systems, readers, _own = found
if item["sha1"] not in sha1s:
return "not_servable", ""
if key in scope.others:
return "shared", display_name(scope.others[key])
if scope.chosen_systems and set(item_systems) & scope.staying_systems:
return "shared", ", ".join(system_title(name, manifest) for name in sorted(
set(item_systems) & scope.staying_systems))
if scope.chosen_cores and readers and readers & scope.staying_cores:
return "shared", ", ".join(sorted(readers & scope.staying_cores))
return "", ""
def _removal_entry(scope: _RemovalScope, manifest: dict, folder: Path, item: dict,
found: tuple) -> tuple[str, dict]:
"""Where a picked file of the record goes: ("remove", entry), ("kept",
entry) or ("gone", item)."""
root, dest = str(item.get("root") or ""), str(item["dest"])
entry = {"dest": dest, "root": root, "sha1": item["sha1"], "size": item["size"],
"systems": found[1]}
if root not in ("", "data"):
return "kept", {**entry, "reason": "outside", "what": ""}
path, why = _below(folder, dest)
info = None
if path is not None:
why, info = _check_ours(path, item)
if why == "gone":
return "gone", item
what = ""
if not why:
why, what = _removal_needed(scope, manifest, item, (root, scope.norm(dest)), found)
if why or path is None or info is None:
return "kept", {**entry, "reason": why or "unreadable", "what": what}
return "remove", {**entry, "path": path, "inode": _stamp(info)}
def _removal_choice(scope: _RemovalScope, recorded: list[str], *, files: Collection[str],
cores: Collection[str], emulators: Collection[str]) -> list | None:
"""The setup's choice once the removal is done, None when it goes."""
if files:
return recorded
if scope.chosen_systems:
choice = _choice_without(recorded, "--system", scope.chosen_systems,
sorted(scope.staying_systems | scope.chosen_systems),
scope.system_ids)
if not _choice_names(record_options(choice), "--system"):
return None
return choice
if scope.chosen_cores and emulators and _choice_names(scope.options, "--emulator"):
return _choice_without(recorded, "--emulator", emulators, [])
if scope.chosen_cores:
left = ([] if scope.named_cores
else sorted(set(scope.known["cores"]) - scope.chosen_cores))
return _choice_without(recorded, "--core", scope.chosen_cores | set(cores), left)
return None
def plan_removal(
plat: str, folder: Path, record: dict, manifest: dict, *,
systems: Collection[str] = (), cores: Collection[str] = (),
emulators: Collection[str] = (), files: Collection[str] = (),
) -> Removal:
"""What removing the chosen part of a setup takes and leaves.
Only the record of plat in folder says what may go: every file it
lists, or those of systems, of cores (or of the emulators' cores), or
the dests in files. A file still needed by what stays (a system or a
core still chosen, another setup of the same folder) stays, and so do a
file changed since, a link, anything that is not a regular file, any
file outside the BIOS folder, and any the list can no longer serve. A
drive that folds case compares dests folded, as FAT and exFAT open them.
"""
folder = Path(os.path.abspath(folder))
setup = record["setups"][plat]
fold = not keeps_case(folder)
scope = _removal_scope(plat, record, manifest, fold, systems=systems, cores=cores,
emulators=emulators, files=files)
planned_entries: dict[str, list] = {"remove": [], "kept": [], "gone": []}
picked: set[str] = set()
planned: set[tuple[str, str]] = set()
for item, key, found in _recorded_served(setup, manifest, fold):
if not _removal_takes(scope, found, str(item["dest"])):
continue
if key in planned:
# Two entries the drive opens as one file: planned once.
continue
planned.add(key)
picked.add(key[1])
where, entry = _removal_entry(scope, manifest, folder, item, found)
planned_entries[where].append(entry)
remove, kept, gone = (planned_entries["remove"], planned_entries["kept"],
planned_entries["gone"])
kept += [{"dest": dest, "root": "", "sha1": "", "size": 0, "systems": [],
"reason": "not_recorded", "what": ""}
for dest in files if scope.norm(dest) not in picked]
choice = _removal_choice(scope, list(setup.get("choice") or []), files=files,
cores=cores, emulators=emulators)
lost = sorted({name for entry in remove for name in entry["systems"]})
return Removal(plat, folder, remove, kept, gone, choice, lost)
def _parent_of(dest: str) -> str:
"""The folder of a dest, "" at the top: dc for dc/dc_boot.bin."""
parent = PurePosixPath(dest).parent.as_posix()
return "" if parent == "." else parent
def _misplaced_known(record: dict, manifests: list[dict], fold: bool
) -> tuple[set[str], bool, set[str]]:
"""The places any list of manifests reads or any setup recorded, whether
a list places files at the top, and the game data dests."""
known: set[str] = set()
top_placed = False
# Game data sits in folders a frontend lists as games: DOOM2.WAD beside
# doom2.wad is a game entry of its own there, never a copy.
games = {_drive_key(str(entry.get("dest", "")), fold) for manifest in manifests
for entry in manifest.get("files") or [] if entry.get("category") == "game_data"}
for manifest in manifests:
for root, dest in _served(manifest, fold):
known.add(dest)
top_placed = top_placed or (root == "" and "/" not in dest)
known |= {_drive_key(str(entry.get("dest", "")), fold)
for entry in manifest.get("omitted_files") or []}
known |= {_drive_key(str(item["dest"]), fold) for setup in record["setups"].values()
for item in setup.get("files") or []}
return known, top_placed, games
def _misplaced_entries(folder: Path, rel_dir: str) -> list[os.DirEntry[str]]:
"""The entries of a folder the installer placed files in, by name: none
when it is reached through a link or cannot be read."""
# The folder reached through no link, as _below reaches a file in it.
inside, why = _below(folder, f"{rel_dir}/-" if rel_dir else "-")
if inside is None or why:
return []
try:
return sorted(os.scandir(inside.parent), key=lambda entry: entry.name)
except OSError:
return []
def _misplaced_candidate(entry: os.DirEntry[str], sizes: Collection[int]
) -> tuple[Path, os.stat_result, str] | None:
"""The path, lstat and sha1 of a lone regular file of a placed file's size."""
path = Path(entry.path)
try:
info = os.lstat(path)
except OSError:
return None
if (not stat.S_ISREG(info.st_mode) or _is_link(info) or info.st_nlink != 1
or info.st_size not in sizes):
return None
try:
sha1 = _digest_file(path, ("sha1",))["sha1"]
except OSError:
return None
return path, info, sha1
def plan_misplaced_removal(
plat: str, folder: Path, record: dict, manifests: list[dict],
) -> Removal:
"""The copies in the wrong place of the BIOS folder that removing takes.
A copy goes when its bytes are those of a file plat's record says the
installer placed, read again now at that place, under that file's own
name, and it sits beside that file (the name in another case), or at
the top of the folder where the list places files there itself:
GBA_BIOS.BIN beside gba_bios.bin, dc_boot.bin at the top of RetroArch's
system folder for dc/dc_boot.bin. A renamed copy stays: a core option,
a launcher or a frontend entry may name it. Only those folders
are read, so a games folder (ports/doom on a card whose install folder
is the roms folder), the save folder and any other tree are never
looked at. Even there these stay:
- a place any list of manifests reads or any setup recorded, in any
root, whole or as the end of the path from a folder down
- a link, anything but a regular file, a file with a second name, and
the installer's own file under a spelling the record does not use.
"""
folder = Path(os.path.abspath(folder))
fold = not keeps_case(folder)
known, top_placed, games = _misplaced_known(record, manifests, fold)
placed: dict[int, list[dict]] = {}
# The folders read: each one the installer placed a file in, and the
# top where the list places files there itself.
folders: dict[str, str] = {"": ""} if top_placed else {}
for item in record["setups"][plat].get("files") or []:
if str(item.get("root") or "") == "" and isinstance(item.get("size"), int):
placed.setdefault(item["size"], []).append(item)
parent = _parent_of(str(item["dest"]))
folders.setdefault(_drive_key(parent, fold), parent)
proven: dict[str, tuple[str, os.stat_result, Path] | None] = {}
def in_place(item: dict) -> tuple[str, os.stat_result, Path] | None:
"""The placed file's dest, lstat and path when it still holds its bytes."""
if item["dest"] not in proven:
path, why = _below(folder, item["dest"])
info = None
if path is not None and not why:
why, info = _check_ours(path, item)
proven[item["dest"]] = ((item["dest"], info, path)
if not why and info and path is not None else None)
return proven[item["dest"]]
def read_somewhere(rel: str) -> bool:
"""Whether rel is a known place, or ends with one that names a
folder: saves/n3ds/Azahar/sysdata/keys.txt is the save folder's
n3ds/Azahar/sysdata/keys.txt. A bare name is no such end."""
parts = _drive_key(rel, fold).split("/")
return "/".join(parts) in known or any(
"/".join(parts[index:]) in known for index in range(1, len(parts) - 1))
remove: list[dict] = []
for rel_dir in sorted(folders.values()):
for entry in _misplaced_entries(folder, rel_dir):
rel = f"{rel_dir}/{entry.name}" if rel_dir else entry.name
if entry.name in (".nomedia", BACKUP_DIR) or read_somewhere(rel):
continue
candidate = _misplaced_candidate(entry, placed)
if candidate is None:
continue
path, info, sha1 = candidate
found = [(other, hit) for other in placed[info.st_size] if other["sha1"] == sha1
for hit in [in_place(other)] if hit is not None]
if any(_drive_key(str(other["dest"]), fold) in games for other, _hit in found):
continue
if any((hit[1].st_dev, hit[1].st_ino) == (info.st_dev, info.st_ino)
for _other, hit in found):
# The installer's own file under a spelling the record does not use.
continue
# Under its twin's own name: a copy renamed (SCPH-39001 USA.bin, a
# game's own title) may be read by that name, a core option or a
# frontend entry naming it. Beside its twin that name differs in
# case only, and at the top it is a twin's anywhere below.
near = [hit for other, hit in found
if PurePosixPath(str(other["dest"])).name.casefold()
== entry.name.casefold()
and ((not rel_dir and top_placed)
or _drive_key(_parent_of(str(other["dest"])), fold) == _drive_key(rel_dir, fold))]
if not near:
continue
remove.append({"dest": rel, "root": "", "sha1": sha1, "size": info.st_size,
"systems": [], "path": path, "inode": _stamp(info),
"placed": near[0][0], "placed_path": near[0][2],
"placed_inode": _stamp(near[0][1])})
remove.sort(key=lambda entry: str(entry["dest"]))
return Removal(plat, folder, remove, [], [], list(record["setups"][plat].get("choice")
or []), [])
from installer.backups import _action_setup, _backup_date
from installer.removal_plan import (
CHANGED_SINCE_PLAN,
REMOVAL_REASONS,
Removal,
_drive_key,
_stamp,
plan_misplaced_removal,
plan_removal,
)
def _left_by_hand(folder: Path, record: dict, fold: bool) -> list[str]:
@@ -526,11 +84,6 @@ def kept_line(item: dict) -> str:
return f"{item['dest']}: {why}"
def _stamp(info: os.stat_result) -> tuple[int, int, int, int]:
"""What tells a file from itself edited: device, inode, size, mtime."""
return info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns
def _other_setup_list(name: str) -> tuple[dict | None, str]:
"""The list of another setup of the folder, fetched from the address
whatever list this run was given, or None and why it is not at hand."""
@@ -861,160 +414,3 @@ def run_removal(args: argparse.Namespace) -> int:
# The files a removal leaves named one by one by default: past this, a
# count and --verbose.
REMOVAL_KEPT_SHOWN = 10
def _backup_date(run: Path) -> str:
"""When a run folder was made, read from its name: 2026-11-03 12:00:00."""
match = re.match(r"(\d{4})(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d)", run.name)
if not match:
return ""
year, month, day, hour, minute, second = match.groups()
return f"{year}-{month}-{day} {hour}:{minute}:{second}"
def backup_words(date: str) -> str:
"""A backup's date as a list says it: 3 Nov 12:00."""
try:
when = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
except ValueError:
return date or "unknown date"
return f"{day_words(when)} {when.hour:02d}:{when.minute:02d}"
def list_backups(folder: Path) -> list[dict]:
"""The backups of a BIOS folder, newest first: {folder, date, files,
bytes, source}, source remove for a removal's and install for the files
an install replaced. Only dated folders holding the marker count."""
folder = Path(os.path.abspath(folder))
found = []
for holder, prefix in KeptFiles._holders(folder):
try:
if not stat.S_ISDIR(os.lstat(holder).st_mode):
continue
runs = sorted(holder.iterdir())
except OSError:
continue
for run in runs:
if not _is_backup_run(run):
continue
under = run / prefix if prefix else run
files = [path for path in _backup_files(run) if under in path.parents]
if files:
found.append({"folder": run, "date": _backup_date(run), "files": len(files),
"bytes": sum(path.stat().st_size for path in files),
"source": "remove" if _backup_note(run) is not None
else "install"})
return sorted(found, key=lambda item: item["folder"].name, reverse=True)
def run_backups(args: argparse.Namespace) -> int:
"""--backups: the backups of the BIOS folder, newest first."""
folder = _action_folder(args)
backups = list_backups(folder)
if not backups:
print(f"No backup of {shown_path(folder)}.")
return 0
print(f"Backups of {shown_path(folder)}, newest first:")
for item in backups:
made = "removed" if item["source"] == "remove" else "replaced by an install"
print(f" {backup_words(item['date'])} {_plural(item['files'], 'file')}, "
f"{format_size(item['bytes'])}, {made}")
print(f" {shown_path(item['folder'])}")
EVENTS.emit("backup", folder=str(spelled(item["folder"])), date=item["date"],
files=item["files"], bytes=item["bytes"], source=item["source"],
status="listed", message="")
print(f"To put one back: {_INVOCATION.launcher()} --restore FOLDER")
return 0
def run_delete_backup(args: argparse.Namespace) -> int:
"""--delete-backup: one backup folder deleted after one confirmation.
0 done, 1 a file failed, 2 refused or declined."""
run = Path(os.path.abspath(os.path.expanduser(str(args.delete_backup))))
if not _is_backup_run(run):
raise Refusal("backup", "not_a_backup", "",
f"Error: {run} is not a folder the installer kept files in.",
" Name one of the dated folders --backups lists.", code=2)
files = _backup_files(run)
size = sum(path.stat().st_size for path in files)
date = _backup_date(run)
source = "remove" if _backup_note(run) is not None else "install"
print(f"Delete the backup of {backup_words(date)}: {_plural(len(files), 'file')}, "
f"{format_size(size)}.")
if args.dry_run:
print("Nothing was deleted (--dry-run).")
return 0
if not args.yes:
if args.no_input or not sys.stdin.isatty():
raise Refusal("option", "needs_yes", "",
"Error: deleting a backup needs a yes: add --yes to delete it "
"without a question.", code=2)
if not _answer_yes("Delete it for good? [y/N] "):
print("Nothing was deleted.")
return QUIT_CODE
failed = 0
# Bottom up, never through a link: a link goes, not what it names. A
# junction is removed as a folder, which leaves its target alone.
for path, info in _tree(run):
try:
if stat.S_ISDIR(info.st_mode):
path.rmdir()
else:
_clear_read_only(path)
path.unlink()
except OSError as exc:
failed += 1
print(f" failed: {path} ({exc.strerror or exc})", file=sys.stderr)
try:
run.rmdir()
except OSError as exc:
failed += 1
print(f" failed: {run} ({exc.strerror or exc})", file=sys.stderr)
EVENTS.emit("backup", folder=str(spelled(run)), date=date, files=len(files), bytes=size,
source=source, status="not_deleted" if failed else "deleted",
message=f"{_plural(failed, 'file')} could not be deleted." if failed else "")
if failed:
return 1
print(f"Deleted, {format_size(size)} freed.")
return 0
def _action_folder(args: argparse.Namespace) -> Path:
"""The BIOS folder a listing of backups is for: --dest, else the one
setup found here."""
if args.dest and not args.platform:
return Path(os.path.abspath(os.path.expanduser(str(args.dest))))
return _action_setup(args)[1]
def _action_setup(args: argparse.Namespace) -> tuple[str, Path]:
"""The setup and BIOS folder a removal is for: --platform and --dest,
the one setup a record names or the search here finds, never a guess.
A choice left open exits 2, naming the option that settles it."""
named = args.platform or ""
plat = normalize_platform(args.platform, say=False) if args.platform else ""
if args.dest:
dest = Path(args.dest)
if plat:
plat, dest = card_dest(named, plat, dest)
folder = Path(os.path.abspath(os.path.expanduser(str(dest))))
if plat:
return plat, folder
setups = sorted((load_record(folder).data or {}).get("setups") or {})
if len(setups) == 1:
return setups[0], folder
raise Refusal("detect", "conflict" if setups else "not_found", "",
f"Error: name the setup of {shown_path(folder)} with --platform"
+ (f": {', '.join(setups)}." if setups else "."), code=2)
found = [(name, path) for name, path in detect_platforms(detect_os())
if not plat or name == plat]
if len(found) == 1:
return found[0][0], Path(os.path.abspath(found[0][1]))
if not found:
raise Refusal("detect", "not_found", plat,
"Error: no setup found here: name its BIOS folder with --dest"
+ ("." if plat else " and its setup with --platform."), code=2)
raise Refusal("detect", "conflict", plat,
"Error: several setups found here: name one with --platform and --dest.",
*(f" --platform {name} --dest {_INVOCATION.word(str(path))}"
for name, path in found), code=2)
+460
View File
@@ -0,0 +1,460 @@
"""Which installed files a removal takes, which it keeps and why."""
from __future__ import annotations
import os
import stat
from collections.abc import Collection, Mapping
from pathlib import Path, PurePosixPath
from typing import NamedTuple
from installer.choices import available_choices, system_ids
from installer.filesystems import keeps_case
from installer.host_fs import _is_link
from installer.install_record import _below, _check_ours, record_argv, record_options
from installer.local_check import _digest_file
from installer.manifest import OTHER_SYSTEMS, entry_copies
from installer.narrowing import designated_cores
from installer.platforms import display_name, entry_systems, system_title
from installer.safe_replace import BACKUP_DIR
# Removal deletes for good, or moves into a backup with --backup, only what
# the install record proves the installer placed: a file it lists, still
# its recorded bytes, the collection able to serve it again. Anything else
# is named and left. Why each file stays, as removal_plan's kept says it.
REMOVAL_REASONS = {
"shared": "still needed",
"changed": "changed since it was installed",
"link": "a link, never followed",
"not_a_file": "not a regular file",
"outside": "outside the BIOS folder",
"not_servable": "the collection cannot give it again",
"unreadable": "could not be read",
"not_recorded": "not installed by RetroBIOS",
}
# Why a planned file stays when it changed between the plan and its removal.
CHANGED_SINCE_PLAN = "changed since the plan"
class Removal(NamedTuple):
"""What a removal takes and leaves. remove holds {dest, root, sha1,
size, systems, path, inode} per file to remove, kept {dest, root, sha1,
size, systems, reason, what} per file left, gone the record's entries
no longer on disk, choice the setup's choice once the removal is done
(None: the setup goes from the record), systems those losing a file."""
plat: str
folder: Path
remove: list
kept: list
gone: list
choice: list | None
systems: list
def _drive_key(dest: str, fold: bool) -> str:
"""dest as the drive compares it, folded where it folds case."""
return dest.casefold() if fold else dest
def _entry_sha1s(entry: dict) -> set[str]:
return {str(copy.get("sha1")) for copy in entry_copies(entry)
if copy.get("sha1")}
def _entry_readers(entry: dict) -> set[str] | None:
"""The cores that read an entry, None for a file of the platform's own list."""
if entry.get("cores") is None:
return None
return {core for copy in entry_copies(entry)
for core in copy.get("cores") or ()}
def _served(manifest: dict, fold: bool) -> dict[tuple[str, str], tuple]:
"""What the list serves, by (root, dest): (the sha1s it serves there,
its systems, the cores that read it, whether the platform's own list
names it). A place listed twice keeps both: a core's copy beside the
platform's own never makes the file a core's alone. A data tree's member
counts under root data, as the record keeps it."""
served: dict[tuple[str, str], tuple] = {}
for entry in [*(manifest.get("files") or []), *(manifest.get("case_variants") or [])]:
dest = str(entry.get("dest"))
place = (str(entry.get("root") or ""), _drive_key(dest, fold))
sha1s, systems, readers, own = served.get(place, (set(), [], set(), False))
mine = _entry_readers(entry)
served[place] = (sha1s | _entry_sha1s(entry),
sorted(set(systems) | set(entry_systems(entry))),
readers | (mine or set()), own or mine is None)
for tree in manifest.get("data_directories") or []:
for rel, meta in (tree.get("files") or {}).items():
dest = f"{tree['dest']}/{rel}" if tree.get("dest") else rel
served[("data", _drive_key(dest, fold))] = (
{str(meta.get("sha1"))}, list(tree.get("systems") or []), set(), True)
return served
def _recorded_served(setup: dict, manifest: dict, fold: bool
) -> list[tuple[dict, tuple[str, str], tuple]]:
"""Each file a setup's record holds, with its place as the drive opens
it and what the list serves there, by the one rule removal reads."""
served = _served(manifest, fold)
places = [(item, (str(item.get("root") or ""), _drive_key(str(item["dest"]), fold)))
for item in setup.get("files") or []]
return [(item, place, served.get(place, (set(), [], set(), False)))
for item, place in places]
def recorded_systems(setup: dict, manifest: dict, folder: Path) -> list[tuple[dict, list[str]]]:
"""Each file a setup's record holds, with the list's systems of it: a
drive that folds case finds a recased dest under the list's spelling."""
return [(item, list(found[1]))
for item, _place, found in _recorded_served(setup, manifest, not keeps_case(folder))]
def _choice_names(options: list[tuple[str, str | None]], flag: str) -> list[str]:
return [name.strip() for option, value in options if option == flag
for name in (value or "").split(",") if name.strip()]
def _choice_without(choice: list[str], flag: str, names: Collection[str],
left: list[str], ids: Mapping[str, str] | None = None) -> list[str]:
"""A recorded choice once names of flag are taken out: those it named
go, and a choice that named none gets the names left. A flag left with
no name is dropped. ids gives a recorded name the id it stands for."""
options = [option for option in record_options(choice) if option[0] != "--all-files"]
gone = {name.casefold() for name in names}
same = ids or {}
remaining = [name for name in (_choice_names(options, flag) or left)
if same.get(name, name).casefold() not in gone]
kept = [option for option in options if option[0] != flag]
if remaining:
kept.append((flag, ",".join(remaining)))
return record_argv(kept)
class _RemovalScope(NamedTuple):
"""What a removal takes out, and what stays chosen once it is done."""
fold: bool
known: dict[str, list[str]]
options: list[tuple[str, str | None]]
chosen_systems: set[str]
chosen_cores: set[str]
staying_systems: set[str]
staying_cores: set[str]
named_cores: list[str]
others: dict[tuple[str, str], str]
wanted: set[str]
system_ids: dict[str, str]
def norm(self, dest: str) -> str:
"""dest as the drive compares it, folded where it folds case."""
return _drive_key(dest, self.fold)
def _removal_scope(
plat: str, record: dict, manifest: dict, fold: bool, *,
systems: Collection[str], cores: Collection[str],
emulators: Collection[str], files: Collection[str],
) -> _RemovalScope:
"""The scope of a removal of plat's setup from record."""
setup = record["setups"][plat]
known = available_choices(list(manifest.get("files") or []),
list(manifest.get("omitted_files") or []))
options = record_options(setup.get("choice") or [])
chosen_systems = set(systems)
chosen_cores = designated_cores(manifest, [*cores, *emulators]) if (
cores or emulators) else set()
others = {(str(item.get("root") or ""), _drive_key(item["dest"], fold)): name
for name, other in record["setups"].items() if name != plat
for item in other.get("files") or []}
wanted = {_drive_key(dest, fold) for dest in files}
# What stays chosen once the removal is done, as an install names it:
# the systems or cores the record named, else every one of the list.
recorded_systems = _choice_names(options, "--system")
ids = system_ids(recorded_systems, manifest, list(manifest.get("files") or []),
list(manifest.get("omitted_files") or []))
named_systems = {ids.get(name, name).casefold() for name in recorded_systems}
staying_systems = {name for name in known["systems"] if not named_systems
or name.casefold() in named_systems} - chosen_systems
named_cores = _choice_names(options, "--core")
staying_cores = (designated_cores(manifest, named_cores) if named_cores
else set(known["cores"]) | set(manifest.get("core_names") or {})
) - chosen_cores
return _RemovalScope(fold, known, options, chosen_systems, chosen_cores,
staying_systems, staying_cores, named_cores, others, wanted, ids)
def _removal_takes(scope: _RemovalScope, found: tuple, dest: str) -> bool:
"""Whether the removal's choice reaches a recorded file at dest."""
_sha1s, item_systems, readers, own = found
if scope.chosen_systems and not (scope.chosen_systems & set(item_systems) or (
not item_systems and OTHER_SYSTEMS in scope.chosen_systems)):
return False
# The platform's own list keeps its files whatever core is removed.
if scope.chosen_cores and (own or not readers & scope.chosen_cores):
return False
return not (scope.wanted and scope.norm(dest) not in scope.wanted)
def _removal_needed(scope: _RemovalScope, manifest: dict, item: dict, key: tuple[str, str],
found: tuple) -> tuple[str, str]:
"""Why a file of ours on disk stays, and what still needs it: ("", "") when it goes."""
sha1s, item_systems, readers, _own = found
if item["sha1"] not in sha1s:
return "not_servable", ""
if key in scope.others:
return "shared", display_name(scope.others[key])
if scope.chosen_systems and set(item_systems) & scope.staying_systems:
return "shared", ", ".join(system_title(name, manifest) for name in sorted(
set(item_systems) & scope.staying_systems))
if scope.chosen_cores and readers and readers & scope.staying_cores:
return "shared", ", ".join(sorted(readers & scope.staying_cores))
return "", ""
def _removal_entry(scope: _RemovalScope, manifest: dict, folder: Path, item: dict,
found: tuple) -> tuple[str, dict]:
"""Where a picked file of the record goes: ("remove", entry), ("kept",
entry) or ("gone", item)."""
root, dest = str(item.get("root") or ""), str(item["dest"])
entry = {"dest": dest, "root": root, "sha1": item["sha1"], "size": item["size"],
"systems": found[1]}
if root not in ("", "data"):
return "kept", {**entry, "reason": "outside", "what": ""}
path, why = _below(folder, dest)
info = None
if path is not None:
why, info = _check_ours(path, item)
if why == "gone":
return "gone", item
what = ""
if not why:
why, what = _removal_needed(scope, manifest, item, (root, scope.norm(dest)), found)
if why or path is None or info is None:
return "kept", {**entry, "reason": why or "unreadable", "what": what}
return "remove", {**entry, "path": path, "inode": _stamp(info)}
def _removal_choice(scope: _RemovalScope, recorded: list[str], *, files: Collection[str],
cores: Collection[str], emulators: Collection[str]) -> list | None:
"""The setup's choice once the removal is done, None when it goes."""
if files:
return recorded
if scope.chosen_systems:
choice = _choice_without(recorded, "--system", scope.chosen_systems,
sorted(scope.staying_systems | scope.chosen_systems),
scope.system_ids)
if not _choice_names(record_options(choice), "--system"):
return None
return choice
if scope.chosen_cores and emulators and _choice_names(scope.options, "--emulator"):
return _choice_without(recorded, "--emulator", emulators, [])
if scope.chosen_cores:
left = ([] if scope.named_cores
else sorted(set(scope.known["cores"]) - scope.chosen_cores))
return _choice_without(recorded, "--core", scope.chosen_cores | set(cores), left)
return None
def plan_removal(
plat: str, folder: Path, record: dict, manifest: dict, *,
systems: Collection[str] = (), cores: Collection[str] = (),
emulators: Collection[str] = (), files: Collection[str] = (),
) -> Removal:
"""What removing the chosen part of a setup takes and leaves.
Only the record of plat in folder says what may go: every file it
lists, or those of systems, of cores (or of the emulators' cores), or
the dests in files. A file still needed by what stays (a system or a
core still chosen, another setup of the same folder) stays, and so do a
file changed since, a link, anything that is not a regular file, any
file outside the BIOS folder, and any the list can no longer serve. A
drive that folds case compares dests folded, as FAT and exFAT open them.
"""
folder = Path(os.path.abspath(folder))
setup = record["setups"][plat]
fold = not keeps_case(folder)
scope = _removal_scope(plat, record, manifest, fold, systems=systems, cores=cores,
emulators=emulators, files=files)
planned_entries: dict[str, list] = {"remove": [], "kept": [], "gone": []}
picked: set[str] = set()
planned: set[tuple[str, str]] = set()
for item, key, found in _recorded_served(setup, manifest, fold):
if not _removal_takes(scope, found, str(item["dest"])):
continue
if key in planned:
# Two entries the drive opens as one file: planned once.
continue
planned.add(key)
picked.add(key[1])
where, entry = _removal_entry(scope, manifest, folder, item, found)
planned_entries[where].append(entry)
remove, kept, gone = (planned_entries["remove"], planned_entries["kept"],
planned_entries["gone"])
kept += [{"dest": dest, "root": "", "sha1": "", "size": 0, "systems": [],
"reason": "not_recorded", "what": ""}
for dest in files if scope.norm(dest) not in picked]
choice = _removal_choice(scope, list(setup.get("choice") or []), files=files,
cores=cores, emulators=emulators)
lost = sorted({name for entry in remove for name in entry["systems"]})
return Removal(plat, folder, remove, kept, gone, choice, lost)
def _parent_of(dest: str) -> str:
"""The folder of a dest, "" at the top: dc for dc/dc_boot.bin."""
parent = PurePosixPath(dest).parent.as_posix()
return "" if parent == "." else parent
def _misplaced_known(record: dict, manifests: list[dict], fold: bool
) -> tuple[set[str], bool, set[str]]:
"""The places any list of manifests reads or any setup recorded, whether
a list places files at the top, and the game data dests."""
known: set[str] = set()
top_placed = False
# Game data sits in folders a frontend lists as games: DOOM2.WAD beside
# doom2.wad is a game entry of its own there, never a copy.
games = {_drive_key(str(entry.get("dest", "")), fold) for manifest in manifests
for entry in manifest.get("files") or [] if entry.get("category") == "game_data"}
for manifest in manifests:
for root, dest in _served(manifest, fold):
known.add(dest)
top_placed = top_placed or (root == "" and "/" not in dest)
known |= {_drive_key(str(entry.get("dest", "")), fold)
for entry in manifest.get("omitted_files") or []}
known |= {_drive_key(str(item["dest"]), fold) for setup in record["setups"].values()
for item in setup.get("files") or []}
return known, top_placed, games
def _misplaced_entries(folder: Path, rel_dir: str) -> list[os.DirEntry[str]]:
"""The entries of a folder the installer placed files in, by name: none
when it is reached through a link or cannot be read."""
# The folder reached through no link, as _below reaches a file in it.
inside, why = _below(folder, f"{rel_dir}/-" if rel_dir else "-")
if inside is None or why:
return []
try:
return sorted(os.scandir(inside.parent), key=lambda entry: entry.name)
except OSError:
return []
def _misplaced_candidate(entry: os.DirEntry[str], sizes: Collection[int]
) -> tuple[Path, os.stat_result, str] | None:
"""The path, lstat and sha1 of a lone regular file of a placed file's size."""
path = Path(entry.path)
try:
info = os.lstat(path)
except OSError:
return None
if (not stat.S_ISREG(info.st_mode) or _is_link(info) or info.st_nlink != 1
or info.st_size not in sizes):
return None
try:
sha1 = _digest_file(path, ("sha1",))["sha1"]
except OSError:
return None
return path, info, sha1
def plan_misplaced_removal(
plat: str, folder: Path, record: dict, manifests: list[dict],
) -> Removal:
"""The copies in the wrong place of the BIOS folder that removing takes.
A copy goes when its bytes are those of a file plat's record says the
installer placed, read again now at that place, under that file's own
name, and it sits beside that file (the name in another case), or at
the top of the folder where the list places files there itself:
GBA_BIOS.BIN beside gba_bios.bin, dc_boot.bin at the top of RetroArch's
system folder for dc/dc_boot.bin. A renamed copy stays: a core option,
a launcher or a frontend entry may name it. Only those folders
are read, so a games folder (ports/doom on a card whose install folder
is the roms folder), the save folder and any other tree are never
looked at. Even there these stay:
- a place any list of manifests reads or any setup recorded, in any
root, whole or as the end of the path from a folder down
- a link, anything but a regular file, a file with a second name, and
the installer's own file under a spelling the record does not use.
"""
folder = Path(os.path.abspath(folder))
fold = not keeps_case(folder)
known, top_placed, games = _misplaced_known(record, manifests, fold)
placed: dict[int, list[dict]] = {}
# The folders read: each one the installer placed a file in, and the
# top where the list places files there itself.
folders: dict[str, str] = {"": ""} if top_placed else {}
for item in record["setups"][plat].get("files") or []:
if str(item.get("root") or "") == "" and isinstance(item.get("size"), int):
placed.setdefault(item["size"], []).append(item)
parent = _parent_of(str(item["dest"]))
folders.setdefault(_drive_key(parent, fold), parent)
proven: dict[str, tuple[str, os.stat_result, Path] | None] = {}
def in_place(item: dict) -> tuple[str, os.stat_result, Path] | None:
"""The placed file's dest, lstat and path when it still holds its bytes."""
if item["dest"] not in proven:
path, why = _below(folder, item["dest"])
info = None
if path is not None and not why:
why, info = _check_ours(path, item)
proven[item["dest"]] = ((item["dest"], info, path)
if not why and info and path is not None else None)
return proven[item["dest"]]
def read_somewhere(rel: str) -> bool:
"""Whether rel is a known place, or ends with one that names a
folder: saves/n3ds/Azahar/sysdata/keys.txt is the save folder's
n3ds/Azahar/sysdata/keys.txt. A bare name is no such end."""
parts = _drive_key(rel, fold).split("/")
return "/".join(parts) in known or any(
"/".join(parts[index:]) in known for index in range(1, len(parts) - 1))
remove: list[dict] = []
for rel_dir in sorted(folders.values()):
for entry in _misplaced_entries(folder, rel_dir):
rel = f"{rel_dir}/{entry.name}" if rel_dir else entry.name
if entry.name in (".nomedia", BACKUP_DIR) or read_somewhere(rel):
continue
candidate = _misplaced_candidate(entry, placed)
if candidate is None:
continue
path, info, sha1 = candidate
found = [(other, hit) for other in placed[info.st_size] if other["sha1"] == sha1
for hit in [in_place(other)] if hit is not None]
if any(_drive_key(str(other["dest"]), fold) in games for other, _hit in found):
continue
if any((hit[1].st_dev, hit[1].st_ino) == (info.st_dev, info.st_ino)
for _other, hit in found):
# The installer's own file under a spelling the record does not use.
continue
# Under its twin's own name: a copy renamed (SCPH-39001 USA.bin, a
# game's own title) may be read by that name, a core option or a
# frontend entry naming it. Beside its twin that name differs in
# case only, and at the top it is a twin's anywhere below.
near = [hit for other, hit in found
if PurePosixPath(str(other["dest"])).name.casefold()
== entry.name.casefold()
and ((not rel_dir and top_placed)
or _drive_key(_parent_of(str(other["dest"])), fold) == _drive_key(rel_dir, fold))]
if not near:
continue
remove.append({"dest": rel, "root": "", "sha1": sha1, "size": info.st_size,
"systems": [], "path": path, "inode": _stamp(info),
"placed": near[0][0], "placed_path": near[0][2],
"placed_inode": _stamp(near[0][1])})
remove.sort(key=lambda entry: str(entry["dest"]))
return Removal(plat, folder, remove, [], [], list(record["setups"][plat].get("choice")
or []), [])
def _stamp(info: os.stat_result) -> tuple[int, int, int, int]:
"""What tells a file from itself edited: device, inode, size, mtime."""
return info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns
+1 -1
View File
@@ -19,7 +19,7 @@ from installer.paths import SavePlace, spelled
from installer.plan_words import NO_CORE_FILES, _core_sized, emptied_words
from installer.platforms import _tui_system_label, system_title
from installer.regions import region_title
from installer.removal import recorded_systems
from installer.removal_plan import recorded_systems
from installer.tally import tally
from installer.text import _names_and, _plural, agree
from installer.tui_commands import _core_base, _tui_system_flags, _tui_systems_chosen, tui_flags, tui_run_files
+2 -1
View File
@@ -30,7 +30,8 @@ from installer.outcome import (
)
from installer.paths import shown_path
from installer.platforms import display_name
from installer.removal import _kept_words, backup_words, kept_line
from installer.removal import _kept_words, kept_line
from installer.backups import backup_words
from installer.romm import ROMM_SCAN_WORDS
from installer.tally import installed_words, left_out_words
from installer.text import _plural, _wrap, agree, cell_width, format_size
+1 -1
View File
@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING
from installer.device import detect_board
from installer.platforms import display_name
from installer.removal import list_backups
from installer.backups import list_backups
from installer.tui_actions import _activate, _device_focus, _install_now, _same_plan
from installer.tui_steps import _focus_plan, _replace, _start_check, _start_run
from installer.tui_folder_actions import _known_choice, _tui_folder_facts
+1 -1
View File
@@ -18,7 +18,7 @@ from installer.plan import CONFIRM_FROM_BYTES
from installer.plan_words import cores_words
from installer.platforms import TARGET_PLATFORMS, _tui_system_label, system_title
from installer.regions import region_title
from installer.removal import backup_words
from installer.backups import backup_words
from installer.setups import setup_list
from installer.tally import tally
from installer.text import _plural, cell_width, format_size
+1 -1
View File
@@ -9,7 +9,7 @@ from installer.detect_os import detected_shown
from installer.manifest import OTHER_SYSTEMS
from installer.offline import LOCAL
from installer.platforms import display_name
from installer.removal import backup_words
from installer.backups import backup_words
from installer.setups import _setup_label
from installer.text import _plural, _wrap, cell_width, format_duration, format_size
from installer.tui_choice import (
+1 -1
View File
@@ -505,7 +505,7 @@ those it imports:
| Sources of files | `offline`, `network`, `lists`, `pack_source`, `from_folders`, `diagnosis`, `download`, `datadirs`, `standalone` |
| What a run keeps | `narrowing`, `choices`, `romm`, `install_record`, `game_files`, `games`, `tally`, `space`, `fetch_plan` |
| Line mode | `destination`, `line_folder`, `line_menu`, `plan_words`, `plan`, `failure_causes`, `outcome` |
| Runs and actions | `arguments`, `run_setups`, `run_files`, `run`, `pack`, `removal`, `handheld_app`, `cli` |
| Runs and actions | `arguments`, `run_setups`, `run_files`, `run`, `pack`, `removal_plan`, `removal`, `backups`, `handheld_app`, `cli` |
| Full screen | `tui_state`, `tui_routes`, `tui_commands`, `tui_keys`, `tui_paint`, `tui_choice`, `tui_folder_scan`, `tui_rows`, `tui_frame`, `tui_plan_view`, `tui_outcome`, `tui_where_views`, `tui_screens`, `tui_help`, `tui_steps`, `tui_folder_actions`, `tui_actions`, `tui_input`, `tui_receive`, `tui_list_words`, `tui_engine`, `tui_session` |
## Tests