Paranoid/IO/Lockfile.pm

filter: Statementstmt 100% 69 / 69 Branchbran 83.3% 5 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 94.9% 93 / 98 CC 5 SCAR 7.6
CC
5
Cov
96%
plock
5.1
BEGIN
1.0
BEGIN
1.0
CRAP 2.1
« Paranoid/Log/File.pm ↑ Summary Paranoid.pm »
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 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
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 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use Fcntl qw(:flock O_RDWR O_CREAT O_EXCL);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use Paranoid::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 4 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
46  
47 4 @EXPORT = qw(plock pexclock pshlock punlock);
48 4 @EXPORT_OK = @EXPORT;
49 4 %EXPORT_TAGS = ( all => [@EXPORT_OK], );
50  
51 4 ▶use constant PRIV_UMASK => 0660;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 #####################################################################
54 #
55 # Module code follows
56 #
57 #####################################################################
58  
59 12 ▶sub plock {
Subroutine plock: called CC=5 SCAR=16.3
Pod documented
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 my $filename = shift;
68 12 my $type = shift;
69 12 my $perms = shift;
70 12 my ( $rv, $fh );
71  
72 12 subPreamble( PDLEVEL1, '$;$$', $filename, $type, $perms );
73  
74 # Set the defaults
75 12 ▶ $perms = PRIV_UMASK unless defined $perms;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perms 10 2 12
76 12 ▶ $type = LOCK_EX unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 2 10 12
77  
78 # Open the file and apply the lock
79 12 ▶ $fh = popen( $filename, O_RDWR | O_CREAT | O_EXCL, $perms )
Condition33%1/3
popen($filename, 194, $perms) || popen($filename, 2, $perms)
l!l&&r!l&&!r
1200
MC/DC0%0/2
popen($filename, 194, $perms) || popen($filename, 2, $perms)
popen($filename, 194, $perms) popen($filename, 2, $perms)
Truth table33%1/3
A || B
A= popen($filename, 194, $perms)B= popen($filename, 2, $perms)
ABresult
1X1
011
000
80 || popen( $filename, O_RDWR, $perms );
81 12 ▶ $rv = pflock( $filename, $type ) if defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh 12 0 12
82  
83 12 subPostamble( PDLEVEL1, '$', $rv );
84  
85 12 return $rv;
86 }
87  
88 2 ▶sub pexclock {
Subroutine pexclock: called CC=1 SCAR=0.0
Pod documented
89  
90 # Purpose: Applies an exclusive lock
91 # Returns: True/false
92 # Usage: $rv = pexclock($filename);
93  
94 2 my $filename = shift;
95 2 my $mode = shift;
96 2 my $rv = 1;
97 2 my $fh;
98  
99 2 subPreamble( PDLEVEL1, '$;$', $filename, $mode );
100  
101 2 $rv = plock( $filename, LOCK_EX, $mode );
102  
103 2 subPostamble( PDLEVEL1, '$', $rv );
104  
105 2 return $rv;
106 }
107  
108 2 ▶sub pshlock {
Subroutine pshlock: called CC=1 SCAR=0.0
Pod documented
109  
110 # Purpose: Applies a shared lock
111 # Returns: True/false
112 # Usage: $rv = pshlock($filename);
113  
114 2 my $filename = shift;
115 2 my $mode = shift;
116 2 my $rv = 1;
117 2 my $fh;
118  
119 2 subPreamble( PDLEVEL1, '$;$', $filename, $mode );
120  
121 2 $rv = plock( $filename, LOCK_SH, $mode );
122  
123 2 subPostamble( PDLEVEL1, '$', $rv );
124  
125 2 return $rv;
126 }
127  
128 2 ▶sub punlock {
Subroutine punlock: called CC=1 SCAR=0.0
Pod documented
129  
130 # Purpose: Removes any existing locks on the file
131 # Returns: True/false
132 # Usage: $rv = punlock($filename);
133  
134 2 my $filename = shift;
135 2 my $mode = shift;
136 2 my $rv = 1;
137 2 my $fh;
138  
139 2 subPreamble( PDLEVEL1, '$;$', $filename, $mode );
140  
141 2 $rv = plock( $filename, LOCK_UN, $mode );
142  
143 2 subPostamble( PDLEVEL1, '$', $rv );
144  
145 2 return $rv;
146 }
147  
148 4 1;
149  
150 __END__
« Paranoid/Log/File.pm ↑ Summary Paranoid.pm »