| 1 | package Ubic::Lockf; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 171 | $Ubic::Lockf::VERSION = '1.59'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 171 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 171 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: file locker with an automatic out-of-scope unlocking mechanism | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 171 | ▶ | use Fcntl qw(:flock); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 171 | ▶ | use Params::Validate; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 171 | ▶ | use POSIX qw(:errno_h); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 171 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 171 | ▶ | use Ubic::Lockf::Alarm; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 171 | ▶ | use parent qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 171 | our @EXPORT = qw(lockf); | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | sub DESTROY ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 313 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 313 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 313 | my $fh = $self->{_fh}; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 313 | ▶ | return unless defined $fh; # already released or dissolved | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 311 | flock $fh, LOCK_UN; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 311 | delete $self->{_fh}; # closes the file if opened by us | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 171 | my %defaults = ( | |||||||||||||||||||||||||||||||||||||||||
| 32 | shared => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | blocking => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | timeout => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | mode => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | sub lockf ($;$) { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 717 | ▶ | my ($param, $opts) = validate_pos(@_, 1, 0); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lockf: called CC=7 SCAR=20.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 717 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 717 | $opts = validate(@{ [ $opts ] }, { | |||||||||||||||||||||||||||||||||||||||||
| 42 | blocking => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | shared => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | silent => 0, # deprecated option, does nothing | ||||||||||||||||||||||||||||||||||||||||||
| 45 | timeout => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | mode => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 717 | $opts = {%defaults, %$opts}; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 717 | my ($fh, $fname); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 717 | ▶ | if (ref $param eq "") { # filename instead of filehandle | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 717 | ▶ | open $fh, ">>", $param or die "Can't open $param: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 717 | $fname = $param; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $fh = $param; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 717 | ▶ | unless (_lockf($fh, $opts, $fname)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 364 | return; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # don't check chmod success - it can fail and it's ok | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 353 | ▶ | chmod ($opts->{mode}, ($fname || $fh)) if defined $opts->{mode}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$fname || $fh
MC/DC0%0/2
$fname || $fh
$fname
$fh
Truth table0%0/3
A || B
A= $fnameB= $fh
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 353 | return bless { | |||||||||||||||||||||||||||||||||||||||||
| 66 | _fh => $fh, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | _fname => $fname, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | sub _lockf ($$;$) { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 717 | ▶ | my ($fh, $opts, $fname) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lockf: called CC=14 SCAR=30.8
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 717 | ▶ | $fname ||= ''; # TODO - discover $fname from $fh, it's possible in most cases with some /proc magic | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$fname ||= ""
MC/DC0%0/1
$fname ||= ""
$fname
Truth table50%1/2
A
A= $fname
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 717 | ▶ | my $mode = ($opts->{shared} ? LOCK_SH : LOCK_EX); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 717 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $opts->{"blocking"} or defined $opts->{"timeout"} and not $opts->{"timeout"}
defined $opts->{"timeout"} and not $opts->{"timeout"}
MC/DC100%3/3
not $opts->{"blocking"} or defined $opts->{"timeout"} and not $opts->{"timeout"}
not $opts->{"blocking"}
defined $opts->{"timeout"}
not $opts->{"timeout"}
Truth table100%4/4
not(A) or B and not(C)
A= not $opts->{"blocking"}B= defined $opts->{"timeout"}C= not $opts->{"timeout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | not $opts->{blocking} | ||||||||||||||||||||||||||||||||||||||||||
| 79 | or (defined $opts->{timeout} and not $opts->{timeout}) # timeout=0 | ||||||||||||||||||||||||||||||||||||||||||
| 80 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 447 | ▶ | return 1 if flock ($fh, $mode | LOCK_NB); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 364 | ▶ | return 0 if ($! == EWOULDBLOCK); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | croak "flock ".($fname || '')." failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$fname || ''
MC/DC0%0/1
$fname || ''
$fname
Truth table0%0/2
A
A= $fname
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 270 | ▶ | unless (flock ($fh, $mode | LOCK_NB)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | my $msg = "$fname already locked, wait..."; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | if (-t STDOUT) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | print $msg; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 269 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if ($opts->{timeout}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | local $SIG{ALRM} = sub { croak "flock $fname failed: timed out" }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $alarm = Ubic::Lockf::Alarm->new($opts->{timeout}); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | flock $fh, $mode or die "flock failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | flock $fh, $mode or die "flock failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | sub name($) | ||||||||||||||||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return $self->{_fname}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | sub dissolve { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 29 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dissolve: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 29 | undef $self->{_fh}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 171 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||||||||||||||||