| 1 | package Acrux::FileLock; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Acrux::FileLock - The Lock File simple interface | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use Acrux::FileLock; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | my $fl = Acrux::FileLock->new( | ||||||||||||||||||||||||||||
| 17 | file => '/tmp/file.lock', | ||||||||||||||||||||||||||||
| 18 | pid => $$, | ||||||||||||||||||||||||||||
| 19 | auto => 0, | ||||||||||||||||||||||||||||
| 20 | flock => 0, | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | if ( $fl->check ) { | ||||||||||||||||||||||||||||
| 24 | warn $fl->error if $fl->error; | ||||||||||||||||||||||||||||
| 25 | die "Already running: $fl->own"; | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | $fl->lock; | ||||||||||||||||||||||||||||
| 29 | die $fl->error if $fl->error; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # . . . do stuff . . . | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | $fl->unlock; | ||||||||||||||||||||||||||||
| 34 | die $fl->error if $fl->error; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ... or with auto-lock and auto-unlock: | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my $fl = Acrux::FileLock->new( | ||||||||||||||||||||||||||||
| 39 | file => '/tmp/file.lock', | ||||||||||||||||||||||||||||
| 40 | pid => $$, | ||||||||||||||||||||||||||||
| 41 | auto => 1, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | die $fl->error if $fl->error; | ||||||||||||||||||||||||||||
| 45 | die "Already running" if $fl->check; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # . . . do stuff . . . | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | The Lock File simple interface | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | This package manages a lock files. It will create a lock file, | ||||||||||||||||||||||||||||
| 54 | query the process within to discover if it's still running, and remove | ||||||||||||||||||||||||||||
| 55 | the lock file. This module based on L<Lock::File>, L<File::TinyLock>, | ||||||||||||||||||||||||||||
| 56 | L<JIP::LockFile>, L<LockFile::Simple> and L<Acrux::FilePid>. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 METHODS | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | This module implements the following methods | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 new | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my $fl = Acrux::FileLock->new( | ||||||||||||||||||||||||||||
| 65 | file => '/tmp/file.lock', | ||||||||||||||||||||||||||||
| 66 | delay => 60, | ||||||||||||||||||||||||||||
| 67 | retries => 5, | ||||||||||||||||||||||||||||
| 68 | pid => $$, | ||||||||||||||||||||||||||||
| 69 | auto => 1, | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | This constructor takes several optional attributes: | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =over 4 | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =item auto | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | auto => 0 | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | If this flag specified as true, then | ||||||||||||||||||||||||||||
| 81 | will be saved the lock file automatically while instance create and | ||||||||||||||||||||||||||||
| 82 | removed the lock file automatically on DESTROY phase. Default: false | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =item debug | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | debug => 0 | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Print debugging messages to STDERR (0=Off (default), 1=On) | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =item delay | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | delay => 60 | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Number of seconds to wait between retries to getting a lockfile | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Default: 60 | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =item file | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | file => '/tmp/test.lock' | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | The name of the lock file to work on. If not specified, a lock | ||||||||||||||||||||||||||||
| 103 | file located in current directory will be created that matches F<./basename($0).lock>. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item flock | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | flock => 1 | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | If this flag is set to true then lockfile will be lock by system with flock | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Default: 0 | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =item pid | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | pid => $$ | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | The pid to write to a new lockfile. If not specified, C<$$> is | ||||||||||||||||||||||||||||
| 118 | used when the lock file doesn't exist. When the lock file does exist, the | ||||||||||||||||||||||||||||
| 119 | pid inside it is used. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item retries | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | retries => 5 | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Number of times to retry getting a lockfile | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Default: 5 | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =back | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 check | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | if ( $fl->check ) { | ||||||||||||||||||||||||||||
| 135 | warn $fl->error if $fl->error; | ||||||||||||||||||||||||||||
| 136 | die "Already running: $fl->own"; | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | This method checks whether the lock is currently considered active. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Returns C<1> if the lock is active or C<0> if the lock is free. | ||||||||||||||||||||||||||||
| 142 | The return value is always boolean and does not contain the PID of the | ||||||||||||||||||||||||||||
| 143 | lock owner. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Before checking the lock state, this method reads the owner information | ||||||||||||||||||||||||||||
| 146 | from the lock file and updates the C<own> and C<uid> attributes when the | ||||||||||||||||||||||||||||
| 147 | file contains valid owner data. These values represent the metadata | ||||||||||||||||||||||||||||
| 148 | stored in the lock file at the time of the call and must not be | ||||||||||||||||||||||||||||
| 149 | considered proof that the corresponding process currently owns the | ||||||||||||||||||||||||||||
| 150 | lock. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | When C<flock> mode is enabled, the lock state is determined exclusively | ||||||||||||||||||||||||||||
| 153 | by the system file lock. The C<own> and C<uid> attributes are still | ||||||||||||||||||||||||||||
| 154 | updated from the lock file, but they do not participate in determining | ||||||||||||||||||||||||||||
| 155 | the lock state. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | In the regular mode, the owner PID and UID are additionally used to | ||||||||||||||||||||||||||||
| 158 | determine whether the lock is still valid. A stale lock file is removed | ||||||||||||||||||||||||||||
| 159 | automatically when possible. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | If a stale lock file cannot be removed by the operating system, the | ||||||||||||||||||||||||||||
| 162 | lock is considered active and C<check> returns C<1>. This condition is | ||||||||||||||||||||||||||||
| 163 | reported through the debug facility and does not set C<error>. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 error | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my $error = $fl->error; | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Returns current error message | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head2 file | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | my $file = $fl->file; | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Accessor for the filename used as the lock file. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head2 lock | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | $self = $self->lock; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | This method creates a lock file and stores the current PID in it. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 own | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | $fl = $fl->own(123); | ||||||||||||||||||||||||||||
| 186 | my $owner_did = $fl->own; | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Accessor and mutator for the PID associated with the lock owner. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | When C<check> reads a lock file containing valid owner information, this | ||||||||||||||||||||||||||||
| 191 | attribute is updated with the PID stored in the file. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | The value represents the owner PID recorded in the lock file at the time | ||||||||||||||||||||||||||||
| 194 | of the last C<check> call. It is metadata and is not, by itself, a | ||||||||||||||||||||||||||||
| 195 | guarantee that the corresponding process currently owns the lock. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | After a successful C<lock>, the attribute contains the PID associated | ||||||||||||||||||||||||||||
| 198 | with the newly acquired lock. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | In C<flock> mode, the operating system does not provide the PID of the | ||||||||||||||||||||||||||||
| 201 | process holding the system lock. Therefore, when inspecting a foreign | ||||||||||||||||||||||||||||
| 202 | lock, C<own> represents only the PID recorded in the lock file. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 pid | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | my $pid = $fl->pid; | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Returns the PID associated with this lock object. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | By default, this is the PID of the current process (C<$$>). The value is | ||||||||||||||||||||||||||||
| 211 | used when creating the lock file and is stored as the owner PID in the | ||||||||||||||||||||||||||||
| 212 | lock file | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | The PID may be specified explicitly when creating the object: | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | my $fl = Acrux::FileLock->new(pid => 123); | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | This can be useful when the object is used to represent a lock owned by | ||||||||||||||||||||||||||||
| 219 | another process | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head2 uid | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | $fl = $fl->uid(1000); | ||||||||||||||||||||||||||||
| 224 | my $owner_uid = $fl->uid; | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | Accessor and mutator for the numeric user ID associated with the lock | ||||||||||||||||||||||||||||
| 227 | owner. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | When C<check> reads a lock file containing valid owner information, this | ||||||||||||||||||||||||||||
| 230 | attribute is updated with the UID stored in the file. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | The value represents the owner UID recorded in the lock file at the time | ||||||||||||||||||||||||||||
| 233 | of the last C<check> call. It is metadata and is not, by itself, a | ||||||||||||||||||||||||||||
| 234 | guarantee that the corresponding process currently owns the lock. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | After a successful C<lock>, the attribute contains the UID associated | ||||||||||||||||||||||||||||
| 237 | with the newly acquired lock. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | In C<flock> mode, the operating system does not provide the UID of the | ||||||||||||||||||||||||||||
| 240 | process holding the system lock. Therefore, when inspecting a foreign | ||||||||||||||||||||||||||||
| 241 | lock, C<uid> represents only the UID recorded in the lock file. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =head2 unlock | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | $self = $self->unlock; | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | This method performs unlocking the lock file and removes it | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 HISTORY | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | See C<Changes> file | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head1 TO DO | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | See C<TODO> file | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | L<Lock::File>, L<File::TinyLock>, L<JIP::LockFile>, L<LockFile::Simple>, | ||||||||||||||||||||||||||||
| 260 | L<Acrux::FilePid> | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | Copyright (C) 1998-2026 D&D Corporation | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | This program is distributed under the terms of the Artistic License Version 2.0 | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =cut | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 2 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 279 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 280 | 2 | ▶ | use File::Basename qw/basename/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 281 | 2 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 282 | 2 | ▶ | use Fcntl qw/O_RDWR O_RDONLY O_WRONLY O_CREAT LOCK_EX LOCK_NB LOCK_UN/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 283 | 2 | ▶ | use Cwd qw/getcwd/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 286 | 2 | RETRIES => 5, | |||||||||||||||||||||||||||
| 287 | DELAY => 60, | ||||||||||||||||||||||||||||
| 288 | 2 | }; | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=20 SCAR=36.1
Pod
documented
| |||||||||||||||||||||||||||||
| 291 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 292 | 0 | ▶ | my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 293 | 6 | my $self = bless {%$args}, $class; | |||||||||||||||||||||||||||
| 294 | 6 | ▶ | $self->{debug} ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"debug"} ||= 0
MC/DC0%0/1
$self->{"debug"} ||= 0
$self->{"debug"}
Truth table50%1/2
A
A= $self->{"debug"}
| |||||||||||||||||||||||||||||
| 295 | 6 | $self->{error} = ""; | |||||||||||||||||||||||||||
| 296 | 6 | ▶ | $self->{file} //= File::Spec->catfile(getcwd, sprintf("%s.lock", basename($0))); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"file"} //= "File::Spec"->catfile(getcwd(), sprintf("%s.lock", basename($0)))
MC/DC0%0/2
$self->{"file"} //= "File::Spec"->catfile(getcwd(), sprintf("%s.lock", basename($0)))
$self->{"file"}
"File::Spec"->catfile(getcwd(), sprintf("%s.lock", basename($0)))
Truth table33%1/3
A //= B
A= $self->{"file"}B= "File::Spec"->catfile(getcwd(), sprintf("%s.lock", basename($0)))
| |||||||||||||||||||||||||||||
| 297 | 6 | ▶ | $self->{pid} //= $$; # Current PID by default | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"pid"} //= $$
MC/DC0%0/2
$self->{"pid"} //= $$
$self->{"pid"}
$$
Truth table33%1/3
A //= B
A= $self->{"pid"}B= $$
| |||||||||||||||||||||||||||||
| 298 | 6 | ▶ | $self->{own} //= 0; # Owner PID | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"own"} //= 0
MC/DC0%0/1
$self->{"own"} //= 0
$self->{"own"}
Truth table50%1/2
A
A= $self->{"own"}
| |||||||||||||||||||||||||||||
| 299 | 6 | ▶ | $self->{uid} //= 0; # Owner UID | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"uid"} //= 0
MC/DC0%0/1
$self->{"uid"} //= 0
$self->{"uid"}
Truth table50%1/2
A
A= $self->{"uid"}
| |||||||||||||||||||||||||||||
| 300 | 6 | ▶ | $self->{auto} //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"auto"} //= 0
MC/DC100%1/1
$self->{"auto"} //= 0
$self->{"auto"}
Truth table100%2/2
A
A= $self->{"auto"}
| |||||||||||||||||||||||||||||
| 301 | 6 | ▶ | $self->{retries} //= RETRIES; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"retries"} //= 5
MC/DC0%0/1
$self->{"retries"} //= 5
$self->{"retries"}
Truth table50%1/2
A
A= $self->{"retries"}
| |||||||||||||||||||||||||||||
| 302 | 6 | ▶ | $self->{delay} //= DELAY; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"delay"} //= 60
MC/DC0%0/1
$self->{"delay"} //= 60
$self->{"delay"}
Truth table50%1/2
A
A= $self->{"delay"}
| |||||||||||||||||||||||||||||
| 303 | 6 | ▶ | $self->{'flock'} ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"flock"} ||= 0
MC/DC0%0/1
$self->{"flock"} ||= 0
$self->{"flock"}
Truth table50%1/2
A
A= $self->{"flock"}
| |||||||||||||||||||||||||||||
| 304 | 6 | $self->{fh} = undef; | |||||||||||||||||||||||||||
| 305 | 6 | $self->{_is_locked} = 0; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | # PID normalize | ||||||||||||||||||||||||||||
| 308 | 6 | ▶ | my $raw_pid = $self->{pid} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'pid'} || 0
MC/DC0%0/1
$self->{'pid'} || 0
$self->{'pid'}
Truth table50%1/2
A
A= $self->{'pid'}
| |||||||||||||||||||||||||||||
| 309 | 6 | ▶ | $self->{pid} = abs(int($raw_pid)) if defined($raw_pid) && $raw_pid =~ /^-?\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $raw_pid and $raw_pid =~ /^-?\d+$/
MC/DC0%0/2
defined $raw_pid and $raw_pid =~ /^-?\d+$/
defined $raw_pid
$raw_pid =~ /^-?\d+$/
Truth table33%1/3
A and B
A= defined $raw_pidB= $raw_pid =~ /^-?\d+$/
| |||||||||||||||||||||||||||||
| 310 | 6 | ▶ | unless (defined($self->{pid}) && $self->{pid} =~ /^[0-9]{1,11}$/) { # Protect | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"pid"} and $self->{"pid"} =~ /^[0-9]{1,11}$/
MC/DC0%0/2
defined $self->{"pid"} and $self->{"pid"} =~ /^[0-9]{1,11}$/
defined $self->{"pid"}
$self->{"pid"} =~ /^[0-9]{1,11}$/
Truth table33%1/3
A and B
A= defined $self->{"pid"}B= $self->{"pid"} =~ /^[0-9]{1,11}$/
| |||||||||||||||||||||||||||||
| 311 | 0 | croak("Incorrect \"pid\" attribute: $raw_pid"); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | # Check etries and delay | ||||||||||||||||||||||||||||
| 315 | 6 | ▶ | croak("Incorrect \"retries\" attribute: " . $self->{retries}) unless $self->{retries} =~ /^[0-9]{1,5}$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 6 | ▶ | croak("Incorrect \"delay\" attribute: " . $self->{delay}) unless $self->{delay} =~ /^[0-9]{1,5}$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | # Lock file | ||||||||||||||||||||||||||||
| 319 | 6 | ▶ | return $self->lock if $self->{auto}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | 2 | return $self; | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 70 | ▶ | sub file { shift->{file} } | ||||||||||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 324 | 25 | ▶ | sub pid { shift->{pid} } | ||||||||||||||||||||||||||
|
Subroutine
pid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 325 | 23 | ▶ | sub own { # Owner PID | ||||||||||||||||||||||||||
|
Subroutine
own: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 326 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 327 | 23 | ▶ | if (scalar(@_) >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 10 | $self->{own} = shift; | |||||||||||||||||||||||||||
| 329 | 10 | return $self; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | 13 | return $self->{own}; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | 12 | ▶ | sub uid { # Owner UID | ||||||||||||||||||||||||||
|
Subroutine
uid: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 334 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 335 | 12 | ▶ | if (scalar(@_) >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 10 | $self->{uid} = shift; | |||||||||||||||||||||||||||
| 337 | 10 | return $self; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | 2 | return $self->{uid}; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | 30 | ▶ | sub error { | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 342 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 343 | 30 | ▶ | if (scalar(@_) >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 26 | $self->{error} = shift; | |||||||||||||||||||||||||||
| 345 | 26 | return $self; | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | 4 | return $self->{error}; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | 8 | ▶ | sub lock { | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=23 SCAR=55.0
Pod
documented
| |||||||||||||||||||||||||||||
| 350 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 351 | 8 | $self->error(undef); | |||||||||||||||||||||||||||
| 352 | 8 | ▶ | if ($self->_is_locked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | 2 | $self->_debug(sprintf("File \"%s\" already locked", $self->file)); | |||||||||||||||||||||||||||
| 354 | 2 | return $self; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | # Signals | ||||||||||||||||||||||||||||
| 358 | 0 | ▶ | $SIG{HUP} = $SIG{QUIT} = $SIG{INT} = $SIG{TERM} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 359 | 0 | $self->_debug( "Caught SIG$_[0]" ); | |||||||||||||||||||||||||||
| 360 | 0 | exit; | |||||||||||||||||||||||||||
| 361 | 6 | }; | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # Using flock | ||||||||||||||||||||||||||||
| 364 | 6 | ▶ | if ($self->_use_flock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | # Initialize or reuse initialized fh | ||||||||||||||||||||||||||||
| 366 | 0 | ▶ | $self->{fh} //= IO::File->new($self->file, O_RDWR | O_CREAT); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"fh"} //= "IO::File"->new($self->file, 66)
MC/DC0%0/2
$self->{"fh"} //= "IO::File"->new($self->file, 66)
$self->{"fh"}
"IO::File"->new($self->file, 66)
Truth table0%0/3
A //= B
A= $self->{"fh"}B= "IO::File"->new($self->file, 66)
| |||||||||||||||||||||||||||||
| 367 | 0 | ▶ | if (my $fh = $self->{fh}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 0 | ▶ | unless (flock $fh, LOCK_EX | LOCK_NB) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 0 | $self->error(sprintf("Can't lock \"%s\": %s", $self->file, $!)); | |||||||||||||||||||||||||||
| 370 | 0 | $self->_debug($self->error); | |||||||||||||||||||||||||||
| 371 | 0 | $self->{fh} = undef; | |||||||||||||||||||||||||||
| 372 | 0 | return $self; | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | # Truncate file | ||||||||||||||||||||||||||||
| 376 | 0 | ▶ | unless (truncate $fh, 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 0 | $self->error(sprintf("Can't truncate \"%s\": %s", $self->file, $!)); | |||||||||||||||||||||||||||
| 378 | 0 | $self->_debug($self->error); | |||||||||||||||||||||||||||
| 379 | 0 | return $self->_rollback; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | 0 | seek $fh, 0, 0; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | # Write data | ||||||||||||||||||||||||||||
| 384 | 0 | ▶ | unless ($fh->printf("%d:%d\n", $self->pid || $$, $>)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->pid || $$
MC/DC0%0/2
$self->pid || $$
$self->pid
$$
Truth table0%0/3
A || B
A= $self->pidB= $$
| |||||||||||||||||||||||||||||
| 385 | 0 | $self->error(sprintf("Can't write message to \"%s\": %s", $self->file, $!)); | |||||||||||||||||||||||||||
| 386 | 0 | $self->_debug($self->error); | |||||||||||||||||||||||||||
| 387 | 0 | return $self->_rollback; | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | # Ok | ||||||||||||||||||||||||||||
| 391 | 0 | $self->{_is_locked} = 1; | |||||||||||||||||||||||||||
| 392 | 0 | ▶ | $self->own($self->pid || $$)->uid($>); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->pid || $$
MC/DC0%0/2
$self->pid || $$
$self->pid
$$
Truth table0%0/3
A || B
A= $self->pidB= $$
| |||||||||||||||||||||||||||||
| 393 | 0 | $self->_debug("Got lock file (flock=true)"); | |||||||||||||||||||||||||||
| 394 | } else { | ||||||||||||||||||||||||||||
| 395 | 0 | $self->error(sprintf("Can't open \"%s\": %s", $self->file, $!)); | |||||||||||||||||||||||||||
| 396 | 0 | $self->_debug($self->error); | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | 0 | return $self; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | # Regular case: save temp file first | ||||||||||||||||||||||||||||
| 402 | 6 | my $tmp_file = sprintf("%s.%d", $self->file, $self->pid); | |||||||||||||||||||||||||||
| 403 | 6 | ▶ | if (my $fh = IO::File->new($tmp_file, O_WRONLY | O_CREAT)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | 6 | ▶ | unless ($fh->printf("%d:%d\n", $self->pid || $$, $>)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->pid || $$
MC/DC0%0/2
$self->pid || $$
$self->pid
$$
Truth table33%1/3
A || B
A= $self->pidB= $$
| |||||||||||||||||||||||||||||
| 405 | 0 | $self->error(sprintf("Can't write message to \"%s\": %s", $tmp_file, $!)); | |||||||||||||||||||||||||||
| 406 | 0 | $self->_debug($self->error); | |||||||||||||||||||||||||||
| 407 | 0 | ▶ | unlink $tmp_file if -f $tmp_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | 0 | return $self; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | 6 | ▶ | unless ($fh->close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | 0 | $self->error(sprintf("Can't close \"%s\": %s", $tmp_file, $!)); | |||||||||||||||||||||||||||
| 412 | 0 | $self->_debug($self->error); | |||||||||||||||||||||||||||
| 413 | 0 | ▶ | unlink $tmp_file if -f $tmp_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 414 | 0 | return $self; | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | # Rename temp file to lock file | ||||||||||||||||||||||||||||
| 418 | 6 | for my $try (0 .. $self->{retries}) { | |||||||||||||||||||||||||||
| 419 | 6 | ▶ | unless ($self->check()) { # not exists, ok | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | 6 | ▶ | if (rename($tmp_file, $self->file)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | 6 | $self->{_is_locked} = 1; | |||||||||||||||||||||||||||
| 422 | 6 | $self->_debug("Got lock file (flock=false)"); | |||||||||||||||||||||||||||
| 423 | 6 | return $self; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | 0 | ▶ | if ($self->{retries} && ($try != $self->{retries})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"retries"} and $try != $self->{"retries"}
MC/DC0%0/2
$self->{"retries"} and $try != $self->{"retries"}
$self->{"retries"}
$try != $self->{"retries"}
Truth table0%0/3
A and B
A= $self->{"retries"}B= $try != $self->{"retries"}
| |||||||||||||||||||||||||||||
| 427 | 0 | $self->_debug(sprintf("Retrying in %d seconds", $self->{delay})); | |||||||||||||||||||||||||||
| 428 | 0 | ▶ | sleep $self->{delay} unless ($try == $self->{retries}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | } else { | ||||||||||||||||||||||||||||
| 432 | 0 | $self->error(sprintf("Can't open \"%s\": %s", $tmp_file, $!)); | |||||||||||||||||||||||||||
| 433 | 0 | $self->_debug($self->error); | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | # Remove temp file in silent mode | ||||||||||||||||||||||||||||
| 437 | 0 | ▶ | unlink $tmp_file if -f $tmp_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # Ok | ||||||||||||||||||||||||||||
| 440 | 0 | return $self; | |||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | 14 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=11 SCAR=40.7
Pod
documented
| |||||||||||||||||||||||||||||
| 443 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 444 | 14 | $self->error(undef); | |||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | # Read owner-data of existed file (see own and uid accessors) | ||||||||||||||||||||||||||||
| 447 | 14 | $self->_read_owner; | |||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 14 | ▶ | return $self->_is_locked | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 450 | if $self->_use_flock; | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | # File not exists. Returns 0 | ||||||||||||||||||||||||||||
| 453 | 14 | ▶ | return 0 unless -f $self->file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | # Check current PID and owner PID | ||||||||||||||||||||||||||||
| 456 | 6 | ▶ | if ($self->own == $self->pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 457 | 6 | $self->_debug(sprintf("An attempt to call the check method twice was detected for PID=%d", $self->own)); | |||||||||||||||||||||||||||
| 458 | 6 | return 1; | |||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | # Check owner PID | ||||||||||||||||||||||||||||
| 462 | 0 | ▶ | if ($self->own && kill(0, $self->own)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->own and kill 0, $self->own
MC/DC0%0/2
$self->own and kill 0, $self->own
$self->own
kill 0, $self->own
Truth table0%0/3
A and B
A= $self->ownB= kill 0, $self->own
| |||||||||||||||||||||||||||||
| 463 | # Oops! Process already exists | ||||||||||||||||||||||||||||
| 464 | 0 | $self->_debug(sprintf("Found valid existing lock file for PID=%d", $self->own)); | |||||||||||||||||||||||||||
| 465 | 0 | return 1; | |||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | # Process for owner PID not exists. Check owner UID | ||||||||||||||||||||||||||||
| 469 | else { | ||||||||||||||||||||||||||||
| 470 | # Owner UID is current user? | ||||||||||||||||||||||||||||
| 471 | 0 | ▶ | if ($self->uid && $self->uid != $>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->uid and $self->uid != $>
MC/DC0%0/2
$self->uid and $self->uid != $>
$self->uid
$self->uid != $>
Truth table0%0/3
A and B
A= $self->uidB= $self->uid != $>
| |||||||||||||||||||||||||||||
| 472 | 0 | $self->_debug("The owner of the lock file owns NOT current user"); | |||||||||||||||||||||||||||
| 473 | # Check process by by /proc/PID (for linux only!) | ||||||||||||||||||||||||||||
| 474 | 0 | ▶ | if (-d File::Spec->catfile("/proc", $self->own)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 475 | 0 | $self->_debug(sprintf("Found valid existing lock file for PID=%d (by /proc/%d)", $self->own, $self->own)); | |||||||||||||||||||||||||||
| 476 | 0 | return 1; | |||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | # Try unlink the not my lock file | ||||||||||||||||||||||||||||
| 481 | 0 | ▶ | unless (unlink $self->file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | 0 | $self->_debug(sprintf("Can't remove stale lock file \"%s\": %s", $self->file, $!)); | |||||||||||||||||||||||||||
| 483 | 0 | return 1; | |||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | # File has removed. Reset owner PID and UID to 0 | ||||||||||||||||||||||||||||
| 487 | 0 | ▶ | unless (-f $self->file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | 0 | $self->own(0)->uid(0); | |||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||
| 490 | 0 | $self->_debug("Found and removed stale lock file"); | |||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 0 | return 0; | |||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | 4 | ▶ | sub unlock { | ||||||||||||||||||||||||||
|
Subroutine
unlock: called CC=6 SCAR=19.1
Pod
documented
| |||||||||||||||||||||||||||||
| 496 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 497 | 4 | $self->error(undef); | |||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | # Remove lock file | ||||||||||||||||||||||||||||
| 500 | 4 | ▶ | if ($self->_is_locked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | # Release file handler first | ||||||||||||||||||||||||||||
| 502 | 3 | ▶ | if ($self->{fh}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 503 | 0 | flock $self->{fh}, LOCK_UN; | |||||||||||||||||||||||||||
| 504 | 0 | $self->{fh} = undef; | |||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | # Unlink file | ||||||||||||||||||||||||||||
| 508 | 3 | ▶ | $self->error(sprintf("Can't remove \"%s\": %s", $self->file, $!))->_debug($self->error) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 509 | unless unlink $self->file; | ||||||||||||||||||||||||||||
| 510 | 3 | ▶ | $self->own(0)->uid(0) unless -f $self->file; # Reset owner PID and UID to 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 511 | } else { | ||||||||||||||||||||||||||||
| 512 | 1 | $self->own(0)->uid(0) # Reset owner PID and UID to 0 | |||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | # Remove temp file in silent mode | ||||||||||||||||||||||||||||
| 516 | 4 | my $tmp_file = sprintf("%s.%d", $self->file, $self->pid); | |||||||||||||||||||||||||||
| 517 | 4 | ▶ | unlink $tmp_file if -f $tmp_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 4 | return $self; | |||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | 14 | ▶ | sub _read_owner { | ||||||||||||||||||||||||||
|
Subroutine
_read_owner: called CC=6 SCAR=19.2
| |||||||||||||||||||||||||||||
| 523 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | # File not exists - skip | ||||||||||||||||||||||||||||
| 526 | 14 | ▶ | return 0 unless -f $self->file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | # Try to load file in ReadOnly mode | ||||||||||||||||||||||||||||
| 529 | 6 | my $fh = IO::File->new($self->file, O_RDONLY); | |||||||||||||||||||||||||||
| 530 | 6 | ▶ | unless ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | 0 | $self->_debug(sprintf("Can't read \"%s\": %s", $self->file, $!)); | |||||||||||||||||||||||||||
| 532 | 0 | return 0; | |||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | # Read first line | ||||||||||||||||||||||||||||
| 536 | 6 | $fh->seek(0, 0); | |||||||||||||||||||||||||||
| 537 | 6 | ▶ | my $line = $fh->getline() // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$fh->getline // ""
MC/DC0%0/1
$fh->getline // ""
$fh->getline
Truth table50%1/2
A
A= $fh->getline
| |||||||||||||||||||||||||||||
| 538 | 6 | chomp $line; | |||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | # Close file | ||||||||||||||||||||||||||||
| 541 | 6 | ▶ | unless ($fh->close) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 542 | 0 | $self->_debug(sprintf("Can't close \"%s\": %s", $self->file, $!)); | |||||||||||||||||||||||||||
| 543 | 0 | return 0; | |||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | # Parse line | ||||||||||||||||||||||||||||
| 547 | 6 | my ($owner_pid, $owner_uid) = (0, 0); | |||||||||||||||||||||||||||
| 548 | 6 | ▶ | if ($line =~ /^(\d+):(\d+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 549 | 6 | ($owner_pid, $owner_uid) = ($1 * 1, $2 * 1); | |||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | # Set owner PID and UID | ||||||||||||||||||||||||||||
| 553 | 6 | $self->own($owner_pid)->uid($owner_uid); | |||||||||||||||||||||||||||
| 554 | 6 | $self->_debug(sprintf("Read owner PID=%d, UID=%d from \"%s\"", $owner_pid, $owner_uid, $self->file)); | |||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | # Ok | ||||||||||||||||||||||||||||
| 557 | 6 | return 1; | |||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | 0 | ▶ | sub _rollback { | ||||||||||||||||||||||||||
|
Subroutine
_rollback: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 560 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 561 | 0 | $self->{_is_locked} = 0; | |||||||||||||||||||||||||||
| 562 | 0 | $self->own(0)->uid(0); | |||||||||||||||||||||||||||
| 563 | 0 | ▶ | return $self unless $self->{fh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 564 | 0 | flock $self->{fh}, LOCK_UN; | |||||||||||||||||||||||||||
| 565 | 0 | $self->{fh}->close; | |||||||||||||||||||||||||||
| 566 | 0 | $self->{fh} = undef; | |||||||||||||||||||||||||||
| 567 | 0 | return $self; | |||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||
| 569 | 12 | ▶ | sub _is_locked { | ||||||||||||||||||||||||||
|
Subroutine
_is_locked: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 570 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 571 | 12 | ▶ | return ($self->{_is_locked} && -f $self->file) ? 1 : 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"_is_locked"} and -f $self->file
MC/DC100%2/2
$self->{"_is_locked"} and -f $self->file
$self->{"_is_locked"}
-f $self->file
Truth table100%3/3
A and B
A= $self->{"_is_locked"}B= -f $self->file
| |||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||
| 573 | 20 | ▶ | sub _use_flock {shift->{'flock'} ? 1 : 0} | ||||||||||||||||||||||||||
|
Subroutine
_use_flock: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | 22 | ▶ | sub _debug { | ||||||||||||||||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 575 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 576 | 22 | ▶ | warn sprintf("%s: %s\n", ref($self), join("\n", @_)) if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 577 | 22 | return $self; | |||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | 4 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 581 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 582 | 4 | ▶ | return unless $self->{auto}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 583 | 2 | $self->_debug("Cleaning up..."); | |||||||||||||||||||||||||||
| 584 | 2 | $self->unlock(); | |||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | 2 | 1; | |||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | __END__ | ||||||||||||||||||||||||||||