kawas
asked on
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.
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.
ASKER
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.
I need only non-stale files served, so I would like to lock a directory containing the files while i process them all.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so there is no way to lock a directory?
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?
ASKER
no other process and on linux (but would prefer a portable solution)
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
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
ASKER
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?
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
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
ASKER
I ended up creating a file that i flock on while processing 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?