api: qga file-write: code/style cleanups/refactoring

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-02-10 16:26:53 +01:00
parent 0cb151937f
commit 2b607ad1ab

View File

@ -471,14 +471,13 @@ __PACKAGE__->register_method({
}, },
content => { content => {
type => 'string', type => 'string',
maxLength => 60*1024, # 60k, smaller than our 64k POST limit maxLength => 60 * 1024, # 60k, smaller than our 64k POST limit
description => "The content to write into the file." description => "The content to write into the file."
}, },
encode => { encode => {
type => 'boolean', type => 'boolean',
description => "If set the content will be encoded as base64" description => "If set, the content will be encoded as base64 (required by QEMU)."
." (required by QEMU). Otherwise the content needs to be encoded" ."Otherwise the content needs to be encoded beforehand - defaults to true.",
." beforehand. (Default is true)",
optional => 1, optional => 1,
default => 1, default => 1,
}, },
@ -490,14 +489,10 @@ __PACKAGE__->register_method({
my $vmid = $param->{vmid}; my $vmid = $param->{vmid};
my $encode = $param->{encode} // 1; my $encode = $param->{encode} // 1;
my $buf;
if ($encode) { my $buf = $encode ? encode_base64($param->{content}) : $param->{content};
$buf = encode_base64($param->{content}); die "content is not base64 encoded\n"
} else { if !$encode && $buf !~ m@^(?:[A-Z0-9+/]{4})*(?:[A-Z0-9+/]{2}==|[A-Z0-9+/]{3}=)?$@mi;
die "content is not base64 encoded\n"
if $param->{content} !~ m@^(?:[A-Z0-9+/]{4})*(?:[A-Z0-9+/]{2}==|[A-Z0-9+/]{3}=)?$@mi;
$buf = $param->{content};
}
my $qgafh = agent_cmd($vmid, "file-open", { path => $param->{file}, mode => 'wb' }, "can't open file"); my $qgafh = agent_cmd($vmid, "file-open", { path => $param->{file}, mode => 'wb' }, "can't open file");
my $write = agent_cmd($vmid, "file-write", { handle => $qgafh, 'buf-b64' => $buf }, "can't write to file"); my $write = agent_cmd($vmid, "file-write", { handle => $qgafh, 'buf-b64' => $buf }, "can't write to file");