blib/lib/DBD/Gofer/Transport/pipeone.pm

filter: Statementstmt 85.5% 101 / 118 Branchbran 50.0% 24 / 48 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 80.9% 17 / 21 Podpod 0.0% 0 / 7 totaltotal 66.9% 146 / 218 CC 32 SCAR 21.7
CC
32
Cov
71%
read_response_from_fh
19.3
receive_response_by_transport
11.0
cmd_as_string
6.0
CRAP 8.7
« blib/lib/DBI/Profile.pm ↑ Summary blib/lib/DBD/Gofer/Transport/Base.pm »
1 package DBD::Gofer::Transport::pipeone;
2  
3 # $Id: pipeone.pm 10087 2007-10-16 12:42:37Z Tim $
4 #
5 # Copyright (c) 2007, Tim Bunce, Ireland
6 #
7 # You may distribute under the terms of either the GNU General Public
8 # License or the Artistic License, as specified in the Perl README file.
9  
10 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use IPC::Open3 qw(open3);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use base qw(DBD::Gofer::Transport::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 our $VERSION = "0.010088";
22  
23 8 __PACKAGE__->mk_accessors(qw(
24 connection_info
25 go_perl
26 ));
27  
28  
29 336 ▶sub new {
Subroutine new: called CC=4 SCAR=17.2
Pod undocumented
30 336 my ($self, $args) = @_;
31 336 ▶ $args->{go_perl} ||= do {
Condition33%1/3
$args->{"go_perl"} ||= do { $INC{"blib.pm"} ? [$^X, "-Mblib"] : [$^X] }
l!l&&r!l&&!r
33600
MC/DC0%0/2
$args->{"go_perl"} ||= do { $INC{"blib.pm"} ? [$^X, "-Mblib"] : [$^X] }
$args->{"go_perl"} do { $INC{"blib.pm"} ? [$^X, "-Mblib"] : [$^X] }
Truth table33%1/3
A ||= B
A= $args->{"go_perl"}B= do { $INC{"blib.pm"} ? [$^X, "-Mblib"] : [$^X] }
ABresult
1X1
011
000
32 0 ▶ ($INC{"blib.pm"}) ? [ $^X, '-Mblib' ] : [ $^X ];
Branch: 1 branch
BranchTrueFalseTotal
$INC{'blib.pm'} ? : 0 0 0
33 };
34 336 ▶ if (not ref $args->{go_perl}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args->{"go_perl"}) 336 0 336
35 # user can override the perl to be used, either with an array ref
36 # containing the command name and args to use, or with a string
37 # (ie via the DSN) in which case, to enable args to be passed,
38 # we split on two or more consecutive spaces (otherwise the path
39 # to perl couldn't contain a space itself).
40 336 $args->{go_perl} = [ split /\s{2,}/, $args->{go_perl} ];
41 }
42 336 return $self->SUPER::new($args);
43 }
44  
45  
46 # nonblock($fh) puts filehandle into nonblocking mode
47 776 ▶sub nonblock {
Subroutine nonblock: called CC=3 SCAR=11.7
Pod undocumented
48 776 my $fh = shift;
49 776 ▶ my $flags = fcntl($fh, F_GETFL, 0)
Branch: 1 branch
BranchTrueFalseTotal
my $flags = fcntl($fh, 3, 0) or croak("Can't get flags for filehandle ${fh}: $!") 776 0 776
50 or croak "Can't get flags for filehandle $fh: $!";
51 776 ▶ fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $flags | 2048 or croak("Can't make filehandle $fh nonblocking: $!") 776 0 776
52 or croak "Can't make filehandle $fh nonblocking: $!";
53 }
54  
55  
56 388 ▶sub start_pipe_command {
Subroutine start_pipe_command: called CC=4 SCAR=14.2
Pod undocumented
57 388 my ($self, $cmd) = @_;
58 388 ▶ $cmd = [ $cmd ] unless ref $cmd eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cmd eq "ARRAY" 0 388 388
59  
60 # if it's important that the subprocess uses the same
61 # (versions of) modules as us then the caller should
62 # set PERL5LIB itself.
63  
64 # limit various forms of insanity, for now
65 388 local $ENV{DBI_TRACE}; # use DBI_GOFER_TRACE instead
66 388 local $ENV{DBI_AUTOPROXY};
67 388 local $ENV{DBI_PROFILE};
68  
69 388 my ($wfh, $rfh, $efh) = (gensym, gensym, gensym);
70 388 ▶ my $pid = open3($wfh, $rfh, $efh, @$cmd)
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open3($wfh, $rfh, $efh, @$cmd) or die "error starting @${cmd}: $!\n" 388 0 388
71 or die "error starting @$cmd: $!\n";
72 388 ▶ if ($self->trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trace) 0 388 388
73 0 $self->trace_msg(sprintf("Started pid $pid: @$cmd {fd: w%d r%d e%d, ppid=$$}\n", fileno $wfh, fileno $rfh, fileno $efh),0);
74 }
75 388 nonblock($rfh);
76 388 nonblock($efh);
77 388 my $ios = IO::Select->new($rfh, $efh);
78  
79 return {
80 388 cmd=>$cmd,
81 pid=>$pid,
82 wfh=>$wfh, rfh=>$rfh, efh=>$efh,
83 ios=>$ios,
84 };
85 }
86  
87  
88 0 ▶sub cmd_as_string {
Subroutine cmd_as_string: not called CC=2 SCAR=17.9
Pod undocumented
89 0 my $self = shift;
90 # XXX meant to return a properly shell-escaped string suitable for system
91 # but its only for debugging so that can wait
92 0 my $connection_info = $self->connection_info;
93 0 ▶ return join " ", map { (m/^[-:\w]*$/) ? $_ : "'$_'" } @{$connection_info->{cmd}};
Branch: 1 branch
BranchTrueFalseTotal
/^[-:\w]*$/ ? : 0 0 0
94 }
95  
96  
97 380 ▶sub transmit_request_by_transport {
Subroutine transmit_request_by_transport: called CC=3 SCAR=11.1
Pod undocumented
98 380 my ($self, $request) = @_;
99  
100 380 my $frozen_request = $self->freeze_request($request);
101  
102 380 my $cmd = [ @{$self->go_perl}, qw(-MDBI::Gofer::Transport::pipeone -e run_one_stdio)];
103 380 my $info = $self->start_pipe_command($cmd);
104  
105 380 my $wfh = delete $info->{wfh};
106 # send frozen request
107 380 local $\;
108 380 ▶ print $wfh $frozen_request
Branch: 1 branch
BranchTrueFalseTotal
print $wfh $frozen_request or warn "error writing to @${cmd}: $!\n" 380 0 380
109 or warn "error writing to @$cmd: $!\n";
110 # indicate that there's no more
111 380 ▶ close $wfh
Branch: 1 branch
BranchTrueFalseTotal
close $wfh or die "error closing pipe to @${cmd}: $!\n" 380 0 380
112 or die "error closing pipe to @$cmd: $!\n";
113  
114 380 $self->connection_info( $info );
115 380 return;
116 }
117  
118  
119 760 ▶sub read_response_from_fh {
Subroutine read_response_from_fh: called CC=15 SCAR=29.6
Pod undocumented
120 760 my ($self, $fh_actions) = @_;
121 760 my $trace = $self->trace;
122  
123 760 ▶ my $info = $self->connection_info || die;
Condition50%1/2
$self->connection_info || die()
l!l
7600
MC/DC0%0/1
$self->connection_info || die()
$self->connection_info
Truth table50%1/2
A
A= $self->connection_info
Aresult
11
00
124 760 my ($ios) = @{$info}{qw(ios)};
125 760 my $errors = 0;
126 760 my $complete;
127  
128 760 ▶ die "No handles to read response from" unless $ios->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $ios->count 0 760 760
129  
130 760 ▶ while ($ios->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ios->count) 1155 380 1535
131 1155 my @readable = $ios->can_read();
132 1155 for my $fh (@readable) {
133 1520 local $_;
134 1520 ▶ my $actions = $fh_actions->{$fh} || die "panic: no action for $fh";
Condition50%1/2
$fh_actions->{$fh} || die("panic: no action for $fh")
l!l
15200
MC/DC0%0/1
$fh_actions->{$fh} || die("panic: no action for $fh")
$fh_actions->{$fh}
Truth table50%1/2
A
A= $fh_actions->{$fh}
Aresult
11
00
135 1520 my $rv = sysread($fh, $_='', 1024*31); # to fit in 32KB slab
136 1520 ▶ unless ($rv) { # error (undef) or end of file (0)
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 760 760 1520
137 760 my $action;
138 760 ▶ unless (defined $rv) { # was an error
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) { } 760 0 760
139 0 ▶ $self->trace_msg("error on handle $fh: $!\n") if $trace >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 0 0
140 0 ▶ $action = $actions->{error} || $actions->{eof};
Condition0%0/3
$actions->{'error'} || $actions->{'eof'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$actions->{'error'} || $actions->{'eof'}
$actions->{'error'} $actions->{'eof'}
Truth table0%0/3
A || B
A= $actions->{'error'}B= $actions->{'eof'}
ABresult
1X1
011
000
141 0 ++$errors;
142 # XXX an error may be a permenent condition of the handle
143 # if so we'll loop here - not good
144 }
145 else {
146 760 $action = $actions->{eof};
147 760 ▶ $self->trace_msg("eof on handle $fh\n") if $trace >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 760 760
148 }
149 760 ▶ if ($action->($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$action($fh)) 760 0 760
150 760 ▶ $self->trace_msg("removing $fh from handle set\n") if $trace >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 760 760
151 760 $ios->remove($fh);
152 }
153 760 next;
154 }
155 # action returns true if the response is now complete
156 # (we finish all handles
157 760 ▶ $actions->{read}->($fh) && ++$complete;
Branch: 1 branch
BranchTrueFalseTotal
$actions->{"read"}->($fh) and ++$complete 380 380 760
158 }
159 1155 ▶ last if $complete;
Branch: 1 branch
BranchTrueFalseTotal
if $complete 380 775 1155
160 }
161 760 return $errors;
162 }
163  
164  
165 380 ▶sub receive_response_by_transport {
Subroutine receive_response_by_transport: called CC=7 SCAR=23.9
Pod undocumented
166 380 my $self = shift;
167  
168 380 ▶ my $info = $self->connection_info || die;
Condition50%1/2
$self->connection_info || die()
l!l
3800
MC/DC0%0/1
$self->connection_info || die()
$self->connection_info
Truth table50%1/2
A
A= $self->connection_info
Aresult
11
00
169 380 my ($pid, $rfh, $efh, $ios, $cmd) = @{$info}{qw(pid rfh efh ios cmd)};
170  
171 380 my $frozen_response;
172 my $stderr_msg;
173  
174 $self->read_response_from_fh( {
175 $efh => {
176 0 ▶ error => sub { warn "error reading response stderr: $!"; 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 380 ▶ eof => sub { warn "eof on stderr" if 0; 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
178 0 ▶ read => sub { $stderr_msg .= $_; 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 },
180 $rfh => {
181 0 ▶ error => sub { warn "error reading response: $!"; 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 380 ▶ eof => sub { warn "eof on stdout" if 0; 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
183 380 ▶ read => sub { $frozen_response .= $_; 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
184 },
185 380 });
186  
187 380 ▶ waitpid $info->{pid}, 0
Branch: 1 branch
BranchTrueFalseTotal
waitpid $info->{"pid"}, 0 or warn "waitpid: $!" 380 0 380
188 or warn "waitpid: $!"; # XXX do something more useful?
189  
190 380 ▶ die ref($self)." command (@$cmd) failed: $stderr_msg"
Branch: 1 branch
BranchTrueFalseTotal
unless $frozen_response 0 380 380
191 if not $frozen_response; # no output on stdout at all
192  
193 # XXX need to be able to detect and deal with corruption
194 380 my $response = $self->thaw_response($frozen_response);
195  
196 380 ▶ if ($stderr_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr_msg) 0 380 380
197 # add stderr messages as warnings (for PrintWarn)
198 0 ▶ $response->add_err(0, $stderr_msg, undef, $self->trace)
Branch: 1 branch
BranchTrueFalseTotal
unless $stderr_msg =~ /^Using .*blib/ and join($", @$cmd) =~ /-Mblib/ 0 0 0
Condition0%0/3
$stderr_msg =~ /^Using .*blib/ and join($", @$cmd) =~ /-Mblib/
!ll&&!rl&&r
000
MC/DC0%0/2
$stderr_msg =~ /^Using .*blib/ and join($", @$cmd) =~ /-Mblib/
$stderr_msg =~ /^Using .*blib/ join($", @$cmd) =~ /-Mblib/
Truth table0%0/3
A and B
A= $stderr_msg =~ /^Using .*blib/B= join($", @$cmd) =~ /-Mblib/
ABresult
0X0
100
111
199 # but ignore warning from old version of blib
200 unless $stderr_msg =~ /^Using .*blib/ && "@$cmd" =~ /-Mblib/;
201 }
202  
203 380 return $response;
204 }
205  
206  
207 8 1;
208  
209 __END__
« blib/lib/DBI/Profile.pm ↑ Summary blib/lib/DBD/Gofer/Transport/Base.pm »