| 1 | package App::CriticDB::DB::Stor; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use parent 'App::CriticDB::DB'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp qw/confess/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Fcntl qw/:flock/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Temp qw//; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Storable qw/nstore_fd fd_retrieve/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION='0.0.6'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | sub storfileUnchanged { | ||||||||||||||||||||||||||
|
Subroutine
storfileUnchanged: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 6 | my ($self)=@_; | |||||||||||||||||||||||||||
| 16 | 6 | return !$self->_fileNewer($$self{file},$$self{mtime}); | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 6 | my ($self)=@_; | |||||||||||||||||||||||||||
| 21 | 6 | ▶ | if($$self{mtime}&&!$self->_fileNewer($$self{file},$$self{mtime})) { return $self } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"mtime"} and not $self->_fileNewer($self->{"file"}, $self->{"mtime"})
MC/DC50%1/2
$self->{"mtime"} and not $self->_fileNewer($self->{"file"}, $self->{"mtime"})
$self->{"mtime"}
not $self->_fileNewer($self->{"file"}, $self->{"mtime"})
Truth table66%2/3
A and not(B)
A= $self->{"mtime"}B= not $self->_fileNewer($self->{"file"}, $self->{"mtime"})
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | if(!-e $$self{file}) { return $self->_init() } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 2 | $$self{mtime}=time(); | |||||||||||||||||||||||||||
| 24 | 2 | ▶ | open(my $fh,'<',$$self{file}) or confess("Read failed on $$self{file}: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | flock($fh, LOCK_SH) or confess("Cannot lock $$self{file} for reading: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 2 | $$self{store}=fd_retrieve($fh); | |||||||||||||||||||||||||||
| 27 | 2 | flock($fh, LOCK_UN); | |||||||||||||||||||||||||||
| 28 | 2 | close($fh); | |||||||||||||||||||||||||||
| 29 | 2 | return $self; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 7 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=9 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 7 | my ($self,$fn)=@_; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | if(!$$self{file}) { confess("Filename has not been specified") } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 7 | ▶ | if(!-e $$self{file}) { delete($$self{mtime}) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 7 | my ($fh,$tmpname,$lockfh); | |||||||||||||||||||||||||||
| 37 | 2 | ▶ | my $failed=sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 38 | 2 | my ($message,$raw)=@_; | |||||||||||||||||||||||||||
| 39 | 2 | ▶ | if($tmpname) { unlink($tmpname) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | if($lockfh) { flock($lockfh,LOCK_UN); close($lockfh) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | if($message) { confess("Failed to $message") } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 1 | confess($raw); | |||||||||||||||||||||||||||
| 43 | 7 | }; | |||||||||||||||||||||||||||
| 44 | 7 | $fh=File::Temp->new(TEMPLATE=>"$$self{file}-XXXXXXXX",UNLINK=>0,SUFFIX=>'.tmp',PERMS=>(0666&(~umask()))); | |||||||||||||||||||||||||||
| 45 | 7 | $tmpname=$fh->filename(); | |||||||||||||||||||||||||||
| 46 | 7 | ▶ | nstore_fd($$self{store},$fh) or &$failed("write to $tmpname"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 7 | ▶ | close($fh) or &$failed("fully write to $tmpname"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 7 | ▶ | open($lockfh,'>>',$$self{file}) or &$failed("open $$self{file}: $!",undef($lockfh)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 6 | ▶ | flock($lockfh,LOCK_EX) or &$failed("lock $$self{file} for writing: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 6 | ▶ | $self->storfileUnchanged() or &$failed(0,"File changed underneath us, not yet handled"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 5 | ▶ | rename($tmpname,$$self{file}) or &$failed("rename $tmpname to $$self{file}: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 5 | flock($lockfh, LOCK_UN); | |||||||||||||||||||||||||||
| 53 | 5 | close($lockfh); | |||||||||||||||||||||||||||
| 54 | 5 | $$self{mtime}=time(); | |||||||||||||||||||||||||||
| 55 | 5 | return $self; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | 1; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | __END__ | ||||||||||||||||||||||||||||