lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <dirent.h> |
| 6 | #include <stdbool.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | #ifndef O_NOATIME |
| 10 | # define O_NOATIME 0 |
| 11 | #endif |
| 12 | |
| 13 | static int |
| 14 | do_test (void) |
| 15 | { |
| 16 | char fname[] = "/tmp/jXXXXXX"; |
| 17 | int fd = mkstemp (fname); |
| 18 | if (fd == -1) |
| 19 | { |
| 20 | puts ("mkstemp failed"); |
| 21 | return 1; |
| 22 | } |
| 23 | |
| 24 | write (fd, "hello", 5); |
| 25 | close (fd); |
| 26 | |
| 27 | struct stat64 st; |
| 28 | if (stat64 (fname, &st) == -1) |
| 29 | { |
| 30 | puts ("first stat failed"); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | /* Make sure there is enough time between the creation and the access. */ |
| 35 | sleep (2); |
| 36 | |
| 37 | fd = open (fname, O_RDONLY | O_NOATIME); |
| 38 | if (fd == -1) |
| 39 | { |
| 40 | puts ("first open failed"); |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | char buf[5]; |
| 45 | read(fd, buf, sizeof (buf)); |
| 46 | close(fd); |
| 47 | |
| 48 | struct stat64 st2; |
| 49 | if (stat64 (fname, &st2) == -1) |
| 50 | { |
| 51 | puts ("second stat failed"); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | bool no_noatime = false; |
| 56 | #ifdef _STATBUF_ST_NSEC |
| 57 | if (st.st_atim.tv_sec != st2.st_atim.tv_sec |
| 58 | || st.st_atim.tv_nsec != st2.st_atim.tv_nsec) |
| 59 | #else |
| 60 | if (st.st_atime != st2.st_atime) |
| 61 | #endif |
| 62 | { |
| 63 | puts ("file atime changed"); |
| 64 | no_noatime = true; |
| 65 | } |
| 66 | |
| 67 | unlink(fname); |
| 68 | |
| 69 | strcpy(fname, "/tmp/dXXXXXX"); |
| 70 | char *d = mkdtemp (fname); |
| 71 | if (d == NULL) |
| 72 | { |
| 73 | puts ("mkdtemp failed"); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | if (stat64 (d, &st) == -1) |
| 78 | { |
| 79 | puts ("third stat failed"); |
| 80 | return 0; |
| 81 | } |
| 82 | sleep (2); |
| 83 | |
| 84 | fd = open64 (d, O_RDONLY|O_NDELAY|O_DIRECTORY|O_NOATIME); |
| 85 | if (fd == -1) |
| 86 | { |
| 87 | puts ("second open failed"); |
| 88 | return 1; |
| 89 | } |
| 90 | DIR *dir = fdopendir (fd); |
| 91 | if (dir == NULL) |
| 92 | { |
| 93 | puts ("fdopendir failed"); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | struct dirent *de; |
| 98 | while ((de = readdir (dir)) != NULL) |
| 99 | ; |
| 100 | |
| 101 | closedir (dir); |
| 102 | |
| 103 | if (stat64 (d, &st2) == -1) |
| 104 | { |
| 105 | puts ("fourth stat failed"); |
| 106 | return 0; |
| 107 | } |
| 108 | #ifdef _STATBUF_ST_NSEC |
| 109 | if (!no_noatime |
| 110 | && (st.st_atim.tv_sec != st2.st_atim.tv_sec |
| 111 | || st.st_atim.tv_nsec != st2.st_atim.tv_nsec)) |
| 112 | #else |
| 113 | if (!no_noatime && st.st_atime != st2.st_atime) |
| 114 | #endif |
| 115 | { |
| 116 | puts ("directory atime changed"); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | rmdir(fname); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | #define TIMEOUT 6 |
| 126 | #define TEST_FUNCTION do_test () |
| 127 | #include "../test-skeleton.c" |