(root)/
glibc-2.38/
libio/
tst-atime.c
       1  #include <errno.h>
       2  #include <fcntl.h>
       3  #include <stdio.h>
       4  #include <stdlib.h>
       5  #include <string.h>
       6  #include <unistd.h>
       7  #include <sys/stat.h>
       8  #include <sys/statvfs.h>
       9  
      10  
      11  static int do_test (void);
      12  #define TEST_FUNCTION do_test ()
      13  #include <test-skeleton.c>
      14  
      15  
      16  static int
      17  do_test (void)
      18  {
      19    char *buf;
      20    int fd;
      21    FILE *fp;
      22    int ch;
      23    struct stat st1;
      24    struct stat st2;
      25  
      26    buf = (char *) malloc (strlen (test_dir) + sizeof "/tst-atime.XXXXXX");
      27    if (buf == NULL)
      28      {
      29        printf ("cannot allocate memory: %m\n");
      30        return 1;
      31      }
      32    stpcpy (stpcpy (buf, test_dir), "/tst-atime.XXXXXX");
      33  
      34    fd = mkstemp (buf);
      35    if (fd == -1)
      36      {
      37        printf ("cannot open temporary file: %m\n");
      38        return 1;
      39      }
      40  
      41  #ifdef ST_NOATIME
      42    /* Make sure the filesystem doesn't have the noatime option set.  If
      43       statvfs is not available just continue.  */
      44    struct statvfs sv;
      45    int e = fstatvfs (fd, &sv);
      46    if (e != ENOSYS)
      47      {
      48        if (e != 0)
      49  	{
      50  	  printf ("cannot statvfs '%s': %m\n", buf);
      51  	  return 1;
      52  	}
      53  
      54        if ((sv.f_flag & ST_NOATIME) != 0)
      55  	{
      56  	  puts ("Bah!  The filesystem is mounted with noatime");
      57  	  return 0;
      58  	}
      59      }
      60  #endif
      61  
      62    /* Make sure it gets removed.  */
      63    add_temp_file (buf);
      64  
      65    if (write (fd, "some string\n", 12) != 12)
      66      {
      67        printf ("cannot write temporary file: %m\n");
      68        return 1;
      69      }
      70  
      71    if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
      72      {
      73        printf ("cannot reposition temporary file: %m\n");
      74        return 1;
      75      }
      76  
      77    fp = fdopen (fd, "r");
      78    if (fp == NULL)
      79      {
      80        printf ("cannot create stream: %m\n");
      81        return 1;
      82      }
      83  
      84    if (fstat (fd, &st1) == -1)
      85      {
      86        printf ("first stat failed: %m\n");
      87        return 1;
      88      }
      89  
      90    sleep (2);
      91  
      92    ch = fgetc (fp);
      93    if (ch != 's')
      94      {
      95        printf ("did not read correct character: got '%c', expected 's'\n", ch);
      96        return 1;
      97      }
      98  
      99    if (fstat (fd, &st2) == -1)
     100      {
     101        printf ("second stat failed: %m\n");
     102        return 1;
     103      }
     104  
     105    if (st1.st_atime > st2.st_atime)
     106      {
     107        puts ("second atime smaller");
     108        return 1;
     109      }
     110    else if (st1.st_atime == st2.st_atime)
     111      {
     112        puts ("atime has not changed");
     113        return 1;
     114      }
     115  
     116    fclose (fp);
     117  
     118    return 0;
     119  }