write to disk all the currently queued data for a file
#include <fcntl.h> int fdatasync( int fd );
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.
/* * 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 ); }
POSIX 1003.4
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |
errno, fcntl(), fstat(), fsync(), open(), stat(), write()