Solved

How to lock a directory

Posted on 2007-11-27
10
1,691 Views
Last Modified: 2010-04-21
I have a directory that contains a bunch of files that represent a file cache. I would like to deploy my code to a web server, but I havent made the code thread safe.

I have read about flock, and pretty much just want confirmation or a better way of doing things.

I would like to lock the directory when i update the file cache. How can I do this?

Thanks.
0
Comment
Question by:kawas
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 20361427
What is the problem you will have if you don't lock the directory?

Are you concerned a script might read some old cache files, and some new, if it runs while you are updating the cache?
0
 
LVL 13

Author Comment

by:kawas
ID: 20361613
exactly. Also, the cache works in such a way that I have a single file that is used as a  list file that allows me to determine if the other files are stale. if they are stale, they get updated and a new list file is created.

I need only non-stale files served, so I would like to lock a directory containing the files while i process them all.

Thanks.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 250 total points
ID: 20361845
You could use flock on the single file.  
0
 
LVL 13

Author Comment

by:kawas
ID: 20361863
so there is no way to lock a directory?
0
 

Expert Comment

by:shayne321
ID: 20361902
Is this a Windows or UNIX/Linux system?  also are you looking to lock it in such a way that no other process can access it, or only so that your script does not access it while an earlier run of your script is still running?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Author Comment

by:kawas
ID: 20361928
no other process and on linux (but would prefer a portable solution)
0
 

Expert Comment

by:shayne321
ID: 20362189
One way to do it if the other processes do not run under the same user ID is to remove world-execute permissions from the dir when your script starts, and add them back when it is done:

chmod(0750, $DIR);
## do stuff
chmod(0755, $DIR);

  However you'd obviously want to be very careful your script can't die in a way you don't anticipate.  Not sure how chmod() behaves on windows, though..  I doubt it would behave correctly.

Shayne
0
 
LVL 13

Author Comment

by:kawas
ID: 20362229
hmm, there isnt a safer way of doing this? My problem doesn't come across as something new. Has anyone ever wrote a script that reads/writes common files? How did you handle the thread issues?
0
 
LVL 84

Expert Comment

by:ozo
ID: 20363708
You can flock a directory, but like flocking a file, it only prevents other processes from doing a flock on the same file.
If all processes cooperate and only do things when the flock grants them permission, that's fine.
is you don't unlock, flock releases the lock when the process exits
0
 
LVL 13

Author Closing Comment

by:kawas
ID: 31411291
I ended up creating a file that i flock on while processing the directory
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connecting to linux server using Net::Telnet module 25 220
perl script help 12 104
Perl troubleshooting error. Base64 bootstrap parameter 5 63
Awk Question 2 124
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now