fsync - synchronise changes to a file
#include <unistd.h> int fsync(int fildes);
The fsync() function can be used by an application to indicate that all data for the open file description named by fildes is to be transferred to the storage device associated with the file described by fildes in an implementation-dependent manner. The fsync() function does not return until the system has completed that action or until an error is detected.
The fsync() function forces all currently queued I/O operations associated with the file indicated by file descriptor fildes to the synchronised I/O completion state. All I/O operations are completed as defined for synchronised I/O file integrity completion.
Upon successful completion, fsync() returns 0. Otherwise, -1 is returned and errno is set to indicate the error. If the fsync() function fails, outstanding I/O operations are not guaranteed to have been completed.
The fsync() function will fail if:
- The fildes argument is not a valid descriptor.
- The fsync() function was interrupted by a signal.
- The fildes argument does not refer to a file on which this operation is possible.
- An I/O error occurred while reading from or writing to the file system.
In the event that any of the queued I/O operations fail, fsync() returns the error conditions defined for read() and write().
The fsync() function should be used by programs which require modifications to a file to be completed before continuing; for example, a program which contains a simple transaction facility might use it to ensure that all modifications to a file or files caused by a transaction are recorded.