Skip to content

Instantly share code, notes, and snippets.

@rfjakob
Last active August 29, 2015 14:14
Show Gist options
  • Save rfjakob/24ef07667fb435f40b21 to your computer and use it in GitHub Desktop.
Save rfjakob/24ef07667fb435f40b21 to your computer and use it in GitHub Desktop.
Test if the mtime changes after fsync() has been called
/* Test if the mtime changes after fsync() has been called
*
* Bug originally reported at
* http://debbugs.gnu.org/cgi/bugreport.cgi?bug=19607
*
* Compile with:
* gcc fsync-mtime.c -o fsync-mtime
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int
main (void)
{
char const *filename = "test.txt";
int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666);
if (fd < 0)
return perror ("open"), 1;
static char const message[] = "This is a test.\n";
int messagelen = sizeof message - 1;
if (write (fd, message, messagelen) != messagelen)
return perror ("write"), 1;
if (fsync (fd) != 0)
return perror ("fsync"), 1;
struct stat st1, st2;
if (fstat (fd, &st1) != 0)
return perror ("fstat"), 1;
if (close (fd) != 0)
return perror ("close"), 1;
sleep (2);
if (stat (filename, &st2) != 0)
return perror ("stat"), 1;
if (! (st1.st_mtim.tv_sec == st2.st_mtim.tv_sec
&& st1.st_mtim.tv_nsec == st2.st_mtim.tv_nsec))
{
printf ("stat #1: %ld.%09ld\n", st1.st_mtim.tv_sec, st1.st_mtim.tv_nsec);
printf ("stat #2: %ld.%09ld\n", st2.st_mtim.tv_sec, st2.st_mtim.tv_nsec);
printf ("last-modified times do not conform to:\n"
"http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_08\n");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment