blib/lib/LWP/Protocol/rsync.pm

filter: Statementstmt 49.7% 92 / 185 Branchbran 22.5% 23 / 102 Conditioncond 12.1% 4 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 75.0% 15 / 20 Podpod 100% 1 / 1 totaltotal 37.4% 135 / 361 CC 64 SCAR 56.5
CC
64
Cov
37%
request
547.5
_http_message_content_to_fh
90.0
_wstat_str
30.0
CRAP 285.0
↑ Summary
1 # Copyright 2012, 2013, 2014, 2018, 2019 Kevin Ryde
2  
3 # This file is part of LWP-Protocol-rsync.
4 #
5 # LWP-Protocol-rsync is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # LWP-Protocol-rsync is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with LWP-Protocol-rsync. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # RFC 5781 rsync schema
20 # RFC 3986 URI general
21 # RFC 2518 WebDAV
22 # RFC 2068 HTTP 1.1
23 # - reason phrase in status line can be changed without affecting protocol
24 # RFC 2616 HTTP 1.1
25 #
26 # cf LWP::Protocol::ftp
27 # LWP::Protocol::http
28 # LWP::Protocol::file
29  
30  
31 package LWP::Protocol::rsync;
32 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use 5.005; # for \z
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use HTTP::Status ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use IPC::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use LWP::MediaTypes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 3 ▶use vars '$VERSION','@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 $VERSION = 2;
44  
45 3 ▶use LWP::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 @ISA = ('LWP::Protocol');
47  
48 # uncomment this to run the ### lines
49 # use Smart::Comments;
50  
51  
52 # $arg is
53 # undef -- use HTTP::Request / HTTP::Response ->content()
54 # scalar -- filename
55 # coderef -- func to call with data blocks of $size bytes
56 #
57 2 ▶sub request {
Subroutine request: called CC=41 SCAR=63.1
Pod documented
58 2 my($self, $request, $proxy, $arg, $size) = @_;
59 2 ▶ $size = 4096 unless defined $size and $size > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size and $size > 0 2 0 2
Condition33%1/3
defined $size and $size > 0
!ll&&!rl&&r
200
MC/DC0%0/2
defined $size and $size > 0
defined $size $size > 0
Truth table33%1/3
A and B
A= defined $sizeB= $size > 0
ABresult
0X0
100
111
60  
61 2 ▶ if (defined $proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proxy) 0 2 2
62 0 return HTTP::Response->new(HTTP::Status::RC_BAD_REQUEST(),
63 'No proxy support for rsync');
64 }
65  
66 # URI::rsync documented in URI.pm
67 2 my $uri = $request->uri;
68 ### $uri
69  
70 2 my $scheme = $uri->scheme;
71 2 ▶ if ($scheme ne 'rsync') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme ne "rsync") 0 2 2
72 0 return HTTP::Response->new(HTTP::Status::RC_INTERNAL_SERVER_ERROR(),
73 "URI scheme not rsync");
74 }
75  
76 2 my $hostport = $uri->host_port; # eg "localhost:9999"
77 2 my $user = $uri->user; # or undef if no "fred@" part present
78 2 my $password = $uri->password;
79 2 my $path = $uri->path;
80 ### $path
81  
82 2 foreach ($hostport, $user, $password, $path) {
83 8 ▶ if (defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 4 4 8
84 4 $_ = URI::Escape::uri_unescape($_); # mutate to undo %20 etc
85 }
86 }
87 ### path unescaped: $path
88  
89 # wildcards bad in $path (but ok in $hostport as "[::1]" etc IPv6)
90 2 ▶ if ($path =~ /[*?[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /[*?[]/) 0 2 2
91 0 return HTTP::Response->new(HTTP::Status::HTTP_NOT_IMPLEMENTED(),
92 "Characters * ? [ not allowed in path");
93 }
94  
95 2 ▶ my $uri_str = "rsync://".(defined $user ? "$user@" : "").$hostport.$path;
Branch: 1 branch
BranchTrueFalseTotal
defined $user ? : 0 2 2
96 ### $uri_str
97  
98 2 my $method = $request->method;
99 ### $method
100  
101 2 ▶ my $dir_listing = ($uri_str =~ m{/\z}
Condition33%1/3
$uri_str =~ m[/\z] || _path_is_modules_or_root($path)
l!l&&r!l&&!r
002
MC/DC0%0/2
$uri_str =~ m[/\z] || _path_is_modules_or_root($path)
$uri_str =~ m[/\z] _path_is_modules_or_root($path)
Truth table33%1/3
A || B
A= $uri_str =~ m[/\z]B= _path_is_modules_or_root($path)
ABresult
1X1
011
000
102 || _path_is_modules_or_root($path));
103 2 my $content_type;
104 2 ▶ if ($dir_listing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_listing) 0 2 2
105 0 $content_type = 'text/plain';
106 }
107 ### $dir_listing
108 ### $content_type
109  
110 2 ▶ if ($method eq 'HEAD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "HEAD") 1 1 2
111 # With --no-dirs a directory sets $listing to either
112 # skipping directory foo # no trailing /
113 # skipping directory . # if trailing /
114 # Without --no-dirs and without trailing slash is
115 # drwxrwxrwt 69,632 2014/03/26 19:18:01 tmp
116 #
117 # For the root directory "rsync://hostname/module/" or
118 # "rsync://hostname/module" both give the full listing of the root
119 # directory, not just the root "/" itself. Use --quiet to suppress that
120 # so as to check just the existence. Likewise the module listing is
121 # suppressed with --quiet.
122  
123 1 $uri_str =~ s{/+\z}{}; # strip trailing slashes
124 ### uri strip trailing slashes: $uri_str
125  
126 1 my $listing;
127 1 ▶ if (my $resp = _run_rsync($password,
Branch: 2 branches
BranchTrueFalseTotal
if (my $resp = _run_rsync($password, ["rsync", "--no-dirs", _path_is_modules_or_root($path) ? "--quiet" : (), $uri_str], \$listing)) 1 0 1
_path_is_modules_or_root($path) ? : 0 1 1
128 [ 'rsync',
129 '--no-dirs',
130 (_path_is_modules_or_root($path)
131 ? '--quiet'
132 : ()),
133 $uri_str, # single arg means --list-only
134 ],
135 \$listing)) {
136 1 return $resp;
137 }
138  
139 0 my ($perms, $length, $mtime) = _parse_listing($listing);
140 ### $perms
141 ### $length
142 ### $mtime
143  
144 # No Content-Length for directory. The size in the listing is the
145 # directory size on disk and is not the length the GET listing will
146 # give. Could fetch the whole listing like GET to find the size, but
147 # the reason for HEAD is not to do a full fetch like that.
148 0 ▶ my $dir_listing ||= ($listing =~ /^(d|skipping directory)/);
Condition0%0/3
my $dir_listing ||= $listing =~ /^(d|skipping directory)/
l!l&&r!l&&!r
000
MC/DC0%0/2
my $dir_listing ||= $listing =~ /^(d|skipping directory)/
my $dir_listing $listing =~ /^(d|skipping directory)/
Truth table0%0/3
A ||= B
A= my $dir_listingB= $listing =~ /^(d|skipping directory)/
ABresult
1X1
011
000
149 0 ▶ if ($dir_listing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_listing) 0 0 0
150 0 undef $length;
151 0 $content_type = 'text/plain';
152 }
153 0 my $resp = HTTP::Response->new(HTTP::Status::RC_OK(),
154 undef,
155 [ _content_headers($length, $mtime, $content_type) ]);
156 0 ▶ unless ($content_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_type) 0 0 0
157 0 LWP::MediaTypes::guess_media_type($uri, $resp->headers);
158 }
159 0 return $resp;
160 }
161  
162 # If :content_file then rsync directly to or from it, otherwise a temp
163 # file.
164 1 my $temp_fh; # if a temp file is used
165 my $filename; # either $arg or $temp_fh->filename()
166 1 ▶ if ($arg && ! ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg and not ref $arg) { } 0 1 1
Condition33%1/3
$arg and not ref $arg
!ll&&!rl&&r
100
MC/DC0%0/2
$arg and not ref $arg
$arg not ref $arg
Truth table33%1/3
A and not(B)
A= $argB= not ref $arg
ABresult
0X0
100
111
167 ### arg is content_file ...
168 0 $filename = $arg;
169 # $arg = \&_content_cb_noop;
170 } else {
171 ### arg not a file, make a temp file ...
172 1 require File::Temp;
173 1 $temp_fh = File::Temp->new;
174 1 $filename = $temp_fh->filename;
175 1 ▶ binmode($temp_fh)
Branch: 1 branch
BranchTrueFalseTotal
binmode $temp_fh or return "HTTP::Response"->new(500, "Cannot binmode on ${filename}: $!") 1 0 1
176 or return HTTP::Response->new(HTTP::Status::RC_INTERNAL_SERVER_ERROR(),
177 "Cannot binmode on $filename: $!");
178 }
179 ### $filename
180  
181 1 ▶ if ($method eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "GET") 1 0 1
182 ### _path_is_modules_or_root: _path_is_modules_or_root($path)
183  
184 # No If-Modified-Since check on a directory or on the modules listing
185 # "rsync://hostname/module".
186 #
187 1 ▶ if (! $dir_listing
Branch: 1 branch
BranchTrueFalseTotal
if (not $dir_listing and defined(my $ims_str = $request->header("If-Modified-Since"))) 0 1 1
Condition33%1/3
not $dir_listing and defined(my $ims_str = $request->header("If-Modified-Since"))
!ll&&!rl&&r
010
MC/DC0%0/2
not $dir_listing and defined(my $ims_str = $request->header("If-Modified-Since"))
not $dir_listing defined(my $ims_str = $request->header("If-Modified-Since"))
Truth table33%1/3
not(A) and B
A= not $dir_listingB= defined(my $ims_str = $request->header("If-Modified-Since"))
ABresult
0X0
100
111
188 && defined (my $ims_str = $request->header('If-Modified-Since'))) {
189 0 ▶ if (defined (my $ims_time = HTTP::Date::str2time($ims_str))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ims_time = &HTTP::Date::str2time($ims_str))) 0 0 0
190 0 my $listing;
191 0 _run_rsync($password,
192 [ 'rsync', $uri_str ], # same as HEAD above
193 \$listing);
194 0 ▶ if (defined $listing) { # if rsync ran successfully
Branch: 1 branch
BranchTrueFalseTotal
if (defined $listing) 0 0 0
195 0 my ($perms, $length, $mtime) = _parse_listing($listing);
196 ### mtime : $mtime
197 ### ims_time: $ims_time
198 0 ▶ if (! (defined $perms && $perms =~ /^d/) # no check of directory listing
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $perms && $perms =~ /^d/ and defined $mtime and $mtime <= $ims_time) 0 0 0
Condition0%0/9
not defined $perms && $perms =~ /^d/ and defined $mtime and $mtime <= $ims_time
!ll&&!rl&&r
000
not defined $perms && $perms =~ /^d/ and defined $mtime
!ll&&!rl&&r
000
defined $perms && $perms =~ /^d/
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $perms && $perms =~ /^d/ and defined $mtime and $mtime <= $ims_time
defined $perms $perms =~ /^d/ defined $mtime $mtime <= $ims_time
Truth table0%0/7
not(A && B) and C and D
A= defined $permsB= $perms =~ /^d/C= defined $mtimeD= $mtime <= $ims_time
ABCDresult
11XX0
0X0X0
100X0
0X100
10100
0X111
10111
199 && defined $mtime && $mtime <= $ims_time) {
200 0 my $resp = HTTP::Response->new (HTTP::Status::RC_NOT_MODIFIED(),
201 undef,
202 [ _content_headers($length, $mtime) ]);
203 0 LWP::MediaTypes::guess_media_type($uri, $resp->headers);
204 0 return $resp;
205 }
206 }
207 }
208 }
209  
210 1 my $mtime;
211  
212 1 ▶ if (! $dir_listing) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir_listing) 1 0 1
213 1 my $stdout;
214 1 ▶ if (my $resp = _run_rsync($password,
Branch: 1 branch
BranchTrueFalseTotal
if (my $resp = _run_rsync($password, ["rsync", "--checksum", "-t", "--inplace", $uri_str, $filename], \$stdout)) 1 0 1
215 [ 'rsync',
216 '--checksum', # no date/size quick check
217 '-t', # -t set destination $filename modtime
218 '--inplace', # write into $filename rather than renaming
219 $uri_str,
220 $filename ],
221 \$stdout)) {
222 1 return $resp;
223 }
224 # if the path was in fact a directory then re-run to get its listing
225 0 $dir_listing = ($stdout =~ /^skipping directory/mi);
226 }
227  
228 # For non-root directory listing must have trailing slash like
229 # "rsync://hostname/module/dirname/" otherwise the listing is just the
230 # directory itself like
231 # "drwxrwxrwt 69,632 2014/03/26 19:38:01 tmp"
232 #
233 0 ▶ if ($dir_listing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_listing) { } 0 0 0
234 0 ▶ unless ($uri_str =~ m{/\z}) { $uri_str .= '/'; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri_str =~ m[/\z]) 0 0 0
235 0 ▶ if (my $resp = _run_rsync($password,
Branch: 1 branch
BranchTrueFalseTotal
if (my $resp = _run_rsync($password, ["rsync", $uri_str], $filename)) 0 0 0
236 [ 'rsync', $uri_str ],
237 $filename)) {
238 0 return $resp;
239 }
240 0 $content_type = 'text/plain';
241 } else {
242 0 $mtime = _stat_mtime($filename)
243 }
244  
245 0 my $resp = HTTP::Response->new(HTTP::Status::RC_OK(),
246 undef,
247 [ _content_headers(-s $filename, $mtime, $content_type) ]);
248 0 ▶ unless ($content_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_type) 0 0 0
249 0 LWP::MediaTypes::guess_media_type($uri, $resp->headers);
250 }
251  
252 # If not read directly into :content_file $arg filename then collect
253 # from $temp_fh into $resp. collect() enforces max_size and has
254 # some callbacks.
255 #
256 # FIXME: Should we worry about those for the :content_file case? For
257 # the max_size we already have the full content, would there be any
258 # merit in truncating it?
259 #
260 0 ▶ if ($temp_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_fh) 0 0 0
261 0 my $readerr;
262 0 ▶ $self->collect($arg, $resp, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
263 0 my $content = "";
264 0 my $bytes = sysread($temp_fh, $content, $size);
265 ### $bytes
266 0 ▶ if (! defined $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bytes) 0 0 0
267 0 $readerr = "$!";
268 0 return '';
269 }
270 0 return \$content;
271 0 });
272 0 ▶ if (defined $readerr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $readerr) 0 0 0
273 0 return HTTP::Response->new(HTTP::Status::RC_INTERNAL_SERVER_ERROR(),
274 "Error reading $filename: $readerr");
275 }
276 }
277  
278 0 return $resp;
279 }
280  
281 0 ▶ if ($method eq 'PUT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "PUT") 0 0 0
282 ### PUT ...
283  
284 # ENHANCE-ME: Does "Content-Range" mean ->content() is only that part of
285 # the data?
286  
287 0 ▶ if ($temp_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_fh) 0 0 0
288 0 ▶ if (defined (my $err = _http_message_content_to_fh($request, $temp_fh,
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $err = _http_message_content_to_fh($request, $temp_fh, $filename))) 0 0 0
289 $filename))) {
290 0 return HTTP::Response->new(HTTP::Status::RC_INTERNAL_SERVER_ERROR(),
291 "Error writing $filename: $err");
292 }
293 }
294 ### ls: system("ls -d $filename")
295  
296 0 ▶ if (my $resp = _run_rsync($password,
Branch: 1 branch
BranchTrueFalseTotal
if (my $resp = _run_rsync($password, ["rsync", "--checksum", "--inplace", $filename, $uri_str])) 0 0 0
297 [ 'rsync',
298 '--checksum', # full check, not date/size quick
299 '--inplace', # write into file, not rename
300 $filename,
301 $uri_str ])) {
302 0 return $resp;
303 }
304  
305 # Per RFC 2616 and webdav RFC 2518
306 # "201 Created" is for newly created destination resource.
307 # ENHANCE-ME: Supposed to be "200 OK" for existing destination modified.
308 #
309 0 return HTTP::Response->new(HTTP::Status::RC_CREATED());
310 }
311  
312 0 return HTTP::Response->new(HTTP::Status::RC_NOT_IMPLEMENTED(),
313 "Unrecognised rsync method: $method");
314 }
315  
316 2 ▶sub _run_rsync {
Subroutine _run_rsync: called CC=9 SCAR=31.3
317 2 my ($password, $command_line, $stdout_ref) = @_;
318 ### _run_rsync() ...
319 # ### diagnostic -ivvvv: splice @$command_line, 1,0, '-ivv'
320  
321 2 ▶ if (! $stdout_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stdout_ref) 0 2 2
322 0 my $stdout;
323 0 $stdout_ref = \$stdout;
324 }
325 2 my $stderr;
326 my $eval;
327 {
328 # Always set $ENV{RSYNC_PASSWORD}, to an empty string if nothing else.
329 # Otherwise rsync will prompt for a password with getpass() or similar
330 # (which opens and read /dev/tty).
331 #
332 # Does --protect-args do anything when talking to the daemon? Turn it
333 # on since certainly don't want space splitting etc. Do this by
334 # $ENV{'RSYNC_PROTECT_ARGS'} since the option is only in rsync 3.1 up.
335 #
336 # $ENV{'TZ'} set to GMT so that the date/time in the listing output will
337 # be in GMT. If the local timezone has any daylight savings then when
338 # the clocks go back times are duplicated and so are ambiguous.
339 #
340 2 ▶ if (! defined $password) { $password = ''; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password) 2 0 2
341 2 local %ENV = (%ENV,
342 RSYNC_PROTECT_ARGS => 1,
343 RSYNC_PASSWORD => $password,
344 TZ => 'GMT+0');
345  
346 ### $command_line
347 ### RSYNC_PASSWORD: $ENV{'RSYNC_PASSWORD'}
348 2 $eval = eval {
349 2 ▶ IPC::Run::run ($command_line,
Branch: 1 branch
BranchTrueFalseTotal
defined $stdout_ref ? : 2 0 2
350 '<', File::Spec->devnull,
351 (defined $stdout_ref
352 ? ('>', $stdout_ref, '2>', \$stderr)
353 : ('>', \$stderr, '2>&1')));
354 0 1 };
355 }
356 2 ▶ if (! $eval) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval) 2 0 2
357 2 my $err = $@;
358 2 return HTTP::Response->new(HTTP::Status::RC_INTERNAL_SERVER_ERROR(),
359 "Cannot run rsync program",
360 [ 'Content-Type' => 'text/plain' ],
361 $err);
362 }
363  
364 # "401 Unauthorized" here applies to all rsync runs, GET, HEAD and PUT.
365 0 my $wstat = $?;
366 ### wstat: sprintf '0x%X', $wstat
367 ### $stdout_ref
368 ### $stderr
369 0 ▶ if ($wstat != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wstat != 0) 0 0 0
370 0 ▶ return HTTP::Response->new(($stderr =~ /\@ERROR: auth failed/
Branch: 2 branches
BranchTrueFalseTotal
$stderr =~ /\@ERROR: auth failed/ ? : 0 0 0
ref $stdout_ref && defined $$stdout_ref ? : 0 0 0
Condition0%0/3
ref $stdout_ref and defined $$stdout_ref
!ll&&!rl&&r
000
MC/DC0%0/2
ref $stdout_ref and defined $$stdout_ref
ref $stdout_ref defined $$stdout_ref
Truth table0%0/3
A and B
A= ref $stdout_refB= defined $$stdout_ref
ABresult
0X0
100
111
371 ? HTTP::Status::RC_UNAUTHORIZED()
372 : HTTP::Status::RC_NOT_FOUND()),
373 undef,
374 [ 'Content-Type' => 'text/plain' ],
375 join ('',
376 "rsync program ", _wstat_str($wstat), "\n",
377 (ref $stdout_ref && defined $$stdout_ref ? ($$stdout_ref, "\n") : ()),
378 $stderr, "\n"));
379 }
380  
381 0 return;
382 }
383  
384 # POSIX::WIFEXITED() and friends either croak (Perl 5.8 up) or don't exist
385 # at all (5.6.x and earlier) if not available from the system.
386 #
387 0 ▶sub _wstat_str {
Subroutine _wstat_str: not called CC=5 SCAR=34.0
388 0 my ($wstat) = @_;
389 0 require POSIX;
390 0 ▶ if (eval { POSIX::WIFEXITED($wstat) }
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { POSIX::WIFEXITED($wstat) } } and defined(my $exit_code = eval { do { POSIX::WEXITSTATUS($wstat) } })) 0 0 0
Condition0%0/3
eval { do { POSIX::WIFEXITED($wstat) } } and defined(my $exit_code = eval { do { POSIX::WEXITSTATUS($wstat) } })
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { POSIX::WIFEXITED($wstat) } } and defined(my $exit_code = eval { do { POSIX::WEXITSTATUS($wstat) } })
eval { do { POSIX::WIFEXITED($wstat) } } defined(my $exit_code = eval { do { POSIX::WEXITSTATUS($wstat) } })
Truth table0%0/3
A and B
A= eval { do { POSIX::WIFEXITED($wstat) } }B= defined(my $exit_code = eval { do { POSIX::WEXITSTATUS($wstat) } })
ABresult
0X0
100
111
391 0 && defined (my $exit_code = eval { POSIX::WEXITSTATUS($wstat) })) {
392 0 return "exit code $exit_code";
393 }
394 0 ▶ if (eval { POSIX::WIFSIGNALED($wstat) }
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { POSIX::WIFSIGNALED($wstat) } } and defined(my $signal_number = eval { do { POSIX::WTERMSIG($wstat) } })) 0 0 0
Condition0%0/3
eval { do { POSIX::WIFSIGNALED($wstat) } } and defined(my $signal_number = eval { do { POSIX::WTERMSIG($wstat) } })
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { POSIX::WIFSIGNALED($wstat) } } and defined(my $signal_number = eval { do { POSIX::WTERMSIG($wstat) } })
eval { do { POSIX::WIFSIGNALED($wstat) } } defined(my $signal_number = eval { do { POSIX::WTERMSIG($wstat) } })
Truth table0%0/3
A and B
A= eval { do { POSIX::WIFSIGNALED($wstat) } }B= defined(my $signal_number = eval { do { POSIX::WTERMSIG($wstat) } })
ABresult
0X0
100
111
395 0 && defined (my $signal_number = eval { POSIX::WTERMSIG($wstat) })) {
396 0 return "signal $signal_number";
397 }
398 0 return sprintf "exit status 0x%X", $wstat;
399 }
400  
401 # $request is a HTTP::Message.
402 # Write its $request->content() bytes to file handle $fh.
403 # If successful return undef.
404 # If error then return a string describing the problem.
405 # $filename is used in the error message.
406 #
407 0 ▶sub _http_message_content_to_fh {
Subroutine _http_message_content_to_fh: not called CC=9 SCAR=45.0
408 0 my ($request, $fh, $filename) = @_;
409 ### _http_message_content_to_fh() ...
410  
411 0 my $content = $request->content;
412 0 ▶ if (! defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 0 0
413 0 return "no content in request";
414 }
415  
416 0 ▶ if (ref($content) eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $content eq 'SCALAR') { } 0 0 0
elsif (ref $content eq 'CODE') { } 0 0 0
elsif (not ref $content) { } 0 0 0
417 ### scalar ref ...
418 0 ▶ if (print $fh $$content) {
Branch: 1 branch
BranchTrueFalseTotal
if (print $fh $$content) 0 0 0
419 0 return; # good
420 }
421 # write error
422  
423 } elsif (ref($content) eq 'CODE') {
424 ### coderef ...
425 0 for (;;) {
426 0 my $buf = &$content();
427 0 ▶ if (length($buf) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf == 0) 0 0 0
428 0 return; # good
429 }
430 0 ▶ print $fh $buf
Branch: 1 branch
BranchTrueFalseTotal
print $fh $buf or last 0 0 0
431 or last; # write error
432 }
433  
434 } elsif (! ref $content) {
435 ### plain scalar ...
436 0 ▶ if (print $fh $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (print $fh $content) 0 0 0
437 0 return; # good
438 }
439 # write error
440  
441 } else {
442 0 return "unrecognised request content()";
443 }
444  
445 0 return "Cannot write $filename: $!";
446 }
447  
448 # sub _content_cb_noop {
449 # }
450  
451 # Return the mtime modification time of a filename or file handle.
452 0 ▶sub _stat_mtime {
Subroutine _stat_mtime: not called CC=1 SCAR=6.9
453 0 my ($fh_or_filename) = @_;
454 0 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$filesize,
455 $atime,$mtime,$ctime,$blksize,$blocks) = stat $fh_or_filename;
456 0 return $mtime;
457 }
458  
459 # $listing is a file info string from rsync.
460 # Return ($perms, $length, $mtime), possibly with undefs if parts not
461 # recognised.
462 # $perms is a string
463 # $length is an integer number
464 # $mtime is a time_t number
465 #
466 # $listing is like
467 # -rw-r--r-- 1,260 2004/10/29 04:50:12 foo.txt
468 # or for a symlink
469 # lrwxrwxrwx 3 2014/03/20 17:21:21 bar -> foo
470 #
471 # rsync 3.1 introduces commas as digit grouping for the size. Or dots if
472 # the decimal point is not a dot in the locale. The "k" etc abbreviations
473 # are confined to --human-readable so don't occur. Code in rsync
474 # lib/compat.c. Any dots or commas are removed for the returned $length.
475 #
476 # Date/time in the listing is in the client-side system timezone.
477 # _run_rsync() above forces it to GMT and that's how it's treated here when
478 # converting to time_t $mtime.
479 #
480 # rsync code in generator.c list_file_entry().
481 #
482 3 ▶sub _parse_listing {
Subroutine _parse_listing: called CC=2 SCAR=7.0
483 3 my ($listing) = @_;
484 ### $listing
485  
486 3 my ($perms, $length, $mtime);
487 3 ▶ if (($perms, $length, my $mtime_str)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $listing =~ m[\s*(\S+)\s+([0-9,.]+)\s+([0-9/]+ [0-9:]+)]) 3 0 3
488 = ($listing =~ m{\s*(\S+)\s+([0-9,.]+)\s+([0-9/]+ [0-9:]+)})) {
489 3 $length =~ tr/.,//d; # delete commas and dots
490 3 $mtime = HTTP::Date::str2time($mtime_str, 'GMT');
491 }
492 3 return ($perms, $length, $mtime);
493 }
494  
495 # return a list
496 0 ▶sub _content_headers {
Subroutine _content_headers: not called CC=2 SCAR=17.9
497 0 my ($length, $mtime, $content_type) = @_;
498 0 ▶ return ('Content-Length' => $length,
Branch: 1 branch
BranchTrueFalseTotal
defined $mtime ? : 0 0 0
499 (defined $mtime ? ('Last-Modified' => HTTP::Date::time2str($mtime)) : ()),
500 'Content-Type' => $content_type,
501 # 'X-Rsync-Perms' => $perms, # any good?
502 );
503 }
504  
505 # Return true if $path has no module part, hence giving a modules listing,
506 # or has a module part but only the root directory.
507 3 ▶sub _path_is_modules_or_root {
Subroutine _path_is_modules_or_root: called CC=1 SCAR=0.0
508 3 my ($path) = @_;
509 3 return scalar($path !~ m{^/[^/]+ # module part
510 /.*
511 [^./]* # some non-. or / means not root dir
512 }x);
513 }
514  
515 3 1;
516 __END__
↑ Summary