fsync, fdatasync - synchronize a file's complete in-core state with that on disk
int fsync(int fd);
fsync(2) copies all in-core parts of a file to disk, and waits until the device reports that all parts are on stable storage. It also updates metadata stat information. It does not necessarily ensure that the entry in the directory containing the file has also reached disk. For that an explicit fsync(2) on the file descriptor of the directory is also needed.
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
- fd is not a valid file descriptor open for writing.
- EROFS, EINVAL
- fd is bound to a special file which does not support synchronization.
- An error occurred during synchronization.
In case the hard disk has write cache enabled, the data may not really be on permanent storage when fsync(2) returns.
When an ext2 file system is mounted with the sync option, directory entries are also implicitely synced by fsync(2).
On kernels before 2.4, fsync(2) on big files can be inefficient. An alternative might be to use the O_SYNC flag to open(2).
POSIX.1b (formerly POSIX.4)
bdflush(2), open(2), sync(2), mount(8), update(8), sync(8)?, fdatasync(2)
The difference between fsync(2) and fdatasync(2) is that fsync modifies the access time metadata in the i-node, while fdatasync doesn't/shouldn't. I think this is true version kernel versions >= 2.4 - for 2.2 and earlier fdatasync was the same as fsync.