]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
tools: download_file_from_url: move check for existing file outside eval
[pve-common.git] / src / PVE / Tools.pm
index b4caf5440f26fcbcfccdaaf3e22ba2173bc1f78f..9046b4f2c4ef7953b60581024b1d3b1f4a42398a 100644 (file)
@@ -105,6 +105,11 @@ use constant {O_PATH    => 0x00200000,
 use constant {AT_EMPTY_PATH => 0x1000,
               AT_FDCWD => -100};
 
+# from <linux/fs.h>
+use constant {RENAME_NOREPLACE => (1 << 0),
+              RENAME_EXCHANGE  => (1 << 1),
+              RENAME_WHITEOUT  => (1 << 2)};
+
 sub run_with_timeout {
     my ($timeout, $code, @param) = @_;
 
@@ -1462,6 +1467,11 @@ sub fsync($) {
     return 0 == syscall(PVE::Syscall::fsync, $fileno);
 }
 
+sub renameat2($$$$$) {
+    my ($olddirfd, $oldpath, $newdirfd, $newpath, $flags) = @_;
+    return 0 == syscall(PVE::Syscall::renameat2, $olddirfd, $oldpath, $newdirfd, $newpath, $flags);
+}
+
 sub sync_mountpoint {
     my ($path) = @_;
     sysopen my $fd, $path, O_RDONLY|O_CLOEXEC or die "failed to open $path: $!\n";
@@ -1852,21 +1862,22 @@ sub download_file_from_url {
 
     print "downloading $url to $dest\n";
 
-    my $tmpdest = "$dest.tmp.$$";
-    eval {
-       if (-f $dest && $checksum_algorithm) {
-           print "calculating checksum of existing file...";
-           my $checksum_got = get_file_hash($checksum_algorithm, $dest);
+    if (-f $dest && $checksum_algorithm) {
+       print "calculating checksum of existing file...";
+       my $checksum_got = get_file_hash($checksum_algorithm, $dest);
 
-           if (lc($checksum_got) eq lc($checksum_expected)) {
-               print "OK, got correct file already, no need to download\n";
-               return;
-           } else {
-               # we could re-download, but may not be safe so just abort for now..
-               die "mismatch (got '$checksum_got' != expect '$checksum_expected'), aborting\n";
-           }
+       if (lc($checksum_got) eq lc($checksum_expected)) {
+           print "OK, got correct file already, no need to download\n";
+           return;
+       } else {
+           # we could re-download, but may not be safe so just abort for now..
+           print "\n";  # the front end expects the error to reside at the last line without any noise
+           die "checksum mismatch: got '$checksum_got' != expect '$checksum_expected', aborting\n";
        }
+    }
 
+    my $tmpdest = "$dest.tmp.$$";
+    eval {
        local $SIG{INT} = sub {
            unlink $tmpdest or warn "could not cleanup temporary file: $!";
            die "got interrupted by signal\n";
@@ -1898,7 +1909,8 @@ sub download_file_from_url {
            if (lc($checksum_got) eq lc($checksum_expected)) {
                print "OK, checksum verified\n";
            } else {
-               die "ERRROR, checksum mismatch: got '$checksum_got' != expect '$checksum_expected'\n";
+               print "\n";  # the front end expects the error to reside at the last line without any noise
+               die "checksum mismatch: got '$checksum_got' != expect '$checksum_expected'\n";
            }
        }