File Coverage

blib/lib/Tie/MLDBM/Lock/File.pm
Criterion Covered Total %
statement 12 33 36.3
branch 0 18 0.0
condition n/a
subroutine 4 7 57.1
pod 0 3 0.0
total 16 61 26.2


line stmt bran cond sub pod time code
1             package Tie::MLDBM::Lock::File;
2              
3 1     1   36412 use Fcntl qw/ :flock /;
  1         2  
  1         173  
4 1     1   814 use IO::File;
  1         10365  
  1         120  
5              
6 1     1   8 use strict;
  1         2  
  1         26  
7 1     1   3 use vars qw/ $VERSION /;
  1         2  
  1         339  
8              
9             $VERSION = '1.04';
10              
11              
12             sub lock_exclusive {
13 0     0 0   my ( $self ) = @_;
14              
15             # This module stores the file handle of the lock file in the self object
16             # under the name 'Lock' - If this is the first lock action which is called of
17             # this module, this file handle will not have been created and as such will
18             # need to be created and stored before any further action can be taken.
19              
20 0 0         unless ( exists $self->{'Lock'} ) {
21              
22             # The filename of the lock file can be specified by the 'Lockfile'
23             # argument which can be passed to the Tie::MLDBM object constructor -
24             # Alternatively, the name 'Tie-MLDBM-Lock-File.lock' is used.
25              
26 0 0         my $file = $self->{'Config'}->{'Lockfile'} or 'Tie-MLDBM-Lock-File.lock';
27              
28             # Open lock file and store file handle in the self object
29              
30 0 0         my $fh = IO::File->new( '+>' . $file ) or
31             croak( __PACKAGE__, '->lock_exclusive : Cannot open temporary lock file - ', $! );
32 0           $self->{'Lock'} = $fh;
33              
34             }
35            
36 0 0         flock( $self->{'Lock'}, LOCK_EX ) or
37             croak( __PACKAGE__, '->lock_exclusive : Cannot acquire exclusive lock on file handle - ', $! );
38              
39 0           return 1;
40             }
41              
42              
43             sub lock_shared {
44 0     0 0   my ( $self ) = @_;
45              
46             # This module stores the file handle of the lock file in the self object
47             # under the name 'Lock' - If this is the first lock action which is called of
48             # this module, this file handle will not have been created and as such will
49             # need to be created and stored before any further action can be taken.
50              
51 0 0         unless ( exists $self->{'Lock'} ) {
52              
53             # The filename of the lock file can be specified by the 'Lockfile'
54             # argument which can be passed to the Tie::MLDBM object constructor -
55             # Alternatively, the name 'Tie-MLDBM-Lock-File.lock' is used.
56              
57 0 0         my $file = $self->{'Config'}->{'Lockfile'} or 'Tie-MLDBM-Lock-File.lock';
58              
59             # Open lock file and store file handle in the self object
60              
61 0 0         my $fh = IO::File->new( '+>' . $file ) or
62             croak( __PACKAGE__, '->lock_shared : Cannot open temporary lock file - ', $! );
63 0           $self->{'Lock'} = $fh;
64              
65             }
66            
67 0 0         flock( $self->{'Lock'}, LOCK_SH ) or
68             croak( __PACKAGE__, '->lock_shared : Cannot acquire shared lock on file handle - ', $! );
69              
70 0           return 1;
71             }
72              
73              
74             sub unlock {
75 0     0 0   my ( $self ) = @_;
76              
77             # This module stores the file handle of the lock file in the self object
78             # under the name 'Lock' - If this object element does not exist then
79             # presumably no lock file has been created and no action should be taken.
80              
81 0 0         if ( exists $self->{'Lock'} ) {
82              
83 0           flock( $self->{'Lock'}, LOCK_UN );
84 0           $self->{'Lock'}->close;
85              
86 0           delete $self->{'Lock'};
87              
88             }
89 0           $self->{'Lock'} = undef;
90              
91 0           return 1;
92             }
93              
94              
95             1;
96              
97              
98             __END__