btrbk: use open3 in run_cmd: fetch stderr separately

pull/299/head
Axel Burri 2019-08-18 11:46:31 +02:00
parent a80e05984a
commit 9f347a1067
1 changed files with 35 additions and 39 deletions

74
btrbk
View File

@ -31,6 +31,8 @@ use warnings FATAL => qw( all ), NONFATAL => qw( deprecated );
use Carp qw(confess); use Carp qw(confess);
use Getopt::Long qw(GetOptions); use Getopt::Long qw(GetOptions);
use Time::Local qw( timelocal timegm timegm_nocheck ); use Time::Local qw( timelocal timegm timegm_nocheck );
use IPC::Open3 qw(open3);
use Symbol qw(gensym);
our $VERSION = '0.29.0-dev'; our $VERSION = '0.29.0-dev';
our $AUTHOR = 'Axel Burri <axel@tty0.ch>'; our $AUTHOR = 'Axel Burri <axel@tty0.ch>';
@ -680,8 +682,7 @@ sub _safe_cmd($$)
sub run_cmd(@) sub run_cmd(@)
{ {
# shell-based implementation. # IPC::Open3 based implementation.
# this needs some redirection magic for filter_stderr to work.
# NOTE: multiple filters are not supported! # NOTE: multiple filters are not supported!
my @cmd_pipe_in = (ref($_[0]) eq "HASH") ? @_ : { @_ }; my @cmd_pipe_in = (ref($_[0]) eq "HASH") ? @_ : { @_ };
@ -803,80 +804,75 @@ sub run_cmd(@)
); );
} }
my $cmd = _assemble_cmd(\@cmd_pipe, $catch_stderr); my $cmd = _assemble_cmd(\@cmd_pipe);
my $cmd_print = _assemble_cmd(\@cmd_pipe); # hide redirection magic from debug output
if(scalar(@unsafe_cmd)) { if(scalar(@unsafe_cmd)) {
ERROR "Unsafe command `$cmd_print` (offending string: " . join(', ', @unsafe_cmd) . ')'; ERROR "Unsafe command `$cmd` (offending string: " . join(', ', @unsafe_cmd) . ')';
return undef; return undef;
} }
if($dryrun && $destructive) { if($dryrun && $destructive) {
DEBUG "### (dryrun) $cmd_print"; DEBUG "### (dryrun) $cmd";
return ""; return "";
} }
DEBUG "### $cmd_print"; DEBUG "### $cmd";
TRACE "Executing command: $cmd";
# disable warnings in this scope (e.g. "permission denied", "no such file"), these cases are handled below.
# NOTE: for some reason this is only needed if we use "use warnings FATAL => qw( all )"
no warnings qw(exec);
# execute command and parse output # execute command
my @exitcode_msg; my ($pid, $out_fh, $err_fh, $stdout, @stderr);
my $stderr_fatal; $err_fh = gensym;
my $ret = `$cmd`; if(eval_quiet { $pid = open3(undef, $out_fh, $err_fh, $cmd); }) {
if(defined($ret)) { $stdout = do { local $/; scalar readline $out_fh };
chomp($ret); @stderr = readline $err_fh;
TRACE "Command output:\n$ret" if($loglevel >= 4); waitpid($pid, 0);
chomp(@stderr);
TRACE "Command stdout:\n$stdout" if(($loglevel >= 4) && defined($stdout));
TRACE "Command stderr:\n" . join("\n", @stderr) if(($loglevel >= 4) && scalar(@stderr));
}
else {
ERROR "Command execution failed ($!): `$cmd`";
return undef;
} }
# fatal errors # fatal errors
if($? == -1) { if($? == -1) {
ERROR "Command execution failed ($!): `$cmd_print`"; ERROR "Command execution failed ($!): `$cmd`";
return undef; return undef;
} }
elsif ($? & 127) { elsif ($? & 127) {
my $signal = $? & 127; my $signal = $? & 127;
ERROR "Command execution failed (child died with signal $signal): `$cmd_print`"; ERROR "Command execution failed (child died with signal $signal): `$cmd`";
return undef; return undef;
} }
my $exitcode = $? >> 8; my $exitcode = $? >> 8;
# handle filter_stderr option my @filtered_err;
if(defined($ret) && $filter_stderr) { if($filter_stderr) {
foreach(split("\n", $ret)) { @filtered_err = map { &{$filter_stderr} ($exitcode) // () } @stderr;
next if($_ eq "");
if(my $href = &{$filter_stderr} ($exitcode)) {
$stderr_fatal //= $href->{fatal};
push @exitcode_msg, $href;
$exitcode_loglevel = "error";
}
}
} }
elsif($has_rsh && ($exitcode == 255)) {
if($has_rsh && ($exitcode == 255)) {
# SSH returns exit status 255 if an error occurred. # SSH returns exit status 255 if an error occurred.
$cmd_print =~ s/ssh -q/ssh/g; @filtered_err = map { { error => $_ } } @stderr;
ERROR "SSH command failed: `$cmd_print`"; $exitcode_loglevel = "error";
return undef;
} }
if($exitcode || $stderr_fatal) { if($exitcode || (grep { $_->{fatal} } @filtered_err)) {
my $log_text = "Command execution failed (exitcode=$exitcode): `$cmd_print`"; my $log_text = "Command execution failed (exitcode=$exitcode): `$cmd`";
$exitcode_loglevel = "error" if(scalar(@filtered_err));
if($exitcode_loglevel eq "error") { ERROR $log_text; } if($exitcode_loglevel eq "error") { ERROR $log_text; }
elsif($exitcode_loglevel eq "warn") { WARN $log_text; } elsif($exitcode_loglevel eq "warn") { WARN $log_text; }
else { DEBUG $log_text; } else { DEBUG $log_text; }
foreach(@exitcode_msg) { foreach(@filtered_err) {
ERROR "... $_->{error}" if(defined($_->{error})); ERROR "... $_->{error}" if(defined($_->{error}));
WARN "... $_->{warn}" if(defined($_->{warn})); WARN "... $_->{warn}" if(defined($_->{warn}));
DEBUG "... $_->{debug}" if(defined($_->{debug}));
} }
return undef; return undef;
} }
else { else {
DEBUG "Command execution successful"; DEBUG "Command execution successful";
} }
return $ret; return $stdout;
} }