File Coverage

blib/lib/Paranoid/IO/Lockfile.pm
Criterion Covered Total %
statement 72 72 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 14 14 100.0
pod 4 4 100.0
total 96 99 96.9


line stmt bran cond sub pod time code
1             # Paranoid::IO::Lockfile -- Paranoid Lockfile support
2             #
3             # $Id: lib/Paranoid/IO/Lockfile.pm, 2.08 2020/12/31 12:10:06 acorliss Exp $
4             #
5             # This software is free software. Similar to Perl, you can redistribute it
6             # and/or modify it under the terms of either:
7             #
8             # a) the GNU General Public License
9             # as published by the
10             # Free Software Foundation ; either version 1
11             # , or any later version
12             # , or
13             # b) the Artistic License 2.0
14             # ,
15             #
16             # subject to the following additional term: No trademark rights to
17             # "Paranoid" have been or are conveyed under any of the above licenses.
18             # However, "Paranoid" may be used fairly to describe this unmodified
19             # software, in good faith, but not as a trademark.
20             #
21             # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22             # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23             #
24             #####################################################################
25              
26             #####################################################################
27             #
28             # Environment definitions
29             #
30             #####################################################################
31              
32             package Paranoid::IO::Lockfile;
33              
34 2     2   964 use 5.008;
  2         7  
35              
36 2     2   11 use strict;
  2         4  
  2         39  
37 2     2   10 use warnings;
  2         3  
  2         54  
38 2     2   12 use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
  2         3  
  2         188  
39 2     2   13 use base qw(Exporter);
  2         4  
  2         167  
40 2     2   13 use Fcntl qw(:flock O_RDWR O_CREAT O_EXCL);
  2         4  
  2         323  
41 2     2   14 use Paranoid;
  2         4  
  2         110  
42 2     2   498 use Paranoid::Debug qw(:all);
  2         5  
  2         323  
43 2     2   532 use Paranoid::IO;
  2         5  
  2         291  
44              
45             ($VERSION) = ( q$Revision: 2.08 $ =~ /(\d+(?:\.\d+)+)/sm );
46              
47             @EXPORT = qw(plock pexclock pshlock punlock);
48             @EXPORT_OK = @EXPORT;
49             %EXPORT_TAGS = ( all => [@EXPORT_OK], );
50              
51 2     2   16 use constant PRIV_UMASK => 0660;
  2         4  
  2         870  
52              
53             #####################################################################
54             #
55             # Module code follows
56             #
57             #####################################################################
58              
59             sub plock {
60              
61             # Purpose: Opens and locks the specified file.
62             # Returns: True/false
63             # Usage: $rv = plock( $filename );
64             # Usage: $rv = plock( $filename, $lockType );
65             # Usage: $rv = plock( $filename, $lockType, $fileMode );
66              
67 6     6 1 19 my $filename = shift;
68 6         11 my $type = shift;
69 6         9 my $perms = shift;
70 6         11 my ( $rv, $fh );
71              
72 6         17 pdebug( 'entering w/(%s)(%s)(%s)', PDLEVEL1, $filename, $type, $perms );
73 6         17 pIn();
74              
75             # Set the defaults
76 6 100       15 $perms = PRIV_UMASK unless defined $perms;
77 6 100       12 $type = LOCK_EX unless defined $type;
78              
79             # Open the file and apply the lock
80 6   33     15 $fh = popen( $filename, O_RDWR | O_CREAT | O_EXCL, $perms )
81             || popen( $filename, O_RDWR, $perms );
82 6 50       21 $rv = pflock( $filename, $type ) if defined $fh;
83              
84 6         16 pOut();
85 6         15 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
86              
87 6         19 return $rv;
88             }
89              
90             sub pexclock {
91              
92             # Purpose: Applies an exclusive lock
93             # Returns: True/false
94             # Usage: $rv = pexclock($filename);
95              
96 1     1 1 3 my $filename = shift;
97 1         3 my $mode = shift;
98 1         2 my $rv = 1;
99 1         2 my $fh;
100              
101 1         4 pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $filename, $mode );
102 1         4 pIn();
103              
104 1         3 $rv = plock( $filename, LOCK_EX, $mode );
105              
106 1         6 pOut();
107 1         3 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
108              
109 1         5 return $rv;
110             }
111              
112             sub pshlock {
113              
114             # Purpose: Applies a shared lock
115             # Returns: True/false
116             # Usage: $rv = pshlock($filename);
117              
118 1     1 1 3 my $filename = shift;
119 1         2 my $mode = shift;
120 1         1 my $rv = 1;
121 1         2 my $fh;
122              
123 1         4 pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $filename, $mode );
124 1         10 pIn();
125              
126 1         3 $rv = plock( $filename, LOCK_SH, $mode );
127              
128 1         4 pOut();
129 1         3 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
130              
131 1         5 return $rv;
132             }
133              
134             sub punlock {
135              
136             # Purpose: Removes any existing locks on the file
137             # Returns: True/false
138             # Usage: $rv = punlock($filename);
139              
140 1     1 1 3 my $filename = shift;
141 1         3 my $mode = shift;
142 1         2 my $rv = 1;
143 1         2 my $fh;
144              
145 1         4 pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $filename, $mode );
146 1         5 pIn();
147              
148 1         4 $rv = plock( $filename, LOCK_UN, $mode );
149              
150 1         13 pOut();
151 1         3 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
152              
153 1         5 return $rv;
154             }
155              
156             1;
157              
158             __END__