Re: Atomic file check/open?
| From: | (Richard Lynch) | Date: | Wed, 12 Jul 2000 21:23:49 +0000 |
| Subject: | Re: Atomic file check/open? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-6282@lists.php.net to get a copy of this message | ||
In article <B58F6C7F.36FB%toledo@kingwoodcable.com>,
toledo@kingwoodcable.com (Kris Kelley) wrote:
> 1. Test for the existence of a file.
> 2. If the file does not already exist, create the file and return true (or
> a file descriptor, either one).
> 3. If the file does already exist, return false and do nothing.
Not really.
> In Unix, C provides a means of doing this with the "open" command:
>
> fdes = open("path/to/file",O_WRONLY|O_CREAT|O_EXCL,0);
You could alter PHP to have such a function, if you were feeling
particularly helpful.
> I need this command so that I can effect a directory lock, allowing only one
> process at a time to look at and move files to and from a particular
> directory. If anybody has an alternative way of doing this, I am open to
> suggestions. I'm not sure if "flock" is a viable option in this case,
> particularly because there will be other programs, not written in PHP, that
> will look at this directory also.
flock() will work if, and only if, every program attempting to access the
file uses flock(). So if those other programs use flock(), you can maybe
be okay. Otherwise, no, it will not work.
I'm also not sure of the semantics (if any) of an flock() on a
directory... You *might* be able to have all programs flock() the
directory itself, and then only one program at a time can move files
to/from it. (At least, if I was designing flock on Un*x and wanted it to
be meaningful for a directory, that's what I would make it do...)
You could maybe use shared memory to keep a semaphore around of when the
file is in use...
Or you could use a database which is designed to handle *exactly* this problem.
Also, your file-based solution will not scale up on a busy server, while
the database will.
I think the short answer is: "Use a database."
--
Richard Lynch | If this was worth $$$ to you, buy a CD
US Customer Support Director | from one of the artists listed here:
Zend Technologies USA | http://www.L-I-E.com/artists.htm
http://www.zend.com | (this has nothing to do with Zend,
duh!)