Bug #81396 [Opn->Ver]: phar:// file fflush breaks file write
| From: | cmb@php.net | Date: | Mon, 30 Aug 2021 12:43:17 +0000 |
| Subject: | Bug #81396 [Opn->Ver]: phar:// file fflush breaks file write | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-236173@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81396&edit=1
ID: 81396
Updated by: cmb@php.net
Reported by: dixyes at gmail dot com
Summary: phar:// file fflush breaks file write
-Status: Open
+Status: Verified
Type: Bug
Package: PHAR related
Operating System: irrelative
PHP Version: 8.0.10
Block user comment: N
Private report: N
New Comment:
When fflush() is called, the complete phar is written to disk, and
during this operation the connection to the $phar_uri file pointer
is severed[1]. Further writes to $phar_uri are then ignored by
the phar, except for the phar entry's size increment. This leads
to the garbage bytes at the end of the string.
It might not be possible to retain the connection with the file
pointer.
[1] <https://github.com/php/php-src/blob/php-7.4.23/ext/phar/phar.c#L3069-L3070>
Previous Comments:
------------------------------------------------------------------------
[2021-08-29 11:03:37] dixyes at gmail dot com
Description:
------------
fflush file make strange result in phar files.
this occurs on PHP 7.4 also, tested in windows and linux
use php -n -dphar.readonly=0 test.php to test
Test script:
---------------
<?php
// make up the phar
$phar = new \Phar(__DIR__ . DIRECTORY_SEPARATOR . 'test.phar');
$phar->addEmptyDir('dir'); // may be irrelative
$phar->stopBuffering();
//$phar_uri = __DIR__ . DIRECTORY_SEPARATOR . 'file';
$phar_uri = 'phar://' . __DIR__ . DIRECTORY_SEPARATOR . 'test.phar' .
DIRECTORY_SEPARATOR . 'dir/file';
// make a file
$f = fopen($phar_uri, 'w+');
fwrite($f, 'this is content ');
fflush($f); // comment out this, things will be ok
fwrite($f, "of file.\n");
fclose($f);
// read it
$f = fopen($phar_uri, 'r');
$content = fread($f, 4096);
if ($content !== "this is content of file.\n") {
throw new Exception("bad file context: $content vs 'this is content of
file.\n'");
} else {
echo "OK\n";
}
Expected result:
----------------
OK
Actual result:
--------------
bad file context with strange things
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81396&edit=1