File Coverage

blib/lib/Paranoid/IO/Lockfile.pm
Criterion Covered Total %
statement 64 64 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 14 14 100.0
pod 4 4 100.0
total 88 91 96.7


line stmt bran cond sub pod time code
1             # Paranoid::IO::Lockfile -- Paranoid Lockfile support
2             #
3             # $Id: lib/Paranoid/IO/Lockfile.pm, 2.10 2022/03/08 00:01:04 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   1482 use 5.008;
  3         11  
35              
36 3     3   16 use strict;
  3         6  
  3         53  
37 3     3   12 use warnings;
  3         5  
  3         81  
38 3     3   13 use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
  3         5  
  3         190  
39 3     3   17 use base qw(Exporter);
  3         5  
  3         236  
40 3     3   19 use Fcntl qw(:flock O_RDWR O_CREAT O_EXCL);
  3         13  
  3         411  
41 3     3   18 use Paranoid;
  3         4  
  3         135  
42 3     3   409 use Paranoid::Debug qw(:all);
  3         7  
  3         423  
43 3     3   486 use Paranoid::IO;
  3         14  
  3         424  
44              
45             ($VERSION) = ( q$Revision: 2.10 $ =~ /(\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   21 use constant PRIV_UMASK => 0660;
  3         6  
  3         1097  
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 36 my $filename = shift;
68 12         17 my $type = shift;
69 12         18 my $perms = shift;
70 12         17 my ( $rv, $fh );
71              
72 12         44 subPreamble( PDLEVEL1, '$;$$', $filename, $type, $perms );
73              
74             # Set the defaults
75 12 100       28 $perms = PRIV_UMASK unless defined $perms;
76 12 100       24 $type = LOCK_EX unless defined $type;
77              
78             # Open the file and apply the lock
79 12   33     36 $fh = popen( $filename, O_RDWR | O_CREAT | O_EXCL, $perms )
80             || popen( $filename, O_RDWR, $perms );
81 12 50       43 $rv = pflock( $filename, $type ) if defined $fh;
82              
83 12         29 subPostamble( PDLEVEL1, '$', $rv );
84              
85 12         40 return $rv;
86             }
87              
88             sub pexclock {
89              
90             # Purpose: Applies an exclusive lock
91             # Returns: True/false
92             # Usage: $rv = pexclock($filename);
93              
94 2     2 1 6 my $filename = shift;
95 2         4 my $mode = shift;
96 2         4 my $rv = 1;
97 2         13 my $fh;
98              
99 2         10 subPreamble( PDLEVEL1, '$;$', $filename, $mode );
100              
101 2         6 $rv = plock( $filename, LOCK_EX, $mode );
102              
103 2         8 subPostamble( PDLEVEL1, '$', $rv );
104              
105 2         10 return $rv;
106             }
107              
108             sub pshlock {
109              
110             # Purpose: Applies a shared lock
111             # Returns: True/false
112             # Usage: $rv = pshlock($filename);
113              
114 2     2 1 5 my $filename = shift;
115 2         5 my $mode = shift;
116 2         4 my $rv = 1;
117 2         13 my $fh;
118              
119 2         10 subPreamble( PDLEVEL1, '$;$', $filename, $mode );
120              
121 2         7 $rv = plock( $filename, LOCK_SH, $mode );
122              
123 2         9 subPostamble( PDLEVEL1, '$', $rv );
124              
125 2         10 return $rv;
126             }
127              
128             sub punlock {
129              
130             # Purpose: Removes any existing locks on the file
131             # Returns: True/false
132             # Usage: $rv = punlock($filename);
133              
134 2     2 1 7 my $filename = shift;
135 2         4 my $mode = shift;
136 2         6 my $rv = 1;
137 2         4 my $fh;
138              
139 2         8 subPreamble( PDLEVEL1, '$;$', $filename, $mode );
140              
141 2         7 $rv = plock( $filename, LOCK_UN, $mode );
142              
143 2         8 subPostamble( PDLEVEL1, '$', $rv );
144              
145 2         9 return $rv;
146             }
147              
148             1;
149              
150             __END__