1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-16 07:11:11 +00:00

Improve the scanning speed

This commit is contained in:
Tw93
2025-12-04 16:36:17 +08:00
parent a17b1d9db3
commit 5ddd345d76
3 changed files with 47 additions and 40 deletions

View File

@@ -272,13 +272,16 @@ safe_clean() {
( (
local size local size
# Timeout protection: prevent du from hanging on problematic paths # 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 [[ -z "$size" || ! "$size" =~ ^[0-9]+$ ]] && size=0
local count local count
# Timeout protection: prevent find from hanging on problematic paths # Quick file count - limit for performance
count=$(run_with_timeout 10 sh -c "find \"$path\" -type f 2> /dev/null | wc -l | tr -d ' '") if [[ "$size" -gt 0 ]]; then
# If timeout or error, set count to 0 to skip this path count=$(find "$path" -type f 2>/dev/null | head -1000 | wc -l | tr -d ' ')
[[ -z "$count" || ! "$count" =~ ^[0-9]+$ ]] && count=0 [[ -z "$count" || ! "$count" =~ ^[0-9]+$ ]] && count=0
else
count=0
fi
# Use index + PID for unique filename # Use index + PID for unique filename
local tmp_file="$temp_dir/result_${idx}.$$" local tmp_file="$temp_dir/result_${idx}.$$"
echo "$size $count" > "$tmp_file" 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 if [[ -t 1 ]]; then MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning $total_paths items..."; fi
for path in "${existing_paths[@]}"; do for path in "${existing_paths[@]}"; do
local size_bytes local size_bytes count
# Timeout protection: prevent du from hanging on problematic paths # Get size quickly - du is fast
size_bytes=$(run_with_timeout 5 get_path_size_kb "$path") size_bytes=$(get_path_size_kb "$path")
[[ -z "$size_bytes" || ! "$size_bytes" =~ ^[0-9]+$ ]] && size_bytes=0 [[ -z "$size_bytes" || ! "$size_bytes" =~ ^[0-9]+$ ]] && size_bytes=0
local count # Quick file count for display - limit for performance
# Timeout protection: prevent find from hanging on problematic paths if [[ "$size_bytes" -gt 0 ]]; then
count=$(run_with_timeout 10 sh -c "find \"$path\" -type f 2> /dev/null | wc -l | tr -d ' '") count=$(find "$path" -type f 2>/dev/null | head -1000 | wc -l | tr -d ' ')
# If timeout or error, set count to 0 to skip this path
[[ -z "$count" || ! "$count" =~ ^[0-9]+$ ]] && count=0 [[ -z "$count" || ! "$count" =~ ^[0-9]+$ ]] && count=0
else
count=0
fi
if [[ "$count" -gt 0 && "$size_bytes" -gt 0 ]]; then if [[ "$count" -gt 0 && "$size_bytes" -gt 0 ]]; then
if [[ "$DRY_RUN" != "true" ]]; then if [[ "$DRY_RUN" != "true" ]]; then
@@ -679,24 +684,21 @@ perform_cleanup() {
"$HOME/Applications" "$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 for search_path in "${search_paths[@]}"; do
[[ -d "$search_path" ]] || continue [[ -d "$search_path" ]] || continue
while IFS= read -r app; do find "$search_path" -maxdepth 3 -name "Info.plist" -path "*/Contents/Info.plist" 2>/dev/null | \
[[ -f "$app/Contents/Info.plist" ]] || continue xargs -I {} /usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" {} 2>/dev/null | \
bundle_id=$(defaults read "$app/Contents/Info.plist" CFBundleIdentifier 2> /dev/null || echo "") grep -v "^$" >> "$installed_bundles" || true
[[ -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)
done done
# Get running applications and LaunchAgents with timeout protection # Get running applications - no timeout needed for fast osascript
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 "") osascript -e 'tell application "System Events" to get bundle identifier of every application process' 2>/dev/null | \
echo "$running_apps" | tr ',' '\n' | sed -e 's/^ *//;s/ *$//' -e '/^$/d' >> "$installed_bundles" tr ',' '\n' | sed -e 's/^ *//;s/ *$//' -e '/^$/d' >> "$installed_bundles" || true
run_with_timeout 5 find ~/Library/LaunchAgents /Library/LaunchAgents \ # Get LaunchAgents - fast operation, no timeout needed
-name "*.plist" -type f 2> /dev/null | while IFS= read -r plist; do find ~/Library/LaunchAgents /Library/LaunchAgents -name "*.plist" -type f 2>/dev/null | \
basename "$plist" .plist xargs -I {} basename {} .plist >> "$installed_bundles" 2>/dev/null || true
done >> "$installed_bundles" 2> /dev/null || true
# Deduplicate # Deduplicate
sort -u "$installed_bundles" -o "$installed_bundles" sort -u "$installed_bundles" -o "$installed_bundles"

View File

@@ -553,19 +553,24 @@ run_with_timeout() {
"$@" & "$@" &
local cmd_pid=$! local cmd_pid=$!
local elapsed=0
while kill -0 "$cmd_pid" 2> /dev/null; do # More efficient wait: use wait with timeout in subshell
if [[ $elapsed -ge $duration ]]; then (
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 kill -TERM "$cmd_pid" 2>/dev/null || true
sleep 1 sleep 0.5
kill -KILL "$cmd_pid" 2>/dev/null || true kill -KILL "$cmd_pid" 2>/dev/null || true
wait "$cmd_pid" 2> /dev/null || true exit 124
return 124 ) &
fi local watcher_pid=$!
sleep 1
((elapsed++)) wait "$cmd_pid" 2>/dev/null
done local exit_code=$?
wait "$cmd_pid" kill "$watcher_pid" 2>/dev/null || true
wait "$watcher_pid" 2>/dev/null || true
return $exit_code
} }
# Menu display helper # Menu display helper

2
mole
View File

@@ -22,7 +22,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/core/common.sh" source "$SCRIPT_DIR/lib/core/common.sh"
# Version info # Version info
VERSION="1.11.20" VERSION="1.11.21"
MOLE_TAGLINE="can dig deep to clean your Mac." MOLE_TAGLINE="can dig deep to clean your Mac."
# Check if Touch ID is already configured # Check if Touch ID is already configured