Atomic file check/open?
| From: | Kris Kelley | Date: | Mon, 10 Jul 2000 17:05:19 +0000 |
| Subject: | Atomic file check/open? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-5758@lists.php.net to get a copy of this message | ||
Forgive me for asking this again, but judging from the replies I received, I
feel that I badly worded my question the first time.
I'm wondering if there's a command that will do the following things in a
single, "atomic" step with respect to other PHP processes that might be
trying the same thing.
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.
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);
This combination of flags tries to open the file for writing, succeeding
only if the file is not already present, failing and returning -1 otherwise.
According to the man pages, this is done atomically, so that there are no
race conditions with other processes that might be trying to access the same
file at the same time.
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.
Thank you again to the people have given input so far, and to everybody for
any additional help.
---Kris Kelley