diff --git a/bin/clean.sh b/bin/clean.sh index eacc6b0..7f790cd 100755 --- a/bin/clean.sh +++ b/bin/clean.sh @@ -271,6 +271,8 @@ safe_clean() { ( local size size=$(get_path_size_kb "$path") + # Ensure size is numeric (additional safety layer) + [[ ! "$size" =~ ^[0-9]+$ ]] && size=0 # Use index + PID for unique filename local tmp_file="$temp_dir/result_${idx}.$$" # Optimization: Skip expensive file counting. Size is the key metric. @@ -344,6 +346,8 @@ safe_clean() { for path in "${existing_paths[@]}"; do local size_bytes size_bytes=$(get_path_size_kb "$path") + # Ensure size_bytes is numeric (additional safety layer) + [[ ! "$size_bytes" =~ ^[0-9]+$ ]] && size_bytes=0 # Optimization: Skip expensive file counting if [[ "$size_bytes" -gt 0 ]]; then @@ -577,12 +581,31 @@ perform_cleanup() { done # Display whitelist status - if [[ $custom_count -gt 0 && $predefined_count -gt 0 ]]; then - echo -e "${BLUE}${ICON_SUCCESS}${NC} Whitelist: $predefined_count core + $custom_count custom patterns active" - elif [[ $custom_count -gt 0 ]]; then - echo -e "${BLUE}${ICON_SUCCESS}${NC} Whitelist: $custom_count custom patterns active" - elif [[ $predefined_count -gt 0 ]]; then - echo -e "${BLUE}${ICON_SUCCESS}${NC} Whitelist: $predefined_count core patterns active" + if [[ $custom_count -gt 0 || $predefined_count -gt 0 ]]; then + local summary="" + [[ $predefined_count -gt 0 ]] && summary+="$predefined_count core" + [[ $custom_count -gt 0 && $predefined_count -gt 0 ]] && summary+=" + " + [[ $custom_count -gt 0 ]] && summary+="$custom_count custom" + summary+=" patterns active" + + echo -e "${BLUE}${ICON_SUCCESS}${NC} Whitelist: $summary" + + # List custom patterns for verification + if [[ $custom_count -gt 0 ]]; then + for pattern in "${WHITELIST_PATTERNS[@]}"; do + local is_custom=true + for default in "${DEFAULT_WHITELIST_PATTERNS[@]}"; do + if [[ "$pattern" == "$default" ]]; then + is_custom=false + break + fi + done + + if [[ "$is_custom" == "true" ]]; then + echo -e " ${GRAY}→ Custom: $pattern${NC}" + fi + done + fi fi fi @@ -775,7 +798,8 @@ perform_cleanup() { fi # Free space now at the end - summary_details+=("Free space now: $(get_free_space)") + local final_free_space=$(get_free_space) + summary_details+=("Free space now: $final_free_space") fi else summary_status="info" diff --git a/lib/core/file_ops.sh b/lib/core/file_ops.sh index b279ba6..af6d82f 100644 --- a/lib/core/file_ops.sh +++ b/lib/core/file_ops.sh @@ -94,6 +94,12 @@ safe_remove() { return 0 fi + # Dry-run mode: log but don't delete + if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then + debug_log "[DRY RUN] Would remove: $path" + return 0 + fi + debug_log "Removing: $path" # Perform the deletion @@ -139,6 +145,12 @@ safe_sudo_remove() { return 1 fi + # Dry-run mode: log but don't delete + if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then + debug_log "[DRY RUN] Would remove (sudo): $path" + return 0 + fi + debug_log "Removing (sudo): $path" # Perform the deletion @@ -257,8 +269,14 @@ get_path_size_kb() { # Use || echo 0 to ensure failure in du (e.g. permission error) doesn't exit script under set -e # Pipefail would normally cause the pipeline to fail if du fails, but || handle catches it. local size - size=$(command du -sk "$path" 2> /dev/null | awk '{print $1}' || echo "0") - echo "${size:-0}" + size=$(command du -sk "$path" 2> /dev/null | awk '{print $1}' || true) + + # Ensure size is a valid number (fix for non-numeric du output) + if [[ "$size" =~ ^[0-9]+$ ]]; then + echo "$size" + else + echo "0" + fi } # Calculate total size for multiple paths