fix #3683: agent file-write: enable user to encode the content themselves

by adding an optional parameter 'encode' (enabled by default). When it
is disabled, the content must be base64 encoded already. This
way, users can send a binary file to the vm by base64 encoding it
themselves

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2022-02-10 15:52:10 +01:00 committed by Thomas Lamprecht
parent e594231bf1
commit 0cb151937f

View File

@ -473,7 +473,15 @@ __PACKAGE__->register_method({
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 => {
type => 'boolean',
description => "If set the content will be encoded as base64"
." (required by QEMU). Otherwise the content needs to be encoded"
." beforehand. (Default is true)",
optional => 1,
default => 1,
},
}, },
}, },
returns => { type => 'null' }, returns => { type => 'null' },
@ -481,7 +489,15 @@ __PACKAGE__->register_method({
my ($param) = @_; my ($param) = @_;
my $vmid = $param->{vmid}; my $vmid = $param->{vmid};
my $buf = encode_base64($param->{content}); my $encode = $param->{encode} // 1;
my $buf;
if ($encode) {
$buf = encode_base64($param->{content});
} else {
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");