From cd5baf9a728f0dd3094909aab0af95c98acb943c Mon Sep 17 00:00:00 2001 From: Tw93 Date: Sun, 4 Jan 2026 17:30:36 +0800 Subject: [PATCH] feat(debug): enhance file removal with detailed debug output - Add file metadata logging in safe_remove() and safe_sudo_remove() - Log file type, size, and age when in debug mode - Support both dry-run and actual removal scenarios - Part of GitHub issue #242 implementation --- lib/core/file_ops.sh | 61 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/core/file_ops.sh b/lib/core/file_ops.sh index 23a9463..bb41170 100644 --- a/lib/core/file_ops.sh +++ b/lib/core/file_ops.sh @@ -96,7 +96,36 @@ safe_remove() { # Dry-run mode: log but don't delete if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then - debug_log "[DRY RUN] Would remove: $path" + if [[ "${MO_DEBUG:-}" == "1" ]]; then + local file_type="file" + [[ -d "$path" ]] && file_type="directory" + [[ -L "$path" ]] && file_type="symlink" + + local file_size="" + local file_age="" + + if [[ -e "$path" ]]; then + local size_kb + size_kb=$(get_path_size_kb "$path" 2>/dev/null || echo "0") + if [[ "$size_kb" -gt 0 ]]; then + file_size=$(bytes_to_human "$((size_kb * 1024))") + fi + + if [[ -f "$path" || -d "$path" ]] && ! [[ -L "$path" ]]; then + local mod_time + mod_time=$(stat -f%m "$path" 2>/dev/null || echo "0") + local now + now=$(date +%s 2>/dev/null || echo "0") + if [[ "$mod_time" -gt 0 && "$now" -gt 0 ]]; then + file_age=$(( (now - mod_time) / 86400 )) + fi + fi + fi + + debug_file_action "[DRY RUN] Would remove" "$path" "$file_size" "$file_age" + else + debug_log "[DRY RUN] Would remove: $path" + fi return 0 fi @@ -147,7 +176,35 @@ safe_sudo_remove() { # Dry-run mode: log but don't delete if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then - debug_log "[DRY RUN] Would remove (sudo): $path" + if [[ "${MO_DEBUG:-}" == "1" ]]; then + local file_type="file" + [[ -d "$path" ]] && file_type="directory" + + local file_size="" + local file_age="" + + if sudo test -e "$path" 2>/dev/null; then + local size_kb + size_kb=$(sudo du -sk "$path" 2>/dev/null | awk '{print $1}' || echo "0") + if [[ "$size_kb" -gt 0 ]]; then + file_size=$(bytes_to_human "$((size_kb * 1024))") + fi + + if sudo test -f "$path" 2>/dev/null || sudo test -d "$path" 2>/dev/null; then + local mod_time + mod_time=$(sudo stat -f%m "$path" 2>/dev/null || echo "0") + local now + now=$(date +%s 2>/dev/null || echo "0") + if [[ "$mod_time" -gt 0 && "$now" -gt 0 ]]; then + file_age=$(( (now - mod_time) / 86400 )) + fi + fi + fi + + debug_file_action "[DRY RUN] Would remove (sudo)" "$path" "$file_size" "$file_age" + else + debug_log "[DRY RUN] Would remove (sudo): $path" + fi return 0 fi