[LA.pm] locking a directory

Ofer Nave ofer at netapt.com
Wed Feb 23 00:54:37 PST 2005


Since I asked the question, thought I'd share an easy answer.  The same 
(or similiar) solution was already suggested by Kevin Scaldeferri and Tim 
Conrow, but I just realized that an implementation already exists in the 
Perl Cookbook.  In the version I have (first edition, 1998 printing), it's 
recipe 7.21 Program: netlock.

The recipe implements a module called File::LockDir to demonstrate the 
concept.  Now why hasn't anyone actually created it and put it on CPAN? :)

-ofer

On Thu, 17 Feb 2005, Ofer Nave wrote:

> 
> Does anyone know how to lock a directory?  I tried the standard flock 
> call, and it didn't like it:
> 
> my $dir = "/netapp/ofer/somedir";
> print "opening $dir\n";
> opendir( SOMEDIR, $dir ) or die( $! );
> print "locking $dir\n";
> flock( SOMEDIR, LOCK_EX ) or die( $! );
> print "locked! sleeping\n";
> sleep( 999 );
> print "unlocking $dir\n";
> flock( SOMEDIR, LOCK_UN ) or die( $! );
> print "closing $dir\n";
> closedir( SOMEDIR ) or die( $! );
> print "done\n";
> 
> [onave at trioval01 ~] ./foo.pl 
> opening /netapp/ofer/somedir
> locking /netapp/ofer/somedir
> flock() on unopened filehandle SOMEDIR at ./foo.pl line 19.
>         (Are you trying to call flock() on dirhandle SOMEDIR?)
> Bad file descriptor at ./foo.pl line 19.
> 
> I've done some searching on CPAN and google with no luck so far.
> 
> -ofer
> 
> _______________________________________________
> Losangeles-pm mailing list
> Losangeles-pm at pm.org
> http://mail.pm.org/mailman/listinfo/losangeles-pm
> 




More information about the Losangeles-pm mailing list