]> Sergey Matveev's repositories - nnn.git/commitdiff
nmv: ensure the tmpfile is cleaned up in all cases
authorNRK <nrk@disroot.org>
Sun, 19 Feb 2023 15:14:53 +0000 (21:14 +0600)
committerNRK <nrk@disroot.org>
Sun, 19 Feb 2023 15:14:53 +0000 (21:14 +0600)
currently, there's a couple exit paths where the tmpfile doesn't get
cleaned up.

plugins/.nmv

index bbbdafe7d92063c1d26561657c07e5f0b6eb4a52..eaebda6c8f1aad24661838e719a82b4493666ee9 100755 (executable)
@@ -37,8 +37,6 @@ esac
 
 exit_status=0
 
-dst_file=$(mktemp "$TMPDIR/.nnnXXXXXX")
-
 if nnn_use_selection "Rename"; then
        # shellcheck disable=SC2154
        arr=$(tr '\0' '\n' < "$selection")
@@ -65,6 +63,9 @@ fi
 lines=$(printf "%s\n" "$arr" | wc -l)
 width=${#lines}
 
+dst_file=$(mktemp "$TMPDIR/.nnnXXXXXX")
+trap 'rm -f "$dst_file"' EXIT
+
 printf "%s" "$arr" | awk '{printf("%'"${width}"'d %s\n", NR, $0)}' > "$dst_file"
 
 items=("~")
@@ -170,5 +171,4 @@ for item in "${items[@]}"; do
        $RM_UTIL "$item"
 done
 
-rm "$dst_file"
 exit $exit_status