diff --git a/.gitignore b/.gitignore index 69f0d201..db839734 100644 --- a/.gitignore +++ b/.gitignore @@ -384,3 +384,6 @@ uploads/ stapler-scripts/gphotos-organizer/album_mapping.json stapler-scripts/gphotos-organizer/album_report.md stapler-scripts/gphotos-organizer/upload_manifest.json + +# retired in-repo Darwin marker (the verified marker is machine-local: ~/.local/state/dunnage/darwin-verified) +stapler-scripts/.disk-cleanup-darwin-ok diff --git a/README.md b/README.md index 4bcea845..85d05d59 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ I use [zplug](https://github.com/zplug/zplug) as my package manager, you can fin The directory [.shell](./shell) contains several scripts that are sourced in the [`.zshrc`](./.zshrc), their names should be fairly self explanatory. +### Disk cleanup (dunnage) + +`disk-cleanup`, `buildclean.py` and `steam-clean` in [stapler-scripts](./stapler-scripts) are thin wrappers over [dunnage](https://github.com/tstapler/homebrew-tap) (`brew "tstapler/homebrew-tap/dunnage"` in the Brewfiles). Dunnage is a dry run by default; copy a `Next:` line to delete. The wrappers fall back to the old tools when dunnage is missing or `DUNNAGE_LEGACY=1`. On macOS they stay on the old tools until a human verifies the binary on that Mac and records its hash: `shasum -a 256 "$(command -v dunnage)" | cut -d' ' -f1 > ~/.local/state/dunnage/darwin-verified` (a rebuilt binary falls back again). The old `steam-clean` binary's source is `stapler-scripts/steam-clean-legacy`; rebuild it with `cargo install --path stapler-scripts/steam-clean-legacy --root ~/.local/share/dunnage/legacy` and link `legacy/bin/steam-clean` to `legacy/steam-clean`. + ### Vim (Neovim) I'm a heavy Vim user, so I have configuration for [Intellij's vim plugin](./.ideavimrc) as well as [actual Vim](./.vimrc) split across a few different files diff --git a/stapler-scripts/Cargo.toml b/stapler-scripts/Cargo.toml index 410a7912..ca6aa3f2 100644 --- a/stapler-scripts/Cargo.toml +++ b/stapler-scripts/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = ["steam-clean", "claude-mem-tree"] +members = ["steam-clean-legacy", "claude-mem-tree"] resolver = "2" diff --git a/stapler-scripts/buildclean.py b/stapler-scripts/buildclean.py index 9dbda252..678ad179 100644 --- a/stapler-scripts/buildclean.py +++ b/stapler-scripts/buildclean.py @@ -10,6 +10,7 @@ import argparse import functools +import hashlib import json import math import os @@ -389,14 +390,29 @@ def run(args: argparse.Namespace) -> int: def dunnage_bin() -> str | None: """The dunnage binary to delegate to, or None to keep the legacy scanner. - DUNNAGE_LEGACY=1 forces legacy; Darwin stays legacy until the Story 5.2.1e marker check lands. + DUNNAGE_LEGACY=1 forces legacy; Darwin delegates only when the machine-local darwin-verified marker + holds the sha256 of the dunnage binary (Story 5.2.1), which only a human on a real Mac writes. """ - if os.environ.get("DUNNAGE_LEGACY") == "1" or sys.platform == "darwin": + if os.environ.get("DUNNAGE_LEGACY") == "1": return None explicit = os.environ.get("DUNNAGE_BIN") if explicit: - return explicit if os.access(explicit, os.X_OK) else None - return shutil.which("dunnage") + binary = explicit if os.access(explicit, os.X_OK) else None + else: + binary = shutil.which("dunnage") + if binary and sys.platform == "darwin" and not darwin_verified(binary): + return None + return binary + + +def darwin_verified(binary: str) -> bool: + marker = Path(os.environ.get("XDG_STATE_HOME") or HOME / ".local/state") / "dunnage" / "darwin-verified" + try: + want = marker.read_text().split()[0] + got = hashlib.sha256(Path(binary).read_bytes()).hexdigest() + except (OSError, IndexError): + return False + return want == got def log_wrapper_use(event: str) -> None: diff --git a/stapler-scripts/disk-cleanup b/stapler-scripts/disk-cleanup index 35f629c5..e1c9d907 100755 --- a/stapler-scripts/disk-cleanup +++ b/stapler-scripts/disk-cleanup @@ -1,10 +1,21 @@ #!/usr/bin/env bash # Disk management front door: runs `dunnage` (tstapler/homebrew-tap, Brewfile) and falls back to the old -# bash cleaner (disk-cleanup-legacy) only while dunnage is not installed. +# bash cleaner (disk-cleanup-legacy) when dunnage is absent, DUNNAGE_LEGACY=1, or on an unverified Darwin. # dunnage is a dry run by default; pass --apply to delete (plan-id rules: `dunnage --help`). set -euo pipefail -if command -v dunnage >/dev/null 2>&1; then +# Darwin stays on the legacy cleaner until a human on a real Mac records this binary's sha256 in +# ${XDG_STATE_HOME:-~/.local/state}/dunnage/darwin-verified (plan Story 5.2.1); CI never writes it. +verified_here() { + [ "$(uname -s)" = Darwin ] || return 0 + local marker="${XDG_STATE_HOME:-$HOME/.local/state}/dunnage/darwin-verified" want got + [ -f "$marker" ] || return 1 + read -r want _ <"$marker" || true + if command -v shasum >/dev/null 2>&1; then got="$(shasum -a 256 "$1" | cut -d' ' -f1)"; else got="$(sha256sum "$1" | cut -d' ' -f1)"; fi + [ -n "$want" ] && [ "$want" = "$got" ] +} + +if command -v dunnage >/dev/null 2>&1 && [ "${DUNNAGE_LEGACY:-}" != 1 ] && verified_here "$(command -v dunnage)"; then args=() for a in "$@"; do [ "$a" = "-v" ] || args+=("$a") # legacy verbose flag; dunnage always itemises @@ -12,5 +23,5 @@ if command -v dunnage >/dev/null 2>&1; then exec dunnage "${args[@]+"${args[@]}"}" fi -echo "disk-cleanup: dunnage not installed (brew install tstapler/homebrew-tap/dunnage); using the legacy cleaner" >&2 +echo "disk-cleanup: using the legacy cleaner (dunnage not installed, DUNNAGE_LEGACY=1, or Darwin binary not verified)" >&2 exec "$(dirname "$(readlink -f "$0")")/disk-cleanup-legacy" "$@" diff --git a/stapler-scripts/steam-clean b/stapler-scripts/steam-clean new file mode 100755 index 00000000..8c49e33c --- /dev/null +++ b/stapler-scripts/steam-clean @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# steam-clean launcher: delegates to `dunnage steam` when dunnage is installed, otherwise runs the old compiled +# steam-clean. Legacy binary lookup: $DUNNAGE_STEAM_LEGACY_BIN, then ~/.local/share/dunnage/legacy/steam-clean, +# then ~/.cargo/bin/steam-clean. Rebuild it with: +# cargo install --path ~/dotfiles/stapler-scripts/steam-clean-legacy --root ~/.local/share/dunnage/legacy +# (installs to .../legacy/bin/steam-clean; copy or link it to .../legacy/steam-clean). +# DUNNAGE_BIN overrides the binary, DUNNAGE_LEGACY=1 forces legacy. Darwin delegates only when +# ${XDG_STATE_HOME:-~/.local/state}/dunnage/darwin-verified holds the dunnage binary's sha256 (plan Story 5.2.1). +set -euo pipefail + +self="$(readlink -f "$0")" + +log_use() { # append-only outcome log; a write failure is ignored + local dir="${XDG_STATE_HOME:-$HOME/.local/state}/dunnage" + { mkdir -p "$dir" && printf '%s steam-clean %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$1" >>"$dir/wrapper-use.log"; } 2>/dev/null || true +} + +bin="${DUNNAGE_BIN:-}" +[ -n "$bin" ] || bin="$(command -v dunnage || true)" + +verified_here() { + [ "$(uname -s)" = Darwin ] || return 0 + local marker="${XDG_STATE_HOME:-$HOME/.local/state}/dunnage/darwin-verified" want got + [ -f "$marker" ] || return 1 + read -r want _ <"$marker" || true + if command -v shasum >/dev/null 2>&1; then got="$(shasum -a 256 "$1" | cut -d' ' -f1)"; else got="$(sha256sum "$1" | cut -d' ' -f1)"; fi + [ -n "$want" ] && [ "$want" = "$got" ] +} + +if [ -n "$bin" ] && [ -x "$bin" ] && [ "${DUNNAGE_LEGACY:-}" != 1 ] && verified_here "$bin"; then + case "${1:-list}" in + tidy) + log_use retired + echo "steam-clean: the tidy TUI is retired. Rank with 'steam-clean list'; delete with 'dunnage --kinds steam' (dry run), then the Next: line." >&2 + exit 64 + ;; + list) [ "$#" -eq 0 ] || shift; set -- steam list "$@" ;; + *) set -- steam "$@" ;; + esac + export DUNNAGE_VIA=steam-clean DISK_CLEANUP_VIA=steam-clean + exec "$bin" "$@" +fi + +legacy="${DUNNAGE_STEAM_LEGACY_BIN:-}" +if [ -z "$legacy" ]; then + for cand in "${XDG_DATA_HOME:-$HOME/.local/share}/dunnage/legacy/steam-clean" "$HOME/.cargo/bin/steam-clean"; do + if [ -x "$cand" ] && [ "$(readlink -f "$cand")" != "$self" ]; then legacy="$cand"; break; fi + done +fi +if [ -n "$legacy" ] && [ -x "$legacy" ] && [ "$(readlink -f "$legacy")" != "$self" ]; then + exec "$legacy" "$@" +fi +echo "steam-clean: refusing to exec itself or no legacy binary found (${legacy:-none}); install dunnage (brew install tstapler/homebrew-tap/dunnage)" >&2 +exit 2 diff --git a/stapler-scripts/steam-clean/Cargo.toml b/stapler-scripts/steam-clean-legacy/Cargo.toml similarity index 100% rename from stapler-scripts/steam-clean/Cargo.toml rename to stapler-scripts/steam-clean-legacy/Cargo.toml diff --git a/stapler-scripts/steam-clean/src/main.rs b/stapler-scripts/steam-clean-legacy/src/main.rs similarity index 100% rename from stapler-scripts/steam-clean/src/main.rs rename to stapler-scripts/steam-clean-legacy/src/main.rs diff --git a/stapler-scripts/steam-clean/steam-clean b/stapler-scripts/steam-clean/steam-clean deleted file mode 100755 index ece7cc56..00000000 --- a/stapler-scripts/steam-clean/steam-clean +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env bash -# steam-clean launcher: delegates to `dunnage steam` when dunnage is installed, otherwise runs the old compiled -# steam-clean (cargo install --path stapler-scripts/steam-clean). Not on PATH until the cutover (plan Task 5.2.1e). -# DUNNAGE_BIN overrides the binary, DUNNAGE_LEGACY=1 forces legacy, DUNNAGE_STEAM_LEGACY_BIN names the old binary. -set -euo pipefail - -self="$(readlink -f "$0")" - -log_use() { # append-only outcome log; a write failure is ignored - local dir="${XDG_STATE_HOME:-$HOME/.local/state}/dunnage" - { mkdir -p "$dir" && printf '%s steam-clean %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$1" >>"$dir/wrapper-use.log"; } 2>/dev/null || true -} - -bin="${DUNNAGE_BIN:-}" -[ -n "$bin" ] || bin="$(command -v dunnage || true)" -if [ -n "$bin" ] && [ -x "$bin" ] && [ "${DUNNAGE_LEGACY:-}" != 1 ] && [ "$(uname -s)" != Darwin ]; then - case "${1:-list}" in - tidy) - log_use retired - echo "steam-clean: the tidy TUI is retired. Rank with 'steam-clean list'; delete with 'dunnage --kinds steam' (dry run), then the Next: line." >&2 - exit 64 - ;; - list) [ "$#" -eq 0 ] || shift; set -- steam list "$@" ;; - *) set -- steam "$@" ;; - esac - export DUNNAGE_VIA=steam-clean DISK_CLEANUP_VIA=steam-clean - exec "$bin" "$@" -fi - -legacy="${DUNNAGE_STEAM_LEGACY_BIN:-$HOME/.cargo/bin/steam-clean}" -if [ -x "$legacy" ] && [ "$(readlink -f "$legacy")" != "$self" ]; then - exec "$legacy" "$@" -fi -echo "steam-clean: refusing to exec itself or a missing legacy binary ($legacy); install dunnage (brew install tstapler/homebrew-tap/dunnage)" >&2 -exit 2 diff --git a/stapler-scripts/tests/wrapper_darwin.sh b/stapler-scripts/tests/wrapper_darwin.sh new file mode 100755 index 00000000..64005201 --- /dev/null +++ b/stapler-scripts/tests/wrapper_darwin.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# Darwin gate (Story 5.2.1, Task 5.2.1e): all three wrappers stay on the legacy tool unless the machine-local +# darwin-verified marker holds the sha256 of the dunnage binary. A `uname` shim fakes Darwin; nothing real is touched. +# shellcheck source=lib.sh +. "$(dirname "$0")/lib.sh" +use_shim +printf '#!/bin/sh\necho Darwin\n' >"$SCRATCH/bin/uname"; chmod +x "$SCRATCH/bin/uname" + +# Scratch copies so disk-cleanup finds a stub legacy script next to it. +mkdir "$SCRATCH/copy" +cp "$SCRIPTS/disk-cleanup" "$SCRIPTS/steam-clean" "$SCRIPTS/buildclean.py" "$SCRATCH/copy/" +printf '#!/usr/bin/env bash\necho "legacy $*"\n' >"$SCRATCH/copy/disk-cleanup-legacy"; chmod +x "$SCRATCH/copy/disk-cleanup-legacy" +printf '#!/usr/bin/env bash\necho "legacy $*"\n' >"$SCRATCH/legacy"; chmod +x "$SCRATCH/legacy" +export DUNNAGE_STEAM_LEGACY_BIN="$SCRATCH/legacy" +marker="$XDG_STATE_HOME/dunnage/darwin-verified" +mkdir -p "$(dirname "$marker")" +good="$(sha256sum "$SCRATCH/bin/dunnage" | cut -d' ' -f1)" + +# buildclean has no shell uname: run it with sys.platform patched. +bc() { + BC_PATH="$SCRATCH/copy/buildclean.py" python3 -I -c 'import os, runpy, sys; sys.platform = "darwin"; sys.argv = ["buildclean.py"] + sys.argv[1:]; runpy.run_path(os.environ["BC_PATH"], run_name="__main__")' "$@" 2>/dev/null +} + +run_all() { # prints which tool each wrapper used: dunnage or legacy + reset_log + dc="$("$SCRATCH/copy/disk-cleanup" --top 1 2>/dev/null)" + sc="$("$SCRATCH/copy/steam-clean" list 2>/dev/null)" + reset_log; bc "$HOME" --min-age-min 1 >/dev/null || true + bcr="dunnage"; [ -s "$SHIM_LOG" ] || bcr="legacy" + [ "$dc" = "legacy --top 1" ] && dcr=legacy || dcr=dunnage + [ "$sc" = "legacy list" ] && scr=legacy || scr=dunnage + echo "$dcr $scr $bcr" +} + +rm -f "$marker" +check "no marker gives legacy" "legacy legacy legacy" "$(run_all)" +echo "0000" >"$marker" +check "marker with another hash gives legacy" "legacy legacy legacy" "$(run_all)" +echo "$good dunnage" >"$marker" +check "marker with the right hash gives dunnage" "dunnage dunnage dunnage" "$(run_all)" +echo "$good" >"$marker" +check "bare-hash marker gives dunnage" "dunnage dunnage dunnage" "$(run_all)" +echo "$good" >"$marker"; echo changed >>"$SCRATCH/bin/dunnage" +check "rebuilt binary falls back to legacy" "legacy legacy legacy" "$(run_all)" + +# The old in-repo marker path is gitignored so a commit cannot flip a Mac. +if command -v git >/dev/null 2>&1; then + git init -q "$SCRATCH/repo" && printf 'stapler-scripts/.disk-cleanup-darwin-ok\n' >"$SCRATCH/repo/.gitignore" + git -C "$SCRATCH/repo" check-ignore -q stapler-scripts/.disk-cleanup-darwin-ok && r=0 || r=$? + check "old in-repo marker path is ignorable" 0 "$r" +fi +finish diff --git a/stapler-scripts/tests/wrapper_steam_clean.sh b/stapler-scripts/tests/wrapper_steam_clean.sh index fcc3bec4..c0962ef1 100755 --- a/stapler-scripts/tests/wrapper_steam_clean.sh +++ b/stapler-scripts/tests/wrapper_steam_clean.sh @@ -2,7 +2,7 @@ # steam-clean launcher (Story 5.2.1, Task 5.2.1c): list passthrough, tidy retired, legacy fallback. # shellcheck source=lib.sh . "$(dirname "$0")/lib.sh" -SC="$SCRIPTS/steam-clean/steam-clean" +SC="$SCRIPTS/steam-clean" use_shim # S-08: steam_clean_list_sort_size_should_map_to_steam_list_and_tidy_should_exit_64_When_wrapped