Comment 150 for bug 317781

Revision history for this message
Volodymyr M. Lisivka (vlisivka-gmail) wrote :

> You can only fsync given a file descriptor, but I think writing an fsync binary that opens the file read-only, fsync on the descriptor, and close the file, should work.

Use this little program to verify your assumptions (I have no time right now):

#include <fcntl.h> /* open(), O_RDONLY */
#include <unistd.h> /* fsync() */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* fprintf(), stderr */

int
main(int argc, char **argv)
{
  char *file_name; /* Name of the file to sync */
  int fd; /* File descriptor */
  int exit_code=0;
  int i;

  /* For each argument, except program itself */
  for(i=1; i<argc; i++)
  {
    file_name=argv[i];

    /* Open file in readonly mode, unbuffered mode */
    fd=open(argv[i], O_RDONLY);

    /* Ignore errors */
    if(fd==-1)
    {
      fprintf(stderr,"Cannot open file \"%s\": %s\n",file_name, strerror(errno));

      /* Ignore errors */
      exit_code=1; /* Return non-zero exit code to indicate problem. */
      continue;
    }

    if(fsync(fd)==-1)
    {
      fprintf(stderr,"Cannot open file \"%s\": %s\n",file_name, strerror(errno));

      /* Ignore errors */
      exit_code=1; /* Return non-zero exit code to indicate problem. */
      continue;
    }
  }

  return exit_code;
}