Skilletor/Source/Url.pm

filter: Statementstmt 93.4% 184 / 197 Branchbran 73.4% 69 / 94 Conditioncond 64.2% 52 / 81 MC/DCmcdc 34.0% 15 / 44 Subroutinesub 100% 32 / 32 Podpod 100% 6 / 6 totaltotal 78.8% 358 / 454 CC 81 SCAR 23.2
CC
81
Cov
82%
_publish
23.3
resolve
17.4
_request
12.6
CRAP 10.2
« Skilletor/Frontmatter.pm ↑ Summary Skilletor/Bundles.pm »
1 package Skilletor::Source::Url;
2 # ABSTRACT: A url source: an https .tar.gz, conditional GET, staged swap into the shared cache
3  
4 35 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Digest::SHA qw( sha256_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Skilletor::Cache qw( url_cache_dir );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Skilletor::Error::Source;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Skilletor::Error::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Skilletor::FSUtil qw( encode_text path_join path_resolve read_text );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use Skilletor::Gzip qw( gunzip_sync inflate_sync );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 35 ▶use Skilletor::Tar qw( parse_tar strip_top_level is_reserved VERSION_FILE );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 35 ▶use Skilletor::URL qw( parse_url );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 35 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 35 with 'Skilletor::Role::Source';
20  
21 35 our $VERSION = '0.001';
22  
23 # The first line of a version file: the format of its tree. 2: an entry's owner executable bit
24 # is kept.
25 35 ▶use constant CACHE_FORMAT => 'format 2';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # A stage or backup tree the publication makes next to a cache dir.
28 35 my $LEFTOVER = qr/\A([0-9a-f]{16})\.(stage|backup)-[A-Za-z0-9]{6}\z/;
29 35 my @TEMP_CHARS = ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9 );
30  
31 35 has url => (
32 is => 'ro',
33 required => 1,
34 );
35  
36 35 has cache_root => (
37 is => 'ro',
38 required => 1,
39 );
40  
41 has timeout_ms => (
42 is => 'ro',
43 15 ▶ default => sub { 60_000 },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 35 );
45  
46 # Tests only: permit http://127.0.0.1 and http://localhost.
47 has allow_http => (
48 is => 'ro',
49 4 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 35 );
51  
52 35 has http => (
53 is => 'lazy',
54 );
55  
56 10 ▶sub _build_http {
Subroutine _build_http: called CC=1 SCAR=0.0
57 10 my ( $self ) = @_;
58 10 return HTTP::Tiny->new(
59 agent => 'node',
60 timeout => $self->timeout_ms / 1000,
61 verify_SSL => 1,
62 max_redirect => 20,
63 );
64 }
65  
66 21 ▶sub cache_dir {
Subroutine cache_dir: called CC=1 SCAR=0.0
Pod documented
67 21 my ( $self ) = @_;
68 21 return url_cache_dir( $self->cache_root, $self->url );
69 }
70  
71 28 ▶sub _assert_scheme {
Subroutine _assert_scheme: called CC=7 SCAR=19.5
72 28 my ( $self ) = @_;
73 28 ▶ my $u = parse_url( $self->url ) or Skilletor::Error::Source->throw('Invalid URL');
Branch: 1 branch
BranchTrueFalseTotal
my $u = parse_url($self->url) or "Skilletor::Error::Source"->throw("Invalid URL") 27 1 28
74 27 ▶ return if $u->{protocol} eq 'https:';
Branch: 1 branch
BranchTrueFalseTotal
if $u->{"protocol"} eq "https:" 1 26 27
75 26 ▶ my $local_http = $u->{protocol} eq 'http:' && ( $u->{hostname} eq '127.0.0.1' || $u->{hostname} eq 'localhost' );
Condition66%4/6
$u->{'protocol'} eq 'http:' && ($u->{'hostname'} eq '127.0.0.1' || $u->{'hostname'} eq 'localhost')
!ll&&!rl&&r
0125
$u->{"hostname"} eq "127.0.0.1" or $u->{"hostname"} eq "localhost"
l!l&&r!l&&!r
2501
MC/DC33%1/3
$u->{'protocol'} eq 'http:' && ($u->{'hostname'} eq '127.0.0.1' || $u->{'hostname'} eq 'localhost')
$u->{'protocol'} eq 'http:' $u->{"hostname"} eq "127.0.0.1" $u->{"hostname"} eq "localhost"
Truth table50%2/4
A && B or C
A= $u->{'protocol'} eq 'http:'B= $u->{"hostname"} eq "127.0.0.1"C= $u->{"hostname"} eq "localhost"
ABCresult
0XX0
1000
11X1
1011
76 26 ▶ return if $self->allow_http && $local_http;
Branch: 1 branch
BranchTrueFalseTotal
if $self->allow_http and $local_http 24 2 26
Condition100%3/3
$self->allow_http and $local_http
!ll&&!rl&&r
1124
MC/DC100%2/2
$self->allow_http and $local_http
$self->allow_http $local_http
Truth table100%3/3
A and B
A= $self->allow_httpB= $local_http
ABresult
0X0
100
111
77 2 Skilletor::Error::Source->throw( 'url source must be https://: '.$self->url );
78 }
79  
80 4 ▶sub _fail { Skilletor::Error::Source->throw( $_[1] ) }
Subroutine _fail: called CC=1 SCAR=0.0
81  
82 # A request with the headers Node's fetch sends, the body decoded as fetch decodes it.
83 24 ▶sub _request {
Subroutine _request: called CC=11 SCAR=25.3
84 24 my ( $self, $method, $headers ) = @_;
85 24 my %h = (
86 accept => '*/*',
87 'accept-language' => '*',
88 'sec-fetch-mode' => 'cors',
89 'accept-encoding' => 'gzip, deflate',
90 %$headers,
91 );
92 24 ▶ if ( exists $headers->{'If-None-Match'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $headers->{"If-None-Match"}) 8 16 24
93 8 $h{pragma} = 'no-cache';
94 8 $h{'cache-control'} = 'no-cache';
95 }
96 24 my $res = $self->http->request( $method, $self->url, { headers => \%h } );
97 24 ▶ if ( $res->{status} == 599 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} == 599) 1 23 24
98 1 ▶ $self->_fail( ( $res->{content} // '' ) =~ /timed? ?out/i ? 'This operation was aborted' : 'fetch failed' );
Branch: 1 branch
BranchTrueFalseTotal
($res->{'content'} // '') =~ /timed? ?out/i ? : 0 1 1
Condition50%1/2
$res->{"content"} // ""
l!l
10
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table50%1/2
A
A= $res->{"content"}
Aresult
11
00
99 }
100 23 ▶ my $encoding = lc( _header( $res, 'content-encoding' ) // '' );
Condition50%1/2
_header($res, "content-encoding") // ""
l!l
023
MC/DC0%0/1
_header($res, "content-encoding") // ""
_header($res, "content-encoding")
Truth table50%1/2
A
A= _header($res, "content-encoding")
Aresult
11
00
101 23 ▶ if ( $method eq 'GET' && length( $res->{content} // '' ) && $encoding =~ /\A(?:x-)?(gzip|deflate)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "GET" and length($res->{"content"} // "") and $encoding =~ /\A(?:x-)?(gzip|deflate)\z/) 0 23 23
Condition87%7/8
$method eq "GET" and length($res->{"content"} // "") and $encoding =~ /\A(?:x-)?(gzip|deflate)\z/
!ll&&!rl&&r
9140
$method eq "GET" and length($res->{"content"} // "")
!ll&&!rl&&r
5414
$res->{"content"} // ""
l!l
171
MC/DC0%0/3
$method eq "GET" and length($res->{"content"} // "") and $encoding =~ /\A(?:x-)?(gzip|deflate)\z/
$method eq "GET" $res->{"content"} $encoding =~ /\A(?:x-)?(gzip|deflate)\z/
Truth table75%3/4
A and B and C
A= $method eq "GET"B= $res->{"content"}C= $encoding =~ /\A(?:x-)?(gzip|deflate)\z/
ABCresult
0XX0
10X0
1100
1111
102 0 ▶ $res->{content} = $1 eq 'gzip' ? gunzip_sync( $res->{content} ) : inflate_sync( $res->{content} );
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'gzip' ? : 0 0 0
103 }
104 23 return $res;
105 }
106  
107 42 ▶sub _header {
Subroutine _header: called CC=3 SCAR=11.0
108 42 my ( $res, $name ) = @_;
109 42 my $v = $res->{headers}{$name};
110 42 ▶ return undef unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 25 17 42
111 17 ▶ return ref $v eq 'ARRAY' ? join( ', ', @$v ) : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 17 17
112 }
113  
114 9 ▶sub _msg { my ( $err ) = @_; return blessed $err && $err->can('message') ? $err->message : "$err" =~ s/ at \S+ line \d+\.?\n?\z//r }
Subroutine _msg: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($err) && $err->can('message') ? : 9 0 9
Condition33%1/3
builtin::blessed($err) and $err->can("message")
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($err) and $err->can("message")
builtin::blessed($err) $err->can("message")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->can("message")
ABresult
0X0
100
111
115  
116 21 ▶sub resolve {
Subroutine resolve: called CC=17 SCAR=28.6
Pod documented
117 21 my ( $self ) = @_;
118 21 $self->_assert_scheme;
119 19 my $dir = $self->cache_dir;
120 19 my $result = eval {
121 19 my $cached = $self->cache_version($dir);
122 19 ▶ my $etag = ( defined $cached && $cached =~ /\Aetag:(.*)\z/s ) ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $cached && $cached =~ /\Aetag:(.*)\z/s ? : 8 11 19
Condition66%2/3
defined $cached and $cached =~ /\Aetag:(.*)\z/s
!ll&&!rl&&r
1108
MC/DC50%1/2
defined $cached and $cached =~ /\Aetag:(.*)\z/s
defined $cached $cached =~ /\Aetag:(.*)\z/s
Truth table66%2/3
A and B
A= defined $cachedB= $cached =~ /\Aetag:(.*)\z/s
ABresult
0X0
100
111
123 19 ▶ my $res = $self->_request( GET => defined $etag ? { 'If-None-Match' => $etag } : {} );
Branch: 1 branch
BranchTrueFalseTotal
defined $etag ? : 8 11 19
124 18 ▶ return +{ dir => $dir, version => $cached } if $res->{status} == 304 && defined $etag;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 304 and defined $etag 1 17 18
Condition66%2/3
$res->{"status"} == 304 and defined $etag
!ll&&!rl&&r
1701
MC/DC50%1/2
$res->{"status"} == 304 and defined $etag
$res->{"status"} == 304 defined $etag
Truth table66%2/3
A and B
A= $res->{"status"} == 304B= defined $etag
ABresult
0X0
100
111
125 17 ▶ $self->_fail( 'HTTP '.$res->{status} ) unless $res->{status} >= 200 && $res->{status} <= 299;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"status"} >= 200 and $res->{"status"} <= 299 3 14 17
Condition66%2/3
$res->{"status"} >= 200 and $res->{"status"} <= 299
!ll&&!rl&&r
0314
MC/DC50%1/2
$res->{"status"} >= 200 and $res->{"status"} <= 299
$res->{"status"} >= 200 $res->{"status"} <= 299
Truth table66%2/3
A and B
A= $res->{"status"} >= 200B= $res->{"status"} <= 299
ABresult
0X0
100
111
126 14 ▶ my $body = $res->{content} // '';
Condition50%1/2
$res->{"content"} // ""
l!l
140
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table50%1/2
A
A= $res->{"content"}
Aresult
11
00
127 14 my $res_etag = _header( $res, 'etag' );
128 14 ▶ my $version = ( defined $res_etag && length $res_etag ) ? 'etag:'.$res_etag : 'sha256:'.sha256_hex($body);
Branch: 1 branch
BranchTrueFalseTotal
defined $res_etag && length $res_etag ? : 13 1 14
Condition66%2/3
defined $res_etag and length $res_etag
!ll&&!rl&&r
1013
MC/DC50%1/2
defined $res_etag and length $res_etag
defined $res_etag length $res_etag
Truth table66%2/3
A and B
A= defined $res_etagB= length $res_etag
ABresult
0X0
100
111
129 14 my @entries = grep { !is_reserved( $_->{name} ) } @{ strip_top_level( parse_tar( gunzip_sync($body) ) ) };
130 8 my $warning = $self->_publish( $dir, \@entries, $version );
131 8 ▶ +{ dir => $dir, version => $version, ( defined $warning ? ( warning => $warning ) : () ) };
Branch: 1 branch
BranchTrueFalseTotal
defined $warning ? : 0 8 8
132 };
133 19 ▶ return $result if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 9 10 19
134 10 my $err = $@;
135 10 ▶ if ( blessed $err && $err->isa('Skilletor::Error::Tar') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($err) and $err->isa("Skilletor::Error::Tar")) 1 9 10
Condition66%2/3
builtin::blessed($err) and $err->isa("Skilletor::Error::Tar")
!ll&&!rl&&r
091
MC/DC50%1/2
builtin::blessed($err) and $err->isa("Skilletor::Error::Tar")
builtin::blessed($err) $err->isa("Skilletor::Error::Tar")
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::Tar")
ABresult
0X0
100
111
136 1 Skilletor::Error::Source->throw( 'url source '.$self->url.' failed: '.$err->message );
137 }
138 9 ▶ if ( -e $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir) 6 3 9
139 return {
140 6 ▶ dir => $dir,
Condition100%2/2
$self->cache_version($dir) // "unknown"
l!l
51
MC/DC100%1/1
$self->cache_version($dir) // "unknown"
$self->cache_version($dir)
Truth table100%2/2
A
A= $self->cache_version($dir)
Aresult
11
00
141 version => $self->cache_version($dir) // 'unknown',
142 warning => 'download failed for '.$self->url.', using cache ('._msg($err).')',
143 };
144 }
145 3 Skilletor::Error::Source->throw( 'url source '.$self->url.' failed: '._msg($err) );
146 }
147  
148 # The cache as it is, without a request: what resolve serves when a download fails, without
149 # the warning.
150 1 ▶sub cached {
Subroutine cached: called CC=3 SCAR=13.3
Pod documented
151 1 my ( $self ) = @_;
152 1 $self->_assert_scheme;
153 1 my $dir = $self->cache_dir;
154 1 ▶ Skilletor::Error::Source->throw( 'url source '.$self->url.' is not in the cache (no fetch)' ) unless -e $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 1 0 1
155 0 ▶ return { dir => $dir, version => $self->cache_version($dir) // 'unknown' };
Condition0%0/2
$self->cache_version($dir) // "unknown"
l!l
00
MC/DC0%0/1
$self->cache_version($dir) // "unknown"
$self->cache_version($dir)
Truth table0%0/2
A
A= $self->cache_version($dir)
Aresult
11
00
156 }
157  
158 6 ▶sub check {
Subroutine check: called CC=6 SCAR=17.9
Pod documented
159 6 my ( $self, $cached_version ) = @_;
160 6 $self->_assert_scheme;
161 5 my $res = $self->_request( HEAD => {} );
162 5 my $etag = _header( $res, 'etag' );
163 5 ▶ return 1 unless defined $etag && length $etag && defined $cached_version && $cached_version =~ /\Aetag:/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $etag and length $etag and defined $cached_version and $cached_version =~ /\Aetag:/ 3 2 5
Condition88%8/9
defined $etag and length $etag and defined $cached_version and $cached_version =~ /\Aetag:/
!ll&&!rl&&r
212
defined $etag and length $etag and defined $cached_version
!ll&&!rl&&r
113
defined $etag and length $etag
!ll&&!rl&&r
104
MC/DC75%3/4
defined $etag and length $etag and defined $cached_version and $cached_version =~ /\Aetag:/
defined $etag length $etag defined $cached_version $cached_version =~ /\Aetag:/
Truth table80%4/5
A and B and C and D
A= defined $etagB= length $etagC= defined $cached_versionD= $cached_version =~ /\Aetag:/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
164 2 ▶ return $etag ne substr( $cached_version, 5 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$etag ne substr($cached_version, 5) ? : 1 1 2
165 }
166  
167 # The version of the tree in a cache dir; undef without a readable, well-formed one of the
168 # current format.
169 27 ▶sub cache_version {
Subroutine cache_version: called CC=7 SCAR=20.2
Pod documented
170 27 my ( $self, $dir ) = @_;
171 27 my $text = eval { read_text( path_join( $dir, VERSION_FILE ) ) };
172 27 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 11 16 27
173 16 my ( $format, $version, @rest ) = split /\n/, $text, -1;
174 16 ▶ return undef if !defined $format || $format ne CACHE_FORMAT || @rest;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $format or $format ne "format 2" or @rest 2 14 16
Condition66%4/6
not defined $format or $format ne "format 2" or @rest
l!l&&r!l&&!r
2014
not defined $format or $format ne "format 2"
l!l&&r!l&&!r
0214
MC/DC33%1/3
not defined $format or $format ne "format 2" or @rest
not defined $format $format ne "format 2" @rest
Truth table50%2/4
not(A) or B or C
A= not defined $formatB= $format ne "format 2"C= @rest
ABCresult
1XX1
01X1
0011
0000
175 14 ▶ return ( defined $version && $version =~ /\A(?:etag|sha256):[^\r\n]+\z/ ) ? $version : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $version && $version =~ /\A(?:etag|sha256):[^\r\n]+\z/ ? : 14 0 14
Condition33%1/3
defined $version and $version =~ /\A(?:etag|sha256):[^\r\n]+\z/
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $version and $version =~ /\A(?:etag|sha256):[^\r\n]+\z/
defined $version $version =~ /\A(?:etag|sha256):[^\r\n]+\z/
Truth table33%1/3
A and B
A= defined $versionB= $version =~ /\A(?:etag|sha256):[^\r\n]+\z/
ABresult
0X0
100
111
176 }
177  
178 10 ▶sub _mkdtemp {
Subroutine _mkdtemp: called CC=4 SCAR=17.1
179 10 my ( $self, $prefix ) = @_;
180 10 for ( 1 .. 100 ) {
181 10 my $path = $prefix.join '', map { $TEMP_CHARS[ int rand @TEMP_CHARS ] } 1 .. 6;
182 10 ▶ return $path if mkdir $path, 0700;
Branch: 1 branch
BranchTrueFalseTotal
if mkdir $path, 448 10 0 10
183 0 ▶ die 'mkdtemp '.$prefix.': '.$! unless $!{EEXIST};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EEXIST"} 0 0 0
184 }
185 0 die 'mkdtemp '.$prefix.': no free name';
186 }
187  
188 8 ▶sub _write_entries {
Subroutine _write_entries: called CC=8 SCAR=22.9
189 8 my ( $self, $dir, $entries ) = @_;
190 8 my $root = path_resolve($dir);
191 8 for my $e (@$entries) {
192 16 my $dest = path_resolve( path_join( $dir, $e->{name} ) );
193 16 ▶ if ( $dest ne $root && index( $dest, $root.'/' ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest ne $root and index($dest, $root . '/') != 0) 0 16 16
Condition33%1/3
$dest ne $root and index($dest, $root . '/') != 0
!ll&&!rl&&r
0160
MC/DC0%0/2
$dest ne $root and index($dest, $root . '/') != 0
$dest ne $root index($dest, $root . '/') != 0
Truth table33%1/3
A and B
A= $dest ne $rootB= index($dest, $root . '/') != 0
ABresult
0X0
100
111
194 0 Skilletor::Error::Tar->throw( 'unsafe tar entry (escapes target): '.$e->{name} );
195 }
196 16 my $bytes_dest = encode_text($dest);
197 16 ▶ if ( $e->{type} eq 'dir' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"type"} eq "dir") 0 16 16
198 0 path($bytes_dest)->mkpath;
199 0 next;
200 }
201 16 path($bytes_dest)->parent->mkpath;
202 16 ▶ sysopen( my $fh, $bytes_dest, Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_TRUNC(), $e->{executable} ? 0777 : 0666 )
Branch: 2 branches
BranchTrueFalseTotal
sysopen my $fh, $bytes_dest, 577, $e->{"executable"} ? 511 : 438 or die 'open ' . $dest . ': ' . $! . "\n" 16 0 16
$e->{'executable'} ? : 7 9 16
203 or die 'open '.$dest.': '.$!."\n";
204 16 binmode $fh;
205 16 print {$fh} $e->{data};
206 16 ▶ close $fh or die 'close '.$dest.': '.$!."\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die 'close ' . $dest . ': ' . $! . "\n" 16 0 16
207 }
208 }
209  
210 # Extract completely, with the tree's version inside it, before replacing the cache; every
211 # rename moves tree and version together. Returns the cleanup warning, if any.
212 8 ▶sub _publish {
Subroutine _publish: called CC=15 SCAR=31.5
213 8 my ( $self, $dir, $entries, $version ) = @_;
214 8 path($dir)->parent->mkpath;
215 8 my $staging = $self->_mkdtemp( $dir.'.stage-' );
216 8 my ( $backup, $old_moved, $published ) = ( undef, 0, 0 );
217 8 my @cleanup_warnings;
218 8 my $ok = eval {
219 8 $self->_write_entries( $staging, $entries );
220 8 path( $staging, VERSION_FILE )->spew_raw( encode_text( CACHE_FORMAT."\n".$version ) );
221 8 ▶ if ( -e $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir) 2 6 8
222 # A rename cannot replace a nonempty directory.
223 2 $backup = $self->_mkdtemp( $dir.'.backup-' );
224 2 ▶ rename $dir, path_join( $backup, 'tree' ) or die 'rename '.$dir.': '.$!."\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $dir, path_join($backup, "tree") or die 'rename ' . $dir . ': ' . $! . "\n" 2 0 2
225 2 $old_moved = 1;
226 }
227 8 ▶ unless ( rename $staging, $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $staging, $dir) 0 8 8
228 0 my $err = $!;
229 0 ▶ if ($old_moved) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_moved) 0 0 0
230 0 ▶ if ( rename path_join( $backup, 'tree' ), $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename path_join($backup, 'tree'), $dir) { } 0 0 0
231 0 $old_moved = 0;
232 }
233 else {
234 0 die 'cache publication failed ('.$err.'); restoration failed ('.$!.'); last good cache remains at '
235 .path_join( $backup, 'tree' )."\n";
236 }
237 }
238 0 die 'rename '.$staging.': '.$err."\n";
239 }
240 # Commit point: cleanup must not turn new bytes into an old-version fallback.
241 8 $published = 1;
242 8 1;
243 };
244 8 my $err = $@;
245 # Keep the backup when restoration failed; the sweep puts it back.
246 8 ▶ my @obsolete = ( $staging, ( $backup && ( !$old_moved || $published ) ? ($backup) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$backup && (!$old_moved || $published) ? : 2 6 8
Condition50%3/6
$backup and !$old_moved || $published
!ll&&!rl&&r
602
not $old_moved or $published
l!l&&r!l&&!r
020
MC/DC33%1/3
$backup and !$old_moved || $published
$backup not $old_moved $published
Truth table50%2/4
A and not(B) or C
A= $backupB= not $old_movedC= $published
ABCresult
0XX0
1000
11X1
1011
247 8 for my $p (@obsolete) {
248 10 ▶ next unless -e $p;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $p 8 2 10
249 2 my $removed = eval { path($p)->remove_tree( { safe => 0 } ); 1 };
250 2 ▶ push @cleanup_warnings, 'cache cleanup failed for '.$p.' ('._msg($@).')' unless $removed && !-e $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $removed and not -e $p 0 2 2
Condition33%1/3
$removed and not -e $p
!ll&&!rl&&r
002
MC/DC0%0/2
$removed and not -e $p
$removed not -e $p
Truth table33%1/3
A and not(B)
A= $removedB= not -e $p
ABresult
0X0
100
111
251 }
252 8 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 8 8
253 8 ▶ return @cleanup_warnings ? join( '; ', @cleanup_warnings ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@cleanup_warnings ? : 0 8 8
254 }
255  
256 # Clear what a run that failed or died left of an update: a stage tree goes; a backup whose
257 # cache dir is missing holds the last good cache and is renamed back; any other backup goes.
258 # Only safe while no resolve runs – the caller holds the sync lock. Never throws.
259 350 ▶sub sweep_cache {
Subroutine sweep_cache: called CC=8 SCAR=20.8
Pod documented
260 350 my ( $class, $cache_root ) = @_;
261 350 ▶ opendir( my $dh, $cache_root ) or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $cache_root or return 68 282 350
262 68 my @names = sort readdir $dh;
263 68 closedir $dh;
264 68 for my $name (@names) {
265 208 ▶ my ( $hash, $kind ) = $name =~ $LEFTOVER or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $name =~ /$LEFTOVER/ or next 3 205 208
266 3 my $p = path_join( $cache_root, $name );
267 3 eval {
268 3 ▶ if ( $kind eq 'backup' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "backup") 2 1 3
269 2 my $dir = path_join( $cache_root, $hash );
270 2 my $tree = path_join( $p, 'tree' );
271 2 ▶ rename $tree, $dir if !-e $dir && lstat($tree) && -d _;
Branch: 1 branch
BranchTrueFalseTotal
if not -e $dir and lstat $tree and -d _ 1 1 2
Condition66%4/6
not -e $dir and lstat $tree and -d _
!ll&&!rl&&r
101
not -e $dir and lstat $tree
!ll&&!rl&&r
101
MC/DC33%1/3
not -e $dir and lstat $tree and -d _
not -e $dir lstat $tree -d _
Truth table50%2/4
not(A) and B and C
A= not -e $dirB= lstat $treeC= -d _
ABCresult
0XX0
10X0
1100
1111
272 }
273 3 path($p)->remove_tree( { safe => 0 } );
274 3 1;
275 };
276 }
277 68 return;
278 }
279  
280 35 1;
281  
282 __END__
« Skilletor/Frontmatter.pm ↑ Summary Skilletor/Bundles.pm »