File Coverage

blib/lib/File/FcntlLock.pm
Criterion Covered Total %
statement 30 32 93.7
branch 4 10 40.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 1 1 100.0
total 45 55 81.8


line stmt bran cond sub pod time code
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
7              
8              
9             package File::FcntlLock;
10              
11 4     4   72 use 5.006;
  4         12  
  4         168  
12 4     4   24 use strict;
  4         4  
  4         156  
13 4     4   20 use warnings;
  4         8  
  4         84  
14 4     4   60 use POSIX;
  4         8  
  4         28  
15 4     4   9692 use Errno;
  4         8  
  4         176  
16 4     4   92 use Carp;
  4         8  
  4         260  
17 4     4   20 use base qw( File::FcntlLock::Core DynaLoader );
  4         8  
  4         1628  
18              
19              
20             our $VERSION = '0.22';
21              
22              
23             bootstrap File::FcntlLock $VERSION;
24              
25             our @EXPORT = @File::FcntlLock::Core::EXPORT;
26              
27              
28             ###########################################################
29             #
30             # Make our exports exportable by child classes
31              
32             sub import
33             {
34 4     4   600 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             sub lock {
44 39     39 1 6038327 my ( $self, $fh, $action ) = @_;
45 39         90 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 50 33     335 my $fd = ( ref( $fh ) or $fh =~ /^\*/ ) ? fileno( $fh ) : $fh;
53 39 50       124 $fd = -1 unless defined $fd;
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 50       89 $action = -1 unless defined $action;
59              
60 39 50       735 if ( $ret = C_fcntl_lock( $fd, $action, $self, $err ) ) {
    0          
61 39         190 $self->{ errno } = $self->{ error } = undef;
62             } elsif ( $err ) {
63 0         0 die "Internal error in File::FcntlLock module detected";
64             } else {
65 0         0 $self->get_error( $self->{ errno } = $! + 0 );
66             }
67              
68 39         198 return $ret;
69             }
70              
71              
72             1;
73              
74              
75             # Local variables:
76             # tab-width: 4
77             # indent-tabs-mode: nil
78             # End: