#50177 [NEW]: Example 1 - file is truncated twice

From: Date: Sat, 14 Nov 2009 22:02:49 +0000
Subject: #50177 [NEW]: Example 1 - file is truncated twice
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-3165@lists.php.net to get a copy of this message
From: chris at beckr dot org Operating system: Mac / Linux PHP version: Irrelevant PHP Bug Type: Documentation problem Bug description: Example 1 - file is truncated twice 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 bug report at http://bugs.php.net/?id=50177&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=50177&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=50177&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=50177&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=50177&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=50177&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=50177&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=50177&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=50177&r=needscript Try newer version: http://bugs.php.net/fix.php?id=50177&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=50177&r=support Expected behavior: http://bugs.php.net/fix.php?id=50177&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=50177&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=50177&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=50177&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50177&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=50177&r=dst IIS Stability: http://bugs.php.net/fix.php?id=50177&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=50177&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=50177&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=50177&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=50177&r=mysqlcfg

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