View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Paul Seamons > File-NFSLock-1.10 > File::NFSLock



Annotate this POD

Related Modules



New  1
Open  3
View/Report Bugs
Module Version: 1.10   Source   Latest Release: File-NFSLock-1.27


File::NFSLock - perl module to do NFS (or not) locking


  use File::NFSLock (uncache);

  my $file = "somefile";

  ### set up a lock - lasts until object looses scope
  if( defined(my $lock = File::NFSLock->new({
    file      => $file,
    lock_type => "NONBLOCKING"
    blocking_timeout   => 10,      # 10 sec
    stale_lock_timeout => 30 * 60, # 60 min
    })) ){
    ### OR
    ### my $lock = File::NFSLock->new($file,"NONBLOCKING",10,30*60)

    ### do write protected stuff on $file
    ### at this point $file is uncached from NFS (most recent)
    open(FILE, "+<$file") || die $!;

    ### or open it any way you like
    ### my $fh = IO::File->open( $file, 'w' ) || die $!

    ### update (uncache across NFS) other files
    # open(FILE2,"someotherfile1");

    ### unlock it
    ### OR
    ### undef $lock;
    ### OR let $lock go out of scope
    die "I couldn't lock the file [$File::NFSLock::errstr]";


Program based of concept of hard linking of files being atomic across NFS. This concept was mentioned in Mail::Box::Locker (which was originally presented in Mail::Folder::Maildir). Some routine flow is taken from there -- particularly the idea of creating a random local file, hard linking a common file to the local file, and then checking the nlink status. Some ideologies were not complete (uncache mechanism, shared locking) and some coding was even incorrect (wrong stat index). File::NFSLock was written to be light, generic, and fast.


Locking occurs by creating a File::NFSLock object. If the object is created successfully, a lock is currently in place and remains in place until the lock object goes out of scope (or calls the unlock method).

A lock object is created by calling the new method and passing two to four parameters in the following manner:

  my $lock = File::NFSLock->new($file,

Additionally, parameters may be passed as a hashref:

  my $lock = File::NFSLock->new({
    file               => $file,
    lock_type          => $lock_type,
    blocking_timeout   => $blocking_timeout,
    stale_lock_timeout => $stale_lock_timeout,


Parameter 1: file

Filename of the file upon which it is anticipated that a write will happen to. Locking will provide the most recent version (uncached) of this file upon a successful file lock. It is not necessary for this file to exist.

Parameter 2: lock_type

Lock type must be one of the following:

  Fcntl::LOCK_SH() (SHARED)

Lock type determines whether the lock will be blocking, non blocking, or shared. Blocking locks will wait until other locks are removed before the process continues. Non blocking locks will return undef if another process currently has the lock. Shared will allow other process to do a shared lock at the same time (shared is not yet implemented).

Parameter 3: blocking_timeout (optional)

Timeout is used in conjunction with a blocking timeout. If specified, File::NFSLock will block up to the number of seconds specified in timeout before returning undef (could not get a lock).

Parameter 4: stale_lock_timeout (optional)

Timeout is used to see if an existing lock file is older than the stale lock timeout. If do_lock fails to get a lock, the modified time is checked and do_lock is attempted again. If the stale_lock_timeout is set to low, a recursion load could exist so do_lock will only recurse 10 times (this is only a problem if the stale_lock_timeout is set too low -- on the order of one or two seconds).


On failure, a global variable, $File::NFSLock::errstr, should be set and should contain the cause for the failure to get a lock. Useful primarily for debugging.


By default File::NFSLock will use a lock file extenstion of ".NFSLock". This is now in a global variable $File::NFSLock::LOCK_EXTENSION that may be changed to suit other purposes (such as compatibility in mail systems).


Features yet to be implemented...

SHARED locks

Need to allow for shared locking. This will allow for safe reading on files. Underway.


Improve the test suite.


Paul T Seamons ( - Performed majority of the programming with copious amounts of input from Rob Brown.

Rob B Brown ( - In addition to helping in the programming, Rob Brown provided most of the core testing to make sure implementation worked properly.

Also Mark Overmeer ( - Author of Mail::Box::Locker, from which some key concepts for File::NFSLock were taken.

Also Kevin Johnson ( - Author of Mail::Folder::Maildir, from which Mark Overmeer based Mail::Box::Locker.


  Copyright (C) 2001, Paul T Seamons

                      Rob B Brown
  This package may be distributed under the terms of either the
  GNU General Public License 
    or the
  Perl Artistic License

  All rights reserved.
syntax highlighting: