ClientHTTPFile.pm

filter: Statementstmt 18.6% 32 / 172 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 30.0% 6 / 20 Podpod 0.0% 0 / 1 totaltotal 13.1% 38 / 288 CC 43 SCAR 41.9
CC
43
Cov
12%
TIEHANDLE
156.0
READ
110.0
WRITE
72.0
CRAP 65.7
« NewHTTPFile.pm ↑ Summary Client.pm »
1 package MogileFS::ClientHTTPFile;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Errno qw(EIO EINVAL EPERM);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use fields ('mg',
Subroutine BEGIN: called CC=1 SCAR=0.0
11 'fid',
12 'devid',
13 'class',
14 'key',
15 'path',
16 'length',
17 'pos',
18 'ua',
19 'eof',
20 'readonly',
21 'readLineChunkSize',
22 4 );
23  
24  
25 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=12 SCAR=50.5
26 0 my MogileFS::ClientHTTPFile $self = shift;
27  
28 0 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
29  
30 0 my %args = @_;
31  
32 0 $self->{devid} = $args{devid};
33 0 $self->{path} = $args{path};
34 0 ▶ $self->{readLineChunkSize} = $args{readLineChunkSize} || 4096;
Condition0%0/2
$args{'readLineChunkSize'} || 4096
l!l
00
MC/DC0%0/1
$args{'readLineChunkSize'} || 4096
$args{'readLineChunkSize'}
Truth table0%0/2
A
A= $args{'readLineChunkSize'}
Aresult
11
00
35  
36 0 ▶ $args{backup_dests} ||= [];
Condition0%0/2
$args{"backup_dests"} ||= []
l!l
00
MC/DC0%0/1
$args{"backup_dests"} ||= []
$args{"backup_dests"}
Truth table0%0/2
A
A= $args{"backup_dests"}
Aresult
11
00
37  
38 0 my $ua = LWP::UserAgent->new( keep_alive => 60, timeout => 5 );
39  
40 0 ▶ while ($self->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"path"}) 0 0 0
41 0 my $req;
42 # overwrite needs changing to create if not exists?
43 0 ▶ if ($args{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'overwrite'}) { } 0 0 0
44 0 $req = HTTP::Request->new( PUT => $self->{path} ); # Ensure file overwritten/created, even if they don't print anything
45 } else {
46 0 $req = HTTP::Request->new( HEAD => $self->{path} );
47 }
48  
49 0 my $res = $ua->request( $req );
50  
51 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
52 0 ▶ if ($args{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'overwrite'}) { } 0 0 0
53 0 $self->{length} = 0;
54 } else {
55 0 ▶ $self->{length} = $res->header( 'Content-Length' ) || 0;
Condition0%0/2
$res->header('Content-Length') || 0
l!l
00
MC/DC0%0/1
$res->header('Content-Length') || 0
$res->header('Content-Length')
Truth table0%0/2
A
A= $res->header('Content-Length')
Aresult
11
00
56 }
57  
58 0 last;
59 } else {
60 0 my $dest = shift @{$args{backup_dests}};
61  
62 0 ▶ if ($dest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest) { } 0 0 0
63 0 $self->{devid} = $dest->[0];
64 0 $self->{path} = $dest->[1];
65 } else {
66 0 $self->{devid} = undef;
67 0 $self->{path} = undef;
68 }
69 }
70 }
71  
72 0 ▶ return unless $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"path"} 0 0 0
73  
74 0 $self->{pos} = 0;
75 0 $self->{ua} = $ua;
76 0 $self->{eof} = 0;
77  
78 0 $self->{mg} = $args{mg};
79 0 $self->{fid} = $args{fid};
80 0 $self->{key} = $args{key};
81 0 ▶ $self->{readonly} = $args{readonly} || 0;
Condition0%0/2
$args{'readonly'} || 0
l!l
00
MC/DC0%0/1
$args{'readonly'} || 0
$args{'readonly'}
Truth table0%0/2
A
A= $args{'readonly'}
Aresult
11
00
82  
83 0 return $self;
84 }
85 4 *new = *TIEHANDLE;
86  
87 0 ▶sub READ {
Subroutine READ: not called CC=10 SCAR=47.0
88 0 my MogileFS::ClientHTTPFile $self = shift;
89 0 my $buf = \$_[0]; shift;
90 0 my ($len, $offset) = @_;
91  
92 0 ▶ defined( $$buf ) or $$buf = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $$buf or $$buf = "" 0 0 0
93 0 ▶ defined( $offset ) or $offset = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $offset or $offset = 0 0 0 0
94  
95 0 ▶ if ($len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) 0 0 0
96 0 $$buf = '';
97 0 return 0;
98 }
99  
100 0 ▶ die "Negative len [$len] passed" if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 0 0 0
101  
102 0 ▶ die "Negative offset [$offset] not supported" if $offset < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offset < 0 0 0 0
103  
104 0 ▶ return 0 if ($self->EOF);
Branch: 1 branch
BranchTrueFalseTotal
if $self->EOF 0 0 0
105  
106 0 my $start = $self->{pos};
107 0 my $end = $self->{pos} + $len - 1;
108  
109 my $req = HTTP::Request->new(GET => $self->{path}, [
110 0 Range => "bytes=$start-$end",
111 ], );
112  
113 0 my $res = $self->{ua}->request( $req );
114  
115 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
116 0 ▶ if ($res->code eq RC_REQUEST_RANGE_NOT_SATISFIABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code eq 416) 0 0 0
117 0 $self->{eof} = 1;
118 0 return 0;
119 }
120
121 0 $! = EIO;
122 0 return;
123 }
124  
125 0 my $length = length( $res->content );
126  
127 0 $self->{pos} += $length;
128  
129 # Behaviour is not correct with offsets < length of existing buffer
130 0 ▶ if ($offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) { } 0 0 0
131 0 $$buf = substr($$buf, 0, $offset) . $res->content;
132 } else {
133 0 $$buf = $res->content;
134 }
135  
136 0 return $length;
137 }
138 4 *read = *READ;
139  
140 0 ▶sub WRITE {
Subroutine WRITE: not called CC=8 SCAR=42.8
141 0 my MogileFS::ClientHTTPFile $self = shift;
142  
143 0 my ($buf, $len, $offset) = @_;
144  
145 0 ▶ if ($self->{readonly}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"readonly"}) 0 0 0
146 0 $! = EPERM;
147 0 return;
148 }
149  
150 0 ▶ if (defined $len || defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len or defined $offset) 0 0 0
Condition0%0/3
defined $len or defined $offset
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $len or defined $offset
defined $len defined $offset
Truth table0%0/3
A or B
A= defined $lenB= defined $offset
ABresult
1X1
011
000
151 0 ▶ $offset = 0 if ! defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
152  
153 0 $buf = substr($buf, $offset, $len);
154 }
155  
156 0 $len = length($buf);
157  
158 0 my $start = $self->{pos};
159 0 my $end = $self->{pos} + $len - 1;
160  
161 my $req = HTTP::Request->new(PUT => $self->{path}, [
162 0 'Content-Range' => "bytes $start-$end/*",
163 ], );
164  
165 0 $req->add_content($buf);
166  
167 0 my $res = $self->{ua}->request( $req );
168  
169 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
170 0 $! = EIO;
171 0 return;
172 }
173  
174 0 ▶ if ($self->{pos} + $len > $self->{length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pos"} + $len > $self->{"length"}) 0 0 0
175 0 $self->{length} = $self->{pos} + $len;
176 }
177  
178 0 $self->{pos} += $len;
179  
180 0 ▶ $self->{eof} = ($self->{pos} == $self->{length} ? 1 :0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'pos'} == $self->{'length'} ? : 0 0 0
181  
182 0 return $len;
183 }
184 4 *write = *WRITE;
185  
186 0 ▶sub EOF {
Subroutine EOF: not called CC=3 SCAR=24.8
187 0 my MogileFS::ClientHTTPFile $self = shift;
188  
189 0 ▶ return 1 if $self->{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 0 0 0
190  
191 0 ▶ return unless $self->{length};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"length"} 0 0 0
192  
193 0 return $self->{pos} >= $self->{length};
194 }
195 4 *eof = *EOF;
196  
197 0 ▶sub TELL {
Subroutine TELL: not called CC=1 SCAR=6.9
198 0 my MogileFS::ClientHTTPFile $self = shift;
199  
200 0 return $self->{pos};
201 }
202 4 *tell = *TELL;
203  
204 0 ▶sub SEEK {
Subroutine SEEK: not called CC=5 SCAR=34.0
205 0 my MogileFS::ClientHTTPFile $self = shift;
206  
207 0 my ($offset, $whence) = @_;
208  
209 0 ▶ if ($whence == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($whence == 1) { } 0 0 0
elsif ($whence == 2) { } 0 0 0
210 0 $offset += $self->{pos};
211 } elsif ($whence == 2) {
212 0 $offset += $self->{length};
213 }
214  
215 0 ▶ if ($offset > $self->{length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > $self->{"length"}) 0 0 0
216 0 $! = EINVAL;
217 0 return 0;
218 }
219  
220 0 $self->{pos} = $offset;
221 0 ▶ $self->{eof} = ($self->{pos} == $self->{length} ? 1 :0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'pos'} == $self->{'length'} ? : 0 0 0
222  
223 0 return 1;
224 }
225 4 *seek = *SEEK;
226  
227 0 ▶sub GETC {
Subroutine GETC: not called CC=1 SCAR=6.9
228 0 my MogileFS::ClientHTTPFile $self = shift;
229  
230 0 $self->READ( my $buf, 1 );
231
232 0 return $buf;
233 }
234 4 *getc = *GETC;
235  
236 0 ▶sub PRINT {
Subroutine PRINT: not called CC=3 SCAR=24.8
237 0 my MogileFS::ClientHTTPFile $self = shift;
238  
239 0 ▶ my $buf = join(defined $, ? $, : "", @_);
Branch: 1 branch
BranchTrueFalseTotal
defined $, ? : 0 0 0
240  
241 0 ▶ $buf .= $\ if defined $\;
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 0 0
242  
243 0 $self->WRITE($buf, length($buf), 0);
244 }
245 4 *print = *PRINT;
246  
247 0 ▶sub PRINTF {
Subroutine PRINTF: not called CC=1 SCAR=6.9
248 0 my MogileFS::ClientHTTPFile $self = shift;
249
250 0 my $buf = sprintf(shift,@_);
251  
252 0 $self->WRITE($buf,length($buf),0);
253 }
254 4 *printf = *PRINTF;
255  
256 0 ▶sub CLOSE {
Subroutine CLOSE: not called CC=3 SCAR=24.8
257 0 my MogileFS::ClientHTTPFile $self = shift;
258  
259 0 ▶ if ($self->{devid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"devid"}) 0 0 0
260 0 my $mg = $self->{mg};
261  
262 my $rv = $mg->{backend}->do_request
263 ("create_close", {
264 fid => $self->{fid},
265 devid => $self->{devid},
266 domain => $mg->{domain},
267 size => $self->{length},
268 key => $self->{key},
269 path => $self->{path},
270 0 });
271
272 0 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 0 0
273 0 $@ = "$mg->{backend}->{lasterr}: $mg->{backend}->{lasterrstr}";
274 0 return undef;
275 }
276 }
277  
278 0 return 1;
279 }
280 4 *close = *CLOSE;
281  
282 0 ▶sub BINMODE {
Subroutine BINMODE: not called CC=1 SCAR=6.9
283 0 return 1;
284 }
285 4 *binmode = *BINMODE;
286  
287 0 ▶sub FILENO {
Subroutine FILENO: not called CC=1 SCAR=6.9
288 # Wanted by perl debugger
289 0 return -1;
290 }
291 4 *fileno = *FILENO;
292  
293 # Must return undef (not just '') on EOF
294 0 ▶sub READLINE {
Subroutine READLINE: not called CC=6 SCAR=37.4
295 0 my MogileFS::ClientHTTPFile $self = shift;
296  
297 0 my $retBuff;
298 0 my $startPos = $self->{pos};
299 0 my $foundEol;
300 READ:
301 0 ▶ while (!$self->EOF) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->EOF) 0 0 0
302 0 my $readBuff;
303 0 my $rc = $self->read($readBuff, $self->{readLineChunkSize});
304 # Undef $/ => we will only exit on EOF (which should be right)
305 0 ▶ $foundEol = index($readBuff, $/) if defined $/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $/ 0 0 0
306 0 ▶ if (defined($foundEol) && $foundEol >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $foundEol and $foundEol >= 0) { } 0 0 0
Condition0%0/3
defined $foundEol and $foundEol >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $foundEol and $foundEol >= 0
defined $foundEol $foundEol >= 0
Truth table0%0/3
A and B
A= defined $foundEolB= $foundEol >= 0
ABresult
0X0
100
111
307 0 $foundEol += length($/);
308 0 ▶ $retBuff ||= '';
Condition0%0/2
$retBuff ||= ""
l!l
00
MC/DC0%0/1
$retBuff ||= ""
$retBuff
Truth table0%0/2
A
A= $retBuff
Aresult
11
00
309 0 $retBuff .= substr($readBuff, 0, $foundEol);
310 # We have over-read, so go back
311 0 $self->seek($startPos + length($retBuff) , 0);
312 0 last READ;
313 }
314 else {
315 # Go round again
316 0 $retBuff .= $readBuff;
317 }
318 }
319 0 return $retBuff;
320 }
321 4 *readline = *READLINE;
322  
323 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod undocumented
324 0 my MogileFS::ClientHTTPFile $self = shift;
325  
326 0 return $self->{path};
327 }
328  
329 4 1;
« NewHTTPFile.pm ↑ Summary Client.pm »