5
0
mirror of git://git.proxmox.com/git/pve-storage.git synced 2024-12-22 13:34:16 +03:00

api: status: rework err cleanup

avoid open3 on local node.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2021-07-29 17:12:02 +02:00
parent a84804c432
commit b11d054be0

View File

@ -5,6 +5,7 @@ use warnings;
use File::Basename; use File::Basename;
use File::Path; use File::Path;
use POSIX qw(ENOENT);
use PVE::Cluster; use PVE::Cluster;
use PVE::Exception qw(raise_param_exc); use PVE::Exception qw(raise_param_exc);
@ -445,10 +446,9 @@ __PACKAGE__->register_method ({
# best effort to match apl_download behaviour # best effort to match apl_download behaviour
chmod 0644, $tmpfilename; chmod 0644, $tmpfilename;
# we simply overwrite the destination file if it already exists my $err_cleanup = sub { unlink $dest, $tmpfilename; die "cleanup failed: $!" if $! && $! != ENOENT };
my $cmd; my $cmd;
my $err_cmd;
if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
my $remip = PVE::Cluster::remote_node_ip($node); my $remip = PVE::Cluster::remote_node_ip($node);
@ -467,14 +467,15 @@ __PACKAGE__->register_method ({
); );
$cmd = ['/usr/bin/scp', @ssh_options, '-p', '--', $tmpfilename, "[$remip]:" . PVE::Tools::shell_quote($dest)]; $cmd = ['/usr/bin/scp', @ssh_options, '-p', '--', $tmpfilename, "[$remip]:" . PVE::Tools::shell_quote($dest)];
$err_cmd = [@remcmd, 'unlink', '--', $dest];
$err_cleanup = sub { run_command([@remcmd, 'rm', '-f', '--', $dest, $tmpfilename]) };
} else { } else {
PVE::Storage::activate_storage($cfg, $param->{storage}); PVE::Storage::activate_storage($cfg, $param->{storage});
File::Path::make_path($dirname); File::Path::make_path($dirname);
$cmd = ['cp', '--', $tmpfilename, $dest]; $cmd = ['cp', '--', $tmpfilename, $dest];
$err_cmd = ['unlink', '--', $dest];
} }
# NOTE: we simply overwrite the destination file if it already exists
my $worker = sub { my $worker = sub {
my $upid = shift; my $upid = shift;
@ -486,7 +487,8 @@ __PACKAGE__->register_method ({
eval { run_command($cmd, errmsg => 'import failed'); }; eval { run_command($cmd, errmsg => 'import failed'); };
if (my $err = $@) { if (my $err = $@) {
eval { run_command($err_cmd) }; eval { $err_cleanup->() };
warn "$@" if $@;
die $err; die $err;
} }
print "finished file import successfully\n"; print "finished file import successfully\n";