简体   繁体   中英

In Perl, how can I check if a file is locked?

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;
}

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.

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;
}

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM