diff options
-rwxr-xr-x | lacme | 17 |
1 files changed, 8 insertions, 9 deletions
@@ -660,12 +660,10 @@ sub spawn($@) { # sub install_cert(%) { my %args = @_; - my $filename = $args{path} // die; + my $path = $args{path} // die; - my ($dirname, $basename) = - $filename =~ /\A(.*)\/([^\/]+)\z/ ? ($1, $2) : ('.', $filename); - my $fh = File::Temp::->new(UNLINK => 0, DIR => $dirname, - TEMPLATE => "$basename.XXXXXX") // die; + my $fh = File::Temp::->new(TEMPLATE => "$path.XXXXXXXXXX", UNLINK => 0) // die; + my $path_tmp = $fh->filename(); eval { if ($args{nochain}) { @@ -707,13 +705,14 @@ sub install_cert(%) { $fh->close() or die "close: $!"; }; - my $path = $fh->filename(); if ($@) { - print STDERR "Unlinking $path\n" if $OPTS{debug}; - unlink $path or warn "unlink($path): $!"; + print STDERR "Unlinking $path_tmp\n" if $OPTS{debug}; + unlink $path_tmp or warn "unlink($path_tmp): $!"; die $@; + } else { + # atomically replace $path if it exists + rename($path_tmp, $path) or die "rename($path_tmp, $path): $!"; } - rename($path, $filename) or die "rename($path, $filename): $!"; } |