Any/OS/AnyEvent.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 195 CC 15 SCAR 39.7
CC
15
Cov
0%
io3
110.0
open4
42.0
socketpair
2.0
CRAP 52.8
1 package Net::SSH::Any::OS::AnyEvent;
2  
3 use strict;
4 use warnings;
5  
6 use Net::SSH::Any::Util qw($debug _debug _debug_hexdump);
7 use Net::SSH::Any::Constants qw(SSHA_LOCAL_IO_ERROR);
8 require Net::SSH::Any::OS::_Base;
9 our @ISA = qw(Net::SSH::Any::OS::_Base);
10  
11 use AnyEvent::Util qw(portable_pipe portable_socketpair);
12  
13 use Carp;
14 our @CARP_NOT = ('Net::SSH::Any::Backend::_Cmd');
15  
16 sub socketpair {
17 my ($os, $any) = @_;
18 my ($a, $b) = portable_socketpair or do {
19 $any->_set_error(SSHA_LOCAL_IO_ERROR, "AnyEvent::Util::portable_socketpair failed: $!");
20 return;
21 };
22 ($a, $b)
23 }
24  
25 sub pipe {
26 my ($os, $any) = @_;
27 my ($r, $w) = portable_pipe or do {
28 $any->_set_error(SSHA_LOCAL_IO_ERROR, "AnyEvent::Util::portable_pipe failed: $!");
29 return;
30 };
31 ($r, $w);
32 }
33  
34 sub open4 {
35 my ($os, $any, $fhs, $close, $pty, $stderr_to_stdout, @cmd) = @_;
36  
37 my $pid;
38 my @opts = ('$$' => \$pid, close_all => 1);
39  
40 push @opts, '<' => $fhs->[0] if defined $fhs->[0];
41 push @opts, '>' => $fhs->[1] if defined $fhs->[1];
42 if ($stderr_to_stdout) {
43 if (defined $fhs->[1]) {
44 push @opts, '2>' => $fhs->[1]
45 }
46 else {
47 push @opts, '2>' => \*STDOUT;
48 }
49 }
50 else {
51 push @opts, '2>' => $fhs->[2] if defined $fhs->[2];
52 }
53  
54 # push @opts, on_prepare => sub { close $_ for @$close };
55  
56 my $cv = AnyEvent::Util::run_cmd(\@cmd, @opts);
57  
58 $debug and $debug & 1024 and _debug("cv: $cv");
59  
60 return { pid => $pid,
61 cv => $cv };
62 }
63  
64 sub export_proc {
65 my ($os, $any, $proc) = @_;
66 undef $proc->{cv};
67 $proc->{pid};
68 }
69  
70 my @retriable = (Errno::EINTR, Errno::EAGAIN);
71 push @retriable, Errno::EWOULDBLOCK if Errno::EWOULDBLOCK != Errno::EAGAIN;
72  
73  
74 sub io3 {
75 my ($os, $any, $proc, $timeout, $data, $in, $out, $err) = @_;
76  
77 my $bout = '';
78 my $berr = '';
79 my ($inw, $wout, $werr);
80 my $cv = AE::cv;
81 $cv->begin;
82  
83 if (@$data) {
84 $cv->begin;
85 $inw = AE::io $in, 1, sub {
86 my $bytes = syswrite($in, $data->[0], 2048);
87 if ($bytes) {
88 substr($data->[0], 0, $bytes, '');
89 shift @$data unless length $data->[0];
90 return unless @$data;
91 }
92 elsif (not defined $bytes and grep $! == $_, @retriable) {
93 return
94 }
95 undef $inw;
96 close $in;
97 $cv->end;
98 $debug and $debug & 1024 and _debug "in done";
99 }
100 }
101  
102 my $write = sub {
103 my ($fh, $buf, $w) = @_;
104 my $bytes = sysread($fh, $$buf, 20480, length $$buf);
105 unless ($bytes) {
106 if (defined $bytes or not grep $! == $_, @retriable) {
107 close $fh;
108 undef $$w;
109 $cv->end;
110 $debug and $debug & 1024 and _debug "out or err done";
111 }
112 }
113 };
114  
115 if ($out) {
116 $cv->begin;
117 $wout = AE::io $out, 0, sub { $write->($out, \$bout, \$wout) };
118 }
119  
120 if ($err) {
121 $cv->begin;
122 $werr = AE::io $err, 0, sub { $write->($err, \$berr, \$werr) };
123 }
124  
125 $cv->end;
126 $cv->recv;
127 $debug and $debug & 1024 and _debug "waiting for child";
128 $proc->{cv}->recv;
129  
130 return ($bout, $berr);
131 }
132  
133 1;