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

Simplify the debug code

This commit is contained in:
Tw93
2025-12-06 12:40:07 +08:00
parent 68e1281d82
commit 3b59920bab
9 changed files with 18 additions and 110 deletions

View File

@@ -179,6 +179,8 @@ safe_remove() {
return 0
fi
debug_log "Removing: $path"
# Perform the deletion (log only on error)
if rm -rf "$path" 2> /dev/null; then
return 0
@@ -210,6 +212,8 @@ safe_sudo_remove() {
return 1
fi
debug_log "Removing (sudo): $path"
# Perform the deletion (log only on error)
if sudo rm -rf "$path" 2> /dev/null; then
return 0
@@ -245,6 +249,8 @@ safe_find_delete() {
fi
# Execute find with safety limits
debug_log "Finding in $base_dir: $pattern (age: ${age_days}d, type: $type_filter)"
command find "$base_dir" \
-maxdepth 3 \
-name "$pattern" \
@@ -281,6 +287,8 @@ safe_sudo_find_delete() {
fi
# Execute find with safety limits
debug_log "Finding (sudo) in $base_dir: $pattern (age: ${age_days}d, type: $type_filter)"
sudo command find "$base_dir" \
-maxdepth 3 \
-name "$pattern" \
@@ -332,7 +340,7 @@ log_error() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1" >> "$LOG_FILE" 2> /dev/null || true
}
# Debug logging - only shown when MO_DEBUG=1
# Debug logging - shown when MO_DEBUG=1
debug_log() {
if [[ "${MO_DEBUG:-}" == "1" ]]; then
echo -e "${GRAY}[DEBUG]${NC} $*" >&2
@@ -548,11 +556,7 @@ run_with_timeout() {
local duration="${1:-0}"
shift || true
if [[ ! "$duration" =~ ^[0-9]+$ ]]; then
duration=0
fi
if [[ "$duration" -le 0 ]]; then
if [[ ! "$duration" =~ ^[0-9]+$ ]] || [[ "$duration" -le 0 ]]; then
"$@"
return $?
fi
@@ -565,40 +569,18 @@ run_with_timeout() {
"$@" &
local cmd_pid=$!
# Create temp marker to track timeout
local timeout_marker
timeout_marker=$(mktemp 2> /dev/null || echo "/tmp/mole-timeout-$$")
# Killer: sleep then kill command if still running
(
sleep "$duration"
if kill -0 "$cmd_pid" 2> /dev/null; then
echo "1" > "$timeout_marker" 2> /dev/null || true
kill -TERM "$cmd_pid" 2> /dev/null || true
sleep 0.5
kill -KILL "$cmd_pid" 2> /dev/null || true
fi
) &
(sleep "$duration"; kill -TERM "$cmd_pid" 2> /dev/null || true) &
local killer_pid=$!
# Wait for command to finish (disable errexit temporarily to prevent exit on wait failure)
local exit_code
set +e
wait "$cmd_pid" 2> /dev/null
exit_code=$?
set -e
# Kill the killer if command finished early
kill "$killer_pid" 2> /dev/null || true
wait "$killer_pid" 2> /dev/null || true
# Check if timeout occurred
if [[ -f "$timeout_marker" ]] && [[ "$(cat "$timeout_marker" 2> /dev/null)" == "1" ]]; then
rm -f "$timeout_marker" 2> /dev/null || true
return 124
fi
rm -f "$timeout_marker" 2> /dev/null || true
return "$exit_code"
}

View File

@@ -11,8 +11,6 @@ MOLE_SUDO_ESTABLISHED="false"
# Start sudo keepalive background process
# Returns: PID of keepalive process
_start_sudo_keepalive() {
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: _start_sudo_keepalive: starting background process..." >&2
# Start background keepalive process with all outputs redirected
# This is critical: command substitution waits for all file descriptors to close
(
@@ -37,7 +35,6 @@ _start_sudo_keepalive() {
) > /dev/null 2>&1 &
local pid=$!
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: _start_sudo_keepalive: background PID = $pid" >&2
echo $pid
}
@@ -61,21 +58,14 @@ has_sudo_session() {
request_sudo() {
local prompt_msg="${1:-Admin access required}"
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: request_sudo: checking existing session..."
if has_sudo_session; then
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: request_sudo: session already exists"
return 0
fi
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: request_sudo: calling request_sudo_access from common.sh..."
# Use the robust implementation from common.sh
if request_sudo_access "$prompt_msg"; then
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: request_sudo: request_sudo_access succeeded"
return 0
else
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: request_sudo: request_sudo_access failed"
return 1
fi
}
@@ -85,39 +75,26 @@ request_sudo() {
ensure_sudo_session() {
local prompt="${1:-Admin access required}"
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: ensure_sudo_session called"
# Check if already established
if has_sudo_session && [[ "$MOLE_SUDO_ESTABLISHED" == "true" ]]; then
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: Sudo session already active"
return 0
fi
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: Checking for old keepalive..."
# Stop old keepalive if exists
if [[ -n "$MOLE_SUDO_KEEPALIVE_PID" ]]; then
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: Stopping old keepalive PID $MOLE_SUDO_KEEPALIVE_PID"
_stop_sudo_keepalive "$MOLE_SUDO_KEEPALIVE_PID"
MOLE_SUDO_KEEPALIVE_PID=""
fi
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: Calling request_sudo..."
# Request sudo access
if ! request_sudo "$prompt"; then
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: request_sudo failed"
MOLE_SUDO_ESTABLISHED="false"
return 1
fi
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: request_sudo succeeded, starting keepalive..."
# Start keepalive
MOLE_SUDO_KEEPALIVE_PID=$(_start_sudo_keepalive)
[[ "${MO_DEBUG:-}" == "1" ]] && echo "DEBUG: Keepalive started with PID $MOLE_SUDO_KEEPALIVE_PID"
MOLE_SUDO_ESTABLISHED="true"
return 0
}

View File

@@ -48,7 +48,6 @@ decode_file_list() {
# Args: $1 = bundle_id, $2 = has_system_files (true/false)
stop_launch_services() {
local bundle_id="$1"
debug_log "stop_launch_services: Stopping services for $bundle_id"
local has_system_files="${2:-false}"
# User-level Launch Agents