Any/Backend/Net_SSH2/DPipe.pm untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 328 CC 27 SCAR 36.3
CC
27
Cov
0%
READ
110.0
WRITE
72.0
READLINE
30.0
CRAP 37.7
1 package Net::SSH::Any::Backend::Net_SSH2::DPipe;
2  
3 use strict;
4 use warnings;
5  
6 require Net::SSH::Any::DPipe;
7 our @ISA = qw(Net::SSH::Any::DPipe);
8  
9 sub _make {
10 my $class = shift;
11 my $handle = Symbol::gensym();
12 tie *$handle, join('::', $class, 'Tie'), @_;
13 bless $handle, $class;
14 $handle;
15 }
16  
17 sub DESTROY {}
18  
19 sub AUTOLOAD {
20 our $AUTOLOAD;
21 my ($name) = $AUTOLOAD =~ /([^:]*)$/;
22 my $sub = sub { tied(*{shift()})->$name(@_) };
23 no strict 'refs';
24 *{$AUTOLOAD} = $sub;
25 goto &$sub;
26 }
27  
28 package Net::SSH::Any::Backend::Net_SSH2::DPipe::Tie;
29  
30 use strict;
31 use warnings;
32  
33 use Carp;
34 our @CARP_NOT = qw(Net::SSH::Any::Backend::Net_SSH2);
35  
36 use Net::SSH::Any::Util qw($debug _debug);
37 use Net::SSH::Any::Constants qw(SSHA_EAGAIN);
38 use Symbol ();
39  
40 require Tie::Handle;
41 our @ISA = qw(Tie::Handle);
42  
43 sub TIEHANDLE {
44 my ($class, $any, $channel) = @_;
45 my $dpipe = { any => $any,
46 channel => $channel,
47 blocking => 1,
48 error => 0 };
49 bless $dpipe, $class;
50 }
51  
52 sub EOF {
53 my $dpipe = shift;
54 my $any = $dpipe->{any};
55 my $channel = $dpipe->{channel};
56 $channel->eof or ($dpipe->error and $dpipe->error != SSHA_EAGAIN);
57 }
58  
59 sub READ {
60 my ($dpipe, undef, $len, $off, $ext) = @_;
61 my $any = $dpipe->{any};
62 my $channel = $dpipe->{channel};
63 my $blocking = $dpipe->{blocking};
64 $any->_clear_error or return;
65  
66 if (defined $len) {
67 return 0 if $len <= 0;
68 }
69 else {
70 $len = 34000;
71 }
72  
73 $ext //= 0;
74  
75 my $bytes;
76 if ($off) {
77 $_[1] = '' unless defined $_[1];
78 if ($off < 0) {
79 $off += length $_[1];
80 croak "Offset outside string" if $off < 0;
81 }
82 elsif (my $after = length($_[1]) - $off) {
83 if ($after > 0) {
84 $_[1] .= ("\x00" x $after);
85 }
86 else { # $after < 0
87 substr ($_[1], $off) = '';
88 }
89 }
90 $bytes = $any->_channel_read($channel, $blocking, my ($buf), $len, $ext);
91 $_[1] .= $buf if $bytes;
92 }
93 else {
94 $bytes = $any->_channel_read($channel, $blocking, $_[1], $len, $ext);
95 $_[1] //= '';
96 }
97 $bytes || $dpipe->_check_error($bytes);
98 }
99  
100 sub WRITE {
101 my ($dpipe, undef, $len, $off) = @_;
102 my $any = $dpipe->{any};
103 my $channel = $dpipe->{channel};
104 my $blocking = $dpipe->{blocking};
105 $any->_clear_error or return;
106 my $bytes;
107 if ($off or defined $len) {
108 if (defined $off) {
109 if ($off < 0) {
110 $off += length $_[1];
111 croak "Offset outside string" if $off < 0;
112 }
113 elsif ($off >= length $_[1]) {
114 return 0;
115 }
116 }
117 else {
118 $off = 0;
119 }
120  
121 $len = length $_[1] - $off unless defined $len;
122 return $dpipe->_check_error unless $len > 0;
123 $bytes = $any->_channel_do($channel, $blocking, 'write', substr($_[1], $off, $len));
124 }
125 else {
126 $bytes = $any->_channel_do($channel, $blocking, 'write', $_[1]);
127 }
128 $bytes || $dpipe->_check_error($bytes);
129 }
130  
131 sub PRINT {
132 my $dpipe = shift;
133 my $any = $dpipe->{any};
134 my $channel = $dpipe->{channel};
135 my $buf = '';
136 my $total = 0;
137 $debug and $debug & 8192 and _debug ("$dpipe->PRINT(...)");
138  
139 while (1) {
140 while (length $buf < 34000 and @_) {
141 $buf .= $, if defined $,;
142 $buf .= shift;
143 $buf .= $\ if defined $\ and not @_;
144 }
145  
146 return $total unless length $buf;
147  
148 my $bytes = $any->_channel_do($channel, 1, 'write', $buf)
149 or return $dpipe->_check_error;
150 $total += $bytes;
151 substr($buf, 0, $bytes, '');
152 }
153 }
154  
155 sub PRINTF {
156 my $dpipe = shift;
157 my $str = sprintf(@_);
158 local $\;
159 $dpipe->PRINT($str);
160 }
161  
162 sub GETC {
163 my $dpipe = shift;
164 $dpipe->READ(my ($buf), 1);
165 return $buf;
166 }
167  
168 sub READLINE {
169 my $dpipe = shift;
170 my $any = $dpipe->{any};
171 my $channel = $dpipe->{channel};
172 $any->_clear_error or return;
173 my $line = '';
174 # TODO: optimize the case where $/ is undef reading in chunks
175 my $off = (defined $/ ? -length $/ : undef);
176 while (1) {
177 unless ($any->_channel_read($channel, 1, my ($char), 1)) {
178 $dpipe->_check_error;
179 return (length($line) ? $line : undef);
180 }
181 if ( defined $off) {
182 ++$off >= 0 and substr($line, $off) eq $/ and return $line;
183 }
184 }
185 }
186  
187 sub CLOSE {
188 my $dpipe = shift;
189 my $any = $dpipe->{any};
190 my $channel = $dpipe->{channel};
191 $any->_channel_close($dpipe->{channel});
192 $any->_check_child_error or $dpipe->_check_error;
193 }
194  
195 sub FILENO { shift->{any}{be_fileno} }
196  
197 sub blocking {
198 my $dpipe = shift;
199 $dpipe->{blocking} = !!shift if @_;
200 $dpipe->{blocking};
201 }
202  
203 # Net_SSH2 backend uses 0 to indicate that everything went well but
204 # that nothing was actually done. Perl uses 0 to indicate EOF. The
205 # following subroutine converts between both.
206 sub _check_error {
207 my ($dpipe, $bytes) = @_;
208 if (defined $bytes) {
209 return $bytes if $bytes;
210 $! = Errno::EAGAIN();
211 }
212 else {
213 $dpipe->{error} = $dpipe->{any}->error or return 0; # EOF!
214 $! = Errno::EIO();
215 }
216 return
217 }
218  
219 *sysread = \&READ;
220 *syswrite = \&WRITE;
221 *getc = \&GETC;
222 *close = \&CLOSE;
223 *print = \&PRINT;
224 *printf = \&PRINTF;
225  
226 sub error { shift->{error} }
227  
228 1;