[LA.pm] locking a directory

Kevin Scaldeferri kevin+lapm at scaldeferri.com
Thu Feb 17 15:01:00 PST 2005


you could flock on a normal file that is uniquely associated to each 
directory (like "${dir}.lock".  Since flock is advisory only, so you 
can't really depend on it outside your application anywhay, using an 
arbitrary convention like this should suffice.


-k


On Feb 17, 2005, at 2:03 PM, 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