File: Windows.pm

package info (click to toggle)
libnet-sftp-foreign-perl 1.73%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 472 kB
  • sloc: perl: 5,377; sh: 48; makefile: 7
file content (121 lines) | stat: -rw-r--r-- 2,767 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package Net::SFTP::Foreign::Backend::Windows;

our $VERSION = '1.70_08';

use strict;
use warnings;

use Carp;
our @CARP_NOT = qw(Net::SFTP::Foreign);

use IPC::Open3;
use POSIX ();
use Net::SFTP::Foreign::Helpers;
use Net::SFTP::Foreign::Constants qw(SSH2_FX_BAD_MESSAGE
				     SFTP_ERR_REMOTE_BAD_MESSAGE);

require Net::SFTP::Foreign::Backend::Unix;
our @ISA = qw(Net::SFTP::Foreign::Backend::Unix);

sub _defaults {
    ( queue_size => 16 )
}

sub _init_transport_streams {
    my ($backend, $sftp) = @_;
    binmode $sftp->{ssh_in};
    binmode $sftp->{ssh_out};
}

sub _open_dev_null {
    my $sftp = shift;
    my $dev_null;
    unless (open $dev_null, '>', 'NUL:') {
	$sftp->_conn_failed("Unable to redirect stderr for slave SSH process to NUL: $!");
	return;
    }
    $dev_null
}

sub _open4 {
    my $backend = shift;
    my $sftp = shift;

    defined $_[3] and croak "setting child PTY is not supported on Windows";

    my $fno = eval { defined $_[2] ? fileno $_[2] : fileno *STDERR };
    unless (defined $fno and $fno >= 0) {
        $sftp->_conn_failed("STDERR or stderr_fh is not a real file handle: " . (length $@ ? $@ : $!));
        return;
    }

    local *SSHERR;
    unless (open(SSHERR, ">>&=", $fno)) {
        $sftp->_conn_failed("Unable to duplicate stderr redirection file handle: $!");
        return undef;
    }

    goto NOTIE unless tied *STDERR;
    local *STDERR;
    unless (open STDERR, ">&=2") {
        $sftp->_conn_failed("Unable to reattach STDERR to fd 2: $!");
        return;
    }
 NOTIE:
    local ($@, $SIG{__DIE__}, $SIG{__WARN__});

    my $ppid = $$;
    my $pid = eval { open3(@_[1,0], ">&SSHERR", @_[4..$#_]) };
    $ppid == $$ or POSIX::_exit(-1);
    $pid;
}

sub _after_init {}

sub _sysreadn {
    my ($sftp, $n) = @_;
    my $bin = \$sftp->{_bin};
    while (1) {
	my $len = length $$bin;
	return 1 if $len >= $n;
	my $read = sysread($sftp->{ssh_in}, $$bin, $n - $len, $len);
	unless ($read) {
	    $sftp->_conn_lost;
	    return undef;
	}
    }
    return $n;
}

sub _do_io {
    my ($backend, $sftp, $timeout) = @_;

    return undef unless $sftp->{_connected};

    my $bin = \$sftp->{_bin};
    my $bout = \$sftp->{_bout};

    while (length $$bout) {
	my $written = syswrite($sftp->{ssh_out}, $$bout, 20480);
	unless ($written) {
	    $sftp->_conn_lost;
	    return undef;
	}
	substr($$bout, 0, $written, "");
    }

    defined $timeout and $timeout <= 0 and return;

    _sysreadn($sftp, 4) or return undef;

    my $len = 4 + unpack N => $$bin;
    if ($len > 256 * 1024) {
        $sftp->_set_status(SSH2_FX_BAD_MESSAGE);
        $sftp->_set_error(SFTP_ERR_REMOTE_BAD_MESSAGE,
                          "bad remote message received");
        return undef;
    }
    _sysreadn($sftp, $len);
}

1;