fdatasync - synchronize a file's in-core data with that on disk
#include <unistd.h> #ifdef _POSIX_SYNCHRONIZED_IO int fdatasync(int fd); #endif
fdatasync(2) flushes all data buffers of a file to disk (before the system call returns). It resembles fsync(2) but is not required to update the metadata such as access time.
Applications that access databases or log files often write a tiny data fragment (e.g., one line in a log file) and then call fsync(2) immediately in order to ensure that the written data is physically stored on the harddisk. Unfortunately, fsync(2) will always initiate two write operations: one for the newly written data and another one in order to update the modification time stored in the inode. If the modification time is not a part of the transaction concept fdatasync(2) can be used to avoid unnecessary inode disk write operations.
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
Currently (Linux 2.2) fdatasync(2) is equivalent to fsync(2)
POSIX1b (formerly POSIX.4)
/*
- This program demonstrates fdatasync
*
- /
- include <sys/stat.h> /* for S_* constants */
- include <sys/types.h> /* for mode_t for creat() */
- include <string.h> /* for strerror() prototype */
- include <fcntl.h> /* for creat() prototype */
- include <stdio.h> /* for fprintf(),stderr protype */
- include <errno.h> /* for errno prototype */
- include <unistd.h> /* for write(2), fdatasync(2), close(2) prototypes */
- define FILENAME "/tmp/fdatasync.example"
- define TEXT "blargh!\n"
int main(int argc,char **argv) {
int fd; int err;
fd = creat(FILENAME,S_IRUSR|S_IWUSR); if (fd==-1) {
fprintf(
stderr, "creat(\"%s\",S_IRUSR|S_IWUSR): %s (%i)\n", FILENAME, strerror(errno), errno);
return 1;
}
err=write(fd,TEXT,strlen(TEXT));
if (err==-1) {
fprintf(
stderr, "write(fd,\"%s\",strlen(\"%s\")): %s (%i)\n", TEXT, TEXT, strerror(errno), errno);
return 1;
}
if (err!=strlen(TEXT)) {
fprintf(stderr,"Failed to write all the data, and I'm lazy and not "
"going to retry\n");
return 1;
}
/* Flush the data only */ if (fdatasync(fd)==-1) {
fprintf(
stderr, "fdatasync(fd): %s (%i)\n", strerror(errno), errno);
return 1;
}
if (close(fd)==-1) {
fprintf(
stderr, "close(fd): %s (%i)\n", strerror(errno), errno);
return 1;
}
printf(
"%s successfully written\n", FILENAME);
return 0;
}
fsync(2), B.O. Gallmeister, POSIX.4, O'Reilly, pp. 220-223 and 343.
4 pages link to fdatasync(2):