| 1 | package Net::SSH::Any::SCP::Getter; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | |||
| 8 | use Net::SSH::Any::Constants qw(SSHA_SCP_ERROR SSHA_REMOTE_CMD_ERROR); | ||
| 9 | use Net::SSH::Any::Util qw($debug _debug _debugf _debug_hexdump | ||
| 10 | _first_defined _inc_numbered _gen_wanted | ||
| 11 | _scp_escape_name _scp_unescape_name); | ||
| 12 | |||
| 13 | require Net::SSH::Any::SCP::Base; | ||
| 14 | our @ISA = qw(Net::SSH::Any::SCP::Base); | ||
| 15 | |||
| 16 | sub _new { | ||
| 17 | my ($class, $any, $opts, @srcs) = @_; | ||
| 18 | my $g = $class->SUPER::_new($any, $opts); | ||
| 19 | $g->{srcs} = \@srcs, | ||
| 20 | $g->{$_} = delete $opts->{$_} for qw(recursive glob request_time); | ||
| 21 | # TODO: | ||
| 22 | # on_start = ... | ||
| 23 | # on_end = ... or enter/leave or whatever | ||
| 24 | $g; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub on_open { | ||
| 28 | my $method = "on_open_$_[1]{type}"; | ||
| 29 | shift->$method(@_) | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _open { | ||
| 33 | my ($g, $type, $perm, $size, $name) = @_; | ||
| 34 | my $action = $g->_push_action(type => $type, | ||
| 35 | perm => $perm, | ||
| 36 | size => $size, | ||
| 37 | name => $name); | ||
| 38 | |||
| 39 | if ( $g->on_open_before_wanted($action) and | ||
| 40 | $g->_check_wanted($action) and | ||
| 41 | $g->on_open($action) ) { return 1 } | ||
| 42 | |||
| 43 | $g->_pop_action; | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub on_close { | ||
| 48 | my $g = shift; | ||
| 49 | my $method = "on_close_$_[0]{type}"; | ||
| 50 | $g->$method(@_); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _close { | ||
| 54 | my ($g, $action, $failed, $error) = @_; | ||
| 55 | $g->_set_remote_error($action, $error) if $failed; | ||
| 56 | $g->on_close($action, $failed); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _write { | ||
| 60 | my $g = shift; | ||
| 61 | $g->on_write($g->{actions}[-1], $_[0]); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _matime { | ||
| 65 | my $g = shift; | ||
| 66 | @{$g}{qw(mtime atime)} = @_; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _remote_error { | ||
| 70 | my ($g, $path, $error) = @_; | ||
| 71 | my $action = { type => 'remote_error', | ||
| 72 | path => $path }; | ||
| 73 | $g->set_remote_error($action, $error); | ||
| 74 | push @{$g->{log}}, $action if $g->{log}; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _clean_actions { | ||
| 78 | my $g = shift; | ||
| 79 | while (my $action = $g->_pop_action(undef, 1)) { | ||
| 80 | $g->_close($action, 2, "broken dpipe"); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | sub on_open_before_wanted { 1 } | ||
| 85 | sub on_open_file { 1 } | ||
| 86 | sub on_open_dir { 1 } | ||
| 87 | sub on_close_file { 1 } | ||
| 88 | sub on_close_dir { 1 } | ||
| 89 | sub on_end_of_get { 1 } | ||
| 90 | |||
| 91 | sub run { | ||
| 92 | my ($g, $opts) = @_; | ||
| 93 | my $any = $g->{any}; | ||
| 94 | |||
| 95 | $debug and $debug & 4096 and _debug "starting SCP Getter run..."; | ||
| 96 | |||
| 97 | my @cmd = $any->_quote_args({quote_args => 1}, | ||
| 98 | # 'strace', '-o', '/tmp/out', | ||
| 99 | $g->{scp_cmd}, | ||
| 100 | '-f', | ||
| 101 | ($g->{request_time} ? '-p' : ()), | ||
| 102 | ($g->{recursive} ? '-r' : ()), | ||
| 103 | ($g->{double_dash} ? '--' : ())); | ||
| 104 | my @files = $any->_quote_args({quote_args => 1, | ||
| 105 | glob_quoting => $g->{glob}}, | ||
| 106 | @{$g->{srcs}}); | ||
| 107 | |||
| 108 | |||
| 109 | # $debug and $debug & 4096 and _debug "wait..."; | ||
| 110 | # sleep 5; | ||
| 111 | # $debug and $debug & 4096 and _debug "welcome to the party!"; | ||
| 112 | # $DB::trace=1; | ||
| 113 | my $dpipe = $any->dpipe({ %$opts, quote_args => 0 }, | ||
| 114 | @cmd, @files); | ||
| 115 | $any->error and return; | ||
| 116 | |||
| 117 | local $SIG{PIPE} = 'IGNORE'; | ||
| 118 | my $buf; | ||
| 119 | |||
| 120 | $dpipe->syswrite("\x00"); # tell remote side to start transfer | ||
| 121 | while (1) { | ||
| 122 | $g->_read_line($dpipe, $buf) or last; | ||
| 123 | $debug and $debug & 4096 and _debug "cmd line: $buf"; | ||
| 124 | |||
| 125 | my $ok = 1; | ||
| 126 | |||
| 127 | # C or D: | ||
| 128 | if (my ($type, $perm, $size, $name) = $buf =~ /^([CD])([0-7]+) (\d+) (.*)$/) { | ||
| 129 | _scp_unescape_name($name); | ||
| 130 | $size = int $size; | ||
| 131 | $perm = oct $perm; | ||
| 132 | if ($type eq 'C') { | ||
| 133 | if ($ok = $g->_open(file => $perm, $size, $name)) { | ||
| 134 | $debug and $debug & 4096 and _debug "transferring file of size $size"; | ||
| 135 | $dpipe->syswrite("\x00"); | ||
| 136 | $buf = ''; | ||
| 137 | while ($size) { | ||
| 138 | my $read = $dpipe->sysread($buf, ($size > 64000 ? 64000 : $size)); | ||
| 139 | unless ($read) { | ||
| 140 | $g->_or_set_error(SSHA_SCP_ERROR, "broken dpipe"); | ||
| 141 | $g->_close($g->_pop_action('file'), 2, "broken dpipe"); | ||
| 142 | $debug and $debug & 4096 and _debug "read failed: " . $any->error; | ||
| 143 | last; | ||
| 144 | } | ||
| 145 | $g->_write($buf) or last; | ||
| 146 | $size -= $read; | ||
| 147 | } | ||
| 148 | my ($error_level, $error_msg) = $g->_read_response($dpipe); | ||
| 149 | $ok = $g->_close($g->_pop_action('file'), $error_level, $error_msg); | ||
| 150 | last if $error_level == 2; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | else { # $type eq 'D' | ||
| 154 | unless ($g->{recursive}) { | ||
| 155 | $g->_or_set_error(SSHA_SCP_ERROR, | ||
| 156 | "SCP protocol error, unexpected directory entry"); | ||
| 157 | last; | ||
| 158 | } | ||
| 159 | $ok = $g->_open(dir => $perm, $size, $name); | ||
| 160 | } | ||
| 161 | |||
| 162 | } | ||
| 163 | elsif (my ($mtime, $atime) = $buf =~ /^T(\d+)\s+\d+\s+(\d+)\s+\d+\s*$/) { | ||
| 164 | $ok = $g->_matime($mtime, $atime); | ||
| 165 | } | ||
| 166 | elsif ($buf =~ /^E$/) { | ||
| 167 | $ok = $g->_close($g->_pop_action('dir')); | ||
| 168 | } | ||
| 169 | elsif (my ($error_level, $path, $error_msg) = $buf =~ /^([\x01\x02])scp:(?:\s(.*))?:\s*(.*)$/) { | ||
| 170 | _scp_unescape_name($path) if defined $path; | ||
| 171 | $g->_remote_error($path, $error_msg); | ||
| 172 | next; # do not reply to errors! | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | $g->_or_set_error(SSHA_SCP_ERROR, "SCP protocol error"); | ||
| 176 | $debug and $debug & 4096 and _debug_hexdump "unknown command received", $buf; | ||
| 177 | last; | ||
| 178 | } | ||
| 179 | |||
| 180 | $dpipe->syswrite( $ok | ||
| 181 | ? "\x00" | ||
| 182 | : ( $g->{aborted} ? "\x02" : "\x01") . $g->last_error . "\x0A" ) | ||
| 183 | or last; | ||
| 184 | } | ||
| 185 | |||
| 186 | $dpipe->close; | ||
| 187 | |||
| 188 | $g->_clean_actions; | ||
| 189 | |||
| 190 | if (not $g->on_end_of_get or $g->{error_count}) { | ||
| 191 | $g->_or_set_error(SSHA_SCP_ERROR, "SCP transfer not completely successful"); | ||
| 192 | } | ||
| 193 | |||
| 194 | if ($any->{error}) { | ||
| 195 | if ($any->{error} == SSHA_REMOTE_CMD_ERROR) { | ||
| 196 | $any->_set_error(SSHA_SCP_ERROR, $any->{error}); | ||
| 197 | } | ||
| 198 | return; | ||
| 199 | } | ||
| 200 | return 1; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; |