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 sahib#554
  • Loading branch information
cebtenzzre committed Aug 12, 2022
1 parent fbb6cb6 commit 358431a
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,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 #
##################################
Expand All @@ -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
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 358431a

Please sign in to comment.