Penguin

fdatasync - synchronize a file's in-core data with that on disk

SYNOPSIS

#include <unistd.h> #ifdef _POSIX_SYNCHRONIZED_IO int fdatasync(int fd); #endif

DESCRIPTION

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.

RETURN VALUE

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

ERRORS

EBADF
fd is not a valid file descriptor open for writing.
EROFS, EINVAL
fd is bound to a special file which does not support synchronization.
EIO
An error occurred during synchronization.

BUGS

Currently (Linux 2.2) fdatasync(2) is equivalent to fsync(2)

CONFORMING TO

POSIX1b (formerly POSIX.4)

EXAMPLES

/*

  • This program demonstrates fdatasync

*

  • /
  1. include <sys/stat.h> /* for S_* constants */
  2. include <sys/types.h> /* for mode_t for creat() */
  3. include <string.h> /* for strerror() prototype */
  4. include <fcntl.h> /* for creat() prototype */
  5. include <stdio.h> /* for fprintf(),stderr protype */
  6. include <errno.h> /* for errno prototype */
  7. include <unistd.h> /* for write(2), fdatasync(2), close(2) prototypes */
  8. define FILENAME "/tmp/fdatasync.example"
  9. 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;

}

SEE ALSO

fsync(2), B.O. Gallmeister, POSIX.4, O'Reilly, pp. 220-223 and 343.

This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.