feat(homebrew): 优化缓存清理功能并改进文件管理
- 添加 ACTIVE_CLEANUP_MANIFEST 变量用于跟踪清理操作 - 实现 format_bytes 函数用于格式化字节大小显示 - 重构 prune_cache_before_today 函数以提供详细的文件统计信息 - 添加已删除和保留文件的详细列表输出 - 使用清单文件安全地处理缓存文件枚举和删除操作 - 改进错误处理和清理失败时的状态返回 - 在 .gitignore 中添加新的 IDE 配置文件忽略规则
This commit is contained in:
@@ -41,6 +41,7 @@ Temporary Items
|
||||
.idea/uiDesigner.xml
|
||||
.idea/jsLibraryMappings.xml
|
||||
.idea/claudeCodeTabState.xml
|
||||
.idea/j2kLlmSettings.xml
|
||||
.idea/.gitignore
|
||||
# 拦截旧版 IDEA 和 Eclipse 文件
|
||||
*.iml
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user