From 82a3f49de87926824350dba333f74c05b18c385b Mon Sep 17 00:00:00 2001 From: Orion Date: Sat, 18 Jul 2026 13:34:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(homebrew):=20=E4=BC=98=E5=8C=96=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=B8=85=E7=90=86=E5=8A=9F=E8=83=BD=E5=B9=B6=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 ACTIVE_CLEANUP_MANIFEST 变量用于跟踪清理操作 - 实现 format_bytes 函数用于格式化字节大小显示 - 重构 prune_cache_before_today 函数以提供详细的文件统计信息 - 添加已删除和保留文件的详细列表输出 - 使用清单文件安全地处理缓存文件枚举和删除操作 - 改进错误处理和清理失败时的状态返回 - 在 .gitignore 中添加新的 IDE 配置文件忽略规则 --- .gitignore | 3 +- homebrew/brew-upgrade-manager.sh | 83 ++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 549731b..e114597 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ Temporary Items .idea/uiDesigner.xml .idea/jsLibraryMappings.xml .idea/claudeCodeTabState.xml +.idea/j2kLlmSettings.xml .idea/.gitignore # 拦截旧版 IDEA 和 Eclipse 文件 *.iml @@ -144,4 +145,4 @@ docker-compose.override.yml # SSL 证书和私钥绝不入库 *.pem *.crt -*.key +*.key \ No newline at end of file diff --git a/homebrew/brew-upgrade-manager.sh b/homebrew/brew-upgrade-manager.sh index 3d35997..f2a8c65 100644 --- a/homebrew/brew-upgrade-manager.sh +++ b/homebrew/brew-upgrade-manager.sh @@ -37,6 +37,7 @@ fi TERMINAL_WIDTH="" ACTIVE_LOG_FILE="" ACTIVE_CLEANUP_MARKER="" +ACTIVE_CLEANUP_MANIFEST="" LOCK_ACQUIRED=0 LOCK_DIR="${HB_LOCK_DIR:-${TMPDIR:-/tmp}/brewup-$(id -u).lock}" FAILED_FORMULAE=() @@ -120,6 +121,9 @@ cleanup_runtime() { if [[ -n "${ACTIVE_CLEANUP_MARKER:-}" && -f "$ACTIVE_CLEANUP_MARKER" ]]; then rm -f "$ACTIVE_CLEANUP_MARKER" fi + if [[ -n "${ACTIVE_CLEANUP_MANIFEST:-}" && -f "$ACTIVE_CLEANUP_MANIFEST" ]]; then + rm -f "$ACTIVE_CLEANUP_MANIFEST" + fi if ((LOCK_ACQUIRED)); then rm -f "$LOCK_DIR/pid" rmdir "$LOCK_DIR" 2>/dev/null || true @@ -361,8 +365,26 @@ run_cask_upgrade() { return 1 } +format_bytes() { + local bytes="$1" unit=0 divisor=1 tenths + local units=(B KiB MiB GiB TiB) + + while ((unit < 4 && bytes >= divisor * 1024)); do + ((unit += 1)) + ((divisor *= 1024)) + done + if ((unit == 0)); then + printf '%d B' "$bytes" + return + fi + + tenths=$(((bytes * 10 + divisor / 2) / divisor)) + printf '%d.%d %s' "$((tenths / 10))" "$((tenths % 10))" "${units[unit]}" +} + prune_cache_before_today() { - local cache_dir cutoff_stamp cutoff_date removed_count + local cache_dir cutoff_stamp cutoff_date file file_size relative_path + local removed_count=0 removed_bytes=0 kept_count=0 kept_bytes=0 cleanup_failed=0 cache_dir="$(brew --cache)" if [[ ! -d "$cache_dir" || "$cache_dir" == "/" || "$cache_dir" == "$HOME" ]]; then @@ -375,20 +397,63 @@ prune_cache_before_today() { cutoff_stamp="$(date -v-1d '+%Y%m%d2359.59')" cutoff_date="$(date '+%Y-%m-%d')" ACTIVE_CLEANUP_MARKER="$(mktemp "${TMPDIR:-/tmp}/brewup-cleanup-cutoff.XXXXXX")" + ACTIVE_CLEANUP_MANIFEST="$(mktemp "${TMPDIR:-/tmp}/brewup-cleanup-files.XXXXXX")" touch -t "$cutoff_stamp" "$ACTIVE_CLEANUP_MARKER" - if ! removed_count="$( - find "$cache_dir" -mindepth 1 \( -type f -o -type l \) \ - ! -newer "$ACTIVE_CLEANUP_MARKER" -print -delete \ - | awk 'END { print NR + 0 }' - )"; then - echo "Error: failed to prune Homebrew cache by calendar date." >&2 + if ! find "$cache_dir" -mindepth 1 \( -type f -o -type l \) \ + ! -newer "$ACTIVE_CLEANUP_MARKER" -print0 > "$ACTIVE_CLEANUP_MANIFEST"; then + echo "Error: failed to enumerate Homebrew cache files to remove." >&2 return 1 fi + echo "Removed cache files:" + while IFS= read -r -d '' file; do + if ! file_size="$(stat -f '%z' -- "$file" 2>/dev/null)"; then + file_size=0 + fi + relative_path="${file#"$cache_dir"/}" + if rm -f -- "$file"; then + removed_count=$((removed_count + 1)) + removed_bytes=$((removed_bytes + file_size)) + printf ' %10s %q\n' "$(format_bytes "$file_size")" "$relative_path" + else + echo "Warning: failed to remove cache file: $file" >&2 + cleanup_failed=1 + fi + done < "$ACTIVE_CLEANUP_MANIFEST" + if ((removed_count == 0)); then + echo " (none)" + fi + printf 'Removed %d Homebrew cache file(s) dated before %s (%s total).\n' \ + "$removed_count" "$cutoff_date" "$(format_bytes "$removed_bytes")" + + if ! find "$cache_dir" -mindepth 1 \( -type f -o -type l \) \ + -newer "$ACTIVE_CLEANUP_MARKER" -print0 > "$ACTIVE_CLEANUP_MANIFEST"; then + echo "Error: failed to enumerate today's retained Homebrew cache files." >&2 + return 1 + fi + + echo "Retained cache files dated today or later:" + while IFS= read -r -d '' file; do + if ! file_size="$(stat -f '%z' -- "$file" 2>/dev/null)"; then + file_size=0 + fi + relative_path="${file#"$cache_dir"/}" + kept_count=$((kept_count + 1)) + kept_bytes=$((kept_bytes + file_size)) + printf ' %10s %q\n' "$(format_bytes "$file_size")" "$relative_path" + done < "$ACTIVE_CLEANUP_MANIFEST" + if ((kept_count == 0)); then + echo " (none)" + fi + printf "Retained %d Homebrew cache file(s) dated %s or later (%s total).\n" \ + "$kept_count" "$cutoff_date" "$(format_bytes "$kept_bytes")" + rm -f "$ACTIVE_CLEANUP_MARKER" ACTIVE_CLEANUP_MARKER="" - echo "Removed ${removed_count} Homebrew cache file(s) dated before ${cutoff_date}; today's cache was kept." + rm -f "$ACTIVE_CLEANUP_MANIFEST" + ACTIVE_CLEANUP_MANIFEST="" + return "$cleanup_failed" } run_cleanup() { @@ -486,4 +551,4 @@ separator printf '\n' print_summary "$overall_status" -exit "$overall_status" \ No newline at end of file +exit "$overall_status"