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.09 2021/12/28 15:46:49 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 3     3   1821 use 5.008;
  3         10  
35              
36 3     3   18 use strict;
  3         6  
  3         86  
37 3     3   15 use warnings;
  3         6  
  3         97  
38 3     3   16 use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
  3         5  
  3         206  
39 3     3   18 use base qw(Exporter);
  3         5  
  3         264  
40 3     3   20 use Fcntl qw(:flock O_RDWR O_CREAT O_EXCL);
  3         6  
  3         475  
41 3     3   22 use Paranoid;
  3         6  
  3         162  
42 3     3   469 use Paranoid::Debug qw(:all);
  3         6  
  3         489  
43 3     3   619 use Paranoid::IO;
  3         8  
  3         558  
44              
45             ($VERSION) = ( q$Revision: 2.09 $ =~ /(\d+(?:\.\d+)+)/sm );
46              
47             @EXPORT = qw(plock pexclock pshlock punlock);
48             @EXPORT_OK = @EXPORT;
49             %EXPORT_TAGS = ( all => [@EXPORT_OK], );
50              
51 3     3   26 use constant PRIV_UMASK => 0660;
  3         7  
  3         1475  
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 12     12 1 40 my $filename = shift;
68 12         18 my $type = shift;
69 12         21 my $perms = shift;
70 12         18 my ( $rv, $fh );
71              
72 12         39 pdebug( 'entering w/(%s)(%s)(%s)', PDLEVEL1, $filename, $type, $perms );
73 12         33 pIn();
74              
75             # Set the defaults
76 12 100       28 $perms = PRIV_UMASK unless defined $perms;
77 12 100       41 $type = LOCK_EX unless defined $type;
78              
79             # Open the file and apply the lock
80 12   33     44 $fh = popen( $filename, O_RDWR | O_CREAT | O_EXCL, $perms )
81             || popen( $filename, O_RDWR, $perms );
82 12 50       47 $rv = pflock( $filename, $type ) if defined $fh;
83              
84 12         57 pOut();
85 12         33 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
86              
87 12         62 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 2     2 1 6 my $filename = shift;
97 2         6 my $mode = shift;
98 2         7 my $rv = 1;
99 2         5 my $fh;
100              
101 2         9 pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $filename, $mode );
102 2         9 pIn();
103              
104 2         7 $rv = plock( $filename, LOCK_EX, $mode );
105              
106 2         129 pOut();
107 2         11 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
108              
109 2         11 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 2     2 1 7 my $filename = shift;
119 2         4 my $mode = shift;
120 2         5 my $rv = 1;
121 2         3 my $fh;
122              
123 2         10 pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $filename, $mode );
124 2         6 pIn();
125              
126 2         6 $rv = plock( $filename, LOCK_SH, $mode );
127              
128 2         14 pOut();
129 2         7 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
130              
131 2         18 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 2     2 1 6 my $filename = shift;
141 2         4 my $mode = shift;
142 2         5 my $rv = 1;
143 2         4 my $fh;
144              
145 2         9 pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $filename, $mode );
146 2         7 pIn();
147              
148 2         8 $rv = plock( $filename, LOCK_UN, $mode );
149              
150 2         15 pOut();
151 2         9 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
152              
153 2         11 return $rv;
154             }
155              
156             1;
157              
158             __END__