blib/lib/File/Flock/Tiny.pm

filter: Statementstmt 100% 56 / 56 Branchbran 81.2% 13 / 16 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 97.1% 101 / 104 CC 12 SCAR 10.0
CC
12
Cov
96%
_open_file
6.0
DESTROY
3.0
lock
2.0
CRAP 2.7
↑ Summary
1 package File::Flock::Tiny;
2  
3 5 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 File::Flock::Tiny - yet another flock package
13  
14 =cut
15  
16 5 our $VERSION = '0.14';
17 5 $VERSION = eval $VERSION;
18  
19 =head1 SYNOPSIS
20  
21 my $lock = File::Flock::Tiny->lock($file);
22 ... # do something
23 $lock->release;
24  
25 =head1 DESCRIPTION
26  
27 Simple wrapper around L<flock|perldoc/flock> for ease of use.
28  
29 =head1 CLASS METHODS
30  
31 =cut
32  
33 39 ▶sub _open_file {
Subroutine _open_file: called CC=6 SCAR=18.0
34 39 my $file = shift;
35 39 my $fh;
36 39 ▶ if ( ref $file && ( ref $file eq 'GLOB' || $file->isa("IO::Handle") ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file and ref $file eq 'GLOB' || $file->isa('IO::Handle')) { } 8 31 39
Condition100%6/6
ref $file and ref $file eq "GLOB" || $file->isa("IO::Handle")
!ll&&!rl&&r
4278
ref $file eq "GLOB" or $file->isa("IO::Handle")
l!l&&r!l&&!r
4427
MC/DC100%3/3
ref $file and ref $file eq "GLOB" || $file->isa("IO::Handle")
ref $file ref $file eq "GLOB" $file->isa("IO::Handle")
Truth table100%4/4
A and B or C
A= ref $fileB= ref $file eq "GLOB"C= $file->isa("IO::Handle")
ABCresult
0XX0
1000
11X1
1011
37 8 ▶ $fh = IO::Handle->new_from_fd( $file, ">>" ) or croak "Coundn't dupe file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Handle"->new_from_fd($file, ">>") or croak("Coundn't dupe file: $!") 8 0 8
38 }
39 else {
40 31 ▶ open $fh, ">>", $file or croak "Couldn't open file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>", $file or croak("Couldn't open file: $!") 31 0 31
41 }
42 39 return $fh;
43 }
44  
45 =head2 File::Flock::Tiny->lock($file)
46  
47 Acquire exclusive lock on the file. I<$file> may be a file name or an opened
48 file handler. If a filename given and the file doesn't exist it will be
49 created. The method returns a lock object, the file remains locked until this
50 object goes out of the scope, or till you call I<release> method on it.
51  
52 =cut
53  
54 23 ▶sub lock {
Subroutine lock: called CC=2 SCAR=7.1
Pod documented
55 23 my $fh = _open_file( $_[1] );
56 23 ▶ flock $fh, LOCK_EX or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or croak($!) 23 0 23
57 23 return bless $fh, "File::Flock::Tiny::Lock";
58 }
59  
60 =head2 File::Flock::Tiny->trylock($file)
61  
62 Same as I<lock>, but doesn't block and returns immediately, if the lock can not
63 be acquired returns undef.
64  
65 =cut
66  
67 16 ▶sub trylock {
Subroutine trylock: called CC=2 SCAR=6.9
Pod documented
68 16 my $fh = _open_file( $_[1] );
69 16 bless $fh, "File::Flock::Tiny::Lock";
70 16 ▶ return unless flock $fh, LOCK_EX | LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
unless flock $fh, 6 6 10 16
71 10 return $fh;
72 }
73  
74 =head2 File::Flock::Tiny->write_pid($file)
75 X<write_pid>
76  
77 Try to lock the file and save the process ID into it. Returns the lock object,
78 or undef if the file was already locked. The lock returned by I<write_pid> will
79 be automatically released when the object goes out of the scope in the process
80 that locked the pid file, in child processes you can release the lock
81 explicitely.
82  
83 =cut
84  
85 4 ▶sub write_pid {
Subroutine write_pid: called CC=2 SCAR=6.9
Pod documented
86 4 my ( $class, $file ) = @_;
87 4 my $lock = $class->trylock($file);
88 4 ▶ $lock->write_pid if $lock;
Branch: 1 branch
BranchTrueFalseTotal
if $lock 2 2 4
89 4 return $lock;
90 }
91  
92 package File::Flock::Tiny::Lock;
93 5 ▶use parent 'IO::Handle';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 5 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 =head1 LOCK OBJECT METHODS
97  
98 Here is the list of methods that you can invoke on a lock object.
99  
100 =head2 $lock->write_pid
101  
102 Truncates locked file and saves PID into it. Also marks the lock object as tied
103 to the current process, so it only will be automatically released when goes out
104 of scope in the current process but not in any of the child processes created
105 after this call. This method may be used to create pid files for daemons, you
106 can lock file in parent process to ensure that there is no another copy of the
107 daemon running already, and then fork and write pid of the child into the file.
108 Here is the simplified example of daemonizing code:
109  
110 my $pid = File::Flock::Tiny->trylock('daemon.pid')
111 or die "Daemon already running";
112 if ( fork == 0 ) {
113 setsid;
114 if (fork) {
115 # intermediate process
116 $pid->close;
117 exit 0;
118 }
119 }
120 else {
121 # parent process
122 $pid->close;
123 exit 0;
124 }
125 # daemon process
126 # perhaps you want to close all opened files here, do not close $pid!
127 $pid->write_pid;
128  
129 It is importand to remember to close the lock file in the parent and
130 intermediate processes, otherwise the lock will be released during destruction
131 of the variable.
132  
133 =cut
134  
135 2 ▶sub write_pid {
Subroutine write_pid: called CC=1 SCAR=0.0
136 2 my $lock = shift;
137 2 $lock->truncate(0);
138 2 $lock->print("$$\n");
139 2 $lock->flush;
140 2 *$lock->{destroy_only_in} = $$;
141 2 return;
142 }
143  
144 =head2 $lock->release
145  
146 Release lock and close the file
147  
148 =cut
149  
150 50 ▶sub release {
Subroutine release: called CC=2 SCAR=6.9
151 50 my $lock = shift;
152 50 ▶ if ( $lock->opened ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock->opened) 35 15 50
153 35 flock $lock, LOCK_UN;
154 35 close $lock;
155 }
156 }
157  
158 39 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
159 39 my $lock = shift;
160 39 ▶ unless ( *$lock->{destroy_only_in} && *$lock->{destroy_only_in} != $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*$lock->{"destroy_only_in"} and *$lock->{"destroy_only_in"} != $$) 38 1 39
Condition100%3/3
*$lock->{"destroy_only_in"} and *$lock->{"destroy_only_in"} != $$
!ll&&!rl&&r
3711
MC/DC100%2/2
*$lock->{"destroy_only_in"} and *$lock->{"destroy_only_in"} != $$
*$lock->{"destroy_only_in"} *$lock->{"destroy_only_in"} != $$
Truth table100%3/3
A and B
A= *$lock->{"destroy_only_in"}B= *$lock->{"destroy_only_in"} != $$
ABresult
0X0
100
111
161 38 $lock->release;
162 }
163 }
164  
165 =head2 $lock->close
166  
167 Close the locked filehandle, but do not release the lock. Normally if you closed
168 the file it will be unlocked, but if you forked after locking the file and when
169 closed the lock in the parent process, the file will still be locked even after
170 the lock went out of the scope in the parent process. The following example
171 demonstrates the use for this method:
172  
173 {
174 my $lock = File::Flock::Tiny->lock("lockfile");
175 my $pid = fork;
176 if( $pid == 0 ) {
177 # We are in child process
178 do_something();
179 }
180 $lock->close;
181 }
182 # file still locked by child. Without $lock->close,
183 # it would be unlocked by parent when $lock went out
184 # of the scope
185  
186 Note, that this behaviour is not portable! It works on Linux and BSD, but on
187 Solaris locks are not inherited by child processes, so the file will be
188 unlocked as soon as the parent process will close it. See also description of
189 L<flock|perlfunc/flock>.
190  
191 =cut
192  
193 5 1;
194  
195 __END__
↑ Summary