1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-23 14:10:07 +00:00

fix: preserve interrupt semantics and restore purge traps

This commit is contained in:
tw93
2026-02-27 11:18:53 +08:00
parent 194fe871e5
commit a9433e4acd
5 changed files with 81 additions and 0 deletions

View File

@@ -569,16 +569,38 @@ select_purge_categories() {
fi
done
local original_stty=""
local previous_exit_trap=""
local previous_int_trap=""
local previous_term_trap=""
local terminal_restored=false
if [[ -t 0 ]] && command -v stty > /dev/null 2>&1; then
original_stty=$(stty -g 2> /dev/null || echo "")
fi
previous_exit_trap=$(trap -p EXIT || true)
previous_int_trap=$(trap -p INT || true)
previous_term_trap=$(trap -p TERM || true)
# Terminal control functions
restore_terminal() {
# Avoid trap churn when restore is called repeatedly via RETURN/EXIT paths.
if [[ "${terminal_restored:-false}" == "true" ]]; then
return
fi
terminal_restored=true
trap - EXIT INT TERM
show_cursor
if [[ -n "${original_stty:-}" ]]; then
stty "${original_stty}" 2> /dev/null || stty sane 2> /dev/null || true
fi
if [[ -n "$previous_exit_trap" ]]; then
eval "$previous_exit_trap"
fi
if [[ -n "$previous_int_trap" ]]; then
eval "$previous_int_trap"
fi
if [[ -n "$previous_term_trap" ]]; then
eval "$previous_term_trap"
fi
}
# shellcheck disable=SC2329
handle_interrupt() {

View File

@@ -249,6 +249,11 @@ safe_remove() {
local rm_exit=0
error_msg=$(rm -rf "$path" 2>&1) || rm_exit=$? # safe_remove
# Preserve interrupt semantics so callers can abort long-running deletions.
if [[ $rm_exit -ge 128 ]]; then
return "$rm_exit"
fi
if [[ $rm_exit -eq 0 ]]; then
# Log successful removal
log_operation "${MOLE_CURRENT_COMMAND:-clean}" "REMOVED" "$path" "$size_human"