#50177 [Opn]: flock: Example 1 - file is truncated twice
| From: | chris at beckr dot org | Date: | Sun, 15 Nov 2009 20:01:53 +0000 |
| Subject: | #50177 [Opn]: flock: Example 1 - file is truncated twice | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-3169@lists.php.net to get a copy of this message | ||
ID: 50177
User updated by: chris at beckr dot org
-Summary: Example 1 - file is truncated twice
Reported By: chris at beckr dot org
Status: Open
Bug Type: Documentation problem
Operating System: Mac / Linux
PHP Version: Irrelevant
New Comment:
Added 'flock' to title
Previous Comments:
------------------------------------------------------------------------
[2009-11-14 22:02:49] chris at beckr dot org
Description:
------------
Example 1 of the flock function documentation shows how to lock a file
for writing. Once the lock has been obtained, the file is ftruncate()d
and date is written.
However, the file is opened using "w", which means that the file will
already be truncated even before the lock is issued, which means that
any readers (LOCK_SH) that validly obtain a lock in the meantime will
see an empty file. Using "r+" instead of "w", everything works as
expected. Reproduced on OS X (HFS+) and Linux (ext3).
Reproduce code:
---------------
---
From manual page: function.flock#Examples
---
A sample reader counterpart:
<?php
while (true) {
$fp = fopen("/tmp/lock.txt", "r");
if (flock($fp, LOCK_SH)) { // open for reading
$contents = '';
while (!feof($fp)) {
$contents .= fread($fp, 8192);
}
echo($contents);
flock($fp, LOCK_UN); // release the lock
} else {
echo "Couldn't get the lock!";
}
fclose($fp);
}
?>
Expected result:
----------------
The reader should print only lines with "Write something here"
Actual result:
--------------
The reader sometimes prints empty lines
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50177&edit=1