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

feat: remove SQLite vacuum optimization, enhance CJK/emoji width calculation, and improve system cleanup and UI feedback.

This commit is contained in:
Tw93
2025-12-29 00:29:42 +08:00
parent 63b915b234
commit b67204f959
12 changed files with 57 additions and 407 deletions

View File

@@ -128,8 +128,6 @@ EOF
items+=('maintenance_scripts|System Log Rotation|Rotate and compress system logs with newsyslog|true')
items+=('swap_cleanup|Virtual Memory Refresh|Reset swap files and dynamic pager service|true')
items+=('network_optimization|Network Stack Optimization|Refresh DNS, rebuild ARP & restart mDNSResponder|true')
items+=('sqlite_vacuum|SQLite Database Optimization|Optimize user databases with VACUUM to reduce size|true')
# Output items as JSON
local first=true
for item in "${items[@]}"; do

View File

@@ -155,29 +155,6 @@ clean_deep_system() {
fi
fi
debug_log "Core symbolication cache section completed"
# Clean Aliyun/DingTalk security component logs (if exists, can be 2-3GB)
# Only clean if the software is installed
debug_log "Checking Aliyun security components..."
local ali_cleaned=0
# List of Aliyun-related paths to clean
local -a ali_paths=(
"/private/var/root/Library/Application Support/ali_bas/bas_http_info/ali_bas_httpclient"
"/private/var/root/Library/Application Support/Aliedr/logs_dir"
"/private/var/root/Library/Application Support/Aliedr/cache"
)
# Clean each path if exists
for ali_path in "${ali_paths[@]}"; do
if sudo test -e "$ali_path" 2> /dev/null; then
debug_log "Found Aliyun component: $ali_path, removing..."
safe_sudo_remove "$ali_path" && ((ali_cleaned++)) || true
fi
done
debug_log "Aliyun security components check completed, cleaned: $ali_cleaned"
[[ $ali_cleaned -gt 0 ]] && log_success "Aliyun security component data"
}
# Clean incomplete Time Machine backups

View File

