]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Storage/Status.pm
fix #3505: status: add checksum and algorithm to file upload
[pve-storage.git] / PVE / API2 / Storage / Status.pm
index cc8ed5ad33b417baa47de2939bb5cff4615e0959..6108ba1a7e33bfbe894ba9685b2fb35e72727cbc 100644 (file)
@@ -388,6 +388,19 @@ __PACKAGE__->register_method ({
                maxLength => 255,
                type => 'string',
            },
+           checksum => {
+               description => "The expected checksum of the file.",
+               type => 'string',
+               requires => 'checksum-algorithm',
+               optional => 1,
+           },
+           'checksum-algorithm' => {
+               description => "The algorithm to calculate the checksum of the file.",
+               type => 'string',
+               enum => ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
+               requires => 'checksum',
+               optional => 1,
+           },
            tmpfilename => {
                description => "The source file name. This parameter is usually set by the REST handler. You can only overwrite it when connecting to the trusted port on localhost.",
                type => 'string',
@@ -480,6 +493,30 @@ __PACKAGE__->register_method ({
            my $upid = shift;
 
            print "starting file import from: $tmpfilename\n";
+
+           eval {
+               my ($checksum, $checksum_algorithm) = $param->@{'checksum', 'checksum-algorithm'};
+               if ($checksum_algorithm) {
+                   print "calculating checksum...";
+
+                   my $checksum_got = PVE::Tools::get_file_hash($checksum_algorithm, $tmpfilename);
+
+                   if (lc($checksum_got) eq lc($checksum)) {
+                       print "OK, checksum verified\n";
+                   } else {
+                       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'\n";
+                   }
+               }
+           };
+           if (my $err = $@) {
+               # unlinks only the temporary file from the http server
+               unlink $tmpfilename;
+               warn "unable to clean up temporory file '$tmpfilename' - $!\n"
+                   if $! && $! != ENOENT;
+               die $err;
+           }
+
            print "target node: $node\n";
            print "target file: $dest\n";
            print "file size is: $size\n";
@@ -487,10 +524,8 @@ __PACKAGE__->register_method ({
 
            eval { run_command($cmd, errmsg => 'import failed'); };
 
-           # unlinks only the temporary file from the http server
-           unlink $tmpfilename;
-           warn "unable to clean up temporary file '$tmpfilename' - $!\n"
-               if $! && $! != ENOENT;
+           unlink $tmpfilename; # the temporary file got only uploaded locally, no need to rm remote
+           warn "unable to clean up temporary file '$tmpfilename' - $!\n" if $! && $! != ENOENT;
 
            if (my $err = $@) {
                eval { $err_cleanup->() };
@@ -500,14 +535,7 @@ __PACKAGE__->register_method ({
            print "finished file import successfully\n";
        };
 
-       my $upid = $rpcenv->fork_worker('imgcopy', undef, $user, $worker);
-
-       # apache removes the temporary file on return, so we need
-       # to wait here to make sure the worker process starts and
-       # opens the file before it gets removed.
-       sleep(1);
-
-       return $upid;
+       return $rpcenv->fork_worker('imgcopy', undef, $user, $worker);
    }});
 
 __PACKAGE__->register_method({