mirror of
https://github.com/tw93/Mole.git
synced 2026-02-12 07:38:59 +00:00
fix: resolve syntax error in size calculation (Issue #203)
- Add numeric validation for size variables in safe_clean() - Fix get_path_size_kb() to handle non-numeric du output - Display custom whitelist patterns in dry-run output (Issue #206)
This commit is contained in:
38
bin/clean.sh
38
bin/clean.sh
@@ -271,6 +271,8 @@ safe_clean() {
|
|||||||
(
|
(
|
||||||
local size
|
local size
|
||||||
size=$(get_path_size_kb "$path")
|
size=$(get_path_size_kb "$path")
|
||||||
|
# Ensure size is numeric (additional safety layer)
|
||||||
|
[[ ! "$size" =~ ^[0-9]+$ ]] && size=0
|
||||||
# Use index + PID for unique filename
|
# Use index + PID for unique filename
|
||||||
local tmp_file="$temp_dir/result_${idx}.$$"
|
local tmp_file="$temp_dir/result_${idx}.$$"
|
||||||
# Optimization: Skip expensive file counting. Size is the key metric.
|
# Optimization: Skip expensive file counting. Size is the key metric.
|
||||||
@@ -344,6 +346,8 @@ safe_clean() {
|
|||||||
for path in "${existing_paths[@]}"; do
|
for path in "${existing_paths[@]}"; do
|
||||||
local size_bytes
|
local size_bytes
|
||||||
size_bytes=$(get_path_size_kb "$path")
|
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
|
# Optimization: Skip expensive file counting
|
||||||
if [[ "$size_bytes" -gt 0 ]]; then
|
if [[ "$size_bytes" -gt 0 ]]; then
|
||||||
@@ -577,12 +581,31 @@ perform_cleanup() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
# Display whitelist status
|
# Display whitelist status
|
||||||
if [[ $custom_count -gt 0 && $predefined_count -gt 0 ]]; then
|
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"
|
local summary=""
|
||||||
elif [[ $custom_count -gt 0 ]]; then
|
[[ $predefined_count -gt 0 ]] && summary+="$predefined_count core"
|
||||||
echo -e "${BLUE}${ICON_SUCCESS}${NC} Whitelist: $custom_count custom patterns active"
|
[[ $custom_count -gt 0 && $predefined_count -gt 0 ]] && summary+=" + "
|
||||||
elif [[ $predefined_count -gt 0 ]]; then
|
[[ $custom_count -gt 0 ]] && summary+="$custom_count custom"
|
||||||
echo -e "${BLUE}${ICON_SUCCESS}${NC} Whitelist: $predefined_count core patterns active"
|
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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -775,7 +798,8 @@ perform_cleanup() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Free space now at the end
|
# 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
|
fi
|
||||||
else
|
else
|
||||||
summary_status="info"
|
summary_status="info"
|
||||||
|
|||||||
@@ -94,6 +94,12 @@ safe_remove() {
|
|||||||
return 0
|
return 0
|
||||||
fi
|
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"
|
debug_log "Removing: $path"
|
||||||
|
|
||||||
# Perform the deletion
|
# Perform the deletion
|
||||||
@@ -139,6 +145,12 @@ safe_sudo_remove() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
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"
|
debug_log "Removing (sudo): $path"
|
||||||
|
|
||||||
# Perform the deletion
|
# 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
|
# 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.
|
# Pipefail would normally cause the pipeline to fail if du fails, but || handle catches it.
|
||||||
local size
|
local size
|
||||||
size=$(command du -sk "$path" 2> /dev/null | awk '{print $1}' || echo "0")
|
size=$(command du -sk "$path" 2> /dev/null | awk '{print $1}' || true)
|
||||||
echo "${size:-0}"
|
|
||||||
|
# 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
|
# Calculate total size for multiple paths
|
||||||
|
|||||||
Reference in New Issue
Block a user