Skip to content

Commit

Permalink
sh format: improve stampfile handling
Browse files Browse the repository at this point in the history
- mktemp in /tmp
- don't assume $0 is a file
- remove stampfiles at exit
- don't create stampfiles if -n is passed

Closes #554
  • Loading branch information
cebtenzzre committed Feb 9, 2022
1 parent bdb591f commit f7b49e8
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/formats/sh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,27 @@ DO_KEEP_DIR_TIMESTAMPS=
# Set to true on -i
DO_ASK_BEFORE_DELETE=

# Tempfiles for saving timestamps
STAMPFILE=
STAMPFILE2=

##################################
# GENERAL LINT HANDLER FUNCTIONS #
##################################

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

COL_RED='\e[0;31m'
COL_BLUE='\e[1;34m'
COL_GREEN='\e[0;32m'
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -380,7 +399,6 @@ do
;;
k)
DO_KEEP_DIR_TIMESTAMPS=true
STAMPFILE=$(mktemp 'rmlint.XXXXXXXX.stamp')
;;
i)
DO_ASK_BEFORE_DELETE=true
Expand All @@ -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 #########
Expand Down

0 comments on commit f7b49e8

Please sign in to comment.