| 1 | # -*- cperl -*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 4 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Copyright (C) 2002-2014 Jens Thoms Toerring <jt@toerring.de> | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | package File::FcntlLock; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use POSIX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Errno; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use base qw( File::FcntlLock::Core DynaLoader ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | bootstrap File::FcntlLock $VERSION; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | our @EXPORT = @File::FcntlLock::Core::EXPORT; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | ########################################################### | ||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||
| 30 | # Make our exports exportable by child classes | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub import | ||||||||||||||||||||||||||||
| 33 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 4 | File::FcntlLock->export_to_level( 1, @_ ); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ########################################################### | ||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||
| 40 | # Function for locking or unlocking a file or determining which | ||||||||||||||||||||||||||||
| 41 | # process holds a lock. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 39 | ▶ | sub lock { | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=7 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 39 | my ( $self, $fh, $action ) = @_; | |||||||||||||||||||||||||||
| 45 | 39 | my ( $ret, $err ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Figure out the file descriptor - we might get a file handle, a | ||||||||||||||||||||||||||||
| 48 | # typeglob or already a file descriptor) and set it to a value which | ||||||||||||||||||||||||||||
| 49 | # will make fcntl(2) fail with EBADF if the argument is undefined or | ||||||||||||||||||||||||||||
| 50 | # is a file handle that's invalid. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 39 | ▶ | my $fd = ( ref( $fh ) or $fh =~ /^\*/ ) ? fileno( $fh ) : $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $fh or $fh =~ /^\*/
MC/DC0%0/2
ref $fh or $fh =~ /^\*/
ref $fh
$fh =~ /^\*/
Truth table33%1/3
A or B
A= ref $fhB= $fh =~ /^\*/
| |||||||||||||||||||||||||||||
| 53 | 39 | ▶ | $fd = -1 unless defined $fd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Set the action argument to something invalid if it's not defined | ||||||||||||||||||||||||||||
| 56 | # which then fcntl(2) fails and errno gets set accordingly | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 39 | ▶ | $action = -1 unless defined $action; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 39 | ▶ | if ( $ret = C_fcntl_lock( $fd, $action, $self, $err ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 39 | $self->{ errno } = $self->{ error } = undef; | |||||||||||||||||||||||||||
| 62 | } elsif ( $err ) { | ||||||||||||||||||||||||||||
| 63 | 0 | die "Internal error in File::FcntlLock module detected"; | |||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 0 | $self->get_error( $self->{ errno } = $! + 0 ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 39 | return $ret; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | 1; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Local variables: | ||||||||||||||||||||||||||||
| 76 | # tab-width: 4 | ||||||||||||||||||||||||||||
| 77 | # indent-tabs-mode: nil | ||||||||||||||||||||||||||||
| 78 | # End: | ||||||||||||||||||||||||||||