1
0
mirror of https://github.com/php/php-src.git synced 2026-04-24 16:38:25 +02:00
Files
archived-php-src/ext/standard/tests/file/fsync.phpt
T
David Gebler cbcfd86026 Add fsync() and fdatasync() functions
fsync is a straightforward wrapper around the same C function
(implemented on Windows API as _commit() with identical signature).

From the man pages:

    fsync() transfers ("flushes") all modified in-core data of (i.e.,
    modified buffer cache pages for) the file referred to by the file
    descriptor fd to the disk device (or other permanent storage
    device) so that all changed information can be retrieved even if
    the system crashes or is rebooted.  This includes writing through
    or flushing a disk cache if present.  The call blocks until the
    device reports that the transfer has completed.

RFC: https://wiki.php.net/rfc/fsync_function

Closes GH-6650.
2021-04-13 16:09:22 +02:00

74 lines
1.6 KiB
PHP

--TEST--
Test fsync() function: basic functionality
--FILE--
<?php
echo "*** Testing fsync(): writing to a file and reading the contents ***\n";
$data = <<<EOD
first line of string
second line of string
third line of string
EOD;
$file_path = __DIR__;
$filename = "$file_path/fsync_basic.tmp";
// opening a file
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
if(PHP_OS_FAMILY == 'Windows') {
$data = str_replace("\r",'', $data);
}
// writing data to the file
var_dump( fwrite($file_handle, $data) );
var_dump( fsync($file_handle) );
var_dump( readfile($filename) );
echo "\n*** Testing fsync(): for return type ***\n";
$return_value = fsync($file_handle);
var_dump( is_bool($return_value) );
fclose($file_handle);
echo "\n*** Testing fsync(): attempting to sync stdin ***\n";
$file_handle = fopen("php://stdin", "w");
var_dump(fsync($file_handle));
fclose($file_handle);
echo "\n*** Testing fsync(): for non-file stream ***\n";
$file_handle = fopen("php://memory", "w");
$return_value = fsync($file_handle);
var_dump( ($return_value) );
fclose($file_handle);
echo "\n*** Done ***";
?>
--CLEAN--
<?php
$file_path = __DIR__;
$filename = "$file_path/fsync_basic.tmp";
unlink($filename);
?>
--EXPECTF--
*** Testing fsync(): writing to a file and reading the contents ***
int(63)
bool(true)
first line of string
second line of string
third line of stringint(63)
*** Testing fsync(): for return type ***
bool(true)
*** Testing fsync(): attempting to sync stdin ***
bool(false)
*** Testing fsync(): for non-file stream ***
Warning: fsync(): Can't fsync this stream! in %s on line %d
bool(false)
*** Done ***