Back to index

glibc  2.9
tst-atime.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <fcntl.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 #include <sys/stat.h>
00008 #include <sys/statvfs.h>
00009 
00010 
00011 static int do_test (void);
00012 #define TEST_FUNCTION do_test ()
00013 #define TIMEOUT 5
00014 #include <test-skeleton.c>
00015 
00016 
00017 static int
00018 do_test (void)
00019 {
00020   char *buf;
00021   int fd;
00022   FILE *fp;
00023   int ch;
00024   struct stat st1;
00025   struct stat st2;
00026   struct statvfs sv;
00027   int e;
00028 
00029   buf = (char *) malloc (strlen (test_dir) + sizeof "/tst-atime.XXXXXX");
00030   if (buf == NULL)
00031     {
00032       printf ("cannot allocate memory: %m\n");
00033       return 1;
00034     }
00035   stpcpy (stpcpy (buf, test_dir), "/tst-atime.XXXXXX");
00036 
00037   fd = mkstemp (buf);
00038   if (fd == -1)
00039     {
00040       printf ("cannot open temporary file: %m\n");
00041       return 1;
00042     }
00043 
00044 #ifdef ST_NOATIME
00045   /* Make sure the filesystem doesn't have the noatime option set.  If
00046      statvfs is not available just continue.  */
00047   e = fstatvfs (fd, &sv);
00048   if (e != ENOSYS)
00049     {
00050       if (e != 0)
00051        {
00052          printf ("cannot statvfs '%s': %m\n", buf);
00053          return 1;
00054        }
00055 
00056       if ((sv.f_flag & ST_NOATIME) != 0)
00057        {
00058          puts ("Bah!  The filesystem is mounted with noatime");
00059          return 0;
00060        }
00061     }
00062 #endif
00063 
00064   /* Make sure it gets removed.  */
00065   add_temp_file (buf);
00066 
00067   if (write (fd, "some string\n", 12) != 12)
00068     {
00069       printf ("cannot write temporary file: %m\n");
00070       return 1;
00071     }
00072 
00073   if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
00074     {
00075       printf ("cannot reposition temporary file: %m\n");
00076       return 1;
00077     }
00078 
00079   fp = fdopen (fd, "r");
00080   if (fp == NULL)
00081     {
00082       printf ("cannot create stream: %m\n");
00083       return 1;
00084     }
00085 
00086   if (fstat (fd, &st1) == -1)
00087     {
00088       printf ("first stat failed: %m\n");
00089       return 1;
00090     }
00091 
00092   sleep (2);
00093 
00094   ch = fgetc (fp);
00095   if (ch != 's')
00096     {
00097       printf ("did not read correct character: got '%c', expected 's'\n", ch);
00098       return 1;
00099     }
00100 
00101   if (fstat (fd, &st2) == -1)
00102     {
00103       printf ("second stat failed: %m\n");
00104       return 1;
00105     }
00106 
00107   if (st1.st_atime > st2.st_atime)
00108     {
00109       puts ("second atime smaller");
00110       return 1;
00111     }
00112   else if (st1.st_atime == st2.st_atime)
00113     {
00114       puts ("atime has not changed");
00115       return 1;
00116     }
00117 
00118   fclose (fp);
00119 
00120   return 0;
00121 }