[pve-devel] [PATCH storage 3/3] status: add checksum and algorithm to file upload

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Jul 20 15:40:50 CEST 2021


On 20.07.21 13:51, Lorenz Stechauner wrote:
> Signed-off-by: Lorenz Stechauner <l.stechauner at proxmox.com>
> ---
>  PVE/API2/Storage/Status.pm | 34 ++++++++++++++++++++++++++++++++++
>  1 file changed, 34 insertions(+)
> 
> diff --git a/PVE/API2/Storage/Status.pm b/PVE/API2/Storage/Status.pm
> index eac5e13..e3bf758 100644
> --- a/PVE/API2/Storage/Status.pm
> +++ b/PVE/API2/Storage/Status.pm
> @@ -389,6 +389,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',
> @@ -478,6 +491,27 @@ __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 = $@) {
> +		unlink $tmpfilename;

missing error handling: `unlink $tmpfilename or warn "error on clean up of temporary file '$tmpfilename' - $!";

> +		die $err;
> +	    }
> +
>  	    print "target node: $node\n";
>  	    print "target file: $dest\n";
>  	    print "file size is: $size\n";
> 






More information about the pve-devel mailing list