locking directories

O

ofer

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@thebox ~] ./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
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
Does anyone know how to lock a directory?

What makes you think you can or should be able to lock a directory?
I tried the standard flock call, and it didn't like it:

Is there a reason to expect otherwise?

C:\> perldoc -f flock

flock FILEHANDLE,OPERATION
Calls flock(2), or an emulation of it, on FILEHANDLE.

....
I've done some searching on CPAN and google with no luck so far.

If you actually explained what you are trying to do, we might be able to
help. But, please, before proceeding further, read the posting guidelines
for this group to learn how you can help others help you.

Sinan.
 
O

ofer

Does anyone know how to lock a directory?
What makes you think you can or should be able to lock a directory?

Because in UNIX-like operating systems, directories are files, and you
can lock files with the built-in flock() function.
Is there a reason to expect otherwise?
C:\> perldoc -f flock
flock FILEHANDLE,OPERATION
Calls flock(2), or an emulation of it, on FILEHANDLE.

Well, I didn't expect it to work, but I also felt it would be
inappropriate to come here for help until after I had exhausted the
not-so-obvious-but-who-knows options as well as the obvious ones. So I
just wanted to mention that I had tried it, to prevent anyone from
suggesting it (in the interest of saving everyone time).
If you actually explained what you are trying to do, we might be able to
help. But, please, before proceeding further, read the posting guidelines
for this group to learn how you can help others help you.

Sure, I'd be happy to.

I'm trying to lock a directory. Probably for the same reason you lock
files: so you know that someone else is still using it, and can
politely leave it alone for the time being.

-ofer
 
B

Brian McCauley

Because in UNIX-like operating systems, directories are files,

Well at least on some filesystems on some Unix-like OSs.
and you can lock files with the built-in flock() function.

I believe there are Unix like OSs on which you can open directories as
files. Sometimes you need to pass special flags to open(2) (i.e. Perl's
sysopen) and sometimes you need to be root.

If you can open then then I suspect you can lock them. However this
would not be portable.
I'm trying to lock a directory. Probably for the same reason you lock
files: so you know that someone else is still using it, and can
politely leave it alone for the time being.

I belive the conventional approach is to lock a pre-arranged file
withing the directory. (Not, of course, that this has anything to do
with Perl).
 
O

ofer

I belive the conventional approach is to lock a pre-arranged file
withing the directory. (Not, of course, that this has anything to do
with Perl).

Yes, I'll probably implement something like this. And you're right
that this has nothing to do with perl... I probably should have posted
this elsewhere. I guess when I'm working on perl code, and I can't
figure something out, my first instinct is to come here, because then
I'll get a perlish solution. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top