[LA.pm] locking a directory

Cynthia Kiser cnk at caltech.edu
Thu Feb 17 17:32:00 PST 2005


>From Tim, who comes to meetings but is not on this list:

>From: Ofer Nave <ofer at netapt.com>
>
>Does anyone know how to lock a directory?  I tried the standard flock 
>call, and it didn't like it:

On Unixen, you can actually lock a directory, since UNIX semantics say a 
directory may be treated like any other file. Run these two command 
lines sequentially:

perl -wle 'open(my $d,">>+.") or die "$!"; flock($d,2) or die "flock: 
$!"; sleep 20' &

perl -wle 'open(my $d,">>+.") or die "$!"; print "here we go ..."; 
flock($d,2) or die "flock: $!"; print "unlocked"

The first will apply an exclusive lock, the second will block until the 
lock is released. Worked for me on Linux.

That said, for portability purposes, and also to avoid freaking people 
out with calls to 'open' on a directory, you should probably just use a 
lock file within the directory.

-- Tim Conrow         tim at ipac.caltech.edu                           |

-- 
Cynthia Kiser
cnk at caltech.edu



More information about the Losangeles-pm mailing list