| 1 | package Net::SSH::Any::Backend::Net_SSH2::SFTP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Net::SSH::Any::Util qw($debug _debug); | ||
| 7 | use Net::SSH::Any::Constants qw(SSHA_CONNECTION_ERROR); | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | our @CARP_NOT = qw(Net::SFTP::Foreign); | ||
| 11 | |||
| 12 | sub _new { | ||
| 13 | my ($class, $any, $dpipe) = @_; | ||
| 14 | my $self = { any => $any, | ||
| 15 | dpipe => $dpipe }; | ||
| 16 | $dpipe->blocking(0); | ||
| 17 | bless $self, $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub _defaults { ( queue_size => 32) } | ||
| 21 | |||
| 22 | sub _dpipe_error { | ||
| 23 | my $self = shift; | ||
| 24 | my $error = $self->{dpipe}->error; | ||
| 25 | join (": ", @_, sprintf("%s (%d)", $error, $error)); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub _conn_failed { | ||
| 29 | my ($self, $sftp, @msg) = @_; | ||
| 30 | $sftp->_conn_failed($self->_dpipe_error(@msg)) | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | sub _conn_lost { | ||
| 35 | my ($self, $sftp, @msg) = @_; | ||
| 36 | $sftp->_conn_lost(undef, undef, $self->_dpipe_error(@msg)); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _init_transport {} | ||
| 40 | |||
| 41 | sub _after_init {} | ||
| 42 | |||
| 43 | sub _do_io { | ||
| 44 | my ($self, $sftp, $timeout) = @_; | ||
| 45 | my $dpipe = $self->{dpipe}; | ||
| 46 | return undef if $self->{any}->error == SSHA_CONNECTION_ERROR; | ||
| 47 | |||
| 48 | my $bin = \$sftp->{_bin}; | ||
| 49 | my $bout = \$sftp->{_bout}; | ||
| 50 | my $deadline; | ||
| 51 | my $len; | ||
| 52 | my $first = 1; | ||
| 53 | while (1) { | ||
| 54 | my $delay = not $first; | ||
| 55 | |||
| 56 | if ($first) { | ||
| 57 | undef $first; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | if (length $$bout) { | ||
| 61 | my $bytes = syswrite $dpipe, $$bout; | ||
| 62 | if ($bytes) { | ||
| 63 | $delay = 0; | ||
| 64 | substr $$bout, 0, $bytes, ''; | ||
| 65 | } | ||
| 66 | elsif (defined $bytes or $! != Errno::EAGAIN()) { | ||
| 67 | $self->_conn_lost($sftp, "write failed"); | ||
| 68 | return undef; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | $debug and $debug & 4096 and _debug "SFTP reading..."; | ||
| 74 | my $bytes = sysread $dpipe, $$bin, 64 * 1024, length($$bin); | ||
| 75 | $debug and $debug & 4096 and _debug "SFTP read " . ($bytes//'<undef>') . " bytes"; | ||
| 76 | if ($bytes) { | ||
| 77 | $delay = 0 | ||
| 78 | } | ||
| 79 | elsif (defined $bytes or $! != Errno::EAGAIN()) { | ||
| 80 | $self->_conn_lost($sftp, "read failed"); | ||
| 81 | return undef; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $lbin = length $$bin; | ||
| 85 | $debug and $debug & 4096 and _debug "SFTP buffers, input: $lbin, ouput: ".length($$bout); | ||
| 86 | |||
| 87 | if (defined $len) { | ||
| 88 | return 1 if $lbin >= $len; | ||
| 89 | } | ||
| 90 | elsif ($lbin >= 4) { | ||
| 91 | $len = 4 + unpack N => $$bin; | ||
| 92 | $debug and $debug & 4096 and _debug "receiving packet of $len bytes"; | ||
| 93 | if ($len > 256 * 1024) { | ||
| 94 | $sftp->_set_status(Net::SFTP::Foreign::Constants::SSH2_FX_BAD_MESSAGE); | ||
| 95 | $sftp->_set_error(Net::SFTP::Foreign::Constants::SFTP_ERR_REMOTE_BAD_MESSAGE, | ||
| 96 | "bad remote message received"); | ||
| 97 | return undef; | ||
| 98 | } | ||
| 99 | return 1 if $lbin >= $len; | ||
| 100 | } | ||
| 101 | |||
| 102 | if ($delay) { | ||
| 103 | if (defined $timeout) { | ||
| 104 | return unless $timeout; | ||
| 105 | if (defined $deadline) { | ||
| 106 | return undef if $deadline < time; | ||
| 107 | } | ||
| 108 | else { | ||
| 109 | $deadline = time + $timeout + 1; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | my ($rv, $wv); | ||
| 113 | vec ($rv='', fileno($dpipe), 1) = 1; | ||
| 114 | $wv = $rv if length $$bout; | ||
| 115 | select ($rv, $wv, undef, 0.1); | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | undef $deadline | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | sub DESTROY { | ||
| 124 | my $self = shift; | ||
| 125 | local ($@, $!, $?, $SIG{__DIE__}); | ||
| 126 | eval { close $self->{dpipe} }; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; |