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