note 37014 deleted from function.flock by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 00:47:38 +0000 |
| Subject: | note 37014 deleted from function.flock by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155431@lists.php.net to get a copy of this message | ||
Note Submitter: bret at alliances dot org
----
A poster above (below?) has the right idea with writing to a tempory file, then copying. However,
achieving the 'right' to use the temp file in the first place is still a race condition
issue:
if (file_exists($temp_filename)) {
$fd = fopen ($temp_filename, 'w');
}
is still wrong, there's still a race condition between checking to whether the temp file exists
and creating it.
Using the 'x' option for opening is a way to avoid this. With the 'x' option
chosen, the fopen will fail (returning FALSE) if the file already exists.
$fd = fopen ($tmp, 'x');
if ($blocking == 1) {
while ($fd === FALSE) {
clearstatcache();
usleep(rand(5,70));
$fd = fopen ($tmp, 'x');
}
}