openat: don’t close (-1)
[gnulib.git] / tests / test-utimens.h
blob575507f735ce73e971e270fabdfd2b2887544932
1 /* Test of file timestamp modification functions.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include "test-utimens-common.h"
19 /* This file is designed to test both utimens(a,b) and
20 utimensat(AT_FDCWD,a,b,0). FUNC is the function to test. Assumes
21 that BASE and ASSERT are already defined. If PRINT, warn before
22 skipping tests with status 77. */
23 static int
24 test_utimens (int (*func) (char const *, struct timespec const *), bool print)
26 struct stat st1;
27 struct stat st2;
29 int fd = open (BASE "file", O_RDWR | O_CREAT | O_TRUNC, 0600);
30 ASSERT (0 <= fd);
31 /* If utimens truncates to worse resolution than the file system
32 supports, then time can appear to go backwards between now and a
33 follow-up utimens with UTIME_NOW or a NULL timespec. Use
34 UTIMECMP_TRUNCATE_SOURCE to compensate, with st1 as the
35 source. */
36 bool check_atime = checkable_atime (fd, &st1);
37 ASSERT (close (fd) == 0);
38 ASSERT (st1.st_atime != Y2K);
39 ASSERT (st1.st_mtime != Y2K);
41 nap ();
42 ASSERT (func (BASE "file", NULL) == 0);
43 ASSERT (stat (BASE "file", &st2) == 0);
44 ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
45 if (check_ctime)
46 ASSERT (ctime_compare (&st1, &st2) < 0);
48 /* On some NFS systems, the 'now' timestamp of creat or a NULL
49 timespec is determined by the server, but the 'now' timestamp
50 determined by gettime() (as is done when using UTIME_NOW) is
51 determined by the client; since the two machines are not
52 necessarily on the same clock, this is another case where time
53 can appear to go backwards. The rest of this test cares about
54 client time, so manually use gettime() to set both times. */
55 struct timespec ts[2];
56 gettime (&ts[0]);
57 ts[1] = ts[0];
58 ASSERT (func (BASE "file", ts) == 0);
59 ASSERT (stat (BASE "file", &st1) == 0);
60 nap ();
63 /* Invalid arguments. */
64 errno = 0;
65 ASSERT (func ("no_such", NULL) == -1);
66 ASSERT (errno == ENOENT);
67 errno = 0;
68 ASSERT (func ("no_such/", NULL) == -1);
69 ASSERT (errno == ENOENT || errno == ENOTDIR);
70 errno = 0;
71 ASSERT (func ("", NULL) == -1);
72 ASSERT (errno == ENOENT);
74 struct timespec ts[2];
75 ts[0].tv_sec = Y2K;
76 ts[0].tv_nsec = UTIME_BOGUS_POS;
77 ts[1].tv_sec = Y2K;
78 ts[1].tv_nsec = 0;
79 errno = 0;
80 ASSERT (func (BASE "file", ts) == -1);
81 ASSERT (errno == EINVAL);
84 struct timespec ts[2];
85 ts[0].tv_sec = Y2K;
86 ts[0].tv_nsec = 0;
87 ts[1].tv_sec = Y2K;
88 ts[1].tv_nsec = UTIME_BOGUS_NEG;
89 errno = 0;
90 ASSERT (func (BASE "file", ts) == -1);
91 ASSERT (errno == EINVAL);
94 struct timespec ts[2];
95 ts[0].tv_sec = Y2K;
96 ts[0].tv_nsec = 0;
97 ts[1] = ts[0];
98 errno = 0;
99 ASSERT (func (BASE "file/", ts) == -1);
100 ASSERT (errno == ENOTDIR || errno == EINVAL);
102 ASSERT (stat (BASE "file", &st2) == 0);
103 ASSERT (st1.st_atime == st2.st_atime);
104 ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
105 ASSERT (utimecmp (BASE "file", &st1, &st2, 0) == 0);
107 /* Set both times. */
109 struct timespec ts[2];
110 ts[0].tv_sec = Y2K;
111 ts[0].tv_nsec = BILLION / 2 - 1;
112 ts[1].tv_sec = Y2K;
113 ts[1].tv_nsec = BILLION - 1;
114 ASSERT (func (BASE "file", ts) == 0);
115 ASSERT (stat (BASE "file", &st2) == 0);
116 if (check_atime)
118 ASSERT (st2.st_atime == Y2K);
119 ASSERT (0 <= get_stat_atime_ns (&st2));
120 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
122 ASSERT (st2.st_mtime == Y2K);
123 ASSERT (0 <= get_stat_mtime_ns (&st2));
124 ASSERT (get_stat_mtime_ns (&st2) < BILLION);
125 if (check_ctime)
126 ASSERT (ctime_compare (&st1, &st2) < 0);
129 /* Play with UTIME_OMIT, UTIME_NOW. */
131 struct stat st3;
132 struct timespec ts[2];
133 ts[0].tv_sec = BILLION;
134 ts[0].tv_nsec = UTIME_OMIT;
135 ts[1].tv_sec = 0;
136 ts[1].tv_nsec = UTIME_NOW;
137 nap ();
138 ASSERT (func (BASE "file", ts) == 0);
139 ASSERT (stat (BASE "file", &st3) == 0);
140 if (check_atime)
142 ASSERT (st3.st_atime == Y2K);
143 ASSERT (0 <= get_stat_atime_ns (&st3));
144 ASSERT (get_stat_atime_ns (&st3) < BILLION / 2);
146 /* See comment above about this utimecmp call. */
147 ASSERT (0 <= utimecmp (BASE "file", &st3, &st1, UTIMECMP_TRUNCATE_SOURCE));
148 if (check_ctime)
149 ASSERT (ctime_compare (&st2, &st3) < 0);
150 nap ();
151 ts[0].tv_nsec = 0;
152 ts[1].tv_nsec = UTIME_OMIT;
153 ASSERT (func (BASE "file", ts) == 0);
154 ASSERT (stat (BASE "file", &st2) == 0);
155 if (check_atime)
157 ASSERT (st2.st_atime == BILLION);
158 ASSERT (get_stat_atime_ns (&st2) == 0);
160 ASSERT (st3.st_mtime == st2.st_mtime);
161 ASSERT (get_stat_mtime_ns (&st3) == get_stat_mtime_ns (&st2));
162 if (check_ctime > 0)
163 ASSERT (ctime_compare (&st3, &st2) < 0);
166 /* Make sure this dereferences symlinks. */
167 if (symlink (BASE "file", BASE "link"))
169 ASSERT (unlink (BASE "file") == 0);
170 if (print)
171 fputs ("skipping test: symlinks not supported on this file system\n",
172 stderr);
173 return 77;
175 ASSERT (lstat (BASE "link", &st1) == 0);
176 ASSERT (st1.st_mtime != Y2K);
177 errno = 0;
178 ASSERT (func (BASE "link/", NULL) == -1);
179 ASSERT (errno == ENOTDIR);
181 struct timespec ts[2];
182 ts[0].tv_sec = Y2K;
183 ts[0].tv_nsec = 0;
184 ts[1] = ts[0];
185 ASSERT (func (BASE "link", ts) == 0);
186 ASSERT (lstat (BASE "link", &st2) == 0);
188 /* Make sure symlink time hasn't been modified.
189 Can't compare symlink atimes, since when func follows the
190 symlink it might update the symlink atime. */
191 ASSERT (st1.st_mtime == st2.st_mtime);
192 ASSERT (get_stat_mtime_ns (&st1) == get_stat_mtime_ns (&st2));
194 ASSERT (stat (BASE "link", &st2) == 0);
195 if (check_atime)
197 ASSERT (st2.st_atime == Y2K);
198 ASSERT (get_stat_atime_ns (&st2) == 0);
200 ASSERT (st2.st_mtime == Y2K);
201 ASSERT (get_stat_mtime_ns (&st2) == 0);
204 /* Cleanup. */
205 ASSERT (unlink (BASE "link") == 0);
206 ASSERT (unlink (BASE "file") == 0);
207 return 0;