1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-12 19:15:13 +00:00

fix: Improve shell script robustness by adding variable validation and safer du output parsing.

This commit is contained in:
Tw93
2025-12-30 18:07:48 +08:00
parent fcde129d2f
commit bb49ec3170
4 changed files with 14 additions and 7 deletions

View File

@@ -122,7 +122,7 @@ whitelist_skipped_count=0
# shellcheck disable=SC2329 # shellcheck disable=SC2329
note_activity() { note_activity() {
if [[ $TRACK_SECTION -eq 1 ]]; then if [[ "${TRACK_SECTION:-0}" == "1" ]]; then
SECTION_ACTIVITY=1 SECTION_ACTIVITY=1
fi fi
} }
@@ -183,7 +183,7 @@ start_section() {
end_section() { end_section() {
stop_section_spinner stop_section_spinner
if [[ $TRACK_SECTION -eq 1 && $SECTION_ACTIVITY -eq 0 ]]; then if [[ "${TRACK_SECTION:-0}" == "1" && "${SECTION_ACTIVITY:-0}" == "0" ]]; then
echo -e " ${GREEN}${ICON_SUCCESS}${NC} Nothing to clean" echo -e " ${GREEN}${ICON_SUCCESS}${NC} Nothing to clean"
fi fi
TRACK_SECTION=0 TRACK_SECTION=0
@@ -275,7 +275,7 @@ safe_clean() {
return 0 return 0
fi fi
if [[ $TRACK_SECTION -eq 1 && $SECTION_ACTIVITY -eq 0 ]]; then if [[ "${TRACK_SECTION:-0}" == "1" && "${SECTION_ACTIVITY:-0}" == "0" ]]; then
stop_section_spinner stop_section_spinner
fi fi

View File

@@ -131,7 +131,14 @@ clean_mail_downloads() {
if [[ -d "$target_path" ]]; then if [[ -d "$target_path" ]]; then
local dir_size_kb=0 local dir_size_kb=0
dir_size_kb=$(get_path_size_kb "$target_path") dir_size_kb=$(get_path_size_kb "$target_path")
if [[ $dir_size_kb -lt ${MOLE_MAIL_DOWNLOADS_MIN_KB:-5120} ]]; then if ! [[ "$dir_size_kb" =~ ^[0-9]+$ ]]; then
dir_size_kb=0
fi
local min_kb="${MOLE_MAIL_DOWNLOADS_MIN_KB:-5120}"
if ! [[ "$min_kb" =~ ^[0-9]+$ ]]; then
min_kb=5120
fi
if [[ "$dir_size_kb" -lt "$min_kb" ]]; then
continue continue
fi fi
while IFS= read -r -d '' file_path; do while IFS= read -r -d '' file_path; do

View File

@@ -569,7 +569,7 @@ start_section() {
# End a section # End a section
# Shows "Nothing to tidy" if no activity was recorded # Shows "Nothing to tidy" if no activity was recorded
end_section() { end_section() {
if [[ $TRACK_SECTION -eq 1 && $SECTION_ACTIVITY -eq 0 ]]; then if [[ "${TRACK_SECTION:-0}" == "1" && "${SECTION_ACTIVITY:-0}" == "0" ]]; then
echo -e " ${GREEN}${ICON_SUCCESS}${NC} Nothing to tidy" echo -e " ${GREEN}${ICON_SUCCESS}${NC} Nothing to tidy"
fi fi
TRACK_SECTION=0 TRACK_SECTION=0
@@ -577,7 +577,7 @@ end_section() {
# Mark activity in current section # Mark activity in current section
note_activity() { note_activity() {
if [[ $TRACK_SECTION -eq 1 ]]; then if [[ "${TRACK_SECTION:-0}" == "1" ]]; then
SECTION_ACTIVITY=1 SECTION_ACTIVITY=1
fi fi
} }

View File

@@ -269,7 +269,7 @@ 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}' || true) size=$(command du -sk "$path" 2> /dev/null | awk 'NR==1 {print $1; exit}' || true)
# Ensure size is a valid number (fix for non-numeric du output) # Ensure size is a valid number (fix for non-numeric du output)
if [[ "$size" =~ ^[0-9]+$ ]]; then if [[ "$size" =~ ^[0-9]+$ ]]; then