Solved

How to lock a directory

Posted on 2007-11-27
10
1,699 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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
 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

825 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