| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | return ($value ? JSON::PP::true() : JSON::PP::false(), 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 51884 | ▶ | return ($value, 0) unless ref $value eq 'ARRAY' || ref $value eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $value eq "ARRAY" or ref $value eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 51882 | ▶ | $active ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$active ||= {}
MC/DC100%1/1
$active ||= {}
$active
Truth table100%2/2
A
A= $active
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 51882 | my $addr = refaddr($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 51882 | ▶ | Carp::croak('Data::Shared: cyclic mount configuration is not supported') if $active->{$addr}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 51881 | local $active->{$addr} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 51881 | my $serializable = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 51881 | ▶ | if (ref $value eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%2/2
$serializable &&= $ok
$serializable
$ok
Truth table100%3/3
A &&= B
A= $serializableB= $ok
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$serializable &&= $ok
$serializable
$ok
Truth table33%1/3
A &&= B
A= $serializableB= $ok
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$number != $number or $number == $INFINITY or $number == -$INFINITY
$number != $number or $number == $INFINITY
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$flags & 16384 and "$value" ne "$number"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 68057 | ▶ | if (utf8::is_utf8($value)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 49328 | ▶ | _refuse_nonfinite($value, $flags) if $flags & B::SVp_NOK(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 12 | for my $digits (@widths) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 12 | ▶ | my $decimal = $digits ? sprintf('%.*g', $digits, $value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 2079 | my $path = "$root->{dir}/.manifest.lock"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 2079 | my $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 2079 | ▶ | if ($root->{readonly}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 94 | ▶ | open $fh, '<', $path or Carp::croak("Data::Shared: cannot open manifest lock '$path': $!"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | my $err = $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | unless (!$exclusive && -e $path && open $fh, '<', $path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not $exclusive and -e $path and open $fh, "<", $path
not $exclusive and -e $path
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2078 | ▶ | if (my $held = $HELD{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$exclusive or $held->{"exclusive"}
MC/DC50%1/2
$exclusive or $held->{"exclusive"}
$exclusive
$held->{"exclusive"}
Truth table66%2/3
A or B
A= $exclusiveB= $held->{"exclusive"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 2073 | ▶ | my $op = $exclusive ? LOCK_EX : LOCK_SH; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 2073 | ▶ | until (flock $fh, $op) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | Carp::croak("Data::Shared: cannot lock manifest: $!") unless $!{EINTR}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 2073 | ▶ | local $HELD{$key} = { exclusive => $exclusive ? 1 : 0 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 1642 | ▶ | Carp::croak("Data::Shared: invalid manifest '$path': expected an object") unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
defined $data->{"version"} and not ref $data->{"version"} and $data->{"version"} eq 1
defined $data->{"version"} and not ref $data->{"version"}
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 1640 | ▶ | Carp::croak("Data::Shared: invalid manifest mounts in '$path'") unless ref $data->{mounts} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 1639 | ▶ | $data->{revision} = 0 unless exists $data->{revision}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
defined $data->{"revision"} and not ref $data->{"revision"} and $data->{"revision"} =~ /\A[0-9]+\z/
defined $data->{"revision"} and not ref $data->{"revision"}
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition44%4/9
exists $data->{"store"} and not defined $data->{"store"} && !ref($data->{"store"}) && $data->{"store"} =~ /\A[0-9a-f]+\z/
defined $data->{'store'} && !ref($data->{'store'}) && $data->{'store'} =~ /\A[0-9a-f]+\z/
defined $data->{"store"} and not ref $data->{"store"}
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
defined $mount->{"name"} and not ref $mount->{"name"} and length $mount->{"name"}
defined $mount->{"name"} and not ref $mount->{"name"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 2997 | ▶ | Carp::croak("Data::Shared: duplicate mount '$mount->{name}' in '$path'") if $names{$mount->{name}}++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 2996 | ▶ | $mount->{args} = [] unless exists $mount->{args}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 2996 | ▶ | Carp::croak("Data::Shared: invalid mount arguments in '$path'") unless ref $mount->{args} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 2995 | ▶ | Carp::croak("Data::Shared: missing mount spec in '$path'") unless defined $mount->{spec}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 1633 | ▶ | if (exists $data->{retained}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | Carp::croak("Data::Shared: invalid retained storage in '$path'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 260 | ▶ | unless ref $data->{retained} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 259 | for my $kept (@{ $data->{retained} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 396 | ▶ | Carp::croak("Data::Shared: invalid retained storage in '$path'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $kept and not ref $kept and length $kept
defined $kept and not ref $kept
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 5687 | ▶ | if (ref $node eq 'ARRAY') { $_ = _untaint_scalar($_) for @$node; return $node } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 17911 | ▶ | return $v unless defined $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition60%3/5
defined $mode && (chmod($mode & 4095, $tmp) || die("cannot set manifest mode: $!"))
chmod $mode & 4095, $tmp or die "cannot set manifest mode: $!"
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 1688 | local ($,, $\); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 1688 | ▶ | print {$fh} $json, "\n" or die "cannot write manifest: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 1688 | ▶ | close $fh or die "cannot close manifest: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 1688 | ▶ | rename $tmp, $path or die "cannot rename manifest to '$path': $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 1687 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1688 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 1688 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | ▶ | close $fh if defined fileno($fh); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||