From cb6f67fae16870babf99bc9bf0a8ce9a7f8735d2 Mon Sep 17 00:00:00 2001 From: "Fred T. Hamster" Date: Wed, 2 Apr 2025 18:57:32 -0400 Subject: [PATCH] adding safeguards to not overwrite same name files there was a possibility that a file like ~blah.xlsx could overwrite blah.xlsx, which is a huge issue. don't want to scrap any active files like that, so now we are checking at different stages in the rename process to ensure there are at least no overwrites. --- scripts/files/renlower.pl | 17 ++++++++++++++--- .../files/replace_spaces_with_underscores.sh | 15 ++++++++++++--- scripts/files/spacem.sh | 15 +++++++++++---- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/scripts/files/renlower.pl b/scripts/files/renlower.pl index ca42de7c..e97a49c2 100644 --- a/scripts/files/renlower.pl +++ b/scripts/files/renlower.pl @@ -33,7 +33,7 @@ exit 0; sub rename_lower { # go through the list of files passed in. foreach $current (&glob_list(@_)) { -#print "unfiltered: '$current'\n"; +#print "renlower--unfiltered: '$current'\n"; if ($current =~ /[A-Z]/) { #print "current is '$current'\n"; local $old_name = $current; @@ -49,10 +49,21 @@ sub rename_lower { #print "command A is: rename [$old_name] [$intermediate_name]\n"; #print "command B is: rename [$intermediate_name] [$new_name]\n"; #print "\n"; + + # safety rails here--we don't want to rename over top of existing files. + if (-e "$intermediate_name") { + print("error: file called '$intermediate_name' already exists; skipping so we do not overwrite.\n"); + next; + } + if (-e "$new_name") { + print("error: file called '$new_name' already exists; skipping so we do not overwrite.\n"); + next; + } + rename($old_name, $intermediate_name) - || die "failed to do initial rename"; + || die "error: failed to do initial rename"; rename($intermediate_name, $new_name) - || die "failed to do secondary rename"; + || die "error: failed to do secondary rename"; print "'$old_name' => '$new_name'\n"; } } diff --git a/scripts/files/replace_spaces_with_underscores.sh b/scripts/files/replace_spaces_with_underscores.sh index b0312b9a..d7891174 100644 --- a/scripts/files/replace_spaces_with_underscores.sh +++ b/scripts/files/replace_spaces_with_underscores.sh @@ -10,6 +10,9 @@ if [ $# -lt 1 ]; then exit 1 fi +# allow error reporting from inside our loop. +retval=0 + while [ $# -gt 0 ]; do file="$1"; shift # first turn spaces into underscores. then process characters we don't want @@ -18,9 +21,15 @@ while [ $# -gt 0 ]; do # underscore dash underscore into just dash. newname="$(echo "$file" | tr -s ' ' '_' | tr -d "\$\!|@&#%}{)(][\\\~',:?><\"" | sed -e 's/__/_/g' | sed -e 's/\([0-9]\)_\./\1./g' | sed -e 's/_-_/-/' )" if [ "$file" != "$newname" ]; then - # we've effected a name change, so let's actually do it. - echo "'$file' => '$newname'" - mv "$file" "$newname" + if [ ! -e "$newname" ]; then + # we've decided on an effective name change, so let's actually rename. + echo "'$file' => '$newname'" + mv "$file" "$newname" + else + echo "error: skipping rename '$file' => '$newname' due to existing file." + retval=1 + fi fi done +exit $retval diff --git a/scripts/files/spacem.sh b/scripts/files/spacem.sh index 2932fa72..60ca640a 100644 --- a/scripts/files/spacem.sh +++ b/scripts/files/spacem.sh @@ -5,8 +5,8 @@ function spacem_out() while [ $# -gt 0 ]; do arg="$1"; shift - if [[ $arg =~ ~* ]]; then -echo "skipping tilde style name: '$arg'" + if [[ $arg == ~* ]]; then +#echo "skipping tilde style name: '$arg'" continue fi @@ -14,13 +14,17 @@ echo "skipping tilde style name: '$arg'" echo "=> did not find a file or directory named '$arg'." continue fi +#echo "name to spacem out is: '$arg'" # we capture the output of the character replacement operation for reporting. # this is done first since some filenames cannot be properly renamed in perl (e.g. if they # have pipe characters apparently). intermediate_name="$(bash "$FEISTY_MEOW_SCRIPTS/files/replace_spaces_with_underscores.sh" "$arg")" local saw_intermediate_result=0 - if [ -z "$intermediate_name" ]; then + if [[ $intermediate_name == error:* ]]; then + echo "error seen during name massage phase 1 on '$arg'" + continue + elif [ -z "$intermediate_name" ]; then # make sure we report something, if there are no further name changes. intermediate_name="'$arg'" else @@ -33,7 +37,10 @@ echo "skipping tilde style name: '$arg'" actual_file="$(echo $intermediate_name | sed -e "s/'\([^']*\)'/\1/")" final_name="$(perl "$FEISTY_MEOW_SCRIPTS/files/renlower.pl" "$actual_file")" local saw_final_result=0 - if [ -z "$final_name" ]; then + if [[ $final_name == error:* ]]; then + echo "error seen during name massage phase 2 on '$arg'" + continue + elif [ -z "$final_name" ]; then final_name="$intermediate_name" else final_name="$(echo $final_name | sed -e 's/.*=> //' )" -- 2.34.1