From 5ddd345d76ce6832e68f696a93e40dd42ed24c61 Mon Sep 17 00:00:00 2001 From: Tw93 Date: Thu, 4 Dec 2025 16:36:17 +0800 Subject: [PATCH] Improve the scanning speed --- bin/clean.sh | 54 ++++++++++++++++++++++++---------------------- lib/core/common.sh | 31 +++++++++++++++----------- mole | 2 +- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/bin/clean.sh b/bin/clean.sh index b3a9e11..c7cac31 100755 --- a/bin/clean.sh +++ b/bin/clean.sh @@ -272,13 +272,16 @@ safe_clean() { ( local size # Timeout protection: prevent du from hanging on problematic paths - size=$(run_with_timeout 5 get_path_size_kb "$path") + size=$(get_path_size_kb "$path") [[ -z "$size" || ! "$size" =~ ^[0-9]+$ ]] && size=0 local count - # Timeout protection: prevent find from hanging on problematic paths - count=$(run_with_timeout 10 sh -c "find \"$path\" -type f 2> /dev/null | wc -l | tr -d ' '") - # If timeout or error, set count to 0 to skip this path - [[ -z "$count" || ! "$count" =~ ^[0-9]+$ ]] && count=0 + # Quick file count - limit for performance + if [[ "$size" -gt 0 ]]; then + count=$(find "$path" -type f 2>/dev/null | head -1000 | wc -l | tr -d ' ') + [[ -z "$count" || ! "$count" =~ ^[0-9]+$ ]] && count=0 + else + count=0 + fi # Use index + PID for unique filename local tmp_file="$temp_dir/result_${idx}.$$" echo "$size $count" > "$tmp_file" @@ -334,15 +337,17 @@ safe_clean() { if [[ -t 1 ]]; then MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning $total_paths items..."; fi for path in "${existing_paths[@]}"; do - local size_bytes - # Timeout protection: prevent du from hanging on problematic paths - size_bytes=$(run_with_timeout 5 get_path_size_kb "$path") + local size_bytes count + # Get size quickly - du is fast + size_bytes=$(get_path_size_kb "$path") [[ -z "$size_bytes" || ! "$size_bytes" =~ ^[0-9]+$ ]] && size_bytes=0 - local count - # Timeout protection: prevent find from hanging on problematic paths - count=$(run_with_timeout 10 sh -c "find \"$path\" -type f 2> /dev/null | wc -l | tr -d ' '") - # If timeout or error, set count to 0 to skip this path - [[ -z "$count" || ! "$count" =~ ^[0-9]+$ ]] && count=0 + # Quick file count for display - limit for performance + if [[ "$size_bytes" -gt 0 ]]; then + count=$(find "$path" -type f 2>/dev/null | head -1000 | wc -l | tr -d ' ') + [[ -z "$count" || ! "$count" =~ ^[0-9]+$ ]] && count=0 + else + count=0 + fi if [[ "$count" -gt 0 && "$size_bytes" -gt 0 ]]; then if [[ "$DRY_RUN" != "true" ]]; then @@ -679,24 +684,21 @@ perform_cleanup() { "$HOME/Applications" ) - # Scan for .app bundles with timeout protection + # Scan for .app bundles - optimized with PlistBuddy and xargs for search_path in "${search_paths[@]}"; do [[ -d "$search_path" ]] || continue - while IFS= read -r app; do - [[ -f "$app/Contents/Info.plist" ]] || continue - bundle_id=$(defaults read "$app/Contents/Info.plist" CFBundleIdentifier 2> /dev/null || echo "") - [[ -n "$bundle_id" ]] && echo "$bundle_id" >> "$installed_bundles" - done < <(run_with_timeout 10 command find "$search_path" -maxdepth 2 -type d -name "*.app" 2> /dev/null || true) + find "$search_path" -maxdepth 3 -name "Info.plist" -path "*/Contents/Info.plist" 2>/dev/null | \ + xargs -I {} /usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" {} 2>/dev/null | \ + grep -v "^$" >> "$installed_bundles" || true done - # Get running applications and LaunchAgents with timeout protection - local running_apps=$(run_with_timeout 5 osascript -e 'tell application "System Events" to get bundle identifier of every application process' 2> /dev/null || echo "") - echo "$running_apps" | tr ',' '\n' | sed -e 's/^ *//;s/ *$//' -e '/^$/d' >> "$installed_bundles" + # Get running applications - no timeout needed for fast osascript + osascript -e 'tell application "System Events" to get bundle identifier of every application process' 2>/dev/null | \ + tr ',' '\n' | sed -e 's/^ *//;s/ *$//' -e '/^$/d' >> "$installed_bundles" || true - run_with_timeout 5 find ~/Library/LaunchAgents /Library/LaunchAgents \ - -name "*.plist" -type f 2> /dev/null | while IFS= read -r plist; do - basename "$plist" .plist - done >> "$installed_bundles" 2> /dev/null || true + # Get LaunchAgents - fast operation, no timeout needed + find ~/Library/LaunchAgents /Library/LaunchAgents -name "*.plist" -type f 2>/dev/null | \ + xargs -I {} basename {} .plist >> "$installed_bundles" 2>/dev/null || true # Deduplicate sort -u "$installed_bundles" -o "$installed_bundles" diff --git a/lib/core/common.sh b/lib/core/common.sh index e14d619..b892c3e 100755 --- a/lib/core/common.sh +++ b/lib/core/common.sh @@ -553,19 +553,24 @@ run_with_timeout() { "$@" & local cmd_pid=$! - local elapsed=0 - while kill -0 "$cmd_pid" 2> /dev/null; do - if [[ $elapsed -ge $duration ]]; then - kill -TERM "$cmd_pid" 2> /dev/null || true - sleep 1 - kill -KILL "$cmd_pid" 2> /dev/null || true - wait "$cmd_pid" 2> /dev/null || true - return 124 - fi - sleep 1 - ((elapsed++)) - done - wait "$cmd_pid" + + # More efficient wait: use wait with timeout in subshell + ( + sleep "$duration" & + local timer_pid=$! + wait "$cmd_pid" 2>/dev/null && kill "$timer_pid" 2>/dev/null && exit 0 + kill -TERM "$cmd_pid" 2>/dev/null || true + sleep 0.5 + kill -KILL "$cmd_pid" 2>/dev/null || true + exit 124 + ) & + local watcher_pid=$! + + wait "$cmd_pid" 2>/dev/null + local exit_code=$? + kill "$watcher_pid" 2>/dev/null || true + wait "$watcher_pid" 2>/dev/null || true + return $exit_code } # Menu display helper diff --git a/mole b/mole index e3e4dc2..d170bd4 100755 --- a/mole +++ b/mole @@ -22,7 +22,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/lib/core/common.sh" # Version info -VERSION="1.11.20" +VERSION="1.11.21" MOLE_TAGLINE="can dig deep to clean your Mac." # Check if Touch ID is already configured