[pve-devel] [PATCH v9 common 2/2] tools: download_file_from_url: move check for existing file outside eval

Lorenz Stechauner l.stechauner at proxmox.com
Wed Jun 16 11:35:58 CEST 2021


it is not necessary to include this block in the eval which when it
fails tries to unlink $tmpdest, because in the check for the existing
file $tmpdest is not used.
---
 src/PVE/Tools.pm | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 3cf7c4d..9046b4f 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -1862,22 +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..
-		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";
-	    }
+	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";
-- 
2.20.1






More information about the pve-devel mailing list