@@ -520,6 +520,11 @@ should_protect_path() {
return 0
fi
# Protect Notes cache (search index issues)
if [[ "$path_lower" =~ com\.apple\.notes ]]; then
return 0
fi
# 2. Protect caches critical for system UI rendering
# These caches are essential for modern macOS (Sonoma/Sequoia) system UI rendering
case "$path" in

View File

@@ -429,11 +429,18 @@ bytes_to_human_kb() {
get_brand_name() {
local name="$1"
# Detect if system primary language is Chinese
local is_chinese=false
local sys_lang
sys_lang=$(defaults read -g AppleLanguages 2> /dev/null | grep -o 'zh-Hans\|zh-Hant\|zh' | head -1 || echo "")
[[ -n "$sys_lang" ]] && is_chinese=true
# Detect if system primary language is Chinese (Cached)
if [[ -z "${MOLE_IS_CHINESE_SYSTEM:-}" ]]; then
local sys_lang
sys_lang=$(defaults read -g AppleLanguages 2> /dev/null | grep -o 'zh-Hans\|zh-Hant\|zh' | head -1 || echo "")
if [[ -n "$sys_lang" ]]; then
export MOLE_IS_CHINESE_SYSTEM="true"
else
export MOLE_IS_CHINESE_SYSTEM="false"
fi
fi
local is_chinese="${MOLE_IS_CHINESE_SYSTEM}"
# Return localized names based on system language
if [[ "$is_chinese" == true ]]; then

View File

@@ -284,7 +284,7 @@ stop_inline_spinner() {
# Try graceful TERM first, then force KILL if needed
if kill -0 "$INLINE_SPINNER_PID" 2> /dev/null; then
kill -TERM "$INLINE_SPINNER_PID" 2> /dev/null || true
sleep 0.05 2> /dev/null || true
sleep 0.1 2> /dev/null || true
# Force kill if still running
if kill -0 "$INLINE_SPINNER_PID" 2> /dev/null; then
kill -KILL "$INLINE_SPINNER_PID" 2> /dev/null || true

View File

@@ -69,7 +69,20 @@ opt_cache_refresh() {
# Run periodic maintenance scripts
opt_maintenance_scripts() {
if [[ -t 1 ]]; then
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Rotating logs..."
fi
local success=false
if run_with_timeout 120 sudo newsyslog > /dev/null 2>&1; then
success=true
fi
if [[ -t 1 ]]; then
stop_inline_spinner
fi
if [[ "$success" == "true" ]]; then
echo -e " ${GREEN}${NC} System logs rotated"
else
echo -e " ${YELLOW}!${NC} Failed to rotate logs"
@@ -204,96 +217,6 @@ opt_network_optimization() {
echo -e " ${GREEN}${NC} mDNSResponder optimized"
}
# SQLite database optimization
# Runs VACUUM on safe user-level databases to reduce size and improve performance
# Only operates on databases that are not in use and meet size threshold
opt_sqlite_vacuum() {
local optimized_count=0
local total_saved_kb=0
local min_size_kb=1024 # Only optimize databases larger than 1MB
# List of safe databases to optimize
# Exclude critical system databases like Mail, Messages, Photos
local -a safe_databases=(
"$HOME/Library/Safari/History.db"
"$HOME/Library/Safari/CloudTabs.db"
)
if [[ -t 1 ]]; then
MOLE_SPINNER_PREFIX=" " start_inline_spinner "Scanning databases..."
fi
for db_path in "${safe_databases[@]}"; do
# Check if database exists
[[ ! -f "$db_path" ]] && continue
# Check if it's a SQLite database
if ! file "$db_path" 2> /dev/null | grep -q "SQLite"; then
continue
fi
# Check size threshold
local db_size_kb
db_size_kb=$(get_path_size_kb "$db_path")
[[ $db_size_kb -lt $min_size_kb ]] && continue
# Check if database is in use
if lsof "$db_path" > /dev/null 2>&1; then
debug_log "Skipping $db_path - in use"
continue
fi
# Check disk space (VACUUM needs ~2x database size as temporary space)
local free_space_kb
free_space_kb=$(df -k "$(dirname "$db_path")" | tail -1 | awk '{print $4}')
if [[ $free_space_kb -lt $((db_size_kb * 2)) ]]; then
debug_log "Skipping $db_path - insufficient disk space"
continue
fi
# Verify database integrity before VACUUM
if ! sqlite3 "$db_path" "PRAGMA integrity_check;" 2> /dev/null | grep -q "ok"; then
debug_log "Skipping $db_path - integrity check failed"
continue
fi
# Get size before optimization
local size_before=$db_size_kb
# Perform VACUUM with error handling
if sqlite3 "$db_path" "VACUUM;" 2> /dev/null; then
# Verify database integrity after VACUUM
if ! sqlite3 "$db_path" "PRAGMA integrity_check;" 2> /dev/null | grep -q "ok"; then
debug_log "Warning: $db_path integrity check failed after VACUUM"
continue
fi
# Get size after optimization
local size_after
size_after=$(get_path_size_kb "$db_path")
local saved_kb=$((size_before - size_after))
if [[ $saved_kb -gt 0 ]]; then
((optimized_count++))
((total_saved_kb += saved_kb))
fi
else
debug_log "Failed to VACUUM $db_path"
fi
done
if [[ -t 1 ]]; then
stop_inline_spinner
fi
if [[ $optimized_count -gt 0 ]]; then
local saved_human=$(bytes_to_human "$((total_saved_kb * 1024))")
echo -e " ${GREEN}${NC} Optimized $optimized_count databases ($saved_human saved)"
else
echo -e " ${GREEN}${NC} Databases already optimized"
fi
}
# Clean Spotlight user caches
# Execute optimization by action name
@@ -312,7 +235,6 @@ execute_optimization() {
local_snapshots) opt_local_snapshots ;;
fix_broken_configs) opt_fix_broken_configs ;;
network_optimization) opt_network_optimization ;;
sqlite_vacuum) opt_sqlite_vacuum ;;
*)
echo -e "${YELLOW}${ICON_ERROR}${NC} Unknown action: $action"
return 1