[Previous]
[Contents]
[Next]

fdatasync()

write to disk all the currently queued data for a file

Synopsis:

#include <fcntl.h>
int fdatasync( int fd );

Description:

The fdatasync() function writes to disk all the currently queued data for the open file specified by fd. All necessary file system information required to retrieve the data is also written to disk. However, whether or not the file access times are updated depends on the system.

The fdatasync() function doesn't return until the transfer is completed.

Returns:

0
Success
-1
An error occurred. errno is set to indicate the error. Outstanding I/O operations aren't guaranteed to have been completed.

Errors:

EBADF
The fd argument isn't a valid file descriptor.
EINVAL
Synchronized I/O isn't supported for this file.
EIO
A physical I/O error occurred (for example, a bad block). The precise meaning is device-dependent.
ENOSYS
The fdatasync() function isn't supported.

Examples:

/*
 *    Write a file and make sure it is on disk.
 */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char buf[512];

int main( void )
  {
    int fd;
    int i;

    if( ( fd = creat("fsync.demo", 0666) ) == -1 ) {
      perror( "Error creating file" );
      exit( EXIT_FAILURE );
    }

    for( i = 0; i < 255; ++i ) {
      memset( buf, i, sizeof( buf ) );
      if( write( fd, buf, sizeof(buf) ) != sizeof(buf) ) {
        perror( "Error writing file" );
        exit( EXIT_FAILURE );
      }
    }

    if( fdatasync( fd ) == -1 ) {
      perror( "Error sync'ing data" );
      exit( EXIT_FAILURE );
    }

    close( fd );
    return( EXIT_SUCCESS );
  }

Classification:

POSIX 1003.4

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

errno, fcntl(), fstat(), fsync(), open(), stat(), write()


[Previous]
[Contents]
[Next]