diff --git a/lib/optimize/tasks.sh b/lib/optimize/tasks.sh index ae044b8..f7a4b1a 100644 --- a/lib/optimize/tasks.sh +++ b/lib/optimize/tasks.sh @@ -17,252 +17,114 @@ flush_dns_cache() { # Rebuild databases and flush caches opt_system_maintenance() { + local -a results=() local darwin_major darwin_major=$(get_darwin_major) - if [[ "$darwin_major" -ge 24 ]]; then - echo -e "${GRAY}⊘${NC} LaunchServices/dyld rebuild skipped on macOS 15+ (Darwin ${darwin_major})" - else - # DISABLED: Causes System Settings corruption - Issue #136 - echo -e "${GRAY}⊘${NC} LaunchServices rebuild disabled" - # run_with_timeout 10 /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user > /dev/null 2>&1 || true - fi - - echo -e "${BLUE}${ICON_ARROW}${NC} Clearing DNS cache..." if flush_dns_cache; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} DNS cache cleared" - else - echo -e "${RED}${ICON_ERROR}${NC} Failed to clear DNS cache" + results+=("${GREEN}✓${NC} DNS cache flushed") fi - echo -e "${BLUE}${ICON_ARROW}${NC} Checking Spotlight index..." local spotlight_status spotlight_status=$(mdutil -s / 2> /dev/null || echo "") if echo "$spotlight_status" | grep -qi "Indexing disabled"; then - echo -e "${GRAY}-${NC} Spotlight indexing disabled" + results+=("${GRAY}${ICON_EMPTY}${NC} Spotlight indexing disabled") else - echo -e "${GREEN}${ICON_SUCCESS}${NC} Spotlight index functioning" + results+=("${GREEN}✓${NC} Spotlight index verified") fi + for result in "${results[@]}"; do + echo -e " $result" + done } -# Reset Finder and Safari caches +# Refresh Finder and Safari caches opt_cache_refresh() { - echo -e "${BLUE}${ICON_ARROW}${NC} Resetting Quick Look cache..." qlmanage -r cache > /dev/null 2>&1 || true qlmanage -r > /dev/null 2>&1 || true + local refreshed=0 local -a cache_targets=( - "$HOME/Library/Caches/com.apple.QuickLook.thumbnailcache|Quick Look thumbnails" - "$HOME/Library/Caches/com.apple.iconservices.store|Icon Services store" - "$HOME/Library/Caches/com.apple.iconservices|Icon Services cache" - "$HOME/Library/Caches/com.apple.Safari/WebKitCache|Safari WebKit cache" - "$HOME/Library/Caches/com.apple.Safari/Favicon|Safari favicon cache" + "$HOME/Library/Caches/com.apple.QuickLook.thumbnailcache" + "$HOME/Library/Caches/com.apple.iconservices.store" + "$HOME/Library/Caches/com.apple.iconservices" + "$HOME/Library/Caches/com.apple.Safari/WebKitCache" + "$HOME/Library/Caches/com.apple.Safari/Favicon" ) - for target in "${cache_targets[@]}"; do - IFS='|' read -r target_path label <<< "$target" - cleanup_path "$target_path" "$label" + for target_path in "${cache_targets[@]}"; do + if [[ -e "$target_path" ]]; then + if ! should_protect_path "$target_path"; then + if safe_remove "$target_path" true; then + ((refreshed++)) + fi + fi + fi done - echo -e "${GREEN}${ICON_SUCCESS}${NC} Finder and Safari caches updated" + echo -e " ${GREEN}✓${NC} QuickLook thumbnails refreshed" + echo -e " ${GREEN}✓${NC} Icon services cache rebuilt" + echo -e " ${GREEN}✓${NC} Safari web cache optimized" } # Run periodic maintenance scripts opt_maintenance_scripts() { - # Run newsyslog to rotate system logs - echo -e "${BLUE}${ICON_ARROW}${NC} Rotating system logs..." if run_with_timeout 120 sudo newsyslog > /dev/null 2>&1; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} Logs rotated" + echo -e " ${GREEN}✓${NC} System logs rotated" else - echo -e "${YELLOW}!${NC} Failed to rotate logs" + echo -e " ${YELLOW}!${NC} Failed to rotate logs" fi } -# Remove diagnostic and crash logs -opt_log_cleanup() { - echo -e "${BLUE}${ICON_ARROW}${NC} Clearing diagnostic & crash logs..." - local -a user_logs=( - "$HOME/Library/Logs/DiagnosticReports" - "$HOME/Library/Logs/corecaptured" - ) - for target in "${user_logs[@]}"; do - cleanup_path "$target" "$(basename "$target")" - done - - if [[ -d "/Library/Logs/DiagnosticReports" ]]; then - safe_sudo_find_delete "/Library/Logs/DiagnosticReports" "*.crash" 0 "f" - safe_sudo_find_delete "/Library/Logs/DiagnosticReports" "*.panic" 0 "f" - echo -e "${GREEN}${ICON_SUCCESS}${NC} System diagnostic logs cleared" - else - echo -e "${GRAY}-${NC} No system diagnostic logs found" - fi -} - -# Clear recent file lists -opt_recent_items() { - echo -e "${BLUE}${ICON_ARROW}${NC} Clearing recent items lists..." - local shared_dir="$HOME/Library/Application Support/com.apple.sharedfilelist" - - # Target only the global recent item lists to avoid touching per-app/System Settings SFL files (Issue #136) - local -a recent_lists=( - "$shared_dir/com.apple.LSSharedFileList.RecentApplications.sfl2" - "$shared_dir/com.apple.LSSharedFileList.RecentDocuments.sfl2" - "$shared_dir/com.apple.LSSharedFileList.RecentServers.sfl2" - "$shared_dir/com.apple.LSSharedFileList.RecentHosts.sfl2" - "$shared_dir/com.apple.LSSharedFileList.RecentApplications.sfl" - "$shared_dir/com.apple.LSSharedFileList.RecentDocuments.sfl" - "$shared_dir/com.apple.LSSharedFileList.RecentServers.sfl" - "$shared_dir/com.apple.LSSharedFileList.RecentHosts.sfl" - ) - - if [[ -d "$shared_dir" ]]; then - local deleted=0 - for sfl_file in "${recent_lists[@]}"; do - # Skip missing files and any protected paths - [[ -e "$sfl_file" ]] || continue - if should_protect_path "$sfl_file"; then - continue - fi - if safe_remove "$sfl_file" true; then - ((deleted++)) - fi - done - - echo -e "${GREEN}${ICON_SUCCESS}${NC} Shared file lists cleared${deleted:+ ($deleted files)}" - fi - - rm -f "$HOME/Library/Preferences/com.apple.recentitems.plist" 2> /dev/null || true - defaults delete NSGlobalDomain NSRecentDocumentsLimit 2> /dev/null || true - - echo -e "${GREEN}${ICON_SUCCESS}${NC} Recent items cleared" -} - -# Radio refresh: reset Bluetooth and Wi-Fi (safe mode - no pairing/password loss) +# Refresh wireless interfaces opt_radio_refresh() { - echo -e "${BLUE}${ICON_ARROW}${NC} Refreshing Bluetooth controller..." # Only restart Bluetooth service, do NOT delete pairing information - sudo pkill -HUP bluetoothd 2> /dev/null || true - echo -e "${GREEN}${ICON_SUCCESS}${NC} Bluetooth controller refreshed" + if sudo pkill -HUP bluetoothd 2> /dev/null; then + echo -e " ${GREEN}✓${NC} Bluetooth controller refreshed" + fi - echo -e "${BLUE}${ICON_ARROW}${NC} Refreshing Wi-Fi service..." local wifi_interface wifi_interface=$(networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}' | head -1) if [[ -n "$wifi_interface" ]]; then if sudo bash -c "trap '' INT TERM; ifconfig '$wifi_interface' down; sleep 1; ifconfig '$wifi_interface' up" 2> /dev/null; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} Wi-Fi interface restarted" - else - echo -e "${YELLOW}!${NC} Failed to restart Wi-Fi interface" + echo -e " ${GREEN}✓${NC} Wi-Fi interface reset" fi - else - echo -e "${GRAY}-${NC} Wi-Fi interface not found" fi # Restart AirDrop interface - # Use atomic execution to ensure interface comes back up even if interrupted sudo bash -c "trap '' INT TERM; ifconfig awdl0 down; ifconfig awdl0 up" 2> /dev/null || true - echo -e "${GREEN}${ICON_SUCCESS}${NC} Wireless services refreshed" -} - -# Mail downloads: clear OLD Mail attachment cache (30+ days) -opt_mail_downloads() { - local min_size_kb=${MOLE_MAIL_DOWNLOADS_MIN_KB:-5120} - local mail_age_days=${MOLE_MAIL_AGE_DAYS:-30} - if ! [[ "$min_size_kb" =~ ^[0-9]+$ ]]; then - min_size_kb=5120 - fi - if ! [[ "$mail_age_days" =~ ^[0-9]+$ ]]; then - mail_age_days=30 - fi - - echo -e "${BLUE}${ICON_ARROW}${NC} Clearing old Mail attachment downloads (${mail_age_days}+ days)..." - local -a mail_dirs=( - "$HOME/Library/Mail Downloads" - "$HOME/Library/Containers/com.apple.mail/Data/Library/Mail Downloads" - ) - - local total_size_kb=0 - local temp_dir - temp_dir=$(create_temp_dir) - - # Parallel size calculation - local idx=0 - for target_path in "${mail_dirs[@]}"; do - ( - local size - size=$(get_path_size_kb "$target_path") - echo "$size" > "$temp_dir/size_$idx" - ) & - ((idx++)) - done - wait - - for i in $(seq 0 $((idx - 1))); do - local size=0 - [[ -f "$temp_dir/size_$i" ]] && size=$(cat "$temp_dir/size_$i") - ((total_size_kb += size)) - done - - if [[ $total_size_kb -lt $min_size_kb ]]; then - echo -e "${GRAY}-${NC} Only $(bytes_to_human $((total_size_kb * 1024))) detected, skipping cleanup" - return - fi - local cleaned=false - for target_path in "${mail_dirs[@]}"; do - if [[ -d "$target_path" ]]; then - safe_find_delete "$target_path" "*" "$mail_age_days" "f" - cleaned=true - fi - done - - if [[ "$cleaned" == "true" ]]; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} Cleaned old attachments (> ${mail_age_days} days)" - else - echo -e "${GRAY}-${NC} No old attachments found" - fi + echo -e " ${GREEN}✓${NC} AirDrop service restarted" } # Saved state: remove OLD app saved states (7+ days) opt_saved_state_cleanup() { - echo -e "${BLUE}${ICON_ARROW}${NC} Removing old saved application states (${MOLE_SAVED_STATE_AGE_DAYS}+ days)..." local state_dir="$HOME/Library/Saved Application State" - if [[ ! -d "$state_dir" ]]; then - echo -e "${GRAY}-${NC} No saved states directory found" - return + if [[ -d "$state_dir" ]]; then + while IFS= read -r -d '' state_path; do + if should_protect_path "$state_path"; then + continue + fi + safe_remove "$state_path" true > /dev/null 2>&1 + done < <(command find "$state_dir" -type d -name "*.savedState" -mtime "+$MOLE_SAVED_STATE_AGE_DAYS" -print0 2> /dev/null) fi - local deleted=0 - while IFS= read -r -d '' state_path; do - if should_protect_path "$state_path"; then - continue - fi - if safe_remove "$state_path" true; then - ((deleted++)) - fi - done < <(command find "$state_dir" -type d -name "*.savedState" -mtime "+$MOLE_SAVED_STATE_AGE_DAYS" -print0 2> /dev/null) - - if [[ $deleted -gt 0 ]]; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} Removed $deleted old saved state(s)" - else - echo -e "${GRAY}-${NC} No old saved states found" - fi + echo -e " ${GREEN}✓${NC} App saved states optimized" } # Swap cleanup: reset swap files opt_swap_cleanup() { - echo -e "${BLUE}${ICON_ARROW}${NC} Removing swapfiles and resetting dynamic pager..." if sudo launchctl unload /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist > /dev/null 2>&1; then sudo launchctl load /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist > /dev/null 2>&1 || true - echo -e "${GREEN}${ICON_SUCCESS}${NC} Swap cache rebuilt" + echo -e " ${GREEN}✓${NC} Swap cache reset" else - echo -e "${YELLOW}!${NC} Could not unload dynamic_pager" + echo -e " ${YELLOW}!${NC} Failed to reset swap" fi } # Startup cache: rebuild kernel caches (handled automatically by modern macOS) opt_startup_cache() { - echo -e "${GRAY}-${NC} Startup cache rebuild skipped (handled by macOS)" + echo -e " ${GRAY}-${NC} Startup cache (auto-managed by macOS)" } # Local snapshots: thin Time Machine snapshots @@ -311,85 +173,35 @@ opt_local_snapshots() { fi } -# Developer cleanup: remove Xcode/simulator cruft -opt_developer_cleanup() { - local -a dev_targets=( - "$HOME/Library/Developer/Xcode/DerivedData|Xcode DerivedData" - "$HOME/Library/Developer/Xcode/iOS DeviceSupport|iOS Device support files" - "$HOME/Library/Developer/CoreSimulator/Caches|CoreSimulator caches" - ) - - for target in "${dev_targets[@]}"; do - IFS='|' read -r target_path label <<< "$target" - cleanup_path "$target_path" "$label" - done - - if command -v xcrun > /dev/null 2>&1; then - echo -e "${BLUE}${ICON_ARROW}${NC} Removing unavailable simulator runtimes..." - if xcrun simctl delete unavailable > /dev/null 2>&1; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} Unavailable simulators removed" - else - echo -e "${YELLOW}!${NC} Could not prune simulator runtimes" - fi - fi - - echo -e "${GREEN}${ICON_SUCCESS}${NC} Developer caches cleaned" -} - # Fix broken system configurations # Repairs corrupted preference files and broken login items opt_fix_broken_configs() { - local broken_prefs=0 - local broken_items=0 + local broken_prefs=$(fix_broken_preferences) + local broken_items=$(fix_broken_login_items) - # Fix broken preferences - echo -e "${BLUE}${ICON_ARROW}${NC} Checking preference files..." - broken_prefs=$(fix_broken_preferences) if [[ $broken_prefs -gt 0 ]]; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} Fixed $broken_prefs broken preference files" + echo -e " ${GREEN}✓${NC} Repaired $broken_prefs corrupted preference files" else - echo -e "${GREEN}${ICON_SUCCESS}${NC} All preference files valid" + echo -e " ${GREEN}✓${NC} All preference files valid" fi - # Fix broken login items - echo -e "${BLUE}${ICON_ARROW}${NC} Checking login items..." - broken_items=$(fix_broken_login_items) if [[ $broken_items -gt 0 ]]; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} Removed $broken_items broken login items" + echo -e " ${GREEN}✓${NC} Removed $broken_items broken login items" else - echo -e "${GREEN}${ICON_SUCCESS}${NC} All login items valid" - fi - - local total=$((broken_prefs + broken_items)) - if [[ $total -gt 0 ]]; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} System configuration repaired" + echo -e " ${GREEN}✓${NC} All login items functional" fi } -# Network Optimization: Flush DNS, reset mDNS, clear ARP +# Network cache optimization opt_network_optimization() { - echo -e "${BLUE}${ICON_ARROW}${NC} Optimizing network settings..." - local steps=0 + flush_dns_cache + echo -e " ${GREEN}✓${NC} DNS cache refreshed" - # 1. Flush DNS cache - if flush_dns_cache; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} DNS cache flushed" - ((steps++)) - fi - - # 2. Clear ARP cache (admin only) if sudo arp -d -a > /dev/null 2>&1; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} ARP cache cleared" - ((steps++)) + echo -e " ${GREEN}✓${NC} ARP cache rebuilt" fi - # 3. Reset network interface statistics (soft reset) - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Network interfaces refreshed" - ((steps++)) - - if [[ $steps -gt 0 ]]; then - echo -e "${GREEN}${ICON_SUCCESS}${NC} Network optimized" - fi + echo -e " ${GREEN}✓${NC} mDNSResponder optimized" } # Clean Spotlight user caches @@ -403,19 +215,15 @@ execute_optimization() { system_maintenance) opt_system_maintenance ;; cache_refresh) opt_cache_refresh ;; maintenance_scripts) opt_maintenance_scripts ;; - log_cleanup) opt_log_cleanup ;; - recent_items) opt_recent_items ;; radio_refresh) opt_radio_refresh ;; - mail_downloads) opt_mail_downloads ;; saved_state_cleanup) opt_saved_state_cleanup ;; swap_cleanup) opt_swap_cleanup ;; startup_cache) opt_startup_cache ;; local_snapshots) opt_local_snapshots ;; - developer_cleanup) opt_developer_cleanup ;; fix_broken_configs) opt_fix_broken_configs ;; network_optimization) opt_network_optimization ;; *) - echo -e "${RED}${ICON_ERROR}${NC} Unknown action: $action" + echo -e "${YELLOW}${ICON_ERROR}${NC} Unknown action: $action" return 1 ;; esac