From: blacklight at samtelecom dot ru
Operating system: unix
PHP version: 4.3.7
PHP Bug Type: Filesystem function related
Bug description: fseek & fwrite error
Description:
------------
multiple calls of fwrite() do not store data in the file, except last
call, when opened large files (about 16mb) with fopen(filename, 'a+b')
This error does not appear when files opened with fopen(filename, 'r+b');
Reproduce code:
---------------
define(BUFFER_SIZE, 65536);
function fCut($f, $pos, $size) {
$fst = fstat($f);
$fs = $fst["size"];
if (($pos < 0) || ($pos >= $fs) || ($size <= 0)) return false;
$ps = $pos;
$sz = $fs - $ps - $size;
while ($sz > 0) {
$s = $sz; if ($s > BUFFER_SIZE) { $s = BUFFER_SIZE; }
fseek($f, $ps + $size);
$buf = fread($f, $s);
fseek($f, $ps);
$l = fwrite($f, $buf);
$sz -= $s; $ps += $s;
}
ftruncate($f, max($fs - $size, $pos));
return true;
} // fCut - cutting $size bites in $f, from $pos
$f = fopen(filename, 'a+b');
flock($f, 2);
fcut($f, 0, 123456);
flock($f, 3);
fclose($f);
Expected result:
----------------
123456 bites of file 'filename' must be deleted from position 0;
Actual result:
--------------
all data still the same, except block that transfered with last calls of
fread/fwrite.
Note: this bug appears only in 'a+b' mode, but in 'r+b' all works fine.
--
Edit bug report at http://bugs.php.net/?id=29503&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=29503&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=29503&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=29503&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=29503&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=29503&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=29503&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=29503&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=29503&r=support
Expected behavior: http://bugs.php.net/fix.php?id=29503&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=29503&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=29503&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=29503&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29503&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=29503&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=29503&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=29503&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=29503&r=float