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. */
24 test_utimens (int (*func
) (char const *, struct timespec
const *), bool print
)
29 int fd
= open (BASE
"file", O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
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
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
);
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
));
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];
58 ASSERT (func (BASE
"file", ts
) == 0);
59 ASSERT (stat (BASE
"file", &st1
) == 0);
63 /* Invalid arguments. */
65 ASSERT (func ("no_such", NULL
) == -1);
66 ASSERT (errno
== ENOENT
);
68 ASSERT (func ("no_such/", NULL
) == -1);
69 ASSERT (errno
== ENOENT
|| errno
== ENOTDIR
);
71 ASSERT (func ("", NULL
) == -1);
72 ASSERT (errno
== ENOENT
);
74 struct timespec ts
[2];
76 ts
[0].tv_nsec
= UTIME_BOGUS_POS
;
80 ASSERT (func (BASE
"file", ts
) == -1);
81 ASSERT (errno
== EINVAL
);
84 struct timespec ts
[2];
88 ts
[1].tv_nsec
= UTIME_BOGUS_NEG
;
90 ASSERT (func (BASE
"file", ts
) == -1);
91 ASSERT (errno
== EINVAL
);
94 struct timespec ts
[2];
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];
111 ts
[0].tv_nsec
= BILLION
/ 2 - 1;
113 ts
[1].tv_nsec
= BILLION
- 1;
114 ASSERT (func (BASE
"file", ts
) == 0);
115 ASSERT (stat (BASE
"file", &st2
) == 0);
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
);
126 ASSERT (ctime_compare (&st1
, &st2
) < 0);
129 /* Play with UTIME_OMIT, UTIME_NOW. */
132 struct timespec ts
[2];
133 ts
[0].tv_sec
= BILLION
;
134 ts
[0].tv_nsec
= UTIME_OMIT
;
136 ts
[1].tv_nsec
= UTIME_NOW
;
138 ASSERT (func (BASE
"file", ts
) == 0);
139 ASSERT (stat (BASE
"file", &st3
) == 0);
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
));
149 ASSERT (ctime_compare (&st2
, &st3
) < 0);
152 ts
[1].tv_nsec
= UTIME_OMIT
;
153 ASSERT (func (BASE
"file", ts
) == 0);
154 ASSERT (stat (BASE
"file", &st2
) == 0);
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
));
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);
171 fputs ("skipping test: symlinks not supported on this file system\n",
175 ASSERT (lstat (BASE
"link", &st1
) == 0);
176 ASSERT (st1
.st_mtime
!= Y2K
);
178 ASSERT (func (BASE
"link/", NULL
) == -1);
179 ASSERT (errno
== ENOTDIR
);
181 struct timespec ts
[2];
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);
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);
205 ASSERT (unlink (BASE
"link") == 0);
206 ASSERT (unlink (BASE
"file") == 0);