| 1 | package Net::SFTP::Foreign::Backend::Windows; | ||
| 2 | |||
| 3 | our $VERSION = '1.70_08'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | our @CARP_NOT = qw(Net::SFTP::Foreign); | ||
| 10 | |||
| 11 | use IPC::Open3; | ||
| 12 | use POSIX (); | ||
| 13 | use Net::SFTP::Foreign::Helpers; | ||
| 14 | use Net::SFTP::Foreign::Constants qw(SSH2_FX_BAD_MESSAGE | ||
| 15 | SFTP_ERR_REMOTE_BAD_MESSAGE); | ||
| 16 | |||
| 17 | require Net::SFTP::Foreign::Backend::Unix; | ||
| 18 | our @ISA = qw(Net::SFTP::Foreign::Backend::Unix); | ||
| 19 | |||
| 20 | sub _defaults { | ||
| 21 | ( queue_size => 16 ) | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _init_transport_streams { | ||
| 25 | my ($backend, $sftp) = @_; | ||
| 26 | binmode $sftp->{ssh_in}; | ||
| 27 | binmode $sftp->{ssh_out}; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _open_dev_null { | ||
| 31 | my $sftp = shift; | ||
| 32 | my $dev_null; | ||
| 33 | unless (open $dev_null, '>', 'NUL:') { | ||
| 34 | $sftp->_conn_failed("Unable to redirect stderr for slave SSH process to NUL: $!"); | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | $dev_null | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _open4 { | ||
| 41 | my $backend = shift; | ||
| 42 | my $sftp = shift; | ||
| 43 | |||
| 44 | defined $_[3] and croak "setting child PTY is not supported on Windows"; | ||
| 45 | |||
| 46 | my $fno = eval { defined $_[2] ? fileno $_[2] : fileno *STDERR }; | ||
| 47 | unless (defined $fno and $fno >= 0) { | ||
| 48 | $sftp->_conn_failed("STDERR or stderr_fh is not a real file handle: " . (length $@ ? $@ : $!)); | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | |||
| 52 | local *SSHERR; | ||
| 53 | unless (open(SSHERR, ">>&=", $fno)) { | ||
| 54 | $sftp->_conn_failed("Unable to duplicate stderr redirection file handle: $!"); | ||
| 55 | return undef; | ||
| 56 | } | ||
| 57 | |||
| 58 | goto NOTIE unless tied *STDERR; | ||
| 59 | local *STDERR; | ||
| 60 | unless (open STDERR, ">&=2") { | ||
| 61 | $sftp->_conn_failed("Unable to reattach STDERR to fd 2: $!"); | ||
| 62 | return; | ||
| 63 | } | ||
| 64 | NOTIE: | ||
| 65 | local ($@, $SIG{__DIE__}, $SIG{__WARN__}); | ||
| 66 | |||
| 67 | my $ppid = $$; | ||
| 68 | my $pid = eval { open3(@_[1,0], ">&SSHERR", @_[4..$#_]) }; | ||
| 69 | $ppid == $$ or POSIX::_exit(-1); | ||
| 70 | $pid; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _after_init {} | ||
| 74 | |||
| 75 | sub _sysreadn { | ||
| 76 | my ($sftp, $n) = @_; | ||
| 77 | my $bin = \$sftp->{_bin}; | ||
| 78 | while (1) { | ||
| 79 | my $len = length $$bin; | ||
| 80 | return 1 if $len >= $n; | ||
| 81 | my $read = sysread($sftp->{ssh_in}, $$bin, $n - $len, $len); | ||
| 82 | unless ($read) { | ||
| 83 | $sftp->_conn_lost; | ||
| 84 | return undef; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | return $n; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _do_io { | ||
| 91 | my ($backend, $sftp, $timeout) = @_; | ||
| 92 | |||
| 93 | return undef unless $sftp->{_connected}; | ||
| 94 | |||
| 95 | my $bin = \$sftp->{_bin}; | ||
| 96 | my $bout = \$sftp->{_bout}; | ||
| 97 | |||
| 98 | while (length $$bout) { | ||
| 99 | my $written = syswrite($sftp->{ssh_out}, $$bout, 20480); | ||
| 100 | unless ($written) { | ||
| 101 | $sftp->_conn_lost; | ||
| 102 | return undef; | ||
| 103 | } | ||
| 104 | substr($$bout, 0, $written, ""); | ||
| 105 | } | ||
| 106 | |||
| 107 | defined $timeout and $timeout <= 0 and return; | ||
| 108 | |||
| 109 | _sysreadn($sftp, 4) or return undef; | ||
| 110 | |||
| 111 | my $len = 4 + unpack N => $$bin; | ||
| 112 | if ($len > 256 * 1024) { | ||
| 113 | $sftp->_set_status(SSH2_FX_BAD_MESSAGE); | ||
| 114 | $sftp->_set_error(SFTP_ERR_REMOTE_BAD_MESSAGE, | ||
| 115 | "bad remote message received"); | ||
| 116 | return undef; | ||
| 117 | } | ||
| 118 | _sysreadn($sftp, $len); | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; |