dm6: Detect when rename fails to clean up $tmp_to
authorZygo Blaxell <zblaxell@faye.furryterror.org>
Tue, 18 May 2010 12:30:08 +0000 (08:30 -0400)
committerZygo Blaxell <zblaxell@faye.furryterror.org>
Tue, 18 May 2010 12:54:57 +0000 (08:54 -0400)
(cherry picked from commit 03d938d317f1660e7c4f1f228768733c98dff1bf)

dm6

diff --git a/dm6 b/dm6
index 4cd11b02f43767f63a913cbaa7715c6fd553eecc..8638e28c55e8d76e9772dde939a22d60af98616e 100755 (executable)
--- a/dm6
+++ b/dm6
@@ -58,12 +58,15 @@ sub link_files {
        print STDERR "\bL";
        link($from, $tmp_to) or die "link: $from -> $tmp_to: $!";
        print STDERR "\bR";
        print STDERR "\bL";
        link($from, $tmp_to) or die "link: $from -> $tmp_to: $!";
        print STDERR "\bR";
-       unless (rename($tmp_to, $to)) {
-               my $saved_bang = $!;
-               print STDERR "\bU";
-               unlink($tmp_to) or warn "unlink: $tmp_to: $!";  # Try, possibly in vain, to clean up
-               die "rename: $tmp_to -> $from: $saved_bang";
-       }
+       my $saved_bang;
+       $saved_bang = $! unless rename($tmp_to, $to);
+
+       # If $to exists and is a hardlink to $tmp_to (or $from),
+       # rename returns success but $tmp_to still exists.
+       print STDERR "\bU";
+       unlink($tmp_to) or warn "unlink: $tmp_to: $!" if -e $tmp_to;
+
+       die "rename: $tmp_to -> $from: $saved_bang" if $saved_bang;
        print STDERR "\b";
 }
 
        print STDERR "\b";
 }