Reputation: 1027
How exactly would I check to see if a file is locked exclusively? I have this function but it is returning 1 no matter what I do:
sub is_file_locked
{
my $theFile;
my $theRC;
($theFile) = @_;
$theRC = open(HANDLE, $theFile);
$theRC = flock(HANDLE, LOCK_EX|LOCK_NB);
close(HANDLE);
return !$theRC;
}
Upvotes: 8
Views: 8921
Reputation: 1027
The final solution:
flock($fh, LOCK_EX) or die "Cannot lock file - $!\n";
if ( is_file_locked($gTestQueuePath) ){ print "locked";} else { print "not locked";}
#1 = locked 0 = not locked
sub is_file_locked
{
my $theFile;
my $theRC;
($theFile) = @_;
$theRC = open(my $HANDLE, ">>", $theFile);
$theRC = flock($HANDLE, LOCK_EX|LOCK_NB);
close($HANDLE);
return !$theRC;
}
close $fh or die "Cannot close";
Upvotes: -2
Reputation: 118128
First off, you should check if open
succeeded.
Also, you should check if you can get a shared lock. flock
with LOCK_EX
would (I think) fail, if there is a shared lock on the file.
However, the file can become locked between the check and the return, creating a race condition, so such a function is of dubious value.
#!/usr/bin/perl
use strict; use warnings;
use Fcntl qw( :flock );
print is_locked_ex($0)
? "$0 : locked exclusively\n"
: "$0 : not locked exclusively\n";
my $test_file = 'test.txt';
open my $fh, '>', $test_file
or die "Cannot open '$test_file' for writing: $!";
if ( flock $fh, LOCK_EX|LOCK_NB ) {
print is_locked_ex($test_file)
? "$test_file : locked exclusively\n"
: "$test_file : not locked exclusively\n";
}
close $fh or die "Cannot close '$test_file': $!";
sub is_locked_ex {
my ($path) = @_;
die "Not a plain file: '$path'" unless -f $path;
return 1 unless open my $fh, '<', $path;
my $ret = not flock $fh, LOCK_SH | LOCK_NB;
close $fh
or die "Cannot close '$path': $!";
return $ret;
}
Upvotes: 7
Reputation: 118605
You have opened $theFile
in read mode and LOCK_EX
isn't meant to be used that way.
Note that the fcntl(2) emulation of flock(3) requires that FILEHANDLE be open with read intent to use LOCK_SH and requires that it be open with write intent to use LOCK_EX.
Upvotes: 10