From faac42cb62d4d07a84f047703dff2fd2e8fb63f6 Mon Sep 17 00:00:00 2001 From: Tw93 Date: Sun, 28 Dec 2025 19:37:42 +0800 Subject: [PATCH] Improve performance and process handling --- lib/clean/caches.sh | 18 ++++++++++++++++++ lib/clean/dev.sh | 13 +++++++++++++ lib/clean/user.sh | 5 +++-- lib/core/file_ops.sh | 10 +++++----- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/clean/caches.sh b/lib/clean/caches.sh index 3ef0332..2297d55 100644 --- a/lib/clean/caches.sh +++ b/lib/clean/caches.sh @@ -241,7 +241,25 @@ clean_project_caches() { # 4. Clean up any stuck processes for pid in $next_pid $py_pid; do if kill -0 "$pid" 2> /dev/null; then + # Send TERM signal first kill -TERM "$pid" 2> /dev/null || true + + # Wait up to 2 seconds for graceful termination + local grace_period=0 + while [[ $grace_period -lt 20 ]]; do + if ! kill -0 "$pid" 2> /dev/null; then + break + fi + sleep 0.1 + ((grace_period++)) + done + + # Force kill if still running + if kill -0 "$pid" 2> /dev/null; then + kill -KILL "$pid" 2> /dev/null || true + fi + + # Final wait (should be instant now) wait "$pid" 2> /dev/null || true else wait "$pid" 2> /dev/null || true diff --git a/lib/clean/dev.sh b/lib/clean/dev.sh index 6dae43f..32414f8 100644 --- a/lib/clean/dev.sh +++ b/lib/clean/dev.sh @@ -321,12 +321,25 @@ clean_dev_network() { safe_clean ~/Library/Caches/wget/* "macOS wget cache" } +# Clean orphaned SQLite temporary files (-shm and -wal files) +# Strategy: Only clean truly orphaned temp files where base database is missing +# This is fast and safe - skip complex checks for files with existing base DB +# Env: DRY_RUN +clean_sqlite_temp_files() { + # Skip this cleanup due to low ROI (收益比低,经常没东西可清理) + # Find scan is still slow even optimized, and orphaned files are rare + return 0 +} + # Main developer tools cleanup function # Calls all specialized cleanup functions # Env: DRY_RUN clean_developer_tools() { stop_section_spinner + # Clean SQLite temporary files first + clean_sqlite_temp_files + clean_dev_npm clean_dev_python clean_dev_go diff --git a/lib/clean/user.sh b/lib/clean/user.sh index 6800c41..cd0a3ce 100644 --- a/lib/clean/user.sh +++ b/lib/clean/user.sh @@ -78,8 +78,9 @@ scan_external_volumes() { start_section_spinner "Scanning $volume_count external volume(s)..." for volume in "${candidate_volumes[@]}"; do - # Verify volume is actually mounted (reduced timeout from 2s to 1s) - run_with_timeout 1 mount | grep -q "on $volume " || continue + # Re-verify volume is still accessible (may have been unmounted since initial scan) + # Use simple directory check instead of slow mount command for better performance + [[ -d "$volume" && -r "$volume" ]] || continue # 1. Clean Trash on volume local volume_trash="$volume/.Trashes" diff --git a/lib/core/file_ops.sh b/lib/core/file_ops.sh index 328d3dc..5adf9e9 100644 --- a/lib/core/file_ops.sh +++ b/lib/core/file_ops.sh @@ -185,13 +185,13 @@ safe_sudo_find_delete() { local age_days="${3:-7}" local type_filter="${4:-f}" - # Validate base directory - if [[ ! -d "$base_dir" ]]; then - log_error "Directory does not exist: $base_dir" - return 1 + # Validate base directory (use sudo for permission-restricted dirs) + if ! sudo test -d "$base_dir" 2>/dev/null; then + debug_log "Directory does not exist (skipping): $base_dir" + return 0 fi - if [[ -L "$base_dir" ]]; then + if sudo test -L "$base_dir" 2>/dev/null; then log_error "Refusing to search symlinked directory: $base_dir" return 1 fi