1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 20:54:50 +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:
Tw93
2025-12-30 14:38:02 +08:00
parent 21a921c986
commit 34d202eb01
2 changed files with 51 additions and 9 deletions

View File

@@ -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"

View File

@@ -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