blib/lib/Data/Shared/Manifest.pm

filter: Statementstmt 97.6% 165 / 169 Branchbran 84.6% 105 / 124 Conditioncond 62.8% 44 / 70 MC/DCmcdc 43.2% 16 / 37 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 4 totaltotal 82.3% 345 / 419 CC 93 SCAR 30.7
CC
93
Cov
87%
read
43.0
with_lock
21.5
snapshot
14.1
CRAP 21.5
« blib/lib/Data/Shared/Storage.pm ↑ Summary blib/lib/Data/Shared/Composite.pm »
1 package Data::Shared::Manifest;
2 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use Scalar::Util qw(refaddr blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Fcntl qw(:flock O_RDWR O_APPEND O_CREAT);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 our $VERSION = '0.02';
11  
12 40 our @CARP_NOT = qw(Data::Shared Data::Shared::Manifest Data::Shared::Storage
13 Data::Shared::Composite);
14  
15 148066 ▶sub snapshot {
Subroutine snapshot: called CC=14 SCAR=26.5
Pod undocumented
16 148066 my ($value, $active) = @_;
17 148066 ▶ return (_scalar_snapshot($value), 1) unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 96181 51885 148066
18 # JSON::XS booleans are JSON::PP::Boolean objects without JSON::PP loaded.
19 51885 ▶ if (blessed($value) && $value->isa('JSON::PP::Boolean')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("JSON::PP::Boolean")) 1 51884 51885
Condition66%2/3
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
!ll&&!rl&&r
5188401
MC/DC50%1/2
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
builtin::blessed($value) $value->isa("JSON::PP::Boolean")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("JSON::PP::Boolean")
ABresult
0X0
100
111
20 1 require JSON::PP;
21 1 ▶ return ($value ? JSON::PP::true() : JSON::PP::false(), 1);
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 0 1
22 }
23 51884 ▶ return ($value, 0) unless ref $value eq 'ARRAY' || ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" or ref $value eq "HASH" 2 51882 51884
Condition100%3/3
ref $value eq "ARRAY" or ref $value eq "HASH"
l!l&&r!l&&!r
516332492
MC/DC100%2/2
ref $value eq "ARRAY" or ref $value eq "HASH"
ref $value eq "ARRAY" ref $value eq "HASH"
Truth table100%3/3
A or B
A= ref $value eq "ARRAY"B= ref $value eq "HASH"
ABresult
1X1
011
000
24 51882 ▶ $active ||= {};
Condition100%2/2
$active ||= {}
l!l
1902432858
MC/DC100%1/1
$active ||= {}
$active
Truth table100%2/2
A
A= $active
Aresult
11
00
25 51882 my $addr = refaddr($value);
26 51882 ▶ Carp::croak('Data::Shared: cyclic mount configuration is not supported') if $active->{$addr};
Branch: 1 branch
BranchTrueFalseTotal
if $active->{$addr} 1 51881 51882
27 51881 local $active->{$addr} = 1;
28 51881 my $serializable = 1;
29 51881 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 51632 249 51881
30 51632 my @copy;
31 51632 for my $item (@$value) {
32 114645 my ($copy, $ok) = snapshot($item, $active);
33 114636 push @copy, $copy;
34 114636 ▶ $serializable &&= $ok;
Condition100%3/3
$serializable &&= $ok
!ll&&!rl&&r
24114630
MC/DC100%2/2
$serializable &&= $ok
$serializable $ok
Truth table100%3/3
A &&= B
A= $serializableB= $ok
ABresult
0X0
100
111
35 }
36 51623 return (\@copy, $serializable);
37 }
38 249 my %copy;
39 249 for my $key (keys %$value) {
40 527 my ($copy, $ok) = snapshot($value->{$key}, $active);
41 527 $copy{$key} = $copy;
42 527 ▶ $serializable &&= $ok;
Condition33%1/3
$serializable &&= $ok
!ll&&!rl&&r
00527
MC/DC0%0/2
$serializable &&= $ok
$serializable $ok
Truth table33%1/3
A &&= B
A= $serializableB= $ok
ABresult
0X0
100
111
43 }
44 249 return (\%copy, $serializable);
45 }
46  
47 40 my %HELD; # "dev:ino" of a held manifest lock => { exclusive }, this process only
48  
49 40 my $INFINITY = 9**9**9;
50  
51 # Checked by value: under PERL_JSON_PP_USE_B JSON::PP quotes a stringified Inf.
52 # Any NV may be one; int() on an Inf sets IOK as well as NOK.
53 4405 ▶sub _refuse_nonfinite {
Subroutine _refuse_nonfinite: called CC=6 SCAR=17.9
54 4405 my ($value, $flags) = @_;
55 4405 my $number = 0 + $value;
56 4405 ▶ return unless $number != $number || $number == $INFINITY || $number == -$INFINITY;
Branch: 1 branch
BranchTrueFalseTotal
unless $number != $number or $number == $INFINITY or $number == -$INFINITY 4392 13 4405
Condition83%5/6
$number != $number or $number == $INFINITY or $number == -$INFINITY
l!l&&r!l&&!r
1304392
$number != $number or $number == $INFINITY
l!l&&r!l&&!r
494392
MC/DC66%2/3
$number != $number or $number == $INFINITY or $number == -$INFINITY
$number != $number $number == $INFINITY $number == -$INFINITY
Truth table75%3/4
A or B or C
A= $number != $numberB= $number == $INFINITYC= $number == -$INFINITY
ABCresult
1XX1
01X1
0011
0000
57 # Before 5.40 the string 'inf' used numerically has a real Inf's flags: only a PV
58 # spelled unlike perl's own marks it a string. No PV: refused.
59 13 ▶ Carp::croak("Data::Shared: '$value' is not a serializable mount argument")
Branch: 1 branch
BranchTrueFalseTotal
unless $flags & 16384 and "$value" ne "$number" 8 5 13
Condition100%3/3
$flags & 16384 and "$value" ne "$number"
!ll&&!rl&&r
355
MC/DC100%2/2
$flags & 16384 and "$value" ne "$number"
$flags & 16384 "$value" ne "$number"
Truth table100%3/3
A and B
A= $flags & 16384B= "$value" ne "$number"
ABresult
0X0
100
111
60 unless ($flags & B::SVp_POK()) && "$value" ne "$number";
61 5 return;
62 }
63  
64 # The backend constructor and the manifest both get this value, so a recorded
65 # path is the one the file was created under.
66 96181 ▶sub _scalar_snapshot {
Subroutine _scalar_snapshot: called CC=13 SCAR=25.7
67 96181 my ($value) = @_;
68 96181 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 28124 68057 96181
69 68057 ▶ if (utf8::is_utf8($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($value)) 10 68047 68057
70 # Before the byte return: an upgraded stringified Inf is UTF-8 too.
71 10 my $wide = B::svref_2object(\$value)->FLAGS;
72 10 ▶ _refuse_nonfinite($value, $wide) if $wide & B::SVp_NOK();
Branch: 1 branch
BranchTrueFalseTotal
if $wide & 8192 3 7 10
73 # Lone surrogates and codepoints above Unicode encode to JSON that JSON::PP cannot reparse.
74 9 ▶ Carp::croak('Data::Shared: mount argument is not representable in a manifest')
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[\x{D800}-\x{DFFF}]|[^\x{0}-\x{10FFFF}]/ 1 8 9
75 if $value =~ /[\x{D800}-\x{DFFF}]|[^\x{0}-\x{10FFFF}]/;
76 # Record the bytes the filesystem and the leaf's SvPV see; JSON cannot tell them apart.
77 # Stringify, not assign: a copy keeps numeric flags, and '123' would replay as 123.
78 8 my $bytes = "$value";
79 8 utf8::encode($bytes);
80 8 return $bytes;
81 }
82 68047 my $flags = B::svref_2object(\$value)->FLAGS;
83 68047 ▶ return $value unless $flags & (B::SVp_IOK() | B::SVp_NOK());
Branch: 1 branch
BranchTrueFalseTotal
unless $flags & 12288 18719 49328 68047
84 49328 ▶ _refuse_nonfinite($value, $flags) if $flags & B::SVp_NOK();
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 8192 4402 44926 49328
85 # A pure number is spelled here: JSON::PP's choice varies by version and flag
86 # history, and perl's NV_DIG spelling loses precision (1+2**-52 prints as 1).
87 49321 ▶ if (!($flags & B::SVp_POK())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flags & 16384) 43950 5371 49321
88 43950 my $copy = $value; # a copy: stringifying $value would
89 43950 my $short = "$copy"; # set POK and change what follows
90 43950 ▶ if (0 + $short != $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 + $short != $value) 12 43938 43950
91 # %.0f for integral values; else the first width that round-trips
92 # this perl's NV (17 double, 21 long double, 36 __float128).
93 12 ▶ my @widths = $value == int($value) ? (0) : (17, 21, 36);
Branch: 1 branch
BranchTrueFalseTotal
$value == int $value ? : 8 4 12
94 12 for my $digits (@widths) {
95 12 ▶ my $decimal = $digits ? sprintf('%.*g', $digits, $value)
Branch: 1 branch
BranchTrueFalseTotal
$digits ? : 4 8 12
96 : sprintf('%.0f', $value);
97 # Numify a copy: $decimal must keep string-only flags or JSON::PP may respell it.
98 12 my $check = $decimal;
99 12 ▶ return $decimal if $check + 0 == $value;
Branch: 1 branch
BranchTrueFalseTotal
if $check + 0 == $value 12 0 12
100 }
101 }
102 }
103 49309 return $value;
104 }
105  
106 14602 ▶sub with_lock {
Subroutine with_lock: called CC=17 SCAR=30.7
Pod undocumented
107 14602 my ($root, $exclusive, $code) = @_;
108 14602 ▶ return $code->() unless defined $root->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root->{"dir"} 12523 2079 14602
109 2079 my $path = "$root->{dir}/.manifest.lock";
110 2079 my $fh;
111 2079 ▶ if ($root->{readonly}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->{'readonly'}) { } 95 1984 2079
112 # A read-only root must never create the lock; a shipped copy may lack it.
113 95 ▶ return $code->() unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 1 94 95
114 94 ▶ open $fh, '<', $path or Carp::croak("Data::Shared: cannot open manifest lock '$path': $!");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $path or Carp::croak("Data::Shared: cannot open manifest lock '${path}': $!") 94 0 94
115 } else {
116 # fcntl-based flock emulation needs O_RDWR for LOCK_SH.
117 1984 ▶ unless (sysopen($fh, $path, O_RDWR | O_APPEND | O_CREAT, 0600)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh, $path, 1090, 384) 1 1983 1984
118 1 my $err = $!;
119 1 ▶ unless (!$exclusive && -e $path && open $fh, '<', $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $exclusive and -e $path and open $fh, "<", $path) 0 1 1
Condition33%2/6
not $exclusive and -e $path and open $fh, "<", $path
!ll&&!rl&&r
001
not $exclusive and -e $path
!ll&&!rl&&r
001
MC/DC0%0/3
not $exclusive and -e $path and open $fh, "<", $path
not $exclusive -e $path open $fh, "<", $path
Truth table25%1/4
not(A) and B and C
A= not $exclusiveB= -e $pathC= open $fh, "<", $path
ABCresult
0XX0
10X0
1100
1111
120 0 $! = $err;
121 0 Carp::croak("Data::Shared: cannot open manifest lock '$path': $!");
122 }
123 }
124 }
125 # flock does not nest across descriptors: relocking in this process would self-deadlock,
126 # so refuse unless both holders are shared. Keyed by dev:ino, since paths alias.
127 2078 my ($dev, $ino) = (stat $fh)[0, 1];
128 2078 ▶ my $key = defined $ino ? "$dev:$ino" : $path;
Branch: 1 branch
BranchTrueFalseTotal
defined $ino ? : 2078 0 2078
129 2078 ▶ if (my $held = $HELD{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $held = $HELD{$key}) 6 2072 2078
130 Carp::croak("Data::Shared: this process already holds the manifest lock for"
131 . " '$root->{dir}'; use the root you were given rather than opening it again")
132 6 ▶ if $exclusive || $held->{exclusive};
Branch: 1 branch
BranchTrueFalseTotal
if $exclusive or $held->{"exclusive"} 5 1 6
Condition66%2/3
$exclusive or $held->{"exclusive"}
l!l&&r!l&&!r
051
MC/DC50%1/2
$exclusive or $held->{"exclusive"}
$exclusive $held->{"exclusive"}
Truth table66%2/3
A or B
A= $exclusiveB= $held->{"exclusive"}
ABresult
1X1
011
000
133 }
134 2073 ▶ my $op = $exclusive ? LOCK_EX : LOCK_SH;
Branch: 1 branch
BranchTrueFalseTotal
$exclusive ? : 1837 236 2073
135 2073 ▶ until (flock $fh, $op) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, $op) 0 2073 2073
136 0 ▶ Carp::croak("Data::Shared: cannot lock manifest: $!") unless $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EINTR"} 0 0 0
137 }
138 2073 ▶ local $HELD{$key} = { exclusive => $exclusive ? 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
$exclusive ? : 1837 236 2073
139 2073 return $code->(); # lexical filehandle releases the lock on return or exception
140 }
141  
142 1643 ▶sub read {
Subroutine read: called CC=33 SCAR=37.6
Pod undocumented
143 1643 my ($path) = @_;
144 1643 ▶ open my $fh, '<', $path or Carp::croak("Data::Shared: cannot read manifest '$path': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or Carp::croak("Data::Shared: cannot read manifest '${path}': $!") 1642 1 1643
145 # $. too: localizing it restores the caller's last-read handle, which bare eof consults.
146 1642 my $json = do { local ($/, $.); <$fh> };
147 1642 ▶ close $fh or Carp::croak("Data::Shared: cannot close manifest '$path': $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or Carp::croak("Data::Shared: cannot close manifest '${path}': $!") 1642 0 1642
148 1642 require JSON::PP;
149 1642 my $data = eval { JSON::PP->new->decode($json) };
150 1642 ▶ Carp::croak("Data::Shared: invalid manifest '$path': $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1642 1642
151 1642 ▶ Carp::croak("Data::Shared: invalid manifest '$path': expected an object") unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 1 1641 1642
152 Carp::croak("Data::Shared: unsupported manifest version in '$path'")
153 1641 ▶ unless defined $data->{version} && !ref $data->{version} && $data->{version} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"version"} and not ref $data->{"version"} and $data->{"version"} eq 1 1 1640 1641
Condition50%3/6
defined $data->{"version"} and not ref $data->{"version"} and $data->{"version"} eq 1
!ll&&!rl&&r
011640
defined $data->{"version"} and not ref $data->{"version"}
!ll&&!rl&&r
001641
MC/DC33%1/3
defined $data->{"version"} and not ref $data->{"version"} and $data->{"version"} eq 1
defined $data->{"version"} not ref $data->{"version"} $data->{"version"} eq 1
Truth table50%2/4
A and not(B) and C
A= defined $data->{"version"}B= not ref $data->{"version"}C= $data->{"version"} eq 1
ABCresult
0XX0
10X0
1100
1111
154 1640 ▶ Carp::croak("Data::Shared: invalid manifest mounts in '$path'") unless ref $data->{mounts} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"mounts"} eq "ARRAY" 1 1639 1640
155 1639 ▶ $data->{revision} = 0 unless exists $data->{revision};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{"revision"} 3 1636 1639
156 Carp::croak("Data::Shared: invalid manifest revision in '$path'")
157 1639 ▶ unless defined $data->{revision} && !ref $data->{revision} && $data->{revision} =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"revision"} and not ref $data->{"revision"} and $data->{"revision"} =~ /\A[0-9]+\z/ 2 1637 1639
Condition50%3/6
defined $data->{"revision"} and not ref $data->{"revision"} and $data->{"revision"} =~ /\A[0-9]+\z/
!ll&&!rl&&r
021637
defined $data->{"revision"} and not ref $data->{"revision"}
!ll&&!rl&&r
001639
MC/DC33%1/3
defined $data->{"revision"} and not ref $data->{"revision"} and $data->{"revision"} =~ /\A[0-9]+\z/
defined $data->{"revision"} not ref $data->{"revision"} $data->{"revision"} =~ /\A[0-9]+\z/
Truth table50%2/4
A and not(B) and C
A= defined $data->{"revision"}B= not ref $data->{"revision"}C= $data->{"revision"} =~ /\A[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
158 Carp::croak("Data::Shared: invalid store identity in '$path'")
159 if exists $data->{store}
160 1637 ▶ && !(defined $data->{store} && !ref $data->{store} && $data->{store} =~ /\A[0-9a-f]+\z/);
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"store"} and not defined $data->{"store"} && !ref($data->{"store"}) && $data->{"store"} =~ /\A[0-9a-f]+\z/ 0 1637 1637
Condition44%4/9
exists $data->{"store"} and not defined $data->{"store"} && !ref($data->{"store"}) && $data->{"store"} =~ /\A[0-9a-f]+\z/
!ll&&!rl&&r
116360
defined $data->{'store'} && !ref($data->{'store'}) && $data->{'store'} =~ /\A[0-9a-f]+\z/
!ll&&!rl&&r
001636
defined $data->{"store"} and not ref $data->{"store"}
!ll&&!rl&&r
001636
MC/DC0%0/4
exists $data->{"store"} and not defined $data->{"store"} && !ref($data->{"store"}) && $data->{"store"} =~ /\A[0-9a-f]+\z/
exists $data->{"store"} defined $data->{"store"} not ref $data->{"store"} $data->{'store'} =~ /\A[0-9a-f]+\z/
Truth table40%2/5
A and not(B and not(C) && D)
A= exists $data->{"store"}B= defined $data->{"store"}C= not ref $data->{"store"}D= $data->{'store'} =~ /\A[0-9a-f]+\z/
ABCDresult
0XXX0
11110
10XX1
110X1
11101
161 # The checks above gave these a PV; older JSON::PP would then re-encode them as strings.
162 1637 $data->{version} = 1;
163 1637 $data->{revision} = 0 + $data->{revision};
164 1637 my %names;
165 1637 for my $mount (@{ $data->{mounts} }) {
166 2999 ▶ Carp::croak("Data::Shared: invalid mount record in '$path'") unless ref $mount eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mount eq "HASH" 0 2999 2999
167 Carp::croak("Data::Shared: invalid mount name in '$path'")
168 2999 ▶ unless defined $mount->{name} && !ref $mount->{name} && length $mount->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mount->{"name"} and not ref $mount->{"name"} and length $mount->{"name"} 2 2997 2999
Condition83%5/6
defined $mount->{"name"} and not ref $mount->{"name"} and length $mount->{"name"}
!ll&&!rl&&r
202997
defined $mount->{"name"} and not ref $mount->{"name"}
!ll&&!rl&&r
112997
MC/DC66%2/3
defined $mount->{"name"} and not ref $mount->{"name"} and length $mount->{"name"}
defined $mount->{"name"} not ref $mount->{"name"} length $mount->{"name"}
Truth table75%3/4
A and not(B) and C
A= defined $mount->{"name"}B= not ref $mount->{"name"}C= length $mount->{"name"}
ABCresult
0XX0
10X0
1100
1111
169 2997 ▶ Carp::croak("Data::Shared: duplicate mount '$mount->{name}' in '$path'") if $names{$mount->{name}}++;
Branch: 1 branch
BranchTrueFalseTotal
if $names{$mount->{"name"}}++ 1 2996 2997
170 2996 ▶ $mount->{args} = [] unless exists $mount->{args};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mount->{"args"} 0 2996 2996
171 2996 ▶ Carp::croak("Data::Shared: invalid mount arguments in '$path'") unless ref $mount->{args} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mount->{"args"} eq "ARRAY" 1 2995 2996
172 2995 ▶ Carp::croak("Data::Shared: missing mount spec in '$path'") unless defined $mount->{spec};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mount->{"spec"} 0 2995 2995
173 }
174 1633 ▶ if (exists $data->{retained}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"retained"}) 260 1373 1633
175 Carp::croak("Data::Shared: invalid retained storage in '$path'")
176 260 ▶ unless ref $data->{retained} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"retained"} eq "ARRAY" 1 259 260
177 259 for my $kept (@{ $data->{retained} }) {
178 396 ▶ Carp::croak("Data::Shared: invalid retained storage in '$path'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kept and not ref $kept and length $kept 1 395 396
Condition50%3/6
defined $kept and not ref $kept and length $kept
!ll&&!rl&&r
01395
defined $kept and not ref $kept
!ll&&!rl&&r
00396
MC/DC33%1/3
defined $kept and not ref $kept and length $kept
defined $kept not ref $kept length $kept
Truth table50%2/4
A and not(B) and C
A= defined $keptB= not ref $keptC= length $kept
ABCresult
0XX0
10X0
1100
1111
179 unless defined $kept && !ref $kept && length $kept;
180 }
181 }
182 # The validated manifest is trusted; untaint it before the backends' taint guards.
183 1631 _untaint($data);
184 # Fingerprint the published bytes; JSON::PP may re-encode decoded numbers differently.
185 1631 ▶ return wantarray ? ($data, $json) : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1631 0 1631
186 }
187  
188 10442 ▶sub _untaint {
Subroutine _untaint: called CC=5 SCAR=16.3
189 10442 my ($node) = @_;
190 10442 ▶ if (ref $node eq 'HASH') { $_ = _untaint_scalar($_) for values %$node; return $node }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "HASH") 4755 5687 10442
191 5687 ▶ if (ref $node eq 'ARRAY') { $_ = _untaint_scalar($_) for @$node; return $node }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "ARRAY") 5687 0 5687
192 0 return $node;
193 }
194  
195 26722 ▶sub _untaint_scalar {
Subroutine _untaint_scalar: called CC=4 SCAR=13.9
196 26722 my ($v) = @_;
197 26722 ▶ return _untaint($v) if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 8811 17911 26722
198 17911 ▶ return $v unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 2551 15360 17911
199 15360 my ($clean) = "$v" =~ /\A(.*)\z/s;
200 # Restore published bytes after JSON decoding, including bytes above 0x7f.
201 15360 utf8::downgrade($clean, 1);
202 # JSON::PP gives numbers IOK/NOK and strings POK only, so "007" stays a string.
203 15360 my $flags = B::svref_2object(\$v)->FLAGS;
204 15360 ▶ return 0 + $clean if $flags & (B::SVp_IOK() | B::SVp_NOK());
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 12288 6926 8434 15360
205 8434 return $clean;
206 }
207  
208 1688 ▶sub write {
Subroutine write: called CC=8 SCAR=21.0
Pod undocumented
209 1688 my ($path, $data) = @_;
210 1688 require JSON::PP;
211 1688 my $json = JSON::PP->new->canonical(1)->ascii(1)->encode($data);
212 1688 require File::Temp;
213 1688 require File::Basename;
214 1688 my ($fh, $tmp) = File::Temp::tempfile('.manifest-XXXXXXXX', DIR => File::Basename::dirname($path), UNLINK => 0);
215 # The replacement keeps the existing manifest's mode.
216 1688 my $mode = (stat $path)[2];
217 1688 my $ok = eval {
218 1688 ▶ chmod $mode & 07777, $tmp or die "cannot set manifest mode: $!" if defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 1262 426 1688
Condition60%3/5
defined $mode && (chmod($mode & 4095, $tmp) || die("cannot set manifest mode: $!"))
!ll&&!rl&&r
42612620
chmod $mode & 4095, $tmp or die "cannot set manifest mode: $!"
l!l
12620
MC/DC0%0/2
defined $mode && (chmod($mode & 4095, $tmp) || die("cannot set manifest mode: $!"))
defined $mode chmod $mode & 4095, $tmp
Truth table33%1/3
A && B
A= defined $modeB= chmod $mode & 4095, $tmp
ABresult
0X0
100
111
219 1688 local ($,, $\);
220 1688 ▶ print {$fh} $json, "\n" or die "cannot write manifest: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $json, "\n" or die "cannot write manifest: $!" 1688 0 1688
221 1688 ▶ close $fh or die "cannot close manifest: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "cannot close manifest: $!" 1688 0 1688
222 1688 ▶ rename $tmp, $path or die "cannot rename manifest to '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp, $path or die "cannot rename manifest to '${path}': $!" 1687 1 1688
223 1687 1;
224 };
225 1688 my $err = $@;
226 1688 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 1687 1688
227 1 ▶ close $fh if defined fileno($fh);
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $fh 0 1 1
228 1 unlink $tmp;
229 1 Carp::croak("Data::Shared: $err");
230 }
231 1687 return "$json\n"; # what read() will slurp back, for the fingerprint
232 }
233  
234 40 1;
235  
236 __END__
« blib/lib/Data/Shared/Storage.pm ↑ Summary blib/lib/Data/Shared/Composite.pm »