Any/Backend/_Cmd.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 72 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 282 CC 17 SCAR 48.3
CC
17
Cov
0%
_run_cmd
272.0
_sftp
6.0
_backend_api_version
2.0
CRAP 125.7
1 package Net::SSH::Any::Backend::_Cmd;
2  
3 use strict;
4 use warnings;
5  
6 use Carp;
7 our @CARP_NOT = qw(Net::SSH::Any);
8  
9 use Net::SSH::Any::Util qw($debug _debug _first_defined _array_or_scalar_to_list);
10 use Net::SSH::Any::Constants qw(:error);
11  
12 sub _backend_api_version { 2 }
13  
14 sub _validate_backend_opts {
15 my $any = shift;
16 $any->_os_loaded or return; # ensure the OS module is loaded
17 1;
18 }
19  
20 sub _connect { 1 }
21  
22 sub _check_connection { 1 }
23  
24 sub _export_proc {
25 my ($any, $proc) = @_;
26 $proc->{pid}
27 }
28  
29 my @stream_names = qw(stdin stdout stderr);
30 sub _run_cmd {
31 my ($any, $opts, $cmd) = @_;
32 my (@fhs, @pipes);
33  
34 $any->_check_connection or return;
35  
36 my $data = $opts->{stdin_data};
37 $opts->{stdin_pipe} = 1 if defined $data;
38  
39 my $dpipe = delete $opts->{stdinout_dpipe};
40 if ($dpipe) {
41 if ($any->_os_has_working_socketpair) {
42 ($fhs[0], $pipes[0]) = $any->_os_socketpair($fhs[0], $pipes[0]) or return;
43 $fhs[1] = $fhs[0];
44 $pipes[1] = undef;
45 }
46 else {
47 $opts->{stdin_pipe} = 1;
48 $opts->{stdout_pipe} = 1;
49 }
50 }
51  
52 for my $stream (@stream_names[@fhs .. 2]) {
53 $debug and $debug & 1024 and _debug "seting up stream $stream";
54 my ($fh, $pipe);
55 if (delete $opts->{"${stream}_pipe"}) {
56 ($pipe, $fh) = $any->_os_pipe or return;
57 ($pipe, $fh) = ($fh, $pipe) if $stream eq 'stdin';
58 }
59 else {
60 $fh = delete $opts->{"${stream}_fh"};
61 unless ($fh) {
62 my $file = (delete($opts->{"${stream}_discard"})
63 ? File::Spec->devnull
64 : delete $opts->{"${stream}_file"} );
65 if (not defined $file and $stream eq 'stdin') {
66 # stdin is redirected from /dev/null by default
67 $file = File::Spec->devnull;
68 }
69 if (defined $file) {
70 my $dir = ($stream eq 'stdin' ? '<' : '>');
71 $fh = $any->_open_file($dir, $file) or return;
72 }
73 }
74 }
75 push @fhs, $fh;
76 push @pipes, $pipe;
77 }
78  
79 $any->_os_set_file_inherit_flag($_, 0)
80 for grep defined, @pipes;
81  
82 my $stderr_to_stdout = (defined $fhs[2] ? 0 : delete $opts->{stderr_to_stdout});
83  
84 my @too_many = grep { /^std(?:in|out|err)_/ and
85 $_ ne 'stdin_data' and
86 defined $opts->{$_} } keys %$opts;
87 @too_many and croak "unsupported options or bad combination ('".join("', '", @too_many)."')";
88  
89 my @cmd = _array_or_scalar_to_list $cmd;
90 unless ($opts->{_local}) {
91 # FIXME: this is quite ugly, the make_cmd call should probably
92 # be done outside this method. Actually, this method should
93 # probably go into the OS module or in Any.
94 @cmd = $any->_make_cmd($opts, @cmd) or return;
95 };
96  
97 $debug and $debug & 1024 and _debug("launching cmd: '", join("', '", @cmd), "'");
98 my $pty = ($any->{be_interactive_login} ? $any->_os_pty($any) : undef);
99 my $proc = $any->_os_open4(\@fhs, \@pipes, $pty, $stderr_to_stdout, @cmd) or return;
100  
101 $debug and $debug & 1024 and _debug("pid: $proc->{pid}");
102  
103 if ($pty) {
104 $any->_os_interactive_login($pty, $proc) or return undef;
105 $any->{be_pty} = $pty;
106 $pty->close_slave;
107 }
108  
109 if ($dpipe) {
110 $pipes[0] = $any->_os_make_dpipe($proc, @pipes[0, 1]) or return;
111 $pipes[1] = undef;
112 $debug and $debug & 1024 and _debug "fh upgraded to dpipe $pipes[0]";
113 }
114  
115 return ($proc, @pipes);
116 }
117  
118 sub _remap_child_error { 1 }
119  
120 sub _io3 {
121 my ($any, $opts, $proc, @pipes) = @_;
122 my @r = $any->_os_io3($proc, $opts->{timeout}, $opts->{stdin_data}, @pipes);
123 $any->_remap_child_error($proc);
124 $? = $proc->{rc};
125 @r;
126 }
127  
128 sub _system {
129 my ($any, $opts, $cmd) = @_;
130 my ($proc, @pipes) = $any->_run_cmd($opts, $cmd) or return;
131 $any->_io3($opts, $proc, @pipes);
132 }
133  
134 sub _capture {
135 my ($any, $opts, $cmd) = @_;
136 $opts->{stdout_pipe} = 1;
137 my ($proc, @pipes) = $any->_run_cmd($opts, $cmd) or return;
138 $any->_io3($opts, $proc, @pipes);
139 }
140  
141 sub _local_capture {
142 # This method, or a better version, should go into Any
143 my ($any, @cmd) = @_;
144 my ($proc, @pipes) = $any->_run_cmd({ stdout_pipe => 1, stderr_to_stdout => 1, _local => 1 },
145 \@cmd);
146 my ($out) = $any->_io3({}, $proc, @pipes);
147 $out // '';
148 }
149  
150 sub _capture2 {
151 my ($any, $opts, $cmd) = @_;
152 $opts->{stdout_pipe} = 1;
153 $opts->{stderr_pipe} = 1;
154 my ($proc, @pipes) = $any->_run_cmd($opts, $cmd) or return;
155 $any->_io3($opts, $proc, @pipes);
156 }
157  
158 sub _dpipe {
159 my ($any, $opts, $cmd) = @_;
160 $opts->{stdinout_dpipe} = 1;
161 my (undef, $dpipe) = $any->_run_cmd($opts, $cmd) or return;
162 $dpipe;
163 }
164  
165 sub _sftp {
166 my ($any, $opts) = @_;
167 my ($cmd, $subsystem);
168 if ($opts->{ssh1}) {
169 $cmd = delete($opts->{remote_sftp_server_cmd}) // $any->{remote_cmd}{sftp_server} // '/usr/lib/sftp-server';
170 }
171 else {
172 $cmd = 'sftp';
173 $subsystem = 1;
174 }
175 my ($proc, $in, $out) = $any->_run_cmd( { stdin_pipe => 1,
176 stdout_pipe => 1,
177 subsystem => $subsystem },
178 $cmd) or return;
179 my $pid = $any->_export_proc($proc) or return;
180 Net::SFTP::Foreign->new(transport => [$out, $in, $pid], %$opts);
181 }
182  
183 1;