diff --git a/README.md b/README.md index ca38e37..7c46e32 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ On Windows, a flag marked "Unix" prints a warning. The run goes on without it, e | `--ascii` | ASCII glyphs only | all | | `-nc`, `--keep-cache` | Skip user cache cleaning (Windows: `%TEMP%`) and, on Linux, the warning about space still held by deleted files that a program keeps open | all | | `-nt`, `--keep-trash` | Skip trash cleaning (Windows: Recycle Bin) | all | -| `--deep-clean` | Also clean the journal, old kernels, leftover configs, snap revisions, crash dumps, npm and Go caches and unused docker and podman data, asking for each | Unix | +| `--soft-clean` | Skip the deep clean. Without this flag, every run asks about each of these before removing it: journal, system logs, old kernels, leftover configs, snap revisions, crash dumps, npm and Go caches, unused docker and podman data, AI models unused for 30 days or more (Ollama, Hugging Face), Rust `target` directories | Unix | | `--changed` | Show the last run's package changes | Unix | | `--changelog PKG` | Show the upstream changelog of `PKG` | Unix | | `--history` | Show the last runs | all | @@ -204,7 +204,7 @@ Every variable, its default and the plugin API: [the documentation](doc/document ## Security -- `up` changes only what you ask for. Cleaning the cache, the Trash and `--deep-clean` ask before deleting anything. +- `up` changes only what you ask for. Cleaning the cache, the Trash and the deep clean ask before deleting anything. Under `-y` the deep clean never touches logs, models, build directories, stopped containers or volumes. - On its own, `up` goes online only for `--weather`, `--self-update`, `--firmware`, `--refresh-mirrors` and one connectivity check in `--doctor`. All other traffic comes from your package managers. - `up` runs with `sudo`, so keep the eMerger folder writable by you alone. If other users can write to it, `up` refuses to start. - Your config, profiles, plugins and hooks follow the same rule: `up` skips any that another user could change. diff --git a/completions/_up b/completions/_up index 3261136..e2faae9 100644 --- a/completions/_up +++ b/completions/_up @@ -49,7 +49,7 @@ _up() { '--keep-cache[skip cache cleaning and the deleted-but-open files warning]' '-nt[no trash clean]' '--keep-trash[no trash clean]' - '--deep-clean[clean journal, old kernels, containers]' + '--soft-clean[skip the deep clean]' '-w[weather]' '--weather[weather]' '--changed[show package diff]' diff --git a/completions/up.bash b/completions/up.bash index ccdc1bd..7a6c588 100644 --- a/completions/up.bash +++ b/completions/up.bash @@ -76,7 +76,7 @@ _up_completion() { --profile --list-profiles --only --except --snapshot --rollback --refresh-mirrors --resume --reboot --reboot-exit --download-only - -nl --no-logo -ni --no-info -nc --keep-cache -nt --keep-trash --deep-clean -w --weather + -nl --no-logo -ni --no-info -nc --keep-cache -nt --keep-trash --soft-clean -w --weather --changed --changelog --history --metrics -err --errors -up --self-update -au --auto-update -rc --rebuild-cache" _up_reply compgen -W "$opts" -- "$cur" diff --git a/completions/up.fish b/completions/up.fish index 5ed0beb..85d7a77 100644 --- a/completions/up.fish +++ b/completions/up.fish @@ -42,7 +42,7 @@ complete -c up -l rollback -d 'revert last snapshot' complete -c up -l reboot-exit -d 'exit 4 if reboot required' complete -c up -l download-only -d 'download, do not install' complete -c up -l metrics -d 'export Prometheus textfile' -r -complete -c up -l deep-clean -d 'journal, old kernels, containers' +complete -c up -l soft-clean -d 'skip the deep clean' complete -c up -l profile -d 'load profile' -xa '(__up_profiles)' complete -c up -l list-profiles -d 'list profiles' complete -c up -l snapshot -d 'pre-update snapshot' diff --git a/doc/documentation.pdf b/doc/documentation.pdf index c1d127c..2aceb07 100644 Binary files a/doc/documentation.pdf and b/doc/documentation.pdf differ diff --git a/doc/documentation.tex b/doc/documentation.tex index 54c83b6..a97d392 100644 --- a/doc/documentation.tex +++ b/doc/documentation.tex @@ -486,9 +486,9 @@ \section{Execution flow}\label{sec:flow} \textit{clean}; serial for system managers, concurrent for safe user-space ones when \texttt{-{}-parallel} is active. \item Optionally clear user cache and trash (skippable with - \texttt{-nc} and \texttt{-nt}), then, with - \texttt{-{}-deep-clean}, the system leftovers and the container - data (Unix). + \texttt{-nc} and \texttt{-nt}), then, unless + \texttt{-{}-soft-clean}, the deep clean: system leftovers, + container data, unused AI models, Rust build directories (Unix). \item Execute \texttt{post.d} hooks. \item Snapshot the installed-packages list into \texttt{pkgs.after} and compute \texttt{pkgs.diff}. @@ -517,7 +517,7 @@ \subsection{All flags} \texttt{-y}, \texttt{-{}-yes} & Assume yes on prompts & all \\ \texttt{-w}, \texttt{-{}-weather} & Show weather line via wttr.in & all \\ \texttt{-{}-ascii} & Force ASCII glyphs only & all \\ -\texttt{-{}-deep-clean} & System leftovers (see \S\ref{sec:deepclean}) & Linux/macOS \\ +\texttt{-{}-soft-clean} & Skip the deep clean (see \S\ref{sec:deepclean}) & Linux/macOS \\ \texttt{-nl}, \texttt{-{}-no-logo} & Hide logo & all \\ \texttt{-ni}, \texttt{-{}-no-info} & Hide system info line & all \\ \texttt{-nc}, \texttt{-{}-keep-cache} & Skip user cache cleaning and the warning about space held by deleted files still open & all \\ @@ -550,7 +550,7 @@ \subsection{All flags} \texttt{-{}-rollback} print a warning and exit 0 without updating. \texttt{-{}-firmware}, \texttt{-{}-no-firmware}, \texttt{-{}-snapshot}, \texttt{-{}-parallel}, \texttt{-{}-resume}, \texttt{-{}-download-only}, -\texttt{-{}-deep-clean} and \texttt{-{}-refresh-mirrors} print a warning +\texttt{-{}-soft-clean} and \texttt{-{}-refresh-mirrors} print a warning and the run goes on without them. \subsection{Combining flags} @@ -733,15 +733,20 @@ \subsection{Cache cleaning} exits, so nothing is deleted; restart the program named. \subsection{Deep clean}\label{sec:deepclean} -\texttt{up -{}-deep-clean} goes after system-level leftovers a normal run -doesn't touch, asking about each one and showing how much it would free. -Skip tasks with \verb|DEEP_CLEAN_SKIP="docker,journal"|. +Every run goes after system-level leftovers, asking about each one and +showing how much it would free; answer no to keep it. \texttt{-{}-soft-clean} +skips the whole deep clean; a list of task keys such as +\verb|DEEP_CLEAN_SKIP="docker,journal"| skips single tasks. Under +\texttt{-y} the logs, models, build directories, stopped containers and +volumes are never touched. Logs are emptied once; no size cap or other +system setting is changed. \begin{longtable}{@{}p{0.22\linewidth} p{0.70\linewidth}@{}} \toprule \textbf{Key} & \textbf{Task} \\ \midrule \endhead -\texttt{journal} & \verb|journalctl --vacuum-size=$JOURNAL_MAX_SIZE| (200M), only if bigger \\ +\texttt{journal} & \verb|journalctl --rotate --vacuum-time=1s|: every archived entry, never under \texttt{-y} \\ +\texttt{syslog} & \texttt{truncate -s 0} the non-empty \verb|/var/log/syslog|, \texttt{messages}, \texttt{user.log} and their \texttt{.1}, naming the program that wrote over half of the last MiB of the biggest; never under \texttt{-y} \\ \texttt{kernels} & dnf: keep the 2 newest kernels; zypper: \texttt{purge-kernels} \\ \texttt{residual} & \texttt{dpkg -{}-purge} config files of removed packages \\ \texttt{snap} & remove disabled snap revisions \\ @@ -750,7 +755,7 @@ \subsection{Deep clean}\label{sec:deepclean} \bottomrule \end{longtable} -Two more sections follow, with the same prompts and skip keys. +Three more sections follow, with the same prompts and skip keys. \begin{longtable}{@{}p{0.22\linewidth} p{0.70\linewidth}@{}} \toprule @@ -767,6 +772,24 @@ \subsection{Deep clean}\label{sec:deepclean} (\verb|~/.docker/desktop|, on macOS \verb|~/Library/Containers/com.docker.docker|) is shown from 1M, since its disk image shrinks only from Docker Desktop \\ +\texttt{ollama} & AI models section: Ollama models (stores in + \verb|$OLLAMA_MODELS|, \verb|~/.ollama/models|, + \verb|/usr/share/ollama/.ollama/models|, \verb|/var/lib/ollama|) + whose weights were not read for \texttt{AI\_UNUSED\_DAYS} (30), as a + menu, biggest first. Removed with \texttt{ollama rm} when the server + answers, else by deleting the manifest and the blobs no other + manifest lists (through \texttt{sudo} when the store is not + writable). Also unfinished downloads (\texttt{-partial} blobs + untouched for a day). Models are never offered under \texttt{-y} \\ +\texttt{huggingface} & Hugging Face hub repos (\verb|$HF_HUB_CACHE|, else + \verb|$HF_HOME/hub|, else \verb|~/.cache/huggingface/hub|) whose + newest blob was not read for \texttt{AI\_UNUSED\_DAYS}, as a menu \\ +\texttt{cargo} & Build artifacts section: cargo \texttt{target} + directories (holding \texttt{CACHEDIR.TAG}, beside a + \texttt{Cargo.toml}) of 1M or more under \texttt{BUILD\_SCAN\_ROOTS} + (\verb|$HOME|, \texttt{:}-separated), hidden directories and + \texttt{node\_modules} not searched, as a menu with the days since + the last build. Never offered under \texttt{-y} \\ \bottomrule \end{longtable} @@ -985,7 +1008,7 @@ \subsection{Every supported variable} \texttt{-{}-ascii} & \texttt{ARG\_ASCII} & \texttt{Ascii} \\ \texttt{-nc} & \texttt{ARG\_KEEP\_CACHE} & \texttt{KeepCache} \\ \texttt{-nt} & \texttt{ARG\_KEEP\_TRASH} & \texttt{KeepTrash} \\ -\texttt{-{}-deep-clean} & \texttt{ARG\_DEEP\_CLEAN} & \texttt{DeepClean} \\ +\texttt{-{}-soft-clean} & \texttt{ARG\_SOFT\_CLEAN} & \texttt{SoftClean} \\ \texttt{-{}-changed} & \texttt{ARG\_CHANGED} & \texttt{Changed} \\ \texttt{-{}-changelog PKG} & \texttt{ARG\_CHANGELOG} & \texttt{Changelog} \\ \texttt{-{}-history} & \texttt{ARG\_HISTORY} & \texttt{History} \\ @@ -1011,7 +1034,8 @@ \subsection{Every supported variable} \texttt{QUIET\_HOURS} & (unset) & Unix & \texttt{"HH:MM-HH:MM"}, may wrap past midnight: a run with \texttt{-y} inside it exits 0 \\ \texttt{CACHE\_EXCLUDE} & (empty) & Unix & \verb|~/.cache| entries never offered (names or globs) \\ \texttt{CACHE\_MENU\_ROWS} & 12 & Unix & rows in the cache menu before ``others'' \\ -\texttt{JOURNAL\_MAX\_SIZE} & 200M & Unix & journal target for \texttt{-{}-deep-clean} \\ +\texttt{AI\_UNUSED\_DAYS} & 30 & Unix & days without a read before an AI model is offered \\ +\texttt{BUILD\_SCAN\_ROOTS} & \verb|$HOME| & Unix & where Rust target directories are searched \\ \texttt{DEEP\_CLEAN\_SKIP} & (empty) & Unix & deep-clean tasks to skip (\S\ref{sec:deepclean}) \\ \texttt{RPI\_ABORT\_ON\_UNDERVOLTAGE} & 0 & Unix & abort even with \texttt{-y} on Pi under-voltage \\ \texttt{RPI\_TEMP\_WARN} & 80 & Unix & Pi SoC temperature warning ($^\circ$C) \\ @@ -1507,10 +1531,12 @@ \section{Security model}\label{sec:security} (\texttt{-{}-refresh-mirrors}), a snapshot (\texttt{-{}-snapshot}), the \texttt{-{}-metrics} file, and deletions inside \verb|~/.cache| and the Trash when cleaning. - \texttt{-{}-deep-clean} also deletes journal entries, old kernels, - leftover configs, snap revisions, crash dumps + The deep clean (unless \texttt{-{}-soft-clean}) also deletes + journal entries, system log contents, old kernels, leftover + configs, snap revisions, crash dumps (\verb|/var/lib/systemd/coredump|), unused containers, images, - build cache and volumes, after asking for each. + build cache and volumes, AI models and Rust target directories, + after asking for each. \item On Windows, UAC elevation happens only for the managers that need admin. They run in an elevated child that loads no config, profile or hook. That child runs the code of the checkout: clone it into a @@ -1840,8 +1866,10 @@ \subsection{Bash library modules (\texttt{src/lib/})} \texttt{clean/cache.sh} & Selective \texttt{\textasciitilde{}/.cache} menu \\ \texttt{clean/open.sh} & Report of deleted files still open \\ \texttt{clean/trash.sh} & Trash \\ -\texttt{clean/deep.sh} & \texttt{-{}-deep-clean} tasks \\ -\texttt{clean/containers.sh} & \texttt{-{}-deep-clean} Containers section \\ +\texttt{clean/deep.sh} & deep clean tasks and shared helpers \\ +\texttt{clean/containers.sh} & deep clean, Containers section \\ +\texttt{clean/ai.sh} & deep clean, AI models section \\ +\texttt{clean/builds.sh} & deep clean, Build artifacts section \\ \bottomrule \end{longtable} \end{center} @@ -1898,7 +1926,7 @@ \subsection{Running tests} Each \texttt{tests/.bats} covers one feature: \texttt{cli.bats} the flags, \texttt{lock.bats} the run lock, \texttt{clean.bats} the cache, -trash and \texttt{-{}-deep-clean}, and so on. +trash and the deep clean, and so on. \texttt{tests/test\_helper.bash} holds the shared fixture (temp \texttt{HOME}, stubs on \texttt{PATH}, \texttt{lib} to source modules). The \texttt{tests/ps\_*.bats} files cover the PowerShell half and skip without diff --git a/install.sh b/install.sh index 35c002c..67dcf93 100755 --- a/install.sh +++ b/install.sh @@ -141,6 +141,7 @@ if [[ ! -f "$CONFIG_DIR/config.sh" ]]; then # ARG_DEV=1 # always include dev toolchains # ARG_WEATHER=1 # always show weather # ARG_KEEP_TRASH=1 # never touch trash +# ARG_SOFT_CLEAN=1 # skip the deep clean # DISK_MIN_FREE_MB=2048 # QUIET_HOURS="23:00-07:00" # skip when timer fires inside this window EOF diff --git a/man/up.1 b/man/up.1 index 106049d..fb34582 100644 --- a/man/up.1 +++ b/man/up.1 @@ -11,8 +11,9 @@ the Windows build are in the eMerger manual, \fIdoc/documentation.pdf\fR. .PP eMerger detects the package managers installed on the host and runs each of them in turn to refresh, upgrade and clean the system, then prints a summary. -It can also clean the user cache, the trash and, with \fB\-\-deep\-clean\fR, -system leftovers and unused container data. Every cleaning step asks first unless \fB\-y\fR is given. +It also cleans the user cache, the trash and, unless \fB\-\-soft\-clean\fR +is given, system leftovers, unused container data, unused AI models and Rust +build directories. Every cleaning step asks first unless \fB\-y\fR is given. .PP Short flags from the set \fBh V n v q y w\fR can be bundled: \fB\-nv\fR is \fB\-n \-v\fR. Other short flags (\fB\-nl\fR, \fB\-qq\fR, \fB\-up\fR and so on) @@ -137,14 +138,19 @@ Skip user cache cleaning and the warning about space held by deleted files still .B \-nt, \-\-keep\-trash Skip trash cleaning. .TP -.B \-\-deep\-clean -Also clean system leftovers, asking for each: journal above -\fBJOURNAL_MAX_SIZE\fR, old kernels (dnf, zypper), config files of removed -packages, disabled snap revisions, crash dumps, npm and Go caches. Then the -Containers section: stopped containers, unused images, build cache and -unused volumes of every local docker context (remote ones skipped) and of -podman (stopped containers and volumes never under \fB\-y\fR), and the size -of Docker Desktop's data. +.B \-\-soft\-clean +Skip the deep clean. Without it a run offers, asking for each: emptying the +systemd journal and the plain-text system logs (syslog, messages, user.log, +naming the program writing most of them), old kernels (dnf, zypper), config +files of removed packages, disabled snap revisions, crash dumps, npm and Go +caches. Then the Containers section: stopped containers, unused images, +build cache and unused volumes of every local docker context (remote ones +skipped) and of podman, and the size of Docker Desktop's data. Then the AI +models section: Ollama and Hugging Face models whose weights were not read +for \fBAI_UNUSED_DAYS\fR, and unfinished Ollama downloads. Then the Build +artifacts section: the cargo \fBtarget\fR directories under +\fBBUILD_SCAN_ROOTS\fR. Under \fB\-y\fR the logs, models, build +directories, stopped containers and volumes are never touched. .SH INSPECTION .TP .B \-\-changed @@ -212,12 +218,16 @@ Seconds before the first retry; the n-th retry waits n times as long (3). .B CACHE_MENU_ROWS Cache entries listed before the rest is grouped as "others" (12). .TP -.B JOURNAL_MAX_SIZE -Journal size \fB\-\-deep\-clean\fR vacuums down to (200M). +.B AI_UNUSED_DAYS +Days without a read after which the deep clean offers an AI model (30). +.TP +.B BUILD_SCAN_ROOTS +Directories, separated by ":", searched for Rust target directories +(\fB$HOME\fR). Hidden directories and node_modules are not searched. .TP .B DEEP_CLEAN_SKIP -Deep-clean tasks to skip: journal, kernels, residual, snap, docker, podman, -coredumps, npm, go. +Deep-clean tasks to skip: journal, syslog, kernels, residual, snap, +coredumps, npm, go, docker, podman, ollama, huggingface, cargo. .TP .B RPI_ABORT_ON_UNDERVOLTAGE On a Raspberry Pi, abort even with \-y when the board is under-voltage (0). @@ -290,7 +300,7 @@ ARG_WEATHER \-w ARG_ASCII \-\-ascii ARG_KEEP_CACHE \-nc ARG_KEEP_TRASH \-nt -ARG_DEEP_CLEAN \-\-deep\-clean +ARG_SOFT_CLEAN \-\-soft\-clean .ft .fi .PP diff --git a/share/profiles/safe.sh b/share/profiles/safe.sh index 22e18c0..85b3715 100644 --- a/share/profiles/safe.sh +++ b/share/profiles/safe.sh @@ -8,3 +8,4 @@ ARG_KEEP_CACHE=1 ARG_KEEP_TRASH=1 ARG_NO_FIRMWARE=1 ARG_EXCEPT="${ARG_EXCEPT:+$ARG_EXCEPT,}flatpak,snap" +ARG_SOFT_CLEAN=1 diff --git a/share/profiles/server.sh b/share/profiles/server.sh index 97fad95..2b8d86b 100644 --- a/share/profiles/server.sh +++ b/share/profiles/server.sh @@ -10,3 +10,4 @@ ARG_NO_INFO=1 ARG_KEEP_CACHE=1 ARG_KEEP_TRASH=1 ARG_NO_FIRMWARE=1 +ARG_SOFT_CLEAN=1 diff --git a/share/profiles/work.sh b/share/profiles/work.sh index fecb87e..67b0d7f 100644 --- a/share/profiles/work.sh +++ b/share/profiles/work.sh @@ -8,3 +8,4 @@ ARG_NO_FIRMWARE=1 ARG_EXCEPT="${ARG_EXCEPT:+$ARG_EXCEPT,}yay,paru" ARG_KEEP_CACHE=1 ARG_KEEP_TRASH=1 +ARG_SOFT_CLEAN=1 diff --git a/src/emerger.sh b/src/emerger.sh index 923734d..f27f575 100755 --- a/src/emerger.sh +++ b/src/emerger.sh @@ -263,7 +263,7 @@ main() { if (( ! ARG_KEEP_CACHE )); then clean_cache; clean_open_deleted; fi (( ARG_KEEP_TRASH )) || clean_trash - if (( ARG_DEEP_CLEAN )); then clean_deep; clean_containers; fi + if (( ! ARG_SOFT_CLEAN )); then clean_deep; clean_containers; clean_ai; clean_builds; fi hooks_run post diff --git a/src/lib/args.sh b/src/lib/args.sh index a4d9cf5..88d1faf 100644 --- a/src/lib/args.sh +++ b/src/lib/args.sh @@ -5,7 +5,7 @@ ARG_HELP=0; ARG_VERSION=0 ARG_SELF_UPDATE=0; ARG_AUTO_UPDATE=0; ARG_ERRORS=0; ARG_REBUILD_CACHE=0; ARG_HISTORY=0 -ARG_NO_LOGO=0; ARG_NO_INFO=0; ARG_KEEP_CACHE=0; ARG_KEEP_TRASH=0; ARG_DEEP_CLEAN=0 +ARG_NO_LOGO=0; ARG_NO_INFO=0; ARG_KEEP_CACHE=0; ARG_KEEP_TRASH=0; ARG_SOFT_CLEAN=0 ARG_WEATHER=0 ARG_FIRMWARE=0; ARG_NO_FIRMWARE=0; ARG_DEV=0; ARG_SECURITY=0 ARG_YES=0; ARG_PARALLEL=0 @@ -117,7 +117,7 @@ args_parse() { -ni|--no-info) ARG_NO_INFO=1 ;; -nc|--keep-cache) ARG_KEEP_CACHE=1 ;; -nt|--keep-trash) ARG_KEEP_TRASH=1 ;; - --deep-clean) ARG_DEEP_CLEAN=1 ;; + --soft-clean) ARG_SOFT_CLEAN=1 ;; -w|--weather) ARG_WEATHER=1 ;; -q|--quiet) QUIET_LEVEL=$(( QUIET_LEVEL + 1 )) ;; diff --git a/src/lib/clean/ai.sh b/src/lib/clean/ai.sh new file mode 100644 index 0000000..a6f7731 --- /dev/null +++ b/src/lib/clean/ai.sh @@ -0,0 +1,194 @@ +#!/usr/bin/env bash +# Deep clean, AI models section: local models not used for AI_UNUSED_DAYS +# days (Ollama, Hugging Face hub cache) and Ollama downloads left unfinished. +# Last use is the access time of a model's weights, so a backup or indexer +# reading them counts as a use. Models are never offered under -y. + +: "${AI_UNUSED_DAYS:=30}" + +# Ollama model stores of a system-wide install (Linux service, Arch package), +# checked after $OLLAMA_MODELS and ~/.ollama/models. +_AI_OLLAMA_SYSTEM_DIRS=(/usr/share/ollama/.ollama/models /var/lib/ollama/.ollama/models /var/lib/ollama) + +# Offer the unused models of every Ollama store and Hugging Face hub cache +# found, and each store's unfinished downloads, then add the growth of free +# space on / to SUMMARY_FREED_KB. Silent when no store exists. +clean_ai() { + # The menu arrays are read and filled by _deep_menu_read and _deep_menu. + # shellcheck disable=SC2034 + local -a stores=() menu_names=() menu_kb=() menu_notes=() menu_refs=() + local hub store free_before + mapfile -t stores < <(_ai_ollama_stores) + hub=$(_ai_hf_hub) + (( ${#stores[@]} > 0 )) || [[ -n $hub ]] || return 0 + ui_title "AI models" + _DEEP_TASK_COUNT=0 + free_before=$(sys_free_space k /) + _AI_OLLAMA_UP="" + + for store in ${stores[@]+"${stores[@]}"}; do _ai_ollama_partial "$store"; done + if ! _deep_is_skipped ollama; then + _deep_menu_read < <(for store in ${stores[@]+"${stores[@]}"}; do _ai_ollama_rows "$store"; done) + _ai_offer ollama "Ollama models" _ai_ollama_remove + fi + # shellcheck disable=SC2034 + menu_names=() menu_kb=() menu_notes=() menu_refs=() + if [[ -n $hub ]] && ! _deep_is_skipped huggingface; then + _deep_menu_read < <(_ai_hf_rows "$hub") + _ai_offer huggingface "Hugging Face models" _ai_hf_remove + fi + + if (( _DEEP_TASK_COUNT == 0 )); then + ui_ok "nothing to clean" + return 0 + fi + _deep_report_freed "$free_before" + return 0 +} + +# Offer clean_ai's menu rows of kind $2 under DEEP_CLEAN_SKIP key $1, +# removed through function $3. +_ai_offer() { + local key="$1" kind="$2" remover="$3" + (( ${#menu_names[@]} > 0 )) || return 0 + ui_sub "${#menu_names[@]} $kind not used for $AI_UNUSED_DAYS days or more" + _deep_menu "$key" "Remove which?" \ + "${#menu_names[@]} unused $kind kept under -y: they are downloaded again on next use" "$remover" +} + +# Print each Ollama model store once, real path, in search order: the +# directories holding a manifests directory. +_ai_ollama_stores() { + local dir real + local -A seen=() + for dir in "${OLLAMA_MODELS:-}" "$HOME/.ollama/models" "${_AI_OLLAMA_SYSTEM_DIRS[@]}"; do + [[ -n $dir && -d $dir/manifests ]] || continue + real=$(cd -P -- "$dir" 2>/dev/null && pwd) || continue + [[ -n ${seen[$real]:-} ]] && continue + seen[$real]=1 + printf '%s\n' "$real" + done +} + +# Print the Hugging Face hub cache directory ($HF_HUB_CACHE, else +# $HF_HOME/hub, else ~/.cache/huggingface/hub), empty when it does not exist. +_ai_hf_hub() { + local hub="${HF_HUB_CACHE:-${HF_HOME:-$HOME/.cache/huggingface}/hub}" + [[ -d $hub ]] && printf '%s\n' "$hub" + return 0 +} + +# Print the digests ("sha256-", the blob file names) manifest $1 lists. +_ai_manifest_digests() { + { grep -o 'sha256:[0-9a-f]\{64\}' -- "$1" 2>/dev/null || true; } | sed 's/:/-/' | sort -u +} + +# Print a menu row "KiBnamenotestore|manifest" for each model +# of Ollama store $1 whose weights were last read AI_UNUSED_DAYS days ago or +# more. A manifest without a weights layer is left out. +_ai_ollama_rows() { + local store="$1" manifest name layer digest bytes blob days + while IFS= read -r manifest; do + layer=$({ grep -o '{[^{}]*vnd\.ollama\.image\.model"[^{}]*}' -- "$manifest" 2>/dev/null || true; } | head -n 1) + [[ $layer =~ sha256:([0-9a-f]{64}) ]] || continue + digest="sha256-${BASH_REMATCH[1]}" + [[ $layer =~ \"size\":([0-9]+) ]] && bytes=${BASH_REMATCH[1]} || bytes=0 + blob="$store/blobs/$digest" + [[ -f $blob ]] || continue + days=$(_deep_days_since "$(_deep_atime "$blob")") + (( days >= AI_UNUSED_DAYS )) || continue + name=$(_ai_ollama_name "${manifest#"$store/manifests/"}") + printf '%s\t%s\t%s\t%s\n' $(( bytes / 1024 )) "$name" "last used $days days ago" "$store|$manifest" + done < <(find "$store/manifests" -type f 2>/dev/null | sort) +} + +# Print the name `ollama list` shows for manifest path $1 (relative to the +# manifests directory): "model:tag" for the default registry and library, +# "host/namespace/model:tag" otherwise. +_ai_ollama_name() { + local path="$1" tag model + tag=${path##*/} + model=${path%/*} + model=${model#registry.ollama.ai/library/} + printf '%s:%s' "$model" "$tag" +} + +# Return 0 when the Ollama server answers, asking it once per clean_ai. +_ai_ollama_up() { + if [[ -z $_AI_OLLAMA_UP ]]; then + _AI_OLLAMA_UP=0 + sys_has ollama && ollama list >/dev/null 2>&1 && _AI_OLLAMA_UP=1 + fi + (( _AI_OLLAMA_UP )) +} + +# Remove the Ollama model of menu ref $1 ("store|manifest") named $2: through +# `ollama rm` when the server answers, else by deleting its manifest and the +# blobs no other manifest of the store lists (through sudo when the store is +# not writable), as `ollama rm` does. +_ai_ollama_remove() { + local store="${1%%|*}" manifest="${1#*|}" name="$2" digest + if _ai_ollama_up; then + run_cmd "Remove $name" ollama rm "$name" || true + return 0 + fi + local -A others=() + local other + while IFS= read -r other; do + [[ $other == "$manifest" ]] && continue + while IFS= read -r digest; do others[$digest]=1; done < <(_ai_manifest_digests "$other") + done < <(find "$store/manifests" -type f 2>/dev/null) + local -a files=("$manifest") + while IFS= read -r digest; do + [[ -n ${others[$digest]:-} || ! -e $store/blobs/$digest ]] || files+=("$store/blobs/$digest") + done < <(_ai_manifest_digests "$manifest") + local -a prefix=() + [[ -w ${manifest%/*} && -w $store/blobs ]] || read -r -a prefix <<<"$(sys_sudo_prefix)" + run_cmd "Remove $name" ${prefix[@]+"${prefix[@]}"} rm -f -- "${files[@]}" || true + return 0 +} + +# Offer to delete the unfinished downloads ("-partial" blobs) Ollama store +# $1 holds, untouched for a day so a running pull keeps its own. +_ai_ollama_partial() { + local store="$1" count kb=0 file + local -a parts=() + mapfile -t parts < <(find "$store/blobs" -maxdepth 1 -type f -name 'sha256-*-partial*' -mtime +0 2>/dev/null) + count=${#parts[@]} + (( count > 0 )) || return 0 + for file in "${parts[@]}"; do kb=$(( kb + $(_clean_size_kb "$file") )); done + local -a prefix=() + [[ -w $store/blobs ]] || read -r -a prefix <<<"$(sys_sudo_prefix)" + _deep_task ollama "Delete unfinished Ollama downloads" "($count files, $(_clean_format_kb "$kb"))" \ + ${prefix[@]+"${prefix[@]}"} rm -f -- "${parts[@]}" +} + +# Print a menu row "KiBnamenotedir" for each model or dataset +# of Hugging Face hub cache $1 whose newest blob was last read +# AI_UNUSED_DAYS days ago or more. +_ai_hf_rows() { + local hub="$1" repo base name blob newest atime days + for repo in "$hub"/models--* "$hub"/datasets--*; do + [[ -d $repo ]] || continue + newest=0 + for blob in "$repo"/blobs/*; do + [[ -f $blob ]] || continue + atime=$(_deep_atime "$blob") + (( atime > newest )) && newest=$atime + done + (( newest > 0 )) || continue + days=$(_deep_days_since "$newest") + (( days >= AI_UNUSED_DAYS )) || continue + base=${repo##*/} + name=${base#*--} + name=${name//--//} + [[ $base == datasets--* ]] && name="dataset $name" + printf '%s\t%s\t%s\t%s\n' "$(_clean_size_kb "$repo")" "$name" "last used $days days ago" "$repo" + done +} + +# Remove the Hugging Face cache directory of menu ref $1, named $2. +_ai_hf_remove() { + run_cmd "Remove $2" rm -rf -- "$1" || true + return 0 +} diff --git a/src/lib/clean/builds.sh b/src/lib/clean/builds.sh new file mode 100644 index 0000000..1f0d9a3 --- /dev/null +++ b/src/lib/clean/builds.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# Deep clean, Build artifacts section: the target directories of Rust +# projects under BUILD_SCAN_ROOTS, what `cargo clean` removes. Hidden +# directories and node_modules are not searched. Never offered under -y: +# the next build starts from scratch. + +# Directories searched for Rust projects, separated by ":". +: "${BUILD_SCAN_ROOTS:=$HOME}" + +# Smallest target directory offered, in KiB. +_BUILDS_MIN_KB=1024 + +# Offer the Rust target directories found, biggest first, then add the +# growth of free space on / to SUMMARY_FREED_KB. Silent when none is found +# or DEEP_CLEAN_SKIP lists cargo. +clean_builds() { + # The menu arrays are read and filled by _deep_menu_read and _deep_menu. + # shellcheck disable=SC2034 + local -a menu_names=() menu_kb=() menu_notes=() menu_refs=() + local free_before + _deep_is_skipped cargo && return 0 + _deep_menu_read < <(_builds_rows) + (( ${#menu_names[@]} > 0 )) || return 0 + ui_title "Build artifacts" + _DEEP_TASK_COUNT=0 + free_before=$(sys_free_space k /) + _deep_menu cargo "Clean which?" \ + "${#menu_names[@]} Rust build directories kept under -y: the next build would start from scratch" \ + _builds_remove + (( _DEEP_TASK_COUNT > 0 )) && _deep_report_freed "$free_before" + return 0 +} + +# Print each directory named target under the BUILD_SCAN_ROOTS that cargo +# made (it holds CACHEDIR.TAG and sits beside a Cargo.toml), not descending +# into hidden directories, node_modules or a target directory. +_builds_targets() { + local root + local -a roots=() + IFS=: read -r -a roots <<<"$BUILD_SCAN_ROOTS" + for root in ${roots[@]+"${roots[@]}"}; do + [[ -n $root && -d $root ]] || continue + find "$root" -xdev -mindepth 1 \( -name '.?*' -o -name node_modules \) -prune \ + -o -type d -name target -prune -print 2>/dev/null || true + done | while IFS= read -r dir; do + [[ -f $dir/CACHEDIR.TAG && -f ${dir%/*}/Cargo.toml ]] && printf '%s\n' "$dir" + done + return 0 +} + +# Print a menu row "KiBprojectnotetarget dir" for each target +# directory of _builds_targets holding _BUILDS_MIN_KB or more. The note +# dates the last build from .rustc_info.json, which every cargo build +# rewrites, else from the directory. +_builds_rows() { + local dir kb project stamp + while IFS= read -r dir; do + kb=$(_clean_size_kb "$dir") + (( kb >= _BUILDS_MIN_KB )) || continue + project=${dir%/*} + [[ $project == "$HOME"/* ]] && project='~'"/${project#"$HOME"/}" + stamp="$dir/.rustc_info.json" + [[ -f $stamp ]] || stamp=$dir + printf '%s\t%s\t%s\t%s\n' "$kb" "$project" \ + "built $(_deep_days_since "$(_deep_mtime "$stamp")") days ago" "$dir" + done < <(_builds_targets) +} + +# Remove target directory $1 of project $2, as `cargo clean` does. +_builds_remove() { + run_cmd "Clean $2" rm -rf -- "$1" || true + return 0 +} diff --git a/src/lib/clean/containers.sh b/src/lib/clean/containers.sh index c551323..a0834e2 100644 --- a/src/lib/clean/containers.sh +++ b/src/lib/clean/containers.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# --deep-clean, Containers section: what docker (every local context) and +# Deep clean, Containers section: what docker (every local context) and # podman keep without using it. Every task asks first unless -y. Stopped # containers (toolbox, distrobox) and unused volumes hold data: never offered # under -y. Remote contexts (ssh://, tcp://) are left alone. diff --git a/src/lib/clean/deep.sh b/src/lib/clean/deep.sh index e6b73d9..6118239 100644 --- a/src/lib/clean/deep.sh +++ b/src/lib/clean/deep.sh @@ -1,13 +1,18 @@ #!/usr/bin/env bash -# --deep-clean: system leftovers a normal run does not touch. Every task -# asks first unless -y. The Containers section of --deep-clean -# (clean/containers.sh) shares these helpers. +# Deep clean, run by default unless --soft-clean: system leftovers, offered +# one task at a time. Every task asks first unless -y. The Containers, AI +# models and Build artifacts sections (clean/containers.sh, clean/ai.sh, +# clean/builds.sh) share these helpers. -# --deep-clean. Task names for DEEP_CLEAN_SKIP: -# journal kernels residual snap docker podman coredumps npm go -: "${JOURNAL_MAX_SIZE:=200M}" +# Task names for DEEP_CLEAN_SKIP: journal syslog kernels residual snap +# coredumps npm go docker podman ollama huggingface cargo : "${DEEP_CLEAN_SKIP:=}" +# Plain-text system logs _deep_syslog offers to empty: the files rsyslog +# writes and the copy logrotate leaves uncompressed. +_DEEP_SYSLOG_FILES=(/var/log/syslog /var/log/syslog.1 /var/log/messages /var/log/messages.1 + /var/log/user.log /var/log/user.log.1) + # Return 0 when task key $1 is listed in DEEP_CLEAN_SKIP (comma, space or # tab separated, compared literally). _deep_is_skipped() { @@ -55,6 +60,7 @@ clean_deep() { free_before=$(sys_free_space k /) _deep_journal + _deep_syslog _deep_kernels _deep_residual _deep_snap @@ -77,33 +83,128 @@ _deep_report_freed() { free_after=$(sys_free_space k /) if [[ -n $free_before && -n $free_after ]] && (( ! DRY_RUN )) && (( free_after > free_before )); then SUMMARY_FREED_KB=$(( SUMMARY_FREED_KB + free_after - free_before )) - ui_ok "freed $(_clean_format_kb $(( free_after - free_before ))) on /" + ui_ok "Freed $(_clean_format_kb $(( free_after - free_before ))) on /" + fi + return 0 +} + +# Offer the rows of the caller's menu_names, menu_kb (KiB) and menu_notes +# as a numbered menu under DEEP_CLEAN_SKIP key $1, asking question $2, and +# call function $4 with the menu_refs entry and the name of each row picked. +# Nothing when the menu is empty or $1 is skipped. Under -y message $3 is +# printed instead: the rows hold data the user may want. +_deep_menu() { + local key="$1" question="$2" kept="$3" remover="$4" i + (( ${#menu_names[@]} > 0 )) || return 0 + _deep_is_skipped "$key" && return 0 + if (( ARG_YES )); then + ui_muted "$kept" + return 0 fi + _DEEP_TASK_COUNT=$(( _DEEP_TASK_COUNT + 1 )) + for i in "${!menu_names[@]}"; do + printf ' %s%2d%s %-36.36s %s%6s%s %s\n' "$C_MAGENTA" $(( i + 1 )) "$C_RESET" \ + "${menu_names[i]}" "$C_BOLD" "$(_clean_format_kb "${menu_kb[i]}")" "$C_RESET" "${menu_notes[i]}" + done + ui_ask "$question" "a/N" "a = all, n = none, or numbers like 1 3 5-7" || true + _clean_parse_pick "$UI_ANSWER" "${#menu_names[@]}" + if (( ${#_CLEAN_PICK[@]} == 0 )); then + ui_muted "skipped" + return 0 + fi + for i in "${_CLEAN_PICK[@]}"; do + "$remover" "${menu_refs[i-1]}" "${menu_names[i-1]}" + done return 0 } +# Fill the caller's menu_names, menu_kb, menu_notes and menu_refs from +# lines "KiBnamenoteref" on stdin, biggest first. +_deep_menu_read() { + local kb name note ref + while IFS=$'\t' read -r kb name note ref; do + menu_kb+=("$kb"); menu_names+=("$name"); menu_notes+=("$note"); menu_refs+=("$ref") + done < <(sort -t $'\t' -k1,1nr) + return 0 +} + +# Print the days between epoch seconds $1 and now, 0 when $1 is in the future. +_deep_days_since() { + local now days + now=$(date +%s) + days=$(( (now - $1) / 86400 )) + (( days < 0 )) && days=0 + printf '%d' "$days" +} + +# Print the last access time of file $1 in epoch seconds (GNU or BSD stat), +# 0 when it cannot be read. +_deep_atime() { + stat -c %X -- "$1" 2>/dev/null || stat -f %a -- "$1" 2>/dev/null || echo 0 +} + +# Print the last modification time of file $1 in epoch seconds (GNU or BSD +# stat), 0 when it cannot be read. +_deep_mtime() { + stat -c %Y -- "$1" 2>/dev/null || stat -f %m -- "$1" 2>/dev/null || echo 0 +} + # Deep clean tasks, called by clean_deep only: each reads its local sudo_cmd # (privilege prefix) and returns 0, offered or not. -# Offer to shrink the systemd journal to JOURNAL_MAX_SIZE when it is larger. +# Offer to empty the systemd journal (rotate, then delete every archived +# file), with its size. Never offered under -y: a log may be needed. _deep_journal() { if sys_has journalctl; then - local usage + local usage usage_bytes # "take up 1.5G" or, under 1 KiB, "take up 512B". usage=$({ journalctl --disk-usage 2>/dev/null || true; } | sed -nE 's/.*take up ([0-9.]+[KMGTPE]?)B?.*/\1/p' | head -n 1) if [[ -n $usage ]]; then - local usage_bytes limit_bytes usage_bytes=$(numfmt --from=iec "$usage" 2>/dev/null || echo 0) - limit_bytes=$(numfmt --from=iec "$JOURNAL_MAX_SIZE" 2>/dev/null || echo 0) - if (( usage_bytes > limit_bytes )); then - _deep_task journal "Shrink journal to $JOURNAL_MAX_SIZE" "(now $usage)" \ - $sudo_cmd journalctl --vacuum-size="$JOURNAL_MAX_SIZE" + if (( usage_bytes > 0 )); then + _deep_task_asked journal "Empty the journal" "(now $usage)" "journal kept under -y" \ + $sudo_cmd journalctl --rotate --vacuum-time=1s fi fi fi return 0 } +# Offer to empty the non-empty system logs of _DEEP_SYSLOG_FILES, with +# their size and the program writing most of the biggest. Never offered +# under -y: a log may be needed. The files are truncated, not deleted: +# rsyslog keeps them open. +_deep_syslog() { + local file bytes total=0 top=0 biggest="" writer + local -a logs=() + for file in "${_DEEP_SYSLOG_FILES[@]}"; do + [[ -f $file ]] || continue + bytes=$(stat -c %s -- "$file" 2>/dev/null || stat -f %z -- "$file" 2>/dev/null || echo 0) + (( bytes > 0 )) || continue + logs+=("$file"); total=$(( total + bytes )) + if (( bytes > top )); then top=$bytes; biggest=$file; fi + done + (( ${#logs[@]} > 0 )) || return 0 + writer=$(_deep_log_writer "$biggest") + printf -v file '%s, ' "${logs[@]}" + _deep_task_asked syslog "Empty system logs" \ + "($(_clean_format_kb $(( total / 1024 ))) in ${file%, }${writer:+, mostly from $writer})" \ + "system logs kept under -y" $sudo_cmd truncate -s 0 "${logs[@]}" + return 0 +} + +# Print the program that wrote more than half of the last MiB of syslog +# file $1 (ISO or classic timestamps), empty when none did or the file +# cannot be read. +_deep_log_writer() { + { tail -c 1048576 -- "$1" 2>/dev/null || true; } | awk ' + { prog = ($1 ~ /^[0-9][0-9][0-9][0-9]-/) ? $3 : $5 + sub(/\[.*/, "", prog); sub(/:$/, "", prog) + if (prog != "") count[prog]++ + lines++ } + END { for (p in count) if (count[p] * 2 > lines) print p }' +} + # Offer to remove installed kernels beyond the newest two (dnf, zypper). _deep_kernels() { if sys_has dnf; then diff --git a/src/lib/help.txt b/src/lib/help.txt index 93e507f..7dc71c6 100644 --- a/src/lib/help.txt +++ b/src/lib/help.txt @@ -53,9 +53,12 @@ CLEANING -nc, --keep-cache Skip user cache cleaning and the warning about space held by deleted files still open -nt, --keep-trash Skip trash cleaning - --deep-clean Also clean journal, old kernels, leftover configs, - snap revisions, crash dumps, npm/go caches, - unused docker/podman data (asks for each) + --soft-clean Skip the deep clean. Without it, every run asks + about each of these before removing it: journal, + system logs, old kernels, leftover configs, snap + revisions, crash dumps, npm/go caches, unused + docker/podman data, unused AI models, Rust + target directories INSPECTION --changed Show packages added/removed/upgraded in the last run diff --git a/src/lib/knobs.sh b/src/lib/knobs.sh index d1d6ba9..abd4e50 100644 --- a/src/lib/knobs.sh +++ b/src/lib/knobs.sh @@ -33,7 +33,7 @@ knob_switch() { # or profile, before any arithmetic reads them. knobs_validate() { for _knob in DISK_MIN_FREE_MB:1024 BATTERY_MIN_PCT:20 RPI_TEMP_WARN:80 \ - RETRY_ATTEMPTS:2 RETRY_DELAY:3 CACHE_MENU_ROWS:12 \ + RETRY_ATTEMPTS:2 RETRY_DELAY:3 CACHE_MENU_ROWS:12 AI_UNUSED_DAYS:30 \ EMERGER_CACHE_TTL:86400 EMERGER_PROMPT_TIMEOUT:60 MANAGER_TIMEOUT:0 \ UI_VERBOSE:0 ARG_ASCII:0; do knob_int "${_knob%%:*}" "${_knob#*:}" diff --git a/src/lib/load.sh b/src/lib/load.sh index db55d39..8175d26 100644 --- a/src/lib/load.sh +++ b/src/lib/load.sh @@ -11,7 +11,7 @@ _LOAD_MODULES=( managers/progress state/estimate run/command run/select run/parallel run/sudo args ignore managers/registry managers/detect managers/icon managers/recipes - clean/size clean/cache clean/open clean/trash clean/deep clean/containers + clean/size clean/cache clean/open clean/trash clean/deep clean/containers clean/ai clean/builds hooks selfupdate autoupdate system/notify system/rpi managers/rpi_eeprom system/reboot state/diff system/preflight system/snapshot system/mirrors state/resume state/lock diff --git a/src/lib/run/select.sh b/src/lib/run/select.sh index 3adf07f..aaeee15 100644 --- a/src/lib/run/select.sh +++ b/src/lib/run/select.sh @@ -27,7 +27,8 @@ run_skip_reason() { return 0 } -# Return 0 if a manager selected for this run, or --deep-clean, needs sudo. +# Return 0 if a manager selected for this run, or the deep clean (Linux, +# not under --soft-clean), needs sudo. # Same selection as the run loop (run_skip_reason). run_needs_sudo() { local manager @@ -37,7 +38,7 @@ run_needs_sudo() { [[ -n $RUN_SKIP_REASON ]] && continue if pkg_needs_sudo "$manager"; then return 0; fi done - (( ${ARG_DEEP_CLEAN:-0} )) + (( ! ${ARG_SOFT_CLEAN:-0} )) && ! sys_is_macos } # Append each detected manager this run updates to the caller's arrays: diff --git a/src/pslib/Args.ps1 b/src/pslib/Args.ps1 index f9bf11b..0d1b3a3 100644 --- a/src/pslib/Args.ps1 +++ b/src/pslib/Args.ps1 @@ -50,7 +50,7 @@ function Args-Parse { '^--reboot-exit$' { $options.RebootExit = $true } '^--download-only$' { $options.DownloadOnly = $true } '^--accept-licenses$' { $options.AcceptLicenses = $true } - '^--deep-clean$' { $options.DeepClean = $true } + '^--soft-clean$' { $options.SoftClean = $true } '^--no-firmware$' { $options.NoFirmware = $true } '^--refresh-mirrors$' { $options.RefreshMirrors = $true } '^--rollback$' { $options.Rollback = $true } @@ -96,7 +96,7 @@ function _Args-Defaults { ListProfiles=$false; Ascii=$false Parallel=$false; Changed=$false; Resume=$false Json=$false; Weather=$false; RebootExit=$false; DownloadOnly=$false; AcceptLicenses=$false - DeepClean=$false; NoFirmware=$false; RefreshMirrors=$false; Rollback=$false + SoftClean=$false; NoFirmware=$false; RefreshMirrors=$false; Rollback=$false Only=''; Except=''; Metrics='' } } @@ -157,7 +157,7 @@ function Args-Warn-Unsupported { $unsupported = [ordered]@{ Firmware = '--firmware'; NoFirmware = '--no-firmware'; Snapshot = '--snapshot' Parallel = '--parallel'; Resume = '--resume'; DownloadOnly = '--download-only' - DeepClean = '--deep-clean'; RefreshMirrors = '--refresh-mirrors' + SoftClean = '--soft-clean'; RefreshMirrors = '--refresh-mirrors' } foreach ($field in $unsupported.Keys) { if ($script:ArgsGlobal.$field) { UI-Warn "$($unsupported[$field]) is not supported on Windows, ignored." } diff --git a/src/pslib/Help.txt b/src/pslib/Help.txt index 3141440..4f485f1 100644 --- a/src/pslib/Help.txt +++ b/src/pslib/Help.txt @@ -61,7 +61,7 @@ NOT AVAILABLE ON WINDOWS --changed --changelog --rollback Print a warning and exit 0, nothing is updated --firmware --no-firmware --snapshot --parallel --resume --download-only - --deep-clean --refresh-mirrors + --soft-clean --refresh-mirrors Print a warning, the run goes on without them EXAMPLES diff --git a/tests/ai.bats b/tests/ai.bats new file mode 100644 index 0000000..0ad8a0f --- /dev/null +++ b/tests/ai.bats @@ -0,0 +1,172 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + setup_env + setup_minbin +} + +teardown() { + chmod -R u+w "$HOME" 2>/dev/null || true + teardown_env +} + +# Print the 64-digit digest made of number $1. +digest() { printf '%064d' "$1"; } + +# Write Ollama manifest $2 (path under manifests/) into store $1, its weights +# layer digest number $3 of $4 bytes (none when $3 is "-"), config digest +# number $5. The weights blob is created with access time $6 ("" for now). +manifest() { + local store="$1" path="$2" layer="$3" bytes="$4" config="$5" atime="${6:-}" + mkdir -p "$store/manifests/${path%/*}" "$store/blobs" + local layers="" + if [ "$layer" != - ]; then + layers="{\"mediaType\":\"application/vnd.ollama.image.model\",\"digest\":\"sha256:$(digest "$layer")\",\"size\":$bytes}," + head -c 2048 /dev/zero >"$store/blobs/sha256-$(digest "$layer")" + [ -z "$atime" ] || touch -a -d "$atime" "$store/blobs/sha256-$(digest "$layer")" + fi + touch "$store/blobs/sha256-$(digest "$config")" "$store/blobs/sha256-$(digest 9)" + printf '{"schemaVersion":2,"config":{"digest":"sha256:%s","size":4},"layers":[%s{"mediaType":"application/vnd.ollama.image.template","digest":"sha256:%s","size":9}]}' \ + "$(digest "$config")" "$layers" "$(digest 9)" >"$store/manifests/$path" +} + +@test "clean_ai: no Ollama store and no Hugging Face cache prints nothing" { + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); clean_ai; echo "rc=$?"' + [ "$status" -eq 0 ] + [ "$output" = "rc=0" ] +} + +@test "clean_ai: lists only models unused for AI_UNUSED_DAYS, biggest first, and removes the picked one's own blobs" { + local s="$HOME/.ollama/models" + manifest "$s" registry.ollama.ai/library/old/7b 1 5242880 7 '200 days ago' + manifest "$s" hf.co/user/big/Q4 2 10485760 7 '40 days ago' + manifest "$s" registry.ollama.ai/library/fresh/1b 3 1048576 7 '' + manifest "$s" registry.ollama.ai/library/nolayer/x - 0 8 + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); DRY_RUN=1; ui_ask() { echo "ASK $1"; UI_ANSWER=2; return 0; }; clean_ai' + [ "$status" -eq 0 ] + [[ "$output" == *"AI models"* ]] + [[ "$output" == *"2 Ollama models not used for 30 days or more"* ]] + [[ "$output" == *" 1 hf.co/user/big:Q4"*"10M last used 40 days ago"*" 2 old:7b"*"5M last used 200 days ago"* ]] + [[ "$output" != *"fresh"* ]] + [[ "$output" != *"nolayer"* ]] + [[ "$output" == *"ASK Remove which?"* ]] + [[ "$output" == *"[dry-run] rm -f -- $s/manifests/registry.ollama.ai/library/old/7b $s/blobs/sha256-$(digest 1)"* ]] + [[ "$output" != *"sha256-$(digest 7)"* ]] + [[ "$output" != *"sha256-$(digest 9)"* ]] +} + +@test "clean_ai: a picked model is deleted for real, the shared blobs stay" { + local s="$HOME/.ollama/models" + manifest "$s" registry.ollama.ai/library/old/7b 1 5242880 7 '200 days ago' + manifest "$s" registry.ollama.ai/library/fresh/1b 3 1048576 7 '' + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); ui_ask() { UI_ANSWER=a; return 0; }; clean_ai' + [ "$status" -eq 0 ] + [ ! -e "$s/manifests/registry.ollama.ai/library/old/7b" ] + [ ! -e "$s/blobs/sha256-$(digest 1)" ] + [ -e "$s/blobs/sha256-$(digest 7)" ] + [ -e "$s/blobs/sha256-$(digest 9)" ] + [ -e "$s/manifests/registry.ollama.ai/library/fresh/1b" ] +} + +@test "clean_ai: a running server removes through ollama rm, asked once" { + manifest "$HOME/.ollama/models" registry.ollama.ai/library/a/1 1 2048 7 '200 days ago' + manifest "$HOME/.ollama/models" registry.ollama.ai/library/b/1 2 2048 7 '200 days ago' + stub ollama 'echo "ollama $*" >>"$HOME/ollama.calls"' + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); DRY_RUN=1; ui_ask() { UI_ANSWER=a; return 0; }; clean_ai' + [ "$status" -eq 0 ] + [[ "$output" == *"[dry-run] ollama rm a:1"* ]] + [[ "$output" == *"[dry-run] ollama rm b:1"* ]] + [ "$(cat "$HOME/ollama.calls")" = "ollama list" ] +} + +@test "clean_ai: a store the user cannot write goes through sudo" { + local s="$HOME/sys/models" + manifest "$s" registry.ollama.ai/library/old/7b 1 2048 7 '200 days ago' + head -c 4096 /dev/zero >"$s/blobs/sha256-$(digest 5)-partial" + touch -d '3 days ago' "$s/blobs/sha256-$(digest 5)-partial" + chmod a-w "$s/blobs" "$s/manifests/registry.ollama.ai/library/old" + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=("$HOME/sys/models"); DRY_RUN=1; ui_confirm() { echo "ASK $1 $3"; return 0; } + ui_ask() { UI_ANSWER=1; return 0; }; clean_ai' + [ "$status" -eq 0 ] + [[ "$output" == *"ASK Delete unfinished Ollama downloads? (1 files, 4K)"* ]] + [[ "$output" == *"[dry-run] sudo rm -f -- $s/blobs/sha256-$(digest 5)-partial"* ]] + [[ "$output" == *"[dry-run] sudo rm -f -- $s/manifests/registry.ollama.ai/library/old/7b"* ]] +} + +@test "clean_ai: a download touched today is left to its pull" { + local s="$HOME/.ollama/models" + manifest "$s" registry.ollama.ai/library/fresh/1b 3 2048 7 '' + touch "$s/blobs/sha256-$(digest 5)-partial" + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); clean_ai' + [ "$status" -eq 0 ] + [[ "$output" == *"nothing to clean"* ]] + [ -e "$s/blobs/sha256-$(digest 5)-partial" ] +} + +@test "clean_ai: models are kept under -y, unfinished downloads are not" { + local s="$HOME/.ollama/models" + manifest "$s" registry.ollama.ai/library/old/7b 1 2048 7 '200 days ago' + touch -d '3 days ago' "$s/blobs/sha256-$(digest 5)-partial" + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); ARG_YES=1; ui_confirm() { return 0; }; clean_ai' + [ "$status" -eq 0 ] + [[ "$output" == *"1 unused Ollama models kept under -y: they are downloaded again on next use"* ]] + [ -e "$s/manifests/registry.ollama.ai/library/old/7b" ] + [ ! -e "$s/blobs/sha256-$(digest 5)-partial" ] +} + +@test "clean_ai: an empty answer removes nothing, AI_UNUSED_DAYS moves the bar" { + manifest "$HOME/.ollama/models" registry.ollama.ai/library/old/7b 1 2048 7 '10 days ago' + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); ui_ask() { UI_ANSWER=""; return 1; }; clean_ai + AI_UNUSED_DAYS=5; clean_ai' + [ "$status" -eq 0 ] + [[ "$output" == *"nothing to clean"*"old:7b"*"skipped"* ]] + [ -e "$HOME/.ollama/models/manifests/registry.ollama.ai/library/old/7b" ] +} + +@test "clean_ai: OLLAMA_MODELS naming the same store as a link lists it once, DEEP_CLEAN_SKIP=ollama hides it" { + manifest "$HOME/real" registry.ollama.ai/library/old/7b 1 2048 7 '200 days ago' + mkdir -p "$HOME/.ollama" + ln -s "$HOME/real" "$HOME/.ollama/models" + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=("$HOME/none"); OLLAMA_MODELS=$HOME/real; DRY_RUN=1 + ui_ask() { UI_ANSWER=n; return 0; }; clean_ai; echo @@; DEEP_CLEAN_SKIP=ollama; clean_ai' + [ "$status" -eq 0 ] + [[ "$output" == *"1 Ollama models not used"*"@@"* ]] + [[ "${output#*@@}" != *"old:7b"* ]] + [[ "${output#*@@}" == *"nothing to clean"* ]] +} + +@test "clean_ai: Hugging Face repos unused for AI_UNUSED_DAYS are offered and removed, recent ones kept" { + local hub="$HOME/.cache/huggingface/hub" + mkdir -p "$hub/models--org--old/blobs" "$hub/datasets--org--set/blobs" "$hub/models--org--new/blobs" "$hub/models--org--empty" + head -c 4096 /dev/zero >"$hub/models--org--old/blobs/a" + touch -a -d '100 days ago' "$hub/models--org--old/blobs/a" + touch -a -d '60 days ago' "$hub/datasets--org--set/blobs/b" + touch "$hub/models--org--new/blobs/c" + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); ui_ask() { UI_ANSWER=1-2; return 0; }; clean_ai' + [ "$status" -eq 0 ] + [[ "$output" == *"2 Hugging Face models not used for 30 days or more"* ]] + [[ "$output" == *"org/old"*"last used 100 days ago"* ]] + [[ "$output" == *"dataset org/set"*"last used 60 days ago"* ]] + [ ! -e "$hub/models--org--old" ] + [ ! -e "$hub/datasets--org--set" ] + [ -d "$hub/models--org--new" ] + [ -d "$hub/models--org--empty" ] +} + +@test "clean_ai: HF_HUB_CACHE moves the hub, DEEP_CLEAN_SKIP=huggingface hides it" { + mkdir -p "$HOME/hub/models--org--old/blobs" + touch -a -d '100 days ago' "$HOME/hub/models--org--old/blobs/a" + run sys_lib '_AI_OLLAMA_SYSTEM_DIRS=(); HF_HUB_CACHE=$HOME/hub; ui_ask() { UI_ANSWER=n; return 0; }; clean_ai + echo @@; DEEP_CLEAN_SKIP=huggingface; clean_ai' + [ "$status" -eq 0 ] + [[ "${output%@@*}" == *"org/old"* ]] + [[ "${output#*@@}" == *"nothing to clean"* ]] +} + +@test "_ai_ollama_name: default library drops the registry, others keep host and namespace" { + run sys_lib '_ai_ollama_name registry.ollama.ai/library/qwen3/30b; echo; _ai_ollama_name hf.co/user/model/Q4_K_M' + [ "$status" -eq 0 ] + [ "$output" = $'qwen3:30b\nhf.co/user/model:Q4_K_M' ] +} diff --git a/tests/builds.bats b/tests/builds.bats new file mode 100644 index 0000000..1dae101 --- /dev/null +++ b/tests/builds.bats @@ -0,0 +1,90 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + setup_env + setup_minbin +} + +teardown() { + teardown_env +} + +# Create Rust project $1 (under HOME) with a cargo target directory of $2 KiB +# whose last build was $3 ("" for now). +project() { + local dir="$HOME/$1" + mkdir -p "$dir/target/debug" + touch "$dir/Cargo.toml" "$dir/target/CACHEDIR.TAG" + head -c $(( $2 * 1024 )) /dev/zero >"$dir/target/debug/blob" + touch "$dir/target/.rustc_info.json" + [ -z "${3:-}" ] || touch -d "$3" "$dir/target/.rustc_info.json" +} + +@test "clean_builds: no Rust project prints nothing" { + mkdir -p "$HOME/code/target" + run sys_lib 'clean_builds; echo "rc=$?"' + [ "$status" -eq 0 ] + [ "$output" = "rc=0" ] +} + +@test "clean_builds: lists cargo targets biggest first and removes the rows picked" { + project code/big 3072 '10 days ago' + project code/small 2048 '' + project code/tiny 4 + project .hidden/p 4096 + project code/node_modules/p 4096 + mkdir -p "$HOME/code/notcargo/target"; touch "$HOME/code/notcargo/Cargo.toml" + head -c 2097152 /dev/zero >"$HOME/code/notcargo/target/x" + project code/big/target/nested 4096 + run sys_lib 'ui_ask() { echo "ASK $1"; UI_ANSWER=1; return 0; }; clean_builds' + [ "$status" -eq 0 ] + [[ "$output" == *"Build artifacts"* ]] + [[ "$output" == *" 1 ~/code/big "*"built 10 days ago"*" 2 ~/code/small"*"built 0 days ago"* ]] + [[ "$output" != *"tiny"* ]] + [[ "$output" != *"hidden"* ]] + [[ "$output" != *"node_modules"* ]] + [[ "$output" != *"notcargo"* ]] + [[ "$output" != *"nested"* ]] + [[ "$output" == *"ASK Clean which?"* ]] + [ ! -e "$HOME/code/big/target" ] + [ -e "$HOME/code/big/Cargo.toml" ] + [ -d "$HOME/code/small/target" ] +} + +@test "clean_builds: kept under -y, skipped on no, DEEP_CLEAN_SKIP=cargo hides the section" { + project code/big 2048 + run sys_lib 'ARG_YES=1; clean_builds; ARG_YES=0 + ui_ask() { UI_ANSWER=n; return 0; }; clean_builds + echo @@; DEEP_CLEAN_SKIP=cargo; clean_builds' + [ "$status" -eq 0 ] + [[ "$output" == *"1 Rust build directories kept under -y: the next build would start from scratch"* ]] + [[ "$output" == *"skipped"* ]] + [ "${output#*@@}" = "" ] + [ -d "$HOME/code/big/target" ] +} + +@test "clean_builds: BUILD_SCAN_ROOTS lists several roots, a project outside HOME keeps its path, dry-run removes nothing" { + project a/p 2048 + project b/q 2048 + local out="$BATS_TEST_TMPDIR/out" + mkdir -p "$out/r/target"; touch "$out/r/Cargo.toml" "$out/r/target/CACHEDIR.TAG" + head -c 2097152 /dev/zero >"$out/r/target/x" + run sys_lib "BUILD_SCAN_ROOTS='$HOME/a::$HOME/missing:$out'; DRY_RUN=1; ui_ask() { UI_ANSWER=a; return 0; }; clean_builds" + [ "$status" -eq 0 ] + [[ "$output" == *"~/a/p"* ]] + [[ "$output" != *"~/b/q"* ]] + [[ "$output" == *"[dry-run] rm -rf -- $out/r/target"* ]] + [[ "$output" == *"[dry-run] rm -rf -- $HOME/a/p/target"* ]] + [ -d "$HOME/a/p/target" ] +} + +@test "clean_builds: a target without .rustc_info.json is dated from the directory" { + project code/p 2048 + rm "$HOME/code/p/target/.rustc_info.json" + touch -d '5 days ago' "$HOME/code/p/target" + run sys_lib 'ui_ask() { UI_ANSWER=n; return 0; }; clean_builds' + [ "$status" -eq 0 ] + [[ "$output" == *"built 5 days ago"* ]] +} diff --git a/tests/clean.bats b/tests/clean.bats index 43b778b..b9e3102 100644 --- a/tests/clean.bats +++ b/tests/clean.bats @@ -70,17 +70,50 @@ teardown() { stub journalctl 'echo "Archived and active journals take up 0B in the file system."' stub dnf 'exit 1' stub go 'exit 1' - run sys_lib 'DRY_RUN=1; ARG_YES=1; clean_deep; echo "rc=$?"' + run sys_lib '_DEEP_SYSLOG_FILES=(); DRY_RUN=1; clean_deep; echo "rc=$?"' [ "$status" -eq 0 ] [[ "$output" == *"rc=0"* ]] - [[ "$output" != *"vacuum-size"* ]] + [[ "$output" != *"journal"* ]] } -@test "deep clean: journal size in plain bytes is compared" { +@test "deep clean: a journal in plain bytes is offered for emptying, kept under -y" { stub journalctl 'echo "Archived and active journals take up 512B in the file system."' - run sys_lib 'sys_is_root() { return 0; }; DRY_RUN=1; ARG_YES=1; JOURNAL_MAX_SIZE=100; clean_deep; echo "rc=$?"' + run sys_lib 'sys_is_root() { return 0; }; _DEEP_SYSLOG_FILES=(); DRY_RUN=1; ui_confirm() { echo "ASK $1 $3"; return 0; } + clean_deep; ARG_YES=1; clean_deep' [ "$status" -eq 0 ] - [[ "$output" == *"[dry-run] journalctl --vacuum-size=100"* ]] + [[ "$output" == *"ASK Empty the journal? (now 512)"* ]] + [[ "$output" == *"[dry-run] journalctl --rotate --vacuum-time=1s"* ]] + [[ "$output" == *"journal kept under -y"* ]] +} + +@test "deep clean: non-empty system logs are offered with total size and main writer, empty or missing ones left out" { + mkdir -p "$HOME/log" + for i in $(seq 1 3); do echo "2026-01-01T00:00:00+00:00 host tracker-miner-fs-3[630074]: warn $i"; done >"$HOME/log/syslog" + echo "Jan 1 00:00:00 host cron[12]: job" >>"$HOME/log/syslog" + head -c 2048 /dev/zero >"$HOME/log/syslog.1" + : >"$HOME/log/messages" + run sys_lib 'sys_is_root() { return 0; }; DRY_RUN=1; ui_confirm() { echo "ASK $1 $3"; return 0; } + _DEEP_SYSLOG_FILES=("$HOME/log/syslog" "$HOME/log/syslog.1" "$HOME/log/messages" "$HOME/log/none") + clean_deep' + [ "$status" -eq 0 ] + [[ "$output" == *"ASK Empty system logs? (2K in $HOME/log/syslog, $HOME/log/syslog.1)"* ]] + [[ "$output" == *"[dry-run] truncate -s 0 $HOME/log/syslog $HOME/log/syslog.1"* ]] + run sys_lib '_deep_log_writer "$HOME/log/syslog"' + [ "$output" = "tracker-miner-fs-3" ] +} + +@test "deep clean: system logs with no majority writer name none, truncated for real on yes, kept under -y" { + mkdir -p "$HOME/log" + printf 'Jan 1 00:00:00 host a[1]: x\nJan 1 00:00:01 host b: y\n' >"$HOME/log/syslog" + run sys_lib 'sys_is_root() { return 0; }; ui_confirm() { echo "ASK $1 $3"; return 0; } + _DEEP_SYSLOG_FILES=("$HOME/log/syslog"); _DEEP_TASK_COUNT=0; sudo_cmd="" + _deep_syslog; [ -s "$HOME/log/syslog" ] || echo emptied + echo z >"$HOME/log/syslog"; ARG_YES=1; _deep_syslog' + [ "$status" -eq 0 ] + [[ "$output" == *"in $HOME/log/syslog)"* ]] + [[ "$output" != *"mostly"* ]] + [[ "$output" == *"emptied"*"system logs kept under -y"* ]] + [ -s "$HOME/log/syslog" ] } @test "clean_trash: macOS empties ~/.Trash" { @@ -157,11 +190,15 @@ teardown() { [ ! -e "$HOME/.cache/a"$'\x1f'keepme ] } -@test "--deep-clean under dry-run exits 0 and prints the Deep clean and Containers sections" { +@test "a plain run under dry-run prints the Deep clean and Containers sections, --soft-clean skips them" { stub docker 'case "$*" in "context ls"*) echo default ;; *info*) exit 0 ;; esac' - run $EMERGER -n -y -nl -nc -nt --deep-clean --only none + run $UP -n -y -nl -nc -nt --only none [ "$status" -eq 0 ] [[ "$output" == *"Deep clean"*"Containers"* ]] + run $UP -n -y -nl -nc -nt --only none --soft-clean + [ "$status" -eq 0 ] + [[ "$output" != *"Deep clean"* ]] + [[ "$output" != *"Containers"* ]] } @test "the cache step reports deleted files still open, --keep-cache skips both" { diff --git a/tests/containers.bats b/tests/containers.bats index 51a7438..9f6e296 100644 --- a/tests/containers.bats +++ b/tests/containers.bats @@ -88,7 +88,7 @@ esac" [ "$status" -eq 0 ] [ "$(cat "$HOME/docker.calls")" = "docker --context default image prune -af docker --context default builder prune -af" ] - [[ "$output" == *"freed 1M on /"* ]] + [[ "$output" == *"Freed 1M on /"* ]] [[ "$output" == *"freed=1024"* ]] } diff --git a/tests/download_only.bats b/tests/download_only.bats index ff80e6e..ca6c4a2 100644 --- a/tests/download_only.bats +++ b/tests/download_only.bats @@ -37,7 +37,7 @@ pkg() { @test "sudo check: --download-only leaves managers without a download mode out" { stub snap 'exit 0' - run lib 'ARG_DOWNLOAD_ONLY=1; ARG_ONLY=snap + run lib 'ARG_DOWNLOAD_ONLY=1; ARG_SOFT_CLEAN=1; ARG_ONLY=snap if run_needs_sudo; then echo sudo; else echo none; fi' [ "$output" = "none" ] } diff --git a/tests/ps_args.bats b/tests/ps_args.bats index 9a22f15..274465a 100644 --- a/tests/ps_args.bats +++ b/tests/ps_args.bats @@ -84,7 +84,7 @@ load ps_helper @test "ps: each Unix-only run flag warns and the run goes on" { local f - for f in --firmware --no-firmware --snapshot --parallel --resume --download-only --deep-clean --refresh-mirrors; do + for f in --firmware --no-firmware --snapshot --parallel --resume --download-only --soft-clean --refresh-mirrors; do run up_ps '' -n -y -nc -nt "$f" [ "$status" -eq 0 ] || { echo "$f: status $status"; return 1; } [[ "$output" == *"$f is not supported on Windows, ignored."* ]] diff --git a/tests/sudo.bats b/tests/sudo.bats index b593632..718bc8e 100644 --- a/tests/sudo.bats +++ b/tests/sudo.bats @@ -46,14 +46,14 @@ teardown() { @test "sudo check: --no-firmware leaves fwupd out" { stub fwupdmgr 'exit 0' - run lib 'ARG_FIRMWARE=1; ARG_NO_FIRMWARE=1; ARG_ONLY=fwupd + run lib 'ARG_SOFT_CLEAN=1; ARG_FIRMWARE=1; ARG_NO_FIRMWARE=1; ARG_ONLY=fwupd if run_needs_sudo; then echo sudo; else echo none; fi' [ "$output" = "none" ] } @test "sudo check: --resume leaves completed managers out" { stub apt 'exit 0' - run lib 'ARG_RESUME=1; ARG_ONLY=apt; RESUME_DONE=(apt) + run lib 'ARG_SOFT_CLEAN=1; ARG_RESUME=1; ARG_ONLY=apt; RESUME_DONE=(apt) if run_needs_sudo; then echo sudo; else echo none; fi' [ "$output" = "none" ] run lib 'ARG_ONLY=apt; RESUME_DONE=() @@ -61,6 +61,14 @@ teardown() { [ "$output" = "sudo" ] } +@test "sudo check: the deep clean needs sudo on Linux, not under --soft-clean or on macOS" { + run lib 'ARG_ONLY=none; if run_needs_sudo; then echo sudo; else echo none; fi + ARG_SOFT_CLEAN=1; if run_needs_sudo; then echo sudo; else echo none; fi + ARG_SOFT_CLEAN=0; sys_is_macos() { return 0; }; if run_needs_sudo; then echo sudo; else echo none; fi' + [ "$status" -eq 0 ] + [ "$output" = $'sudo\nnone\nnone' ] +} + @test "pkg_run: sudo prefix only when not root" { stub apt 'exit 0' run lib 'DRY_RUN=1; PATH=$STUBS:/usr/bin:/bin; sys_is_root() { return 0; }; pkg_run apt 2>&1' diff --git a/tests/test_helper.bash b/tests/test_helper.bash index 6fdab1b..1117511 100644 --- a/tests/test_helper.bash +++ b/tests/test_helper.bash @@ -1,12 +1,14 @@ # Shared fixture of the bash test files: `load test_helper` from a file's top. # Point HOME at a fresh temp directory, the old value kept in HOME_BAK, and -# export REPO_DIR, LIB, EMERGER (the entry point command), NO_COLOR and an -# empty stub directory STUBS. +# export REPO_DIR, LIB, UP (the entry point command), EMERGER (UP under +# --soft-clean, so a run never reaches the host's logs, containers or +# models), NO_COLOR and an empty stub directory STUBS. setup_home() { export REPO_DIR="${BATS_TEST_DIRNAME}/.." export LIB="$REPO_DIR/src/lib" - export EMERGER="bash $REPO_DIR/src/emerger.sh" + export UP="bash $REPO_DIR/src/emerger.sh" + export EMERGER="$UP --soft-clean" export HOME_BAK="$HOME" export HOME="$(mktemp -d)" export NO_COLOR=1 @@ -70,7 +72,7 @@ setup_minbin() { mkdir -p "$MINBIN" local t p for t in bash sh awk cat cut date df du find grep head mkdir mktemp numfmt \ - paste rm sed sleep sort stat tail timeout touch tr uname wc id comm join env mv; do + paste rm sed sleep sort stat tail timeout touch tr truncate uname wc id comm join env mv; do p=$(command -v "$t") && ln -s "$p" "$MINBIN/$t" done }