• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1732
  • Last Modified:

How to lock a directory

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
kawas
Asked:
kawas
  • 5
  • 2
  • 2
  • +1
1 Solution
 
Adam314Commented:
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
 
kawasAuthor Commented:
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
 
Adam314Commented:
You could use flock on the single file.  
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kawasAuthor Commented:
so there is no way to lock a directory?
0
 
shayne321Commented:
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
 
kawasAuthor Commented:
no other process and on linux (but would prefer a portable solution)
0
 
shayne321Commented:
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
 
kawasAuthor Commented:
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
 
ozoCommented:
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
 
kawasAuthor Commented:
I ended up creating a file that i flock on while processing the directory
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now