lib/Mogura/Server/Store.pm

filter: Statementstmt 84.4% 196 / 232 Branchbran 62.3% 101 / 162 Conditioncond 59.0% 75 / 127 MC/DCmcdc 31.1% 19 / 61 Subroutinesub 94.4% 34 / 36 Podpod 0.0% 0 / 21 totaltotal 66.5% 425 / 639 CC 137 SCAR 45.2
CC
137
Cov
71%
receive
552.0
__ANON__
54.5
can_disconnect
27.2
CRAP 91.6
↑ Summary lib/Mogura/Scanner.pm »
1 package Mogura::Server::Store;
2 4 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojo::JSON qw(to_json from_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Mogura::Protocol qw(random_id valid_id now artifact_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Mogura::Server::Tags;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use constant BLOCKS_REMOVAL => q{finished=0 OR
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 COALESCE(json_extract(record, '$.cleanup_status'), '') NOT IN ('unmounted', 'not_needed')};
12  
13 has state =>
14 0 ▶ sub { path($ENV{XDG_STATE_HOME} || path($ENV{HOME}, '.local/state'), 'mogura')->to_string };
Subroutine __ANON__: not called CC=2 SCAR=14.0
Condition0%0/3
$ENV{'XDG_STATE_HOME'} || path($ENV{'HOME'}, '.local/state')
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'XDG_STATE_HOME'} || path($ENV{'HOME'}, '.local/state')
$ENV{'XDG_STATE_HOME'} path($ENV{'HOME'}, '.local/state')
Truth table0%0/3
A || B
A= $ENV{'XDG_STATE_HOME'}B= path($ENV{'HOME'}, '.local/state')
ABresult
1X1
011
000
15  
16 508 ▶sub db {
Subroutine db: called CC=3 SCAR=11.0
Pod undocumented
17 508 my ($self) = @_;
18 508 ▶ return $self->{db} if ($self->{pid} // 0) == $$;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"} // 0) == $$ 502 6 508
Condition100%2/2
$self->{"pid"} // 0
l!l
5026
MC/DC100%1/1
$self->{"pid"} // 0
$self->{"pid"}
Truth table100%2/2
A
A= $self->{"pid"}
Aresult
11
00
19 6 path($self->state)->make_path({mode => 0700});
20 6 my $db = DBI->connect('dbi:SQLite:dbname=' . path($self->state, 'mogura.sqlite'),
21 '', '', {RaiseError => 1, PrintError => 0, sqlite_unicode => 1, AutoInactiveDestroy => 1});
22 6 $db->do('PRAGMA busy_timeout=5000');
23 6 $db->do('PRAGMA synchronous=FULL');
24 6 $self->{pid} = $$;
25 6 return $self->{db} = $db;
26 }
27  
28 10 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
29 10 my ($self) = @_;
30 10 my $db = $self->db;
31 10 $db->do('PRAGMA journal_mode=WAL');
32 10 $db->do('CREATE TABLE IF NOT EXISTS settings(name TEXT PRIMARY KEY, value TEXT NOT NULL)');
33 10 $db->do("INSERT OR IGNORE INTO settings VALUES ('session', ?)",
34 undef, random_id() . random_id());
35 10 $db->do("INSERT OR IGNORE INTO settings VALUES ('id',?)", undef, random_id());
36 10 $db->do(
37 'CREATE TABLE IF NOT EXISTS scanners(id TEXT PRIMARY KEY, digest TEXT UNIQUE, seen INTEGER, inventory TEXT)'
38 );
39 10 $db->do(
40 q{CREATE TABLE IF NOT EXISTS jobs(id TEXT PRIMARY KEY, scanner TEXT NOT NULL, token TEXT NOT NULL,
41 run TEXT, created TEXT NOT NULL, tags TEXT NOT NULL, note TEXT NOT NULL,
42 record TEXT NOT NULL, finished INTEGER NOT NULL DEFAULT 0)}
43 );
44 10 $db->do('CREATE INDEX IF NOT EXISTS assigned ON jobs(scanner,finished,created)');
45 10 $db->do('CREATE INDEX IF NOT EXISTS removal_blockers ON jobs(scanner) WHERE ' . BLOCKS_REMOVAL);
46 10 $db->do(
47 'CREATE TABLE IF NOT EXISTS receipts(job TEXT, run TEXT, kind TEXT, hash TEXT NOT NULL, indexed INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(job,run,kind))'
48 );
49 10 $db->do('CREATE INDEX IF NOT EXISTS pending_indexes ON receipts(job,kind) WHERE indexed=0');
50 10 $db->do('CREATE INDEX IF NOT EXISTS history ON jobs(created DESC,id DESC)');
51 10 return $self;
52 }
53  
54 42 ▶sub transaction {
Subroutine transaction: called CC=2 SCAR=7.4
Pod undocumented
55 42 my ($self, $operation) = @_;
56 42 my $db = $self->db;
57 42 $db->begin_work;
58 42 my $result;
59 42 ▶ eval { $result = $operation->($db); $db->commit; 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $result = &$operation($db); $db->commit; 1 } } or do { my $error = $@; $db->rollback; die $error } 42 0 42
60 0 my $error = $@;
61 0 $db->rollback;
62 0 die $error;
63 };
64 42 return $result;
65 }
66  
67 8 ▶sub register {
Subroutine register: called CC=3 SCAR=11.7
Pod undocumented
68 8 my ($self, $id, $token) = @_;
69 8 ▶ die 'Invalid scanner name' unless ($id // '') =~ /\A[a-zA-Z0-9_-]{1,64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($id // "") =~ /\A[a-zA-Z0-9_-]{1,64}\z/u 0 8 8
Condition50%1/2
$id // ""
l!l
80
MC/DC0%0/1
$id // ""
$id
Truth table50%1/2
A
A= $id
Aresult
11
00
70 8 $self->db->do('INSERT INTO scanners(id,digest) VALUES (?,?)', undef, $id, sha256_hex($token));
71 }
72  
73 38 ▶sub authenticate {
Subroutine authenticate: called CC=3 SCAR=11.0
Pod undocumented
74 38 my ($self, $header) = @_;
75 38 ▶ return unless ($header // '') =~ /\ABearer ([a-f0-9]{64})\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($header // "") =~ /\ABearer ([a-f0-9]{64})\z/u 1 37 38
Condition100%2/2
$header // ""
l!l
371
MC/DC100%1/1
$header // ""
$header
Truth table100%2/2
A
A= $header
Aresult
11
00
76 return
77 37 scalar $self->db->selectrow_array('SELECT id FROM scanners WHERE digest=?', undef,
78 sha256_hex($1));
79 }
80  
81 17 ▶sub inventories {
Subroutine inventories: called CC=4 SCAR=14.6
Pod undocumented
82 17 my ($self, $page) = @_;
83 17 ▶ my $rows = $self->db->selectall_arrayref(
Condition50%1/2
$page // 1
l!l
170
MC/DC0%0/1
$page // 1
$page
Truth table50%1/2
A
A= $page
Aresult
11
00
84 'SELECT id,seen,inventory FROM scanners WHERE digest IS NOT NULL ORDER BY id LIMIT 5 OFFSET ?',
85 {Slice => {}},
86 (($page // 1) - 1) * 4
87 );
88 return [
89 map {
90 17 {
91 17 ▶ %{from_json($_->{inventory} // '{"disks":[]}')},
Condition50%2/4
$_->{"inventory"} // "{\"disks\":[]}"
l!l
170
$_->{"seen"} // 0
l!l
170
MC/DC0%0/1
$_->{"inventory"} // "{\"disks\":[]}"
$_->{"inventory"}
MC/DC0%0/1
$_->{"seen"} // 0
$_->{"seen"}
Truth table50%1/2
A
A= $_->{"inventory"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"seen"}
Aresult
11
00
92 scanner_id => $_->{id},
93 17 stale => ($_->{seen} // 0) <
94 time - 30
95 }
96 } @$rows
97 ];
98 }
99  
100 35 ▶sub selectable {
Subroutine selectable: called CC=10 SCAR=23.0
Pod undocumented
101 35 my ($self, $scanner, $token) = @_;
102 35 my ($json) = $self->db->selectrow_array(
103 'SELECT inventory FROM scanners WHERE id=? AND digest IS NOT NULL AND seen>=?',
104 undef, $scanner, time - 30);
105 35 ▶ return unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 1 34 35
106 34 my $inv = from_json($json);
107 34 ▶ return if @{$inv->{blocked} // []} || @{$inv->{errors} // []};
Branch: 1 branch
BranchTrueFalseTotal
if @{$inv->{"blocked"} // [];} or @{$inv->{"errors"} // [];} 1 33 34
Condition85%6/7
@{$inv->{"blocked"} // [];} or @{$inv->{"errors"} // [];}
l!l&&r!l&&!r
1033
$inv->{"blocked"} // []
l!l
727
$inv->{"errors"} // []
l!l
258
MC/DC50%1/2
@{$inv->{"blocked"} // [];} or @{$inv->{"errors"} // [];}
@{$inv->{"blocked"} // [];} @{$inv->{"errors"} // [];}
MC/DC100%1/1
$inv->{"blocked"} // []
$inv->{"blocked"}
MC/DC100%1/1
$inv->{"errors"} // []
$inv->{"errors"}
Truth table66%2/3
A or B
A= @{$inv->{"blocked"} // [];}B= @{$inv->{"errors"} // [];}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $inv->{"blocked"}
Aresult
11
00
Truth table100%2/2
A
A= $inv->{"errors"}
Aresult
11
00
108 107 ▶ my @targets = grep { $_ && $_->{selectable} && ($_->{token} // '') eq $token }
Condition87%7/8
$_ && $_->{'selectable'} && ($_->{'token'} // '') eq $token
!ll&&!rl&&r
304532
$_ and $_->{"selectable"}
!ll&&!rl&&r
29177
$_->{"token"} // ""
l!l
770
MC/DC66%2/3
$_ && $_->{'selectable'} && ($_->{'token'} // '') eq $token
$_ $_->{"selectable"} $_->{"token"}
Truth table75%3/4
A and B && C
A= $_B= $_->{"selectable"}C= $_->{"token"}
ABCresult
0XX0
10X0
1100
1111
109 33 ($inv->{host}, map { @{$_->{targets}} } @{$inv->{disks}});
110 33 ▶ return @targets == 1 ? $targets[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@targets == 1 ? : 32 1 33
111 }
112  
113 # Linux assigns a disk sequence to the whole attachment for the lifetime of a boot.
114 44 ▶sub _attachment {
Subroutine _attachment: called CC=8 SCAR=21.0
115 44 my ($target) = @_;
116 44 ▶ return unless ref($target) eq 'HASH' && ref($target->{disk}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $target eq "HASH" and ref $target->{"disk"} eq "HASH" 7 37 44
Condition66%2/3
ref $target eq "HASH" and ref $target->{"disk"} eq "HASH"
!ll&&!rl&&r
0737
MC/DC50%1/2
ref $target eq "HASH" and ref $target->{"disk"} eq "HASH"
ref $target eq "HASH" ref $target->{"disk"} eq "HASH"
Truth table66%2/3
A and B
A= ref $target eq "HASH"B= ref $target->{"disk"} eq "HASH"
ABresult
0X0
100
111
117 37 my ($boot, $sequence) = ($target->{boot_id}, $target->{disk}{diskseq});
118 return
119 37 ▶ unless defined($boot)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $boot and not ref $boot and $boot =~ /\S/u and ($sequence // "") =~ /\A[1-9][0-9]*\z/u 3 34 37
Condition81%9/11
defined $boot and not ref $boot and $boot =~ /\S/u and ($sequence // "") =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
1234
defined $boot and not ref $boot and $boot =~ /\S/u
!ll&&!rl&&r
1036
defined $boot and not ref $boot
!ll&&!rl&&r
1036
$sequence // ""
l!l
342
MC/DC50%2/4
defined $boot and not ref $boot and $boot =~ /\S/u and ($sequence // "") =~ /\A[1-9][0-9]*\z/u
defined $boot not ref $boot $boot =~ /\S/u $sequence
Truth table60%3/5
A and not(B) and C and D
A= defined $bootB= not ref $bootC= $boot =~ /\S/uD= $sequence
ABCDresult
0XXX0
10XX0
110X0
11100
11111
120 && !ref($boot)
121 && $boot =~ /\S/
122 && ($sequence // '') =~ /\A[1-9][0-9]*\z/;
123 34 return "$boot:$sequence";
124 }
125  
126 21 ▶sub can_disconnect {
Subroutine can_disconnect: called CC=22 SCAR=33.0
Pod undocumented
127 21 my ($self, $scan) = @_;
128 21 ▶ return unless ($scan->{cleanup_status} // '') eq 'unmounted';
Branch: 1 branch
BranchTrueFalseTotal
unless ($scan->{"cleanup_status"} // "") eq "unmounted" 2 19 21
Condition50%1/2
$scan->{"cleanup_status"} // ""
l!l
210
MC/DC0%0/1
$scan->{"cleanup_status"} // ""
$scan->{"cleanup_status"}
Truth table50%1/2
A
A= $scan->{"cleanup_status"}
Aresult
11
00
129 19 ▶ my $attachment = _attachment($scan->{target}) // return;
Condition100%2/2
_attachment($scan->{"target"}) // (return)
l!l
127
MC/DC100%1/1
_attachment($scan->{"target"}) // (return)
_attachment($scan->{"target"})
Truth table100%2/2
A
A= _attachment($scan->{"target"})
Aresult
11
00
130 12 ▶ my $current = $self->selectable($scan->{scanner_id}, $scan->{token}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $current = $self->selectable($scan->{"scanner_id"}, $scan->{"token"}) or return 12 0 12
131 12 ▶ return unless (_attachment($current->{identity}) // '') eq $attachment;
Branch: 1 branch
BranchTrueFalseTotal
unless (_attachment($current->{"identity"}) // "") eq $attachment 3 9 12
Condition100%2/2
_attachment($current->{"identity"}) // ""
l!l
102
MC/DC100%1/1
_attachment($current->{"identity"}) // ""
_attachment($current->{"identity"})
Truth table100%2/2
A
A= _attachment($current->{"identity"})
Aresult
11
00
132  
133 9 my $disk = $scan->{target}{disk};
134 9 ▶ return unless length($disk->{serial} // '') || length($disk->{wwn} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($disk->{"serial"} // "") or length($disk->{"wwn"} // "") 0 9 9
Condition28%2/7
length($disk->{"serial"} // "") or length($disk->{"wwn"} // "")
l!l&&r!l&&!r
900
$disk->{"serial"} // ""
l!l
90
$disk->{"wwn"} // ""
l!l
00
MC/DC0%0/2
length($disk->{"serial"} // "") or length($disk->{"wwn"} // "")
$disk->{"serial"} $disk->{"wwn"}
Truth table33%1/3
A or B
A= $disk->{"serial"}B= $disk->{"wwn"}
ABresult
1X1
011
000
135 9 for my $field (qw(serial wwn)) {
136 17 ▶ return if ($disk->{$field} // '') ne ($current->{identity}{disk}{$field} // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($disk->{$field} // "") ne ($current->{"identity"}{"disk"}{$field} // "") 1 16 17
Condition100%4/4
$disk->{$field} // ""
l!l
98
$current->{"identity"}{"disk"}{$field} // ""
l!l
98
MC/DC100%1/1
$disk->{$field} // ""
$disk->{$field}
MC/DC100%1/1
$current->{"identity"}{"disk"}{$field} // ""
$current->{"identity"}{"disk"}{$field}
Truth table100%2/2
A
A= $disk->{$field}
Aresult
11
00
Truth table100%2/2
A
A= $current->{"identity"}{"disk"}{$field}
Aresult
11
00
137 }
138  
139 8 my $jobs = $self->db->prepare(
140 q{SELECT json_extract(record, '$.target') FROM jobs WHERE scanner=? AND id<>? AND (}
141 . BLOCKS_REMOVAL
142 . ')');
143 8 $jobs->execute($scan->{scanner_id}, $scan->{id});
144 8 ▶ while (my $row = $jobs->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $jobs->fetchrow_arrayref) 13 3 16
145 13 ▶ my $target = defined($row->[0]) ? from_json($row->[0]) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $row->[0] ? : 13 0 13
146 13 ▶ next if ref($target) eq 'HASH' && ($target->{kind} // '') eq 'host';
Branch: 1 branch
BranchTrueFalseTotal
if ref $target eq "HASH" and ($target->{"kind"} // "") eq "host" 0 13 13
Condition40%2/5
ref $target eq "HASH" and ($target->{"kind"} // "") eq "host"
!ll&&!rl&&r
0130
$target->{"kind"} // ""
l!l
013
MC/DC0%0/2
ref $target eq "HASH" and ($target->{"kind"} // "") eq "host"
ref $target eq "HASH" $target->{"kind"}
Truth table33%1/3
A and B
A= ref $target eq "HASH"B= $target->{"kind"}
ABresult
0X0
100
111
147 13 ▶ my $other = _attachment($target) // return;
Condition100%2/2
_attachment($target) // (return)
l!l
121
MC/DC100%1/1
_attachment($target) // (return)
_attachment($target)
Truth table100%2/2
A
A= _attachment($target)
Aresult
11
00
148 12 ▶ return if $attachment eq $other;
Branch: 1 branch
BranchTrueFalseTotal
if $attachment eq $other 4 8 12
149 }
150 3 return 1;
151 }
152  
153 22 ▶sub submit {
Subroutine submit: called CC=4 SCAR=14.4
Pod undocumented
154 22 my ($self, $id, $scanner, $token, $tags, $note) = @_;
155 22 ▶ die 'Invalid job' unless valid_id($id) && ($token // '') =~ /\A[a-f0-9]{64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless valid_id($id) and ($token // "") =~ /\A[a-f0-9]{64}\z/u 0 22 22
Condition40%2/5
valid_id($id) and ($token // "") =~ /\A[a-f0-9]{64}\z/u
!ll&&!rl&&r
0022
$token // ""
l!l
220
MC/DC0%0/2
valid_id($id) and ($token // "") =~ /\A[a-f0-9]{64}\z/u
valid_id($id) $token
Truth table33%1/3
A and B
A= valid_id($id)B= $token
ABresult
0X0
100
111
156 return $self->transaction(
157 22 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
158 22 my ($db) = @_;
159 22 my $old = $self->get($id);
160 22 ▶ return $old->{scanner_id} eq $scanner && $old->{token} eq $token ? 200 : 409 if $old;
Branch: 2 branches
BranchTrueFalseTotal
if $old 2 20 22
$old->{'scanner_id'} eq $scanner && $old->{'token'} eq $token ? : 2 0 2
Condition33%1/3
$old->{"scanner_id"} eq $scanner and $old->{"token"} eq $token
!ll&&!rl&&r
002
MC/DC0%0/2
$old->{"scanner_id"} eq $scanner and $old->{"token"} eq $token
$old->{"scanner_id"} eq $scanner $old->{"token"} eq $token
Truth table33%1/3
A and B
A= $old->{"scanner_id"} eq $scannerB= $old->{"token"} eq $token
ABresult
0X0
100
111
161 20 ▶ my $target = $self->selectable($scanner, $token) or return 409;
Branch: 1 branch
BranchTrueFalseTotal
my $target = $self->selectable($scanner, $token) or return 409 17 3 20
162 17 ▶ return 409 if $db->selectrow_array(
Branch: 1 branch
BranchTrueFalseTotal
if $db->selectrow_array("SELECT 1 FROM jobs WHERE scanner=? AND token=? AND\n (finished=0 OR json_extract(record,'\$.cleanup_status')='needs_cleanup')", undef, $scanner, $token) 2 15 17
163 q{SELECT 1 FROM jobs WHERE scanner=? AND token=? AND
164 (finished=0 OR json_extract(record,'$.cleanup_status')='needs_cleanup')}, undef,
165 $scanner, $token
166 );
167 my $record = {
168 status => 'queued',
169 phase => 'queued',
170 cleanup_status => 'unknown',
171 target => $target->{identity},
172 15 warnings => []
173 };
174 15 $db->do(
175 'INSERT INTO jobs(id,scanner,token,created,tags,note,record) VALUES (?,?,?,?,?,?,?)',
176 undef, $id, $scanner, $token, now(), to_json($tags), $note, to_json($record)
177 );
178 15 return 202;
179 }
180 22 );
181 }
182  
183 2 ▶sub cancel {
Subroutine cancel: called CC=1 SCAR=0.0
Pod undocumented
184 2 my ($self, $id) = @_;
185 return $self->transaction(
186 2 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
187 2 my ($db) = @_;
188 2 ▶ my $scan = $self->get($id) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $scan = $self->get($id) or return 2 0 2
189 2 my $status = $scan->{status};
190 2 ▶ if ($status eq 'queued') {
Branch: 2 branches
BranchTrueFalseTotal
if ($status eq 'queued') { } 1 1 2
elsif ($status eq 'running') { } 1 0 1
191 1 $db->do(
192 q{UPDATE jobs SET finished=1, record=json_set(record,
193 '$.status','cancelled','$.phase','finished','$.finished_at',?,
194 '$.cleanup_status','not_needed') WHERE id=?}, undef, now(), $id
195 );
196 }
197 elsif ($status eq 'running') {
198 1 $db->do(
199 q{UPDATE jobs SET record=json_set(record,'$.status','cancelling') WHERE id=?},
200 undef, $id);
201 }
202  
203 # A repeated click or a completion racing the form keeps the last result.
204 2 return 1;
205 }
206 2 );
207 }
208  
209 2 ▶sub pending_cancellations {
Subroutine pending_cancellations: called CC=1 SCAR=0.0
Pod undocumented
210 2 my ($self, $scanner) = @_;
211 2 return $self->db->selectall_arrayref(
212 q{SELECT id,run FROM jobs WHERE scanner=?
213 AND finished=0 AND json_extract(record,'$.status')='cancelling' AND run IS NOT NULL},
214 {Slice => {}}, $scanner
215 );
216 }
217  
218 2 ▶sub offer {
Subroutine offer: called CC=1 SCAR=0.0
Pod undocumented
219 2 my ($self, $scanner) = @_;
220 2 return $self->db->selectrow_hashref(
221 'SELECT id,token FROM jobs WHERE scanner=? AND run IS NULL AND finished=0 ORDER BY created,id LIMIT 1',
222 undef, $scanner
223 );
224 }
225  
226 6 ▶sub claim {
Subroutine claim: called CC=1 SCAR=0.1
Pod undocumented
227 6 my ($self, $scanner, $id, $run) = @_;
228 return $self->transaction(
229 6 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=22.1
230 6 my ($db) = @_;
231 6 ▶ my $row = $db->selectrow_hashref(
Branch: 1 branch
BranchTrueFalseTotal
my $row = $db->selectrow_hashref("SELECT run,token,finished,json_extract(record,'\$.status') AS status FROM jobs WHERE scanner=? AND id=?", undef, $scanner, $id) or return 5 1 6
232 q{SELECT run,token,finished,json_extract(record,'$.status') AS status FROM jobs WHERE scanner=? AND id=?},
233 undef, $scanner, $id
234 ) or return;
235 5 ▶ if (defined $row->{run}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"run"}) 2 3 5
236 2 ▶ return unless $row->{run} eq $run;
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"run"} eq $run 1 1 2
237 1 ▶ return 'cancel' if $row->{status} eq 'cancelling' || $row->{status} eq 'cancelled';
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"status"} eq "cancelling" or $row->{"status"} eq "cancelled" 0 1 1
Condition33%1/3
$row->{"status"} eq "cancelling" or $row->{"status"} eq "cancelled"
l!l&&r!l&&!r
001
MC/DC0%0/2
$row->{"status"} eq "cancelling" or $row->{"status"} eq "cancelled"
$row->{"status"} eq "cancelling" $row->{"status"} eq "cancelled"
Truth table33%1/3
A or B
A= $row->{"status"} eq "cancelling"B= $row->{"status"} eq "cancelled"
ABresult
1X1
011
000
238 1 return 1;
239 }
240 3 ▶ return 'expired' if $row->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"finished"} 0 3 3
241 3 ▶ unless ($self->selectable($scanner, $row->{token})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->selectable($scanner, $row->{"token"})) 0 3 3
242 0 $db->do(
243 q{UPDATE jobs SET finished=1,record=json_set(record,'$.status','failed','$.phase','finished','$.error','Target no longer available','$.cleanup_status','not_needed') WHERE id=?},
244 undef, $id
245 );
246 0 return 'expired';
247 }
248 $db->do(
249 3 q{UPDATE jobs SET run=?, record=json_set(record,'$.status','running','$.phase','validating') WHERE id=?},
250 undef, $run, $id
251 );
252 3 return 1;
253 }
254 6 );
255 }
256  
257 23 ▶sub owns {
Subroutine owns: called CC=1 SCAR=0.0
Pod undocumented
258 23 my ($self, $scanner, $id, $run) = @_;
259 return
260 23 scalar $self->db->selectrow_array('SELECT 1 FROM jobs WHERE scanner=? AND id=? AND run=?',
261 undef, $scanner, $id, $run);
262 }
263  
264 121 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
265 121 my ($self, $id) = @_;
266 121 ▶ return unless valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless valid_id($id) 1 120 121
267 120 ▶ my $row = $self->db->selectrow_hashref('SELECT * FROM jobs WHERE id=?', undef, $id) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $row = $self->db->selectrow_hashref("SELECT * FROM jobs WHERE id=?", undef, $id) or return 99 21 120
268 return {
269 99 %{from_json($row->{record})},
270 id => $id,
271 scanner_id => $row->{scanner},
272 token => $row->{token},
273 created_at => $row->{created},
274 tags => from_json($row->{tags}),
275 note => $row->{note}
276 99 };
277 }
278  
279 5 ▶sub history {
Subroutine history: called CC=4 SCAR=13.9
Pod undocumented
280 5 my ($self, $tag, $page) = @_;
281 5 ▶ my $filter = length($tag) ? 'WHERE EXISTS(SELECT 1 FROM json_each(tags) WHERE value=?)' : '';
Branch: 1 branch
BranchTrueFalseTotal
length $tag ? : 4 1 5
282 5 ▶ my $rows = $self->db->selectall_arrayref(
Branch: 1 branch
BranchTrueFalseTotal
length $tag ? : 4 1 5
283 "SELECT id, scanner AS scanner_id, created AS created_at, tags, json_extract(record,'\$.status') AS status, json_extract(record,'\$.phase') AS phase, json_extract(record,'\$.cleanup_status') AS cleanup_status FROM jobs $filter ORDER BY created DESC,id DESC LIMIT 51 OFFSET ?",
284 {Slice => {}},
285 (length($tag) ? ($tag) : ()),
286 ($page - 1) * 50
287 );
288 5 $_->{tags} = from_json($_->{tags}) for @$rows;
289 5 return $rows;
290 }
291  
292 2 ▶sub change_tags {
Subroutine change_tags: called CC=1 SCAR=0.0
Pod undocumented
293 2 my ($self, $id, $operation, $values) = @_;
294 return $self->transaction(
295 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
296 2 my $tags = $self->get($id)->{tags};
297 $tags =
298 $operation eq 'add'
299 ? Mogura::Server::Tags->validate([@$tags, @$values])
300 2 ▶ : [grep { $_ ne $values->[0] } @$tags];
Branch: 1 branch
BranchTrueFalseTotal
$operation eq 'add' ? : 1 1 2
301 2 $self->db->do('UPDATE jobs SET tags=? WHERE id=?', undef, to_json($tags), $id);
302 2 return $tags;
303 }
304 2 );
305 }
306  
307 51 ▶sub directory {
Subroutine directory: called CC=2 SCAR=7.1
Pod undocumented
308 51 my ($self, $id) = @_;
309 51 ▶ die 'Invalid job' unless valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless valid_id($id) 0 51 51
310 51 return path($self->state, 'artifacts', $id);
311 }
312  
313 127 ▶sub artifact {
Subroutine artifact: called CC=6 SCAR=18.2
Pod undocumented
314 127 my ($self, $id, $kind) = @_;
315 127 ▶ my $name = artifact_name($kind) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $name = artifact_name($kind) or return 126 1 127
316 return
317 126 ▶ unless valid_id($id)
Branch: 1 branch
BranchTrueFalseTotal
unless valid_id($id) and $self->db->selectrow_array("SELECT 1 FROM receipts WHERE job=? AND kind=?", undef, $id, $kind) 82 44 126
Condition100%3/3
valid_id($id) and $self->db->selectrow_array("SELECT 1 FROM receipts WHERE job=? AND kind=?", undef, $id, $kind)
!ll&&!rl&&r
18144
MC/DC100%2/2
valid_id($id) and $self->db->selectrow_array("SELECT 1 FROM receipts WHERE job=? AND kind=?", undef, $id, $kind)
valid_id($id) $self->db->selectrow_array("SELECT 1 FROM receipts WHERE job=? AND kind=?", undef, $id, $kind)
Truth table100%3/3
A and B
A= valid_id($id)B= $self->db->selectrow_array("SELECT 1 FROM receipts WHERE job=? AND kind=?", undef, $id, $kind)
ABresult
0X0
100
111
318 && $self->db->selectrow_array('SELECT 1 FROM receipts WHERE job=? AND kind=?',
319 undef, $id, $kind);
320 44 my $file = $self->directory($id)->child($name);
321 44 ▶ return -f $file && !-l $file ? $file : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $file && !(-l $file) ? : 44 0 44
Condition33%1/3
-f $file and not -l $file
!ll&&!rl&&r
0044
MC/DC0%0/2
-f $file and not -l $file
-f $file not -l $file
Truth table33%1/3
A and not(B)
A= -f $fileB= not -l $file
ABresult
0X0
100
111
322 }
323  
324 0 ▶sub receive {
Subroutine receive: not called CC=23 SCAR=63.1
Pod undocumented
325 0 my ($self, $scanner, $id, $run, $kind, $source, $size, $digest) = @_;
326 0 ▶ my $name = artifact_name($kind) or die 'Invalid kind';
Branch: 1 branch
BranchTrueFalseTotal
my $name = artifact_name($kind) or die "Invalid kind" 0 0 0
327 0 ▶ die 'Unassigned run' unless $self->owns($scanner, $id, $run);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->owns($scanner, $id, $run) 0 0 0
328 0 ▶ die 'Incomplete upload' unless -s $source == $size;
Branch: 1 branch
BranchTrueFalseTotal
unless -s $source == $size 0 0 0
329 0 ▶ open my $fh, '<:raw', $source or die "Open upload: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $source or die "Open upload: $!" 0 0 0
330 0 ▶ die 'Checksum mismatch' unless Digest::SHA->new(256)->addfile($fh)->hexdigest eq $digest;
Branch: 1 branch
BranchTrueFalseTotal
unless "Digest::SHA"->new(256)->addfile($fh)->hexdigest eq $digest 0 0 0
331 0 my ($accepted) =
332 $self->db->selectrow_array('SELECT hash FROM receipts WHERE job=? AND run=? AND kind=?',
333 undef, $id, $run, $kind);
334 0 ▶ die 'Conflicting receipt' if defined($accepted) && $accepted ne $digest;
Branch: 1 branch
BranchTrueFalseTotal
if defined $accepted and $accepted ne $digest 0 0 0
Condition0%0/3
defined $accepted and $accepted ne $digest
!ll&&!rl&&r
000
MC/DC0%0/2
defined $accepted and $accepted ne $digest
defined $accepted $accepted ne $digest
Truth table0%0/3
A and B
A= defined $acceptedB= $accepted ne $digest
ABresult
0X0
100
111
335 0 ▶ return if defined($accepted) && $self->artifact($id, $kind);
Branch: 1 branch
BranchTrueFalseTotal
if defined $accepted and $self->artifact($id, $kind) 0 0 0
Condition0%0/3
defined $accepted and $self->artifact($id, $kind)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $accepted and $self->artifact($id, $kind)
defined $accepted $self->artifact($id, $kind)
Truth table0%0/3
A and B
A= defined $acceptedB= $self->artifact($id, $kind)
ABresult
0X0
100
111
336  
337 0 ▶ if ($kind eq 'log') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'log') { } 0 0 0
338 0 ▶ seek $fh, 0, 0 or die "Seek log: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die "Seek log: $!" 0 0 0
339 0 while (1) {
340 0 my $read = read $fh, my $chunk, 65536;
341 0 ▶ die "Read log: $!" unless defined $read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 0 0
342 0 ▶ last unless $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 0 0 0
343 0 ▶ die 'Expected a text log' if index($chunk, "\0") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($chunk, "\0") != -1 0 0 0
344 }
345 }
346 else {
347 0 require Mogura::ResultStream;
348 0 Mogura::ResultStream->valid_artifact($source, $kind);
349 }
350 0 my $file = $self->directory($id)->make_path({mode => 0700})->child($name);
351  
352 # An immutable link survives a crash before the receipt commits. A retry
353 # checks the actual file before adopting an orphaned publication.
354 0 ▶ $fh->sync or die "Sync upload: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->sync or die "Sync upload: $!" 0 0 0
355 0 ▶ unless (link $source, $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (link $source, $file) 0 0 0
356 0 ▶ die "Publish upload: $!" unless $!{EEXIST};
Branch: 1 branch
BranchTrueFalseTotal
unless $!{"EEXIST"} 0 0 0
357 0 ▶ open my $old, '<:raw', $file or die "Open published artifact: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $old, "<:raw", $file or die "Open published artifact: $!" 0 0 0
358 0 ▶ die 'Conflicting artifact'
Branch: 1 branch
BranchTrueFalseTotal
unless -s $old == $size and "Digest::SHA"->new(256)->addfile($old)->hexdigest eq $digest 0 0 0
Condition0%0/3
-s $old == $size and "Digest::SHA"->new(256)->addfile($old)->hexdigest eq $digest
!ll&&!rl&&r
000
MC/DC0%0/2
-s $old == $size and "Digest::SHA"->new(256)->addfile($old)->hexdigest eq $digest
-s $old == $size "Digest::SHA"->new(256)->addfile($old)->hexdigest eq $digest
Truth table0%0/3
A and B
A= -s $old == $sizeB= "Digest::SHA"->new(256)->addfile($old)->hexdigest eq $digest
ABresult
0X0
100
111
359 unless -s $old == $size && Digest::SHA->new(256)->addfile($old)->hexdigest eq $digest;
360 }
361 0 my $dir = $file->dirname;
362 0 for my $parent ($dir, $dir->dirname, path($self->state)) {
363 0 ▶ $parent->open('<')->sync or die "Sync artifact directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
$parent->open("<")->sync or die "Sync artifact directory: $!" 0 0 0
364 }
365 0 $self->db->do('INSERT OR IGNORE INTO receipts(job,run,kind,hash) VALUES (?,?,?,?)',
366 undef, $id, $run, $kind, $digest);
367 }
368  
369 10 ▶sub update_run {
Subroutine update_run: called CC=1 SCAR=0.1
Pod undocumented
370 10 my ($self, $scanner, $id, $run, $data, $final) = @_;
371 return $self->transaction(
372 10 ▶ sub {
Subroutine __ANON__: called CC=37 SCAR=40.0
373 10 my ($db) = @_;
374 10 ▶ my $row = $db->selectrow_hashref(
Branch: 1 branch
BranchTrueFalseTotal
my $row = $db->selectrow_hashref("SELECT record,finished FROM jobs WHERE id=? AND scanner=? AND run=?", undef, $id, $scanner, $run) or return 9 1 10
375 'SELECT record,finished FROM jobs WHERE id=? AND scanner=? AND run=?',
376 undef, $id, $scanner, $run)
377 or return;
378 9 my $old = from_json($row->{record});
379 9 ▶ if ($row->{finished}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"finished"}) 3 6 9
380 3 ▶ if ($final
Branch: 1 branch
BranchTrueFalseTotal
if ($final and ($old->{"cleanup_status"} // "") eq "needs_cleanup" and ($data->{"cleanup_status"} // "") eq "unmounted" and $data->{"sequence"} > $old->{"sequence"}) 1 2 3
Condition69%9/13
$final and ($old->{"cleanup_status"} // "") eq "needs_cleanup" and ($data->{"cleanup_status"} // "") eq "unmounted" and $data->{"sequence"} > $old->{"sequence"}
!ll&&!rl&&r
201
$final and ($old->{"cleanup_status"} // "") eq "needs_cleanup" and ($data->{"cleanup_status"} // "") eq "unmounted"
!ll&&!rl&&r
201
$final and ($old->{"cleanup_status"} // "") eq "needs_cleanup"
!ll&&!rl&&r
111
$old->{"cleanup_status"} // ""
l!l
20
$data->{"cleanup_status"} // ""
l!l
10
MC/DC25%1/4
$final and ($old->{"cleanup_status"} // "") eq "needs_cleanup" and ($data->{"cleanup_status"} // "") eq "unmounted" and $data->{"sequence"} > $old->{"sequence"}
$final $old->{"cleanup_status"} $data->{"cleanup_status"} $data->{"sequence"} > $old->{"sequence"}
Truth table40%2/5
A and B and C and D
A= $finalB= $old->{"cleanup_status"}C= $data->{"cleanup_status"}D= $data->{"sequence"} > $old->{"sequence"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
381 && ($old->{cleanup_status} // '') eq 'needs_cleanup'
382 && ($data->{cleanup_status} // '') eq 'unmounted'
383 && $data->{sequence} > $old->{sequence})
384 {
385 $db->do(
386 q{UPDATE jobs SET record=json_set(record,'$.cleanup_status','unmounted','$.sequence',?) WHERE id=?},
387 1 undef, 0 + $data->{sequence}, $id
388 );
389 }
390 3 return 1;
391 }
392 6 ▶ return !$final if ($old->{sequence} // 0) > $data->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if ($old->{"sequence"} // 0) > $data->{"sequence"} 0 6 6
Condition100%2/2
$old->{"sequence"} // 0
l!l
33
MC/DC100%1/1
$old->{"sequence"} // 0
$old->{"sequence"}
Truth table100%2/2
A
A= $old->{"sequence"}
Aresult
11
00
393 6 ▶ return 1 if !$final && ($old->{sequence} // 0) == $data->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if not $final and ($old->{"sequence"} // 0) == $data->{"sequence"} 0 6 6
Condition60%3/5
not $final and ($old->{"sequence"} // 0) == $data->{"sequence"}
!ll&&!rl&&r
510
$old->{"sequence"} // 0
l!l
01
MC/DC0%0/2
not $final and ($old->{"sequence"} // 0) == $data->{"sequence"}
not $final $old->{"sequence"}
Truth table66%2/3
not(A) and B
A= not $finalB= $old->{"sequence"}
ABresult
0X0
100
111
394 6 ▶ my $cleanup = $data->{cleanup_status} // '';
Condition50%1/2
$data->{"cleanup_status"} // ""
l!l
60
MC/DC0%0/1
$data->{"cleanup_status"} // ""
$data->{"cleanup_status"}
Truth table50%1/2
A
A= $data->{"cleanup_status"}
Aresult
11
00
395 6 ▶ return unless $cleanup =~ /\A(?:unknown|mounted|unmounted|not_needed|needs_cleanup)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cleanup =~ /\A(?:unknown|mounted|unmounted|not_needed|needs_cleanup)\z/u 0 6 6
396 my $record =
397 6 ▶ {map { exists $data->{$_} ? ($_ => $data->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{$_} ? : 12 60 72
398 qw(sequence phase started_at finished_at warnings error failed_phase syft_version grype_version grype_db sbom_sha256 exclusions)
399 };
400 6 $record->{target} = $old->{target};
401 $record->{status} =
402 $final ? $data->{status}
403 6 ▶ : ($data->{phase} // '') eq 'finished' ? 'transferring'
Branch: 2 branches
BranchTrueFalseTotal
$final ? : 5 1 6
($data->{'phase'} // '') eq 'finished' ? : 0 1 1
Condition50%1/2
$data->{"phase"} // ""
l!l
10
MC/DC0%0/1
$data->{"phase"} // ""
$data->{"phase"}
Truth table50%1/2
A
A= $data->{"phase"}
Aresult
11
00
404 : 'running';
405  
406 6 ▶ if ($old->{status} eq 'cancelling') {
Branch: 1 branch
BranchTrueFalseTotal
if ($old->{"status"} eq "cancelling") 1 5 6
407 1 ▶ if (!$final) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $final) { } 0 1 1
elsif ($record->{'status'} eq 'interrupted') { } 0 1 1
408 0 $record->{status} = 'cancelling';
409 }
410 elsif ($record->{status} eq 'interrupted') {
411 0 $record->{status} = 'cancelled';
412 }
413 }
414 $record->{outcome} = $final ? $record->{status} : $data->{status}
415 6 ▶ if ($data->{phase} // '') eq 'finished';
Branch: 2 branches
BranchTrueFalseTotal
if ($data->{"phase"} // "") eq "finished" 5 1 6
$final ? : 5 0 5
Condition50%1/2
$data->{"phase"} // ""
l!l
60
MC/DC0%0/1
$data->{"phase"} // ""
$data->{"phase"}
Truth table50%1/2
A
A= $data->{"phase"}
Aresult
11
00
416 6 $record->{cleanup_status} = $cleanup;
417  
418 6 ▶ if ($final) {
Branch: 1 branch
BranchTrueFalseTotal
if ($final) 5 1 6
419 return
420 5 ▶ unless ($record->{status} // '') =~
Branch: 1 branch
BranchTrueFalseTotal
unless ($record->{"status"} // "") =~ /\A(?:completed|partial|failed|interrupted|cancelled)\z/u 0 5 5
Condition50%1/2
$record->{"status"} // ""
l!l
50
MC/DC0%0/1
$record->{"status"} // ""
$record->{"status"}
Truth table50%1/2
A
A= $record->{"status"}
Aresult
11
00
421 /\A(?:completed|partial|failed|interrupted|cancelled)\z/;
422 5 ▶ return if $record->{status} eq 'cancelled' && $old->{status} ne 'cancelling';
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"status"} eq "cancelled" and $old->{"status"} ne "cancelling" 0 5 5
Condition33%1/3
$record->{"status"} eq "cancelled" and $old->{"status"} ne "cancelling"
!ll&&!rl&&r
500
MC/DC0%0/2
$record->{"status"} eq "cancelled" and $old->{"status"} ne "cancelling"
$record->{"status"} eq "cancelled" $old->{"status"} ne "cancelling"
Truth table33%1/3
A and B
A= $record->{"status"} eq "cancelled"B= $old->{"status"} ne "cancelling"
ABresult
0X0
100
111
423 5 ▶ if ($record->{status} eq 'completed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"status"} eq "completed") 3 2 5
424 return
425 unless $cleanup eq
426 3 ▶ (($old->{target}{kind} // '') eq 'host' ? 'not_needed' : 'unmounted');
Branch: 2 branches
BranchTrueFalseTotal
unless $cleanup eq (($old->{"target"}{"kind"} // "") eq "host" ? "not_needed" : "unmounted") 1 2 3
($old->{'target'}{'kind'} // '') eq 'host' ? : 0 3 3
Condition50%1/2
$old->{"target"}{"kind"} // ""
l!l
03
MC/DC0%0/1
$old->{"target"}{"kind"} // ""
$old->{"target"}{"kind"}
Truth table50%1/2
A
A= $old->{"target"}{"kind"}
Aresult
11
00
427 2 ▶ return if grep { !$self->artifact($id, $_) } qw(syft cdx grype);
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $self->artifact($id, $_);} "syft", "cdx", "grype" 1 1 2
428 }
429 }
430 4 ▶ $db->do('UPDATE jobs SET record=?,finished=? WHERE id=?',
Branch: 1 branch
BranchTrueFalseTotal
$final ? : 3 1 4
431 undef, to_json($record), $final ? 1 : 0, $id);
432 4 return 1;
433 }
434 10 );
435 }
436  
437 4 1;
↑ Summary lib/Mogura/Scanner.pm »