#50177 [Opn->Csd]: flock: Example 1 - file is truncated twice

From: Date: Mon, 16 Nov 2009 11:19:59 +0000
Subject: #50177 [Opn->Csd]: flock: Example 1 - file is truncated twice
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-3173@lists.php.net to get a copy of this message
ID: 50177 Updated by: vrana@php.net Reported By: chris at beckr dot org -Status: Open +Status: Closed Bug Type: Documentation problem Operating System: Mac / Linux PHP Version: Irrelevant New Comment: This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. Previous Comments: ------------------------------------------------------------------------ [2009-11-15 20:01:52] chris at beckr dot org Added 'flock' to title ------------------------------------------------------------------------ [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

« previous php.doc.bugs (#3173) next »