Table of Contents

PHP RFC: fsync() function

Introduction

fsync() is a function similar to fflush(), however where fflush() only instructs the application to flush its internal buffers out to the OS, fsync() further instructs the OS to flush its buffers to physical storage, ensuring a completed and persisted write before returning any success. PHP provides plain stream wrappers for most common file system functions inherited from C and is indeed one of few major languages to not provide any interface to fsync. This RFC proposes implementing fsync in core.

Proposal

This RFC would add an fsync() function accepting a single parameter of a stream resource. The implementation of this function would be a thin wrapper around the standard C fsync (or equivalent _commit on Windows API, which uses an identical signature).

The related function fdatasync() which syncs data but not necessarily metadata would also be added, however this is not supported on Windows and the proposal there is to still provide fdatasync() but merely as an alias of fsync(). On POSIX, fdatasync() is properly implemented.

$fp = fopen('file.txt', 'w');
fwrite($fp, 'string');
var_dump(fsync($fp));
 
bool(true)
 
$fp = fopen('php://memory','w+');
fwrite($fp,"Test line 1\nLine 2\n");
var_dump(fsync($fp));
 
Warning: fsync(): Can't fsync this stream in php shell code on line 1
bool(false)
 
$fp = fopen('php://stdin', 'w');
var_dump(fsync($fp));
bool(false)

Sample documentation

fsync() - synchronize changes to a file

fdatasync() - synchronize the data of a file

fsync ( resource $stream ) : bool

Request that all data for the open file pointer $stream is to be transferred to the storage device. The manner of synchronization is OS dependent. The fsync() function shall not return until the system has completed that action or until an error is detected.

Returns true on success or false on failure.

If the resource indicated by $stream is not a file pointer, an E_WARNING is emitted.

Backward Incompatible Changes

None.

Proposed PHP Version(s)

8.1

RFC Impact

To SAPIs

None, this function would be available in all SAPIs supporting plain stream wrappers.

To Existing Extensions

No.

Open Issues

Proposed Voting Choices

Accept RFC as proposed.

Implementation

PR here

Vote

fsync Function
Real name Yes No
alcaeus (alcaeus)  
ashnazg (ashnazg)  
bmajdak (bmajdak)  
crell (crell)  
derick (derick)  
galvao (galvao)  
gasolwu (gasolwu)  
girgias (girgias)  
jasny (jasny)  
jhdxr (jhdxr)  
kalle (kalle)  
kguest (kguest)  
kocsismate (kocsismate)  
marandall (marandall)  
narf (narf)  
nicolasgrekas (nicolasgrekas)  
nikic (nikic)  
ocramius (ocramius)  
petk (petk)  
ramsey (ramsey)  
rasmus (rasmus)  
reywob (reywob)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
sirsnyder (sirsnyder)  
stas (stas)  
tandre (tandre)  
theodorejb (theodorejb)  
trowski (trowski)  
yunosh (yunosh)  
zimt (zimt)  
Final result: 30 1
This poll has been closed.

References

https://man7.org/linux/man-pages/man2/fsync.2.html