blib/lib/Path/Class/File/Lockable.pm

filter: Statementstmt 92.4% 49 / 53 Branchbran 50.0% 5 / 10 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 82.6% 81 / 98 CC 9 SCAR 13.6
CC
9
Cov
80%
lock
10.0
lock_info
2.1
unlock
2.0
CRAP 3.9
↑ Summary
1 package Path::Class::File::Lockable;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base qw( Path::Class::File );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::NFSLock;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Fcntl qw(LOCK_EX LOCK_NB);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.03';
11  
12 =head1 NAME
13  
14 Path::Class::File::Lockable - lock your files with Path::Class::File
15  
16 =head1 SYNOPSIS
17  
18 my $file = Path::Class::File::Lockable->new('path/to/file');
19 $file->lock;
20 # do stuff with $file
21 $file->unlock;
22  
23 =head1 DESCRIPTION
24  
25 Path::Class::File::Lockable uses simple files to indicate whether
26 a file is locked or not. It does not use flock(), since that is
27 unstable over NFS. Effort has been made to avoid race conditions.
28  
29 Path::Class::File::Lockable is intended for long-standing locks, as in a
30 Subversion workspace. See SVN::Class for example.
31  
32 =head1 METHODS
33  
34 This is a subclass of Path::Class::File. Only new or overridden methods
35 are documented here.
36  
37 =cut
38  
39 =head2 lock_ext
40  
41 Returns the file extension used to indicate a lock file. Default is
42 C<.lock>.
43  
44 =cut
45  
46 9 ▶sub lock_ext {'.lock'}
Subroutine lock_ext: called CC=1 SCAR=0.0
Pod documented
47  
48 =head2 lock_file
49  
50 Returns a Path::Class::File object representing the lock file
51 itself. No check is made as to whether the lock file exists.
52  
53 =cut
54  
55 9 ▶sub lock_file {
Subroutine lock_file: called CC=1 SCAR=0.0
Pod documented
56 9 my $self = shift;
57 9 return Path::Class::File->new( join( '', $self, $self->lock_ext ) );
58 }
59  
60 =head2 lock_info
61  
62 Returns a colon-limited string with the contents of the lock file.
63 Will croak if the lock file does not exist.
64  
65 B<Note> that the owner and timestamp in the file contents
66 are not from a stat() of the file.
67 They are written
68 at the time the lock file is created. So chown'ing or touch'ing
69 a lock file do not alter its status.
70  
71 See lock_owner() and lock_time() for easier ways to get at specific
72 information.
73  
74 =cut
75  
76 4 ▶sub lock_info {
Subroutine lock_info: called CC=2 SCAR=7.4
Pod documented
77 4 my $self = shift;
78 4 my $lfile = $self->lock_file;
79 4 ▶ if ( !-s $lfile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $lfile) 0 4 4
80 0 croak "no such lock file: $lfile";
81 }
82 4 return $lfile->slurp;
83 }
84  
85 =head2 lock_owner
86  
87 Returns the name of the person who locked the file.
88  
89 =cut
90  
91 1 ▶sub lock_owner {
Subroutine lock_owner: called CC=1 SCAR=0.0
Pod documented
92 1 my $self = shift;
93 1 return ( split( m/:/, $self->lock_info ) )[0];
94 }
95  
96 =head2 lock_time
97  
98 Returns the time the file was locked (in Epoch seconds).
99  
100 =cut
101  
102 1 ▶sub lock_time {
Subroutine lock_time: called CC=1 SCAR=0.0
Pod documented
103 1 my $self = shift;
104 1 return ( split( m/:/, $self->lock_info ) )[1];
105 }
106  
107 =head2 lock_pid
108  
109 Returns the PID of the process that locked the file.
110  
111 =cut
112  
113 1 ▶sub lock_pid {
Subroutine lock_pid: called CC=1 SCAR=0.0
Pod documented
114 1 my $self = shift;
115 1 return ( split( m/:/, $self->lock_info ) )[2];
116 }
117  
118 =head2 locked
119  
120 Returns true if the file has an existing lock file.
121  
122 =cut
123  
124 2 ▶sub locked {
Subroutine locked: called CC=1 SCAR=0.0
Pod documented
125 2 my $self = shift;
126 2 return -s $self->lock_file;
127 }
128  
129 =head2 lock( [I<owner>] )
130  
131 Acquire a lock on the file.
132  
133 This method should be NFS-safe via File::NFSLock.
134  
135 =cut
136  
137 1 ▶sub lock {
Subroutine lock: called CC=7 SCAR=23.0
Pod documented
138 1 my $self = shift;
139 1 my $owner;
140 1 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 1 1
141 0 require Win32;
142 0 $owner = Win32::LoginName();
143 }
144 else {
145 1 ▶ $owner = shift || getlogin() || ( getpwuid($<) )[0] || 'anonymous';
Condition37%3/8
shift() || getlogin || (getpwuid $<)[0] || 'anonymous'
l!l
10
shift() or getlogin or (getpwuid $<)[0]
l!l&&r!l&&!r
010
shift() or getlogin
l!l&&r!l&&!r
001
MC/DC0%0/3
shift() || getlogin || (getpwuid $<)[0] || 'anonymous'
shift() getlogin (getpwuid $<)[0]
Truth table25%1/4
A or B or C
A= shift()B= getloginC= (getpwuid $<)[0]
ABCresult
1XX1
01X1
0011
0000
146 }
147  
148 # we have to lock our lock file first, to avoid
149 # NFS and race condition badness.
150 # so obtain a lock on our lock file, write our lock
151 # then release the lock on our lock file.
152 # we can't use File::NFSLock all by itself since it is
153 # not persistent across processes.
154 1 my $lock = File::NFSLock->new(
155 { file => $self->lock_file,
156 lock_type => LOCK_EX | LOCK_NB,
157 blocking_timeout => 5,
158 stale_lock_timeout => 5
159 }
160 );
161  
162 1 ▶ if ( !$lock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lock) 0 1 1
163 0 croak "can't get safe lock on lock file: $File::NFSLock::errstr";
164 }
165  
166 1 ▶ my $fh = $self->lock_file->openw() or croak "can't write lock file: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->lock_file->openw or croak("can't write lock file: $!") 1 0 1
167 1 print {$fh} join( ':', $owner, time(), $$ );
168 1 $fh->close;
169  
170 1 $lock->unlock;
171 }
172  
173 =head2 unlock
174  
175 Removes lock file. Uses system() call to enable unlinking across
176 NFS. Will croak on any error.
177  
178 =cut
179  
180 1 ▶sub unlock {
Subroutine unlock: called CC=2 SCAR=7.1
Pod documented
181 1 my $self = shift;
182 1 ▶ $self->lock_file->remove or croak "can't unlink lock file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->lock_file->remove or croak("can't unlink lock file: $!") 1 0 1
183 1 return 1;
184 }
185  
186 =head1 AUTHOR
187  
188 Peter Karman, C<< <karman at cpan.org> >>
189  
190 =head1 BUGS
191  
192 Please report any bugs or feature requests to
193 C<bug-path-class-file-lockable at rt.cpan.org>, or through the web interface at
194 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Path-Class-File-Lockable>.
195 I will be notified, and then you'll automatically be notified of progress on
196 your bug as I make changes.
197  
198 =head1 SUPPORT
199  
200 You can find documentation for this module with the perldoc command.
201  
202 perldoc Path::Class::File::Lockable
203  
204 You can also look for information at:
205  
206 =over 4
207  
208 =item * AnnoCPAN: Annotated CPAN documentation
209  
210 L<http://annocpan.org/dist/Path-Class-File-Lockable>
211  
212 =item * CPAN Ratings
213  
214 L<http://cpanratings.perl.org/d/Path-Class-File-Lockable>
215  
216 =item * RT: CPAN's request tracker
217  
218 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Path-Class-File-Lockable>
219  
220 =item * Search CPAN
221  
222 L<http://search.cpan.org/dist/Path-Class-File-Lockable>
223  
224 =back
225  
226 =head1 ACKNOWLEDGEMENTS
227  
228 There are lots of lock file modules on CPAN. Some of them are probably better
229 suited to your needs than this one.
230  
231 The Minnesota Supercomputing Institute C<< http://www.msi.umn.edu/ >>
232 sponsored the development of this software.
233  
234 =head1 SEE ALSO
235  
236 File::NFSLock, Path::Class::File
237  
238 =head1 COPYRIGHT & LICENSE
239  
240 Copyright 2007 by the Regents of the University of Minnesota.
241 All rights reserved.
242  
243 This program is free software; you can redistribute it and/or modify it
244 under the same terms as Perl itself.
245  
246 =cut
247  
248 3 1;
↑ Summary