| 1 | package Mogura::Scanner::Spool; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Mojo::File qw(path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Mojo::JSON qw(decode_json encode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Mogura::Protocol qw(valid_id now artifact_name); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | has 'config'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 509 | ▶ | sub directory { | ||||||||||||||||||||||||||
|
Subroutine
directory: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 509 | my ($self, $id) = @_; | |||||||||||||||||||||||||||
| 11 | 509 | ▶ | die 'Invalid scan ID' unless valid_id($id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 509 | return path($self->config->state, 'spool', $id); | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 215 | ▶ | sub write_record { | ||||||||||||||||||||||||||
|
Subroutine
write_record: called CC=8 SCAR=23.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 215 | my ($self, $path, $value) = @_; | |||||||||||||||||||||||||||
| 17 | 215 | my $tmp = "$path.tmp"; | |||||||||||||||||||||||||||
| 18 | 215 | ▶ | open my $fh, '>:raw', $tmp or die "Open $tmp: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 215 | ▶ | print {$fh} encode_json($value) or die "Write $tmp: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 215 | ▶ | $fh->flush && $fh->sync or die "Sync $tmp: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fh->flush and $fh->sync
MC/DC0%0/2
$fh->flush and $fh->sync
$fh->flush
$fh->sync
Truth table33%1/3
A and B
A= $fh->flushB= $fh->sync
| |||||||||||||||||||||||||||||
| 21 | 215 | ▶ | close $fh or die "Close $tmp: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 215 | ▶ | rename $tmp, $path or die "Publish $path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 215 | ▶ | path($path)->dirname->open('<')->sync or die "Sync record directory: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 206 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 206 | my ($self, $scan) = @_; | |||||||||||||||||||||||||||
| 28 | 206 | my $dir = $self->directory($scan->{id})->make_path({mode => 0700}); | |||||||||||||||||||||||||||
| 29 | 206 | $scan->{sequence}++; | |||||||||||||||||||||||||||
| 30 | 206 | $self->write_record($dir->child('scan.json'), $scan); | |||||||||||||||||||||||||||
| 31 | 206 | return $scan; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 72 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 72 | my ($self, $id) = @_; | |||||||||||||||||||||||||||
| 36 | 72 | my $file = $self->directory($id)->child('scan.json'); | |||||||||||||||||||||||||||
| 37 | 72 | ▶ | return unless -e $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 47 | return decode_json($file->slurp); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 58 | ▶ | sub artifact { | ||||||||||||||||||||||||||
|
Subroutine
artifact: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 58 | my ($self, $id, $kind) = @_; | |||||||||||||||||||||||||||
| 43 | 58 | ▶ | my $name = artifact_name($kind) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 58 | my $file = $self->directory($id)->child($name); | |||||||||||||||||||||||||||
| 45 | 58 | ▶ | return -f $file && !-l $file ? $file : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-f $file and not -l $file
MC/DC50%1/2
-f $file and not -l $file
-f $file
not -l $file
Truth table66%2/3
A and not(B)
A= -f $fileB= not -l $file
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 116 | ▶ | sub log { | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 116 | my ($self, $id, $message) = @_; | |||||||||||||||||||||||||||
| 50 | 116 | ▶ | open my $fh, '>>:encoding(UTF-8)', $self->directory($id)->child('scan.log') or die "Log: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 116 | print {$fh} now() . " $message\n"; | |||||||||||||||||||||||||||
| 52 | 116 | ▶ | close $fh or die "Log: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 3 | 1; | |||||||||||||||||||||||||||