This source file includes following definitions.
- do_futimens
- do_fdutimens
- do_lutimens
- do_lutimens1
- do_utimens
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include "utimens.h"
22
23 #include <fcntl.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "ignore-value.h"
29 #include "macros.h"
30
31 #define BASE "test-fdutimensat.t"
32
33 #include "test-futimens.h"
34 #include "test-lutimens.h"
35 #include "test-utimens.h"
36
37 static int dfd = AT_FDCWD;
38
39
40 static int
41 do_futimens (int fd, struct timespec const times[2])
42 {
43 return fdutimensat (fd, dfd, NULL, times, 0);
44 }
45
46
47 static int
48 do_fdutimens (char const *name, struct timespec const times[2])
49 {
50 int result;
51 int nofollow_result;
52 int nofollow_errno;
53 int fd = openat (dfd, name, O_WRONLY);
54 if (fd < 0)
55 fd = openat (dfd, name, O_RDONLY);
56 errno = 0;
57 nofollow_result = fdutimensat (fd, dfd, name, times, AT_SYMLINK_NOFOLLOW);
58 nofollow_errno = errno;
59 result = fdutimensat (fd, dfd, name, times, 0);
60 ASSERT (result == nofollow_result
61 || (nofollow_result == -1 && nofollow_errno == ENOSYS));
62 if (0 <= fd)
63 {
64 int saved_errno = errno;
65 close (fd);
66 errno = saved_errno;
67 }
68 return result;
69 }
70
71
72 static int
73 do_lutimens (const char *name, struct timespec const times[2])
74 {
75 return lutimensat (dfd, name, times);
76 }
77
78
79 static int
80 do_lutimens1 (const char *name, struct timespec const times[2])
81 {
82 return fdutimensat (-1, dfd, name, times, AT_SYMLINK_NOFOLLOW);
83 }
84
85
86 static int
87 do_utimens (const char *name, struct timespec const times[2])
88 {
89 return fdutimensat (-1, dfd, name, times, 0);
90 }
91
92 int
93 main (void)
94 {
95 int result1;
96 int result2;
97 int result3;
98 int fd;
99
100
101 ignore_value (system ("rm -rf " BASE "*"));
102
103
104 result1 = test_utimens (do_utimens, true);
105 ASSERT (test_utimens (do_fdutimens, false) == result1);
106 result2 = test_futimens (do_futimens, result1 == 0);
107 result3 = test_lutimens (do_lutimens, (result1 + result2) == 0);
108
109 ASSERT (result1 <= result3);
110 ASSERT (test_lutimens (do_lutimens1, (result1 + result2) == 0) == result3);
111 dfd = open (".", O_RDONLY);
112 ASSERT (0 <= dfd);
113 ASSERT (test_utimens (do_utimens, false) == result1);
114 ASSERT (test_utimens (do_fdutimens, false) == result1);
115 ASSERT (test_futimens (do_futimens, false) == result2);
116 ASSERT (test_lutimens (do_lutimens, false) == result3);
117 ASSERT (test_lutimens (do_lutimens1, false) == result3);
118
119
120 ASSERT (mkdir (BASE "dir", 0700) == 0);
121 ASSERT (chdir (BASE "dir") == 0);
122 fd = creat ("file", 0600);
123 ASSERT (0 <= fd);
124 errno = 0;
125 ASSERT (fdutimensat (AT_FDCWD, fd, ".", NULL, 0) == -1);
126 ASSERT (errno == ENOTDIR);
127 {
128 struct timespec ts[2];
129 struct stat st;
130 ts[0].tv_sec = Y2K;
131 ts[0].tv_nsec = 0;
132 ts[1] = ts[0];
133 ASSERT (fdutimensat (fd, dfd, BASE "dir/file", ts, 0) == 0);
134 ASSERT (stat ("file", &st) == 0);
135 ASSERT (st.st_atime == Y2K);
136 ASSERT (get_stat_atime_ns (&st) == 0);
137 ASSERT (st.st_mtime == Y2K);
138 ASSERT (get_stat_mtime_ns (&st) == 0);
139 }
140 ASSERT (close (fd) == 0);
141 ASSERT (close (dfd) == 0);
142 errno = 0;
143 ASSERT (fdutimensat (-1, dfd, ".", NULL, 0) == -1);
144 ASSERT (errno == EBADF);
145
146
147 ASSERT (chdir ("..") == 0);
148 ASSERT (unlink (BASE "dir/file") == 0);
149 ASSERT (rmdir (BASE "dir") == 0);
150 return result1 | result2 | result3;
151 }