| 1 | # Copyright (C) 1996, David Muir Sharnoff | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package File::BasicFlock; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | require Exporter; | |||||||||||||||||||||||||||
| 6 | 9 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 7 | 9 | @EXPORT = qw(lock unlock); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # It would be nice if I could use fcntl.ph and | ||||||||||||||||||||||||||||
| 13 | # errno.ph, but alas, that isn't safe. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use POSIX qw(EAGAIN ENOENT EEXIST O_RDWR); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use Fcntl qw(LOCK_SH LOCK_EX LOCK_NB LOCK_UN); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use vars qw($VERSION %locks %lockHandle %shared $debug); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 9 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 9 | $VERSION = 98.120200; | |||||||||||||||||||||||||||
| 22 | 9 | $debug = 0; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 9 | my $gensym = "sym0000"; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub lock | ||||||||||||||||||||||||||||
| 31 | 743 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=11 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 743 | my ($file, $shared, $nonblocking) = @_; | |||||||||||||||||||||||||||
| 33 | #my $f = new FileHandle; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 743 | $gensym++; | |||||||||||||||||||||||||||
| 36 | 743 | my $f = "File::BasicFlock::$gensym"; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 743 | my $previous = exists $locks{$file}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 743 | ▶ | unless (sysopen($f, $file, O_RDWR)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | croak "open $file: $!"; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 743 | ▶ | $locks{$file} = $locks{$file} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$File::BasicFlock::locks{$file} || 0
MC/DC0%0/1
$File::BasicFlock::locks{$file} || 0
$File::BasicFlock::locks{$file}
Truth table50%1/2
A
A= $File::BasicFlock::locks{$file}
| |||||||||||||||||||||||||||||
| 44 | 743 | $shared{$file} = $shared; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 743 | $lockHandle{$file} = $f; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 743 | my $flags; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 743 | ▶ | $flags = $shared ? LOCK_SH : LOCK_EX; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 743 | ▶ | $flags |= LOCK_NB | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | if $nonblocking; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 743 | my $r = flock($f, $flags); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 743 | ▶ | print " ($$ " if $debug and $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$File::BasicFlock::debug and $r
MC/DC0%0/2
$File::BasicFlock::debug and $r
$File::BasicFlock::debug
$r
Truth table33%1/3
A and B
A= $File::BasicFlock::debugB= $r
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 743 | ▶ | return 1 if $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 678 | ▶ | if ($nonblocking and $! == EAGAIN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$nonblocking and $! == 11
MC/DC0%0/2
$nonblocking and $! == 11
$nonblocking
$! == 11
Truth table33%1/3
A and B
A= $nonblockingB= $! == 11
| |||||||||||||||||||||||||||||
| 60 | 678 | ▶ | if (! $previous) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 678 | delete $locks{$file}; | |||||||||||||||||||||||||||
| 62 | 678 | my $f = $lockHandle{$file}; | |||||||||||||||||||||||||||
| 63 | 678 | close($f); | |||||||||||||||||||||||||||
| 64 | 678 | delete $lockHandle{$file}; | |||||||||||||||||||||||||||
| 65 | 678 | delete $shared{$file}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 678 | return 0; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | croak "flock $f $flags: $!"; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub unlock | ||||||||||||||||||||||||||||
| 73 | 65 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unlock: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 65 | my ($file) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 65 | ▶ | croak "no lock on $file" unless exists $locks{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 65 | delete $locks{$file}; | |||||||||||||||||||||||||||
| 79 | 65 | my $f = $lockHandle{$file}; | |||||||||||||||||||||||||||
| 80 | 65 | delete $lockHandle{$file}; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 65 | ▶ | return 0 unless defined $f; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 65 | ▶ | print " $$) " if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 65 | ▶ | flock($f, LOCK_UN) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | or croak "flock $f UN: $!"; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 65 | close($f); | |||||||||||||||||||||||||||
| 89 | 65 | return 1; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 9 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 93 | 9 | my $f; | |||||||||||||||||||||||||||
| 94 | 9 | for $f (keys %locks) { | |||||||||||||||||||||||||||
| 95 | 0 | &unlock($f); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | __DATA__ | ||||||||||||||||||||||||||||