From 9e7fc4144564c42b9130de9317a5fc475ffa0980 Mon Sep 17 00:00:00 2001 From: Tw93 Date: Mon, 8 Dec 2025 15:40:39 +0800 Subject: [PATCH] Enhanced optimization for better use --- bin/uninstall.sh | 19 ++++++++++++++----- lib/check/health_json.sh | 11 +++++++++++ lib/core/app_protection.sh | 8 ++++---- lib/core/timeout.sh | 2 +- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/bin/uninstall.sh b/bin/uninstall.sh index 05ef107..ef2398b 100755 --- a/bin/uninstall.sh +++ b/bin/uninstall.sh @@ -222,14 +222,24 @@ scan_applications() { app_size=$(bytes_to_human "$((app_size_kb * 1024))") fi - # Get last used date - simplified for speed - # Note: mdls can be slow (40ms+ per app), so we use file mtime instead + # Get last used date local last_used="Never" local last_used_epoch=0 if [[ -d "$app_path" ]]; then - # Use file modification time (much faster than mdls) - last_used_epoch=$(get_file_mtime "$app_path") + # Try mdls first with short timeout (0.05s) for accuracy, fallback to mtime for speed + local metadata_date + metadata_date=$(run_with_timeout 0.05 mdls -name kMDItemLastUsedDate -raw "$app_path" 2> /dev/null || echo "") + + if [[ "$metadata_date" != "(null)" && -n "$metadata_date" ]]; then + last_used_epoch=$(date -j -f "%Y-%m-%d %H:%M:%S %z" "$metadata_date" "+%s" 2> /dev/null || echo "0") + fi + + # Fallback if mdls failed or returned nothing + if [[ "$last_used_epoch" -eq 0 ]]; then + last_used_epoch=$(get_file_mtime "$app_path") + fi + if [[ $last_used_epoch -gt 0 ]]; then local days_ago=$(((current_epoch - last_used_epoch) / 86400)) @@ -282,7 +292,6 @@ scan_applications() { done ) & spinner_pid=$! - disown 2> /dev/null || true # Process apps in parallel batches for app_data_tuple in "${app_data_tuples[@]}"; do diff --git a/lib/check/health_json.sh b/lib/check/health_json.sh index 8eecb60..2292f12 100644 --- a/lib/check/health_json.sh +++ b/lib/check/health_json.sh @@ -84,6 +84,12 @@ get_uptime_days() { echo "$uptime_days" } +# JSON escape helper +json_escape() { + # Escape backslash, double quote, tab, and newline + echo -n "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/ /\\t/g' | tr '\n' ' ' +} + # Generate JSON output generate_health_json() { # System info @@ -127,6 +133,11 @@ EOF for item in "${items[@]}"; do IFS='|' read -r action name desc safe <<< "$item" + # Escape strings + action=$(json_escape "$action") + name=$(json_escape "$name") + desc=$(json_escape "$desc") + [[ "$first" == "true" ]] && first=false || echo "," cat << EOF diff --git a/lib/core/app_protection.sh b/lib/core/app_protection.sh index ff5abb5..44ef3b8 100755 --- a/lib/core/app_protection.sh +++ b/lib/core/app_protection.sh @@ -420,7 +420,7 @@ find_app_files() { [[ -f ~/Library/Preferences/"$bundle_id".plist ]] && files_to_clean+=("$HOME/Library/Preferences/$bundle_id.plist") [[ -d ~/Library/Preferences/ByHost ]] && while IFS= read -r -d '' pref; do files_to_clean+=("$pref") - done < <(find ~/Library/Preferences/ByHost \( -name \"$bundle_id*.plist\" \) -print0 2> /dev/null) + done < <(find ~/Library/Preferences/ByHost \( -name "$bundle_id*.plist" \) -print0 2> /dev/null) # Logs [[ -d ~/Library/Logs/"$app_name" ]] && files_to_clean+=("$HOME/Library/Logs/$app_name") @@ -435,7 +435,7 @@ find_app_files() { # Group Containers [[ -d ~/Library/Group\ Containers ]] && while IFS= read -r -d '' container; do files_to_clean+=("$container") - done < <(find ~/Library/Group\ Containers -type d \( -name \"*$bundle_id*\" \) -print0 2> /dev/null) + done < <(find ~/Library/Group\ Containers -type d \( -name "*$bundle_id*" \) -print0 2> /dev/null) # WebKit data [[ -d ~/Library/WebKit/"$bundle_id" ]] && files_to_clean+=("$HOME/Library/WebKit/$bundle_id") @@ -514,7 +514,7 @@ find_app_system_files() { # Privileged Helper Tools [[ -d /Library/PrivilegedHelperTools ]] && while IFS= read -r -d '' helper; do system_files+=("$helper") - done < <(find /Library/PrivilegedHelperTools \( -name \"$bundle_id*\" \) -print0 2> /dev/null) + done < <(find /Library/PrivilegedHelperTools \( -name "$bundle_id*" \) -print0 2> /dev/null) # System Preferences [[ -f /Library/Preferences/"$bundle_id".plist ]] && system_files+=("/Library/Preferences/$bundle_id.plist") @@ -522,7 +522,7 @@ find_app_system_files() { # Installation Receipts [[ -d /private/var/db/receipts ]] && while IFS= read -r -d '' receipt; do system_files+=("$receipt") - done < <(find /private/var/db/receipts \( -name \"*$bundle_id*\" \) -print0 2> /dev/null) + done < <(find /private/var/db/receipts \( -name "*$bundle_id*" \) -print0 2> /dev/null) # System Logs [[ -d /Library/Logs/"$app_name" ]] && system_files+=("/Library/Logs/$app_name") diff --git a/lib/core/timeout.sh b/lib/core/timeout.sh index abc8c0b..5ff9576 100644 --- a/lib/core/timeout.sh +++ b/lib/core/timeout.sh @@ -81,7 +81,7 @@ run_with_timeout() { shift || true # No timeout if duration is invalid or zero - if [[ ! "$duration" =~ ^[0-9]+$ ]] || [[ "$duration" -le 0 ]]; then + if [[ ! "$duration" =~ ^[0-9]+(\.[0-9]+)?$ ]] || [[ $(echo "$duration <= 0" | bc -l 2> /dev/null) -eq 1 ]]; then "$@" return $? fi