1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-05 06:13:49 +00:00

feat(clean, optimize): enhance recent items cleanup safety

This commit is contained in:
Tw93
2025-12-18 17:19:18 +08:00
parent faf2c7b431
commit 8eeed7d079
10 changed files with 33 additions and 102 deletions

View File

@@ -258,6 +258,7 @@ select_purge_categories() {
fi
}
# shellcheck disable=SC2329
handle_interrupt() {
restore_terminal
exit 130

View File

@@ -76,6 +76,8 @@ clean_finder_metadata() {
# Clean macOS system caches
clean_macos_system_caches() {
# Clean saved application states with protection for System Settings
# Note: safe_clean already calls should_protect_path for each file
safe_clean ~/Library/Saved\ Application\ State/* "Saved application states"
# REMOVED: Spotlight cache cleanup can cause system UI issues

View File

@@ -519,6 +519,10 @@ should_protect_path() {
*/Library/Group\ Containers/com.apple.systempreferences* | */Library/Group\ Containers/com.apple.Settings*)
return 0
;;
# Shared file lists for System Settings (macOS Sequoia) - Issue #136
*/com.apple.sharedfilelist/*com.apple.Settings* | */com.apple.sharedfilelist/*com.apple.SystemSettings* | */com.apple.sharedfilelist/*systempreferences*)
return 0
;;
esac
# 3. Extract bundle ID from app container/group container paths

View File

@@ -11,7 +11,6 @@ set -euo pipefail
readonly MOLE_TM_THIN_TIMEOUT=180
readonly MOLE_TM_THIN_VALUE=9999999999
# Helper function: Flush DNS cache
flush_dns_cache() {
sudo dscacheutil -flushcache 2> /dev/null && sudo killall -HUP mDNSResponder 2> /dev/null
}
@@ -98,8 +97,24 @@ opt_recent_items() {
echo -e "${BLUE}${ICON_ARROW}${NC} Clearing recent items lists..."
local shared_dir="$HOME/Library/Application Support/com.apple.sharedfilelist"
if [[ -d "$shared_dir" ]]; then
safe_find_delete "$shared_dir" "*.sfl2" 0 "f"
echo -e "${GREEN}${ICON_SUCCESS}${NC} Shared file lists cleared"
# Use safe removal with protection checks instead of find -delete
# This prevents accidental deletion of System Settings files
local deleted=0
while IFS= read -r -d '' sfl_file; do
# Check if file should be protected (System Settings, etc.)
if should_protect_path "$sfl_file"; then
continue
fi
if safe_remove "$sfl_file" true; then
((deleted++))
fi
done < <(command find "$shared_dir" -maxdepth 5 -name "*.sfl2" -type f -print0 2> /dev/null)
if [[ $deleted -gt 0 ]]; then
echo -e "${GREEN}${ICON_SUCCESS}${NC} Shared file lists cleared ($deleted files)"
else
echo -e "${GREEN}${ICON_SUCCESS}${NC} Shared file lists cleared"
fi
fi
rm -f "$HOME/Library/Preferences/com.apple.recentitems.plist" 2> /dev/null || true
@@ -116,13 +131,9 @@ opt_radio_refresh() {
echo -e "${GREEN}${ICON_SUCCESS}${NC} Bluetooth controller refreshed"
echo -e "${BLUE}${ICON_ARROW}${NC} Refreshing Wi-Fi service..."
# Only restart Wi-Fi service, do NOT delete saved networks
# Safe alternative: just restart the Wi-Fi interface
local wifi_interface
wifi_interface=$(networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}' | head -1)
if [[ -n "$wifi_interface" ]]; then
# Use atomic execution to ensure interface comes back up even if interrupted
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
@@ -140,8 +151,6 @@ opt_radio_refresh() {
# Mail downloads: clear OLD Mail attachment cache (30+ days)
opt_mail_downloads() {
# Validate configuration parameters
# Validate configuration parameters
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
@@ -183,8 +192,6 @@ opt_mail_downloads() {
echo -e "${GRAY}-${NC} Only $(bytes_to_human $((total_size_kb * 1024))) detected, skipping cleanup"
return
fi
# Only delete old attachments (safety window)
local cleaned=false
for target_path in "${mail_dirs[@]}"; do
if [[ -d "$target_path" ]]; then
@@ -210,10 +217,8 @@ opt_saved_state_cleanup() {
return
fi
# Only delete old saved states (safety window)
local deleted=0
while IFS= read -r -d '' state_path; do
# Protect system critical components
if should_protect_path "$state_path"; then
continue
fi
@@ -233,7 +238,6 @@ opt_saved_state_cleanup() {
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
# Safe swap reset: just restart the pager, don't manually rm files
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"
else
@@ -241,10 +245,8 @@ opt_swap_cleanup() {
fi
}
# Startup cache: rebuild kernel caches
# Startup cache: rebuild kernel caches (handled automatically by modern macOS)
opt_startup_cache() {
# kextcache/PrelinkedKernel rebuilds are legacy and heavy.
# Modern macOS (Big Sur+) handles this automatically and securely (SSV).
echo -e "${GRAY}-${NC} Startup cache rebuild skipped (handled by macOS)"
}