tags:

views:

24

answers:

2

PHP lacks any specific function to fsync on a file AFAIK. I do however feel the urge to fsync a logfile I am appending to from PHP.

Is there any native PHP function known to cause an fsync? Or any workaround?

A: 

If you're dealing with a regular file, closing it with fclose will most likely commit your changes to disk. To assure that log messages hit the disk, just open and close the file for each write to your log.

Writes to the system log should take place immediately and are handled by the OS.

advait
I'm not familiar at all on how the kernel manages its buffers, but I doubt this is true. If you have references, please provide them.
Artefacto
@Artefacto: you are correct. I just looked through the php codebase and there are no explicit calls to fsync dealing with file operations. @korkman, PHP extensions would be the way to go.
advait
A: 

I'm afraid it's not possible. The only reference to fsync in the sources is in the implementation of the flush operation for regular filesystem streams and it's just to explicitly say they're not fsyncing, only calling fflush.

If you really need this, you have to do it in a PHP extension.

Artefacto
I'm starting to think of an fsync-deamon that listens on a unix-socket and performs arbitary sync-actions. Might not be a performant, but certainly easier to maintain than an extension.
korkman
@korkman An extension for this would be trivial to make. You could also just patch the default flush operation so that it called fsync, though that could have a performance hit in other places where your scripts would call flush and an fsync was not necessary.
Artefacto