From 3c0ec5704bbe489c4f91744e0713125fdae99002 Mon Sep 17 00:00:00 2001 From: Cebtenzzre Date: Sat, 27 Jun 2020 12:56:09 -0400 Subject: [PATCH] sh format: improve stampfile handling - mktemp in /tmp - don't assume $0 is a file - remove stampfiles at exit - don't create stampfiles if -n is passed Closes #554 --- lib/formats/sh.sh | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/formats/sh.sh b/lib/formats/sh.sh index ee956eeb..a8405350 100644 --- a/lib/formats/sh.sh +++ b/lib/formats/sh.sh @@ -41,6 +41,10 @@ DO_KEEP_DIR_TIMESTAMPS= # Set to true on -i DO_ASK_BEFORE_DELETE= +# Tempfiles for saving timestamps +STAMPFILE= +STAMPFILE2= + ################################## # GENERAL LINT HANDLER FUNCTIONS # ################################## @@ -51,6 +55,19 @@ COL_GREEN='\e[0;32m' COL_YELLOW='\e[0;33m' COL_RESET='\e[0m' +exit_cleanup() { + trap - INT TERM EXIT + if [ -n "$STAMPFILE" ]; then + rm -f -- "$STAMPFILE" + fi + if [ -n "$STAMPFILE2" ]; then + rm -f -- "$STAMPFILE2" + fi +} + +trap exit_cleanup EXIT +trap exit INT TERM + print_progress_prefix() { if [ -n "$DO_SHOW_PROGRESS" ]; then PROGRESS_PERC=0 @@ -218,12 +235,15 @@ cp_reflink() { printf "${COL_YELLOW}Reflinking to original: ${COL_RESET}%%s\n" "$1" if original_check "$1" "$2"; then if [ -z "$DO_DRY_RUN" ]; then - touch -mr "$1" -- "$0" + if [ -z "$STAMPFILE2" ]; then + STAMPFILE2=$(mktemp "${TMPDIR:-/tmp}/rmlint.XXXXXXXX.stamp") + fi + touch -mr "$1" -- "$STAMPFILE2" if [ -d "$1" ]; then rm -rf -- "$1" fi cp --archive --reflink=always -- "$2" "$1" - touch -mr "$0" -- "$1" + touch -mr "$STAMPFILE2" -- "$1" fi fi } @@ -278,7 +298,6 @@ remove_cmd() { if [ -n "$DO_KEEP_DIR_TIMESTAMPS" ]; then # Swap back old directory timestamp: touch -r "$STAMPFILE" -- "$(dirname "$1")" - rm -- "$STAMPFILE" fi if [ -n "$DO_DELETE_EMPTY_DIRS" ]; then @@ -380,7 +399,6 @@ do ;; k) DO_KEEP_DIR_TIMESTAMPS=true - STAMPFILE=$(mktemp 'rmlint.XXXXXXXX.stamp') ;; i) DO_ASK_BEFORE_DELETE=true @@ -402,11 +420,12 @@ then ask fi -if [ -n "$DO_DRY_RUN" ] -then +if [ -n "$DO_DRY_RUN" ]; then printf "#${COL_YELLOW} ////////////////////////////////////////////////////////////${COL_RESET}\n" printf "#${COL_YELLOW} /// ${COL_RESET} This is only a dry run; nothing will be modified! ${COL_YELLOW}///${COL_RESET}\n" printf "#${COL_YELLOW} ////////////////////////////////////////////////////////////${COL_RESET}\n" +elif [ -n "$DO_KEEP_DIR_TIMESTAMPS" ]; then + STAMPFILE=$(mktemp "${TMPDIR:-/tmp}/rmlint.XXXXXXXX.stamp") fi ######### START OF AUTOGENERATED OUTPUT #########