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

fix: avoid orphan service false positives and warm large file threshold

This commit is contained in:
Tw93
2026-01-23 20:16:06 +08:00
parent 500b6dc554
commit 83166f2fe8
3 changed files with 68 additions and 13 deletions

View File

@@ -3,18 +3,19 @@ package main
import "time" import "time"
const ( const (
maxEntries = 30 maxEntries = 30
maxLargeFiles = 20 maxLargeFiles = 20
barWidth = 24 barWidth = 24
spotlightMinFileSize = 100 << 20 spotlightMinFileSize = 100 << 20
defaultViewport = 12 largeFileWarmupMinSize = 1 << 20
overviewCacheTTL = 7 * 24 * time.Hour defaultViewport = 12
overviewCacheFile = "overview_sizes.json" overviewCacheTTL = 7 * 24 * time.Hour
duTimeout = 30 * time.Second overviewCacheFile = "overview_sizes.json"
mdlsTimeout = 5 * time.Second duTimeout = 30 * time.Second
maxConcurrentOverview = 8 mdlsTimeout = 5 * time.Second
batchUpdateSize = 100 maxConcurrentOverview = 8
cacheModTimeGrace = 30 * time.Minute batchUpdateSize = 100
cacheModTimeGrace = 30 * time.Minute
// Worker pool limits. // Worker pool limits.
minWorkers = 16 minWorkers = 16

View File

@@ -37,7 +37,7 @@ func scanPathConcurrent(root string, filesScanned, dirsScanned, bytesScanned *in
largeFilesHeap := &largeFileHeap{} largeFilesHeap := &largeFileHeap{}
heap.Init(largeFilesHeap) heap.Init(largeFilesHeap)
largeFileMinSize := int64(0) largeFileMinSize := int64(largeFileWarmupMinSize)
// Worker pool sized for I/O-bound scanning. // Worker pool sized for I/O-bound scanning.
numWorkers := max(runtime.NumCPU()*cpuMultiplier, minWorkers) numWorkers := max(runtime.NumCPU()*cpuMultiplier, minWorkers)

View File

@@ -345,6 +345,50 @@ clean_orphaned_system_services() {
"cn.i4tools.*:/Applications/i4Tools.app" "cn.i4tools.*:/Applications/i4Tools.app"
) )
local mdfind_cache_file=""
_system_service_app_exists() {
local bundle_id="$1"
local app_path="$2"
[[ -n "$app_path" && -d "$app_path" ]] && return 0
if [[ -n "$app_path" ]]; then
local app_name
app_name=$(basename "$app_path")
case "$app_path" in
/Applications/*)
[[ -d "$HOME/Applications/$app_name" ]] && return 0
[[ -d "/Applications/Setapp/$app_name" ]] && return 0
;;
/Library/Input\ Methods/*)
[[ -d "$HOME/Library/Input Methods/$app_name" ]] && return 0
;;
esac
fi
if [[ -n "$bundle_id" ]] && [[ "$bundle_id" =~ ^[a-zA-Z0-9._-]+$ ]] && [[ ${#bundle_id} -ge 5 ]]; then
if [[ -z "$mdfind_cache_file" ]]; then
mdfind_cache_file=$(mktemp "${TMPDIR:-/tmp}/mole_mdfind_cache.XXXXXX")
register_temp_file "$mdfind_cache_file"
fi
if grep -Fxq "FOUND:$bundle_id" "$mdfind_cache_file" 2> /dev/null; then
return 0
fi
if ! grep -Fxq "NOTFOUND:$bundle_id" "$mdfind_cache_file" 2> /dev/null; then
local app_found
app_found=$(run_with_timeout 2 mdfind "kMDItemCFBundleIdentifier == '$bundle_id'" 2> /dev/null | head -1 || echo "")
if [[ -n "$app_found" ]]; then
echo "FOUND:$bundle_id" >> "$mdfind_cache_file"
return 0
fi
echo "NOTFOUND:$bundle_id" >> "$mdfind_cache_file"
fi
fi
return 1
}
# Scan system LaunchDaemons # Scan system LaunchDaemons
if [[ -d /Library/LaunchDaemons ]]; then if [[ -d /Library/LaunchDaemons ]]; then
while IFS= read -r -d '' plist; do while IFS= read -r -d '' plist; do
@@ -364,6 +408,9 @@ clean_orphaned_system_services() {
# shellcheck disable=SC2053 # shellcheck disable=SC2053
if [[ "$bundle_id" == $file_pattern ]] && [[ ! -d "$app_path" ]]; then if [[ "$bundle_id" == $file_pattern ]] && [[ ! -d "$app_path" ]]; then
if _system_service_app_exists "$bundle_id" "$app_path"; then
continue
fi
orphaned_files+=("$plist") orphaned_files+=("$plist")
local size_kb local size_kb
size_kb=$(sudo du -sk "$plist" 2> /dev/null | awk '{print $1}' || echo "0") size_kb=$(sudo du -sk "$plist" 2> /dev/null | awk '{print $1}' || echo "0")
@@ -392,6 +439,9 @@ clean_orphaned_system_services() {
# shellcheck disable=SC2053 # shellcheck disable=SC2053
if [[ "$bundle_id" == $file_pattern ]] && [[ ! -d "$app_path" ]]; then if [[ "$bundle_id" == $file_pattern ]] && [[ ! -d "$app_path" ]]; then
if _system_service_app_exists "$bundle_id" "$app_path"; then
continue
fi
orphaned_files+=("$plist") orphaned_files+=("$plist")
local size_kb local size_kb
size_kb=$(sudo du -sk "$plist" 2> /dev/null | awk '{print $1}' || echo "0") size_kb=$(sudo du -sk "$plist" 2> /dev/null | awk '{print $1}' || echo "0")
@@ -408,6 +458,7 @@ clean_orphaned_system_services() {
while IFS= read -r -d '' helper; do while IFS= read -r -d '' helper; do
local filename local filename
filename=$(basename "$helper") filename=$(basename "$helper")
local bundle_id="$filename"
# Skip Apple system files # Skip Apple system files
[[ "$filename" == com.apple.* ]] && continue [[ "$filename" == com.apple.* ]] && continue
@@ -418,6 +469,9 @@ clean_orphaned_system_services() {
# shellcheck disable=SC2053 # shellcheck disable=SC2053
if [[ "$filename" == $file_pattern ]] && [[ ! -d "$app_path" ]]; then if [[ "$filename" == $file_pattern ]] && [[ ! -d "$app_path" ]]; then
if _system_service_app_exists "$bundle_id" "$app_path"; then
continue
fi
orphaned_files+=("$helper") orphaned_files+=("$helper")
local size_kb local size_kb
size_kb=$(sudo du -sk "$helper" 2> /dev/null | awk '{print $1}' || echo "0") size_kb=$(sudo du -sk "$helper" 2> /dev/null | awk '{print $1}' || echo "0")