blib/lib/AnyEvent/MCP/Workdirs.pm

filter: Statementstmt 100% 58 / 58 Branchbran 100% 14 / 14 Conditioncond 76.6% 23 / 30 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 88.6% 109 / 123 CC 23 SCAR 22.6
CC
23
Cov
93%
_read
14.9
new
7.0
refresh
4.0
CRAP 9.6
« bin/mcp-multibroker ↑ Summary blib/lib/AnyEvent/MCP/Server/Connect.pm »
1 =head1 NAME
2  
3 AnyEvent::MCP::Workdirs - persistent filesystem workdir IDs
4  
5 =head1 DESCRIPTION
6  
7 The registry assigns small positive integers to pathname strings.
8 C<mcp-fs> supplies logical absolute pathnames without following symlinks;
9 relative local workdir arguments are made absolute lexically. The same
10 logical pathname on two hosts gets the same ID even if its symlink resolves
11 to a different physical directory. IDs compress pathnames, not host
12 identities or permissions. The registry is shared by instances using the same file.
13 Permission-rule identities remain independent of these selectors.
14  
15 It is used by C<mcp-fs> so assistants can refer to a workdir by a short,
16 stable-while-retained number instead of repeating its pathname. It is
17 not a filesystem authorization mechanism: the selected workdir and the
18 permission engine still determine what an operation may access.
19 Applications that do not expose workdirs do not need this module.
20  
21 use AnyEvent::MCP::Workdirs;
22  
23 my $registry = AnyEvent::MCP::Workdirs->new (
24 file => $registry_file, paths => [$resolved_project_path],
25 );
26 my $id = $registry->{ids}[0];
27 $registry->refresh; # repeat periodically while the server runs
28  
29 See L<mcp-fs/SANDBOX GRANTS> for how the server interprets these IDs,
30 and L<AnyEvent::MCP::File> for the locked state-file operations.
31  
32 =head1 METHODS
33  
34 =over
35  
36 =item $registry = AnyEvent::MCP::Workdirs->new (file => $file, paths => [$path...], expiry => $seconds)
37  
38 Resolve IDs for the supplied pathnames, reserving new allocations under
39 the file lock. C<expiry> defaults to seven days. C<ids> contains the assigned
40 IDs in path order and stays fixed for this object's lifetime. The caller
41 refreshes immediately and periodically while its server is running.
42  
43 =item $registry->refresh
44  
45 Lock and reread the JSON registry, overwrite this server's assigned IDs with
46 their paths and the current C<last_use>, expire old entries after C<expiry>
47 seconds, and atomically replace the file. This never resolves or reallocates
48 the server's IDs. Startup allocations reuse the smallest available positive
49 IDs; configured paths retain their previous IDs if nobody reused them.
50  
51 Entries are C<ID =E<gt> [workdir, last_use]>. The JSON uses UTF-8 with pretty printing.
52 Manual changes require restarting filesystem servers using the registry.
53  
54 =back
55  
56 =cut
57  
58 package AnyEvent::MCP::Workdirs;
59  
60 179 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 179 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
62 179 ▶use AnyEvent::MCP::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
63 179 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 179 our @ISA = qw(AnyEvent::MCP::Base);
66  
67 179 my $json = JSON::XS->new->utf8->canonical->pretty;
68  
69 322 ▶sub _read {
Subroutine _read: called CC=14 SCAR=27.0
70 322 my ($self, $lock) = @_;
71  
72 322 local $/;
73 322 my $text = <$lock>;
74 322 ▶ my $entries = length $text ? $json->decode ($text) : {};
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 261 61 322
75 322 ▶ ref $entries eq "HASH" or die "$self->{file}: expected workdir ID object\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $entries eq "HASH" or die qq[$self->{"file"}: expected workdir ID object\n] 321 1 322
76  
77 321 for my $id (keys %$entries) {
78 320 my $entry = $entries->{$id};
79 320 ▶ $id =~ /\A[1-9][0-9]*\z/ && ref $entry eq "ARRAY" && @$entry == 2
Branch: 1 branch
BranchTrueFalseTotal
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1] and not ref $entry->[1] and $entry->[1] =~ /\A[0-9]+\z/u or die qq[$self->{"file"}: invalid workdir entry '${id}'\n] 317 3 320
Condition74%20/27
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1] and not ref $entry->[1] and $entry->[1] =~ /\A[0-9]+\z/u
!ll&&!rl&&r
21317
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1] and not ref $entry->[1]
!ll&&!rl&&r
20318
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1]
!ll&&!rl&&r
20318
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u
!ll&&!rl&&r
20318
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0]
!ll&&!rl&&r
20318
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0]
!ll&&!rl&&r
20318
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0]
!ll&&!rl&&r
20318
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2
!ll&&!rl&&r
11318
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY"
!ll&&!rl&&r
10319
MC/DC30%3/10
$id =~ /\A[1-9][0-9]*\z/u and ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and length $entry->[0] and not $entry->[0] =~ /\0/u and defined $entry->[1] and not ref $entry->[1] and $entry->[1] =~ /\A[0-9]+\z/u
$id =~ /\A[1-9][0-9]*\z/u ref $entry eq "ARRAY" @$entry == 2 defined $entry->[0] not ref $entry->[0] length $entry->[0] not $entry->[0] =~ /\0/u defined $entry->[1] not ref $entry->[1] $entry->[1] =~ /\A[0-9]+\z/u
Truth table36%4/11
A and B and C and D and not(E) and F and not(G) and H and not(I) and J
A= $id =~ /\A[1-9][0-9]*\z/uB= ref $entry eq "ARRAY"C= @$entry == 2D= defined $entry->[0]E= not ref $entry->[0]F= length $entry->[0]G= not $entry->[0] =~ /\0/uH= defined $entry->[1]I= not ref $entry->[1]J= $entry->[1] =~ /\A[0-9]+\z/u
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
111110XXXX0
1111110XXX0
11111110XX0
111111110X0
11111111100
11111111111
80 && defined $entry->[0] && !ref $entry->[0] && length $entry->[0] && $entry->[0] !~ /\0/
81 && defined $entry->[1] && !ref $entry->[1] && $entry->[1] =~ /\A[0-9]+\z/
82 or die "$self->{file}: invalid workdir entry '$id'\n";
83 }
84  
85 $entries
86 318 }
87  
88 178 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
89 178 my ($class, %kv) = @_;
90  
91 178 my $self = $class->SUPER::new (expiry => 7 * 86400, %kv);
92 178 my $lock = AnyEvent::MCP::File::lock $self->{file};
93 178 my $entries = $self->_read ($lock);
94 178 my $now = time;
95 178 my %active = map { $_ => 1 } @{$self->{paths}};
96  
97 178 for my $id (keys %$entries) {
98 delete $entries->{$id}
99 152 ▶ unless $active{$entries->{$id}[0]} || $entries->{$id}[1] > $now - $self->{expiry};
Branch: 1 branch
BranchTrueFalseTotal
unless $active{$entries->{$id}[0]} or $entries->{$id}[1] > $now - $self->{"expiry"} 2 150 152
Condition100%3/3
$active{$entries->{$id}[0]} or $entries->{$id}[1] > $now - $self->{"expiry"}
l!l&&r!l&&!r
117332
MC/DC100%2/2
$active{$entries->{$id}[0]} or $entries->{$id}[1] > $now - $self->{"expiry"}
$active{$entries->{$id}[0]} $entries->{$id}[1] > $now - $self->{"expiry"}
Truth table100%3/3
A or B
A= $active{$entries->{$id}[0]}B= $entries->{$id}[1] > $now - $self->{"expiry"}
ABresult
1X1
011
000
100 }
101  
102 178 my %id = map { $entries->{$_}[0] => $_ } keys %$entries;
103 178 my @ids;
104 178 my $next = 1;
105  
106 178 for my $path (@{$self->{paths}}) {
107 194 ▶ unless (exists $id{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $id{$path}) 76 118 194
108 76 ▶ ++$next while exists $entries->{$next};
Branch: 1 branch
BranchTrueFalseTotal
exists $entries->{$next} and do { ++$next } 24 76 100
109 76 $id{$path} = $next++;
110 76 $entries->{$id{$path}} = [$path, $now];
111 }
112  
113 194 push @ids, 0 + $id{$path};
114 }
115  
116 # reserve allocations before another server can choose the same IDs.
117 178 AnyEvent::MCP::File::replace $self->{file}, $json->encode ($entries);
118 178 $self->{ids} = \@ids;
119  
120 178 $self
121 }
122  
123 144 ▶sub refresh {
Subroutine refresh: called CC=4 SCAR=13.9
Pod documented
124 144 my ($self) = @_;
125  
126 144 my $lock = AnyEvent::MCP::File::lock $self->{file};
127 144 my $entries = $self->_read ($lock);
128 140 my $now = time;
129  
130 140 for (0 .. $#{$self->{paths}}) {
131 153 $entries->{$self->{ids}[$_]} = [$self->{paths}[$_], $now];
132 }
133  
134 140 for my $id (keys %$entries) {
135 166 ▶ delete $entries->{$id} if $entries->{$id}[1] <= $now - $self->{expiry};
Branch: 1 branch
BranchTrueFalseTotal
if $entries->{$id}[1] <= $now - $self->{"expiry"} 1 165 166
136 }
137  
138 140 AnyEvent::MCP::File::replace $self->{file}, $json->encode ($entries);
139 }
140  
141 179 1;
« bin/mcp-multibroker ↑ Summary blib/lib/AnyEvent/MCP/Server/Connect.pm »