| 1 | package Net::SSH::Any::SCP::Base; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | our @CARP_NOT = qw(Net::SSH::Any); | ||
| 7 | |||
| 8 | use Net::SSH::Any::Constants qw(SSHA_SCP_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 | sub _new { | ||
| 14 | my ($class, $any, $opts) = @_; | ||
| 15 | my $self = { any => $any, | ||
| 16 | log => delete($opts->{log}), | ||
| 17 | wanted => _gen_wanted(delete @{$opts}{qw(wanted not_wanted)}), | ||
| 18 | double_dash => _first_defined(delete($opts->{double_dash}), 1), | ||
| 19 | scp_cmd => _first_defined(delete($opts->{remote_scp_cmd}), $any->{remote_cmd}{scp}, 'scp'), | ||
| 20 | actions => [], | ||
| 21 | error_count => 0, | ||
| 22 | aborted => 0, | ||
| 23 | last_error => undef }; | ||
| 24 | bless $self, $class; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _or_set_error { shift->{any}->_or_set_error(@_) } | ||
| 28 | |||
| 29 | sub _read_line { | ||
| 30 | my $self = shift; | ||
| 31 | my $dpipe = shift; | ||
| 32 | $debug and $debug & 4096 and _debug("$self->_read_line($dpipe)..."); | ||
| 33 | for ($_[0]) { | ||
| 34 | $_ = ''; | ||
| 35 | $dpipe->sysread($_, 1) or return; | ||
| 36 | if ($_ ne "\x00") { | ||
| 37 | while (1) { | ||
| 38 | unless ($dpipe->sysread($_, 1, length $_)) { | ||
| 39 | $self->_or_set_error(SSHA_SCP_ERROR, 'broken dpipe'); | ||
| 40 | return; | ||
| 41 | } | ||
| 42 | last if /\x0A$/; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | $debug and $debug & 4096 and _debug_hexdump("line read", $_); | ||
| 46 | return length $_; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _read_response { | ||
| 51 | my ($self, $dpipe) = @_; | ||
| 52 | if ($self->_read_line($dpipe, my $buf)) { | ||
| 53 | $buf eq "\x00" and return 0; | ||
| 54 | $buf =~ /^([\x01\x02])(.*)$/ and return(wantarray ? (ord($1), $2) : ord($1)); | ||
| 55 | $debug and $debug & 4096 and _debug_hexdump "failed to read response", $buf; | ||
| 56 | $self->_or_set_error(SSHA_SCP_ERROR, "SCP protocol error"); | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | $self->_or_set_error(SSHA_SCP_ERROR, "broken dpipe"); | ||
| 60 | } | ||
| 61 | wantarray ? (2, $self->{any}->error) : 2 | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _push_action { | ||
| 65 | my ($self, %a) = @_; | ||
| 66 | push @{$self->{actions}}, \%a; | ||
| 67 | unless (defined $a{path}) { | ||
| 68 | # We don't use File::Spec here because we didn't know what | ||
| 69 | # the remote file system path separator may be. | ||
| 70 | # TODO: allow to change how paths are joined from some setting. | ||
| 71 | $a{path} = ( $a{name} =~ m|/| | ||
| 72 | ? $a{name} | ||
| 73 | : join('/', map $_->{name}, @{$self->{actions}}) ); | ||
| 74 | } | ||
| 75 | defined $self->{$_} and $a{$_} = $self->{$_} for qw(mtime atime); | ||
| 76 | push @{$self->{log}}, \%a if $self->{log}; | ||
| 77 | \%a; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _pop_action { | ||
| 81 | my ($g, $type, $may_be_undef) = @_; | ||
| 82 | my $action = pop @{$g->{actions}}; | ||
| 83 | unless ($action) { | ||
| 84 | $may_be_undef and return; | ||
| 85 | croak "internal error: _pop_action called but action stack is empty!"; | ||
| 86 | } | ||
| 87 | if (defined $type) { | ||
| 88 | $action->{type} eq $type or | ||
| 89 | croak "internal error: $type action expected at top of the queue but $action->{type} found"; | ||
| 90 | } | ||
| 91 | $action | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _set_error { | ||
| 95 | my ($self, $action, $origin, $error) = @_; | ||
| 96 | unless (defined ($action->{error})) { | ||
| 97 | $action->{error} = $error; | ||
| 98 | $action->{error_origin} = $origin; | ||
| 99 | $self->{error_count}++; | ||
| 100 | } | ||
| 101 | return | ||
| 102 | } | ||
| 103 | |||
| 104 | sub set_local_error { | ||
| 105 | my ($self, $action, $error) = @_; | ||
| 106 | $error = $! unless defined $error; | ||
| 107 | $self->{last_error} = $error; | ||
| 108 | $self->_set_error($action, 'local', $error); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub last_error { | ||
| 112 | my $self = shift; | ||
| 113 | my $error = $self->{last_error}; | ||
| 114 | (defined $error ? $error : 'unknown error') | ||
| 115 | } | ||
| 116 | |||
| 117 | sub abort { | ||
| 118 | my $self = shift; | ||
| 119 | $self->_or_set_error(SSHA_SCP_ERROR, @_) if @_; | ||
| 120 | $self->{aborted} = 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub set_remote_error { | ||
| 124 | my ($self, $action, $error) = @_; | ||
| 125 | $self->_set_error($action, 'remote', $error); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _check_wanted { | ||
| 129 | my ($self, $action) = @_; | ||
| 130 | if (my $wanted = $self->{wanted}) { | ||
| 131 | unless ($wanted->($action)) { | ||
| 132 | $debug and $debug & 4096 and | ||
| 133 | _debugf("%s->set_not_wanted, %s", $self, $action->{path}); | ||
| 134 | $action->{not_wanted} = 1; | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | 1; | ||
| 139 | } | ||
| 140 | |||
| 141 | 1; |