Multi/FlockFiles.pm

filter: Statementstmt 96.2% 77 / 80 Branchbran 82.3% 28 / 34 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 8 totaltotal 88.3% 129 / 146 CC 19 SCAR 10.1
CC
19
Cov
92%
filehandle_path
5.5
lock_path
4.0
DESTROY
3.0
CRAP 2.8
« Multi/Base.pm ↑ Summary Multi/Base/Iterative.pm »
1 #!perl
2  
3 package File::Lock::Multi::FlockFiles;
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings (FATAL => 'all');
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Lock::Multi::Base::Iterative;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use base q(File::Lock::Multi::Base::Iterative);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 __PACKAGE__->mk_accessors(qw(format clean _id _fh _mine));
13  
14 3 return 1;
15  
16 sub __Validators {
17 13 ▶ my $class = shift;
Subroutine __Validators: called CC=1 SCAR=0.0
18  
19 return(
20 13 $class->SUPER::__Validators(
21 format => { default => "%s.%i" },
22 clean => { regex => qr/^\d+$/, default => 1 },
23 @_
24 )
25 );
26 }
27  
28 sub locked {
29 253 ▶ my $self = shift;
Subroutine locked: called CC=2 SCAR=6.9
Pod undocumented
30 253 ▶ return $self->_fh ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_fh ? : 31 222 253
31 }
32  
33 sub lock_non_block_for {
34 143 ▶ my($self, $id) = @_;
Subroutine lock_non_block_for: called CC=3 SCAR=11.0
Pod undocumented
35 143 ▶ croak "lock_non_block_for called while already locked" if $self->locked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->locked 1 142 143
36 142 ▶ if(my($fh, $mine) = $self->obtain_lock_for($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->obtain_lock_for($id)) { } 18 124 142
37 18 $self->_fh($fh);
38 18 $self->_id($id);
39 18 $self->_mine($mine);
40 18 return $id;
41 } else {
42 124 return;
43 }
44 }
45  
46 sub obtain_lock_for {
47 229 ▶ my($self, $id) = @_;
Subroutine obtain_lock_for: called CC=3 SCAR=11.0
Pod undocumented
48 229 my $path = $self->obtain_path($id);
49 229 ▶ if(my($fh, $mine) = $self->lock_path($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->lock_path($path)) { } 38 191 229
50 38 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 18 20 38
51 18 return($fh, $mine);
52 } else {
53 20 return $fh;
54 }
55 } else {
56 191 return;
57 }
58 }
59  
60 sub obtain_path {
61 229 ▶ my($self, $id) = @_;
Subroutine obtain_path: called CC=1 SCAR=0.0
Pod undocumented
62 229 return $self->format_path($id);
63 }
64  
65 sub format_path {
66 250 ▶ my($self, $id) = @_;
Subroutine format_path: called CC=1 SCAR=0.0
Pod undocumented
67 250 return sprintf($self->format, $self->file, $id);
68 }
69  
70 sub path {
71 21 ▶ my $self = shift;
Subroutine path: called CC=2 SCAR=7.1
Pod undocumented
72 21 ▶ croak "can not obtain a path without an ID" unless defined $self->_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_id 0 21 21
73 21 return $self->format_path($self->_id);
74 }
75  
76 sub _release {
77 18 ▶ my $self = shift;
Subroutine _release: called CC=2 SCAR=6.9
78 18 ▶ $self->_clean if $self->clean;
Branch: 1 branch
BranchTrueFalseTotal
if $self->clean 17 1 18
79 18 $self->_fh->close;
80 18 $self->_fh(undef);
81 18 $self->_id(undef);
82 18 $self->_mine(undef);
83 18 return 1;
84 }
85  
86 sub _clean {
87 17 ▶ my $self = shift;
Subroutine _clean: called CC=1 SCAR=0.0
88 17 $self->__clean($self->path, $self->_mine);
89 }
90  
91 sub __clean {
92 18 ▶ my($self, $path, $mine) = @_;
Subroutine __clean: called CC=3 SCAR=11.0
93 18 ▶ if($mine || $self->clean > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mine or $self->clean > 1) 11 7 18
Condition100%3/3
$mine or $self->clean > 1
l!l&&r!l&&!r
1017
MC/DC100%2/2
$mine or $self->clean > 1
$mine $self->clean > 1
Truth table100%3/3
A or B
A= $mineB= $self->clean > 1
ABresult
1X1
011
000
94 11 unlink($path);
95 }
96 }
97  
98 sub lock_path {
99 229 ▶ my($self, $path) = @_;
Subroutine lock_path: called CC=4 SCAR=14.0
Pod undocumented
100 229 LOCK_NB or die "LOCK_NB is not supported on this operating system";
101 229 my($fh, $mine) = $self->filehandle_path($path);
102 229 ▶ if(flock($fh, LOCK_EX | LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $fh, 6) { } 38 191 229
103 38 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 38 0 38
104 38 return($fh, $mine);
105 } else {
106 0 return $fh;
107 }
108 } else {
109 191 ▶ $self->__clean($path, $mine) if $mine;
Branch: 1 branch
BranchTrueFalseTotal
if $mine 1 190 191
110 191 return;
111 }
112 }
113  
114 sub filehandle_path {
115 229 ▶ my($self, $path) = @_;
Subroutine filehandle_path: called CC=5 SCAR=17.1
Pod undocumented
116 229 my $mine = 0;
117  
118 229 ▶ unless(-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path) 14 215 229
119 14 ▶ open(my $dummy, '>>', $path) or croak "create $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $dummy, ">>", $path or croak("create ${path}: $!") 0 14 14
120 14 $mine = 1;
121 }
122  
123 229 ▶ if(open(my $fh, '<', $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $path) { } 229 0 229
124 229 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 229 0 229
125 229 return($fh, $mine);
126 } else {
127 0 return $fh;
128 }
129 } else {
130 0 croak "open('<', '$path'): $!";
131 }
132 }
133  
134 sub DESTROY {
135 12 ▶ my $self = shift;
Subroutine DESTROY: called CC=3 SCAR=11.1
136 12 ▶ $self->release if $self->locked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->locked 8 4 12
137 12 ▶ $self->SUPER::DESTROY if $self->SUPER::can('DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $self->SUPER::can("DESTROY") 12 0 12
138 }
139  
140 __END__
« Multi/Base.pm ↑ Summary Multi/Base/Iterative.pm »