1 /* Test of file timestamp modification functions.
2 Copyright (C) 2009-2011 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 <http://www.gnu.org/licenses/>. */
17 #include "test-utimens-common.h"
19 /* This file is designed to test both fdutimens(a,NULL,b) and
20 futimens(a,b). FUNC is the function to test. Assumes that BASE
21 and ASSERT are already defined. If PRINT, warn before skipping
22 tests with status 77. */
24 test_futimens (int (*func) (int, struct timespec const *),
27 int fd = creat (BASE "file", 0600);
34 ASSERT (fstat (fd, &st1) == 0);
37 result = func (fd, NULL);
38 if (result == -1 && errno == ENOSYS)
40 ASSERT (close (fd) == 0);
41 ASSERT (unlink (BASE "file") == 0);
43 fputs ("skipping test: "
44 "setting fd time not supported on this file system\n",
49 ASSERT (fstat (fd, &st2) == 0);
50 /* If utimens truncates to less resolution than the file system
51 supports, then time can appear to go backwards between now and a
52 follow-up utimens with UTIME_NOW or a NULL timespec. Use
53 UTIMECMP_TRUNCATE_SOURCE to compensate, with st1 as the
55 ASSERT (0 <= utimecmp (BASE "file", &st2, &st1, UTIMECMP_TRUNCATE_SOURCE));
57 ASSERT (st1.st_ctime < st2.st_ctime
58 || (st1.st_ctime == st2.st_ctime
59 && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
61 /* On some NFS systems, the 'now' timestamp of creat or a NULL
62 timespec is determined by the server, but the 'now' timestamp
63 determined by gettime() (as is done when using UTIME_NOW) is
64 determined by the client; since the two machines are not
65 necessarily on the same clock, this is another case where time
66 can appear to go backwards. The rest of this test cares about
67 client time, so manually use gettime() to set both times. */
68 struct timespec ts[2];
71 ASSERT (func (fd, ts) == 0);
72 ASSERT (fstat (fd, &st1) == 0);
76 /* Invalid arguments. */
79 ASSERT (func (AT_FDCWD, NULL) == -1);
80 ASSERT (errno == EBADF);
84 ASSERT (func (-1, NULL) == -1);
85 ASSERT (errno == EBADF);
89 ASSERT (func (99, NULL) == -1);
90 ASSERT (errno == EBADF);
95 ASSERT (close (fd0) == 0);
97 ASSERT (func (fd0, NULL) == -1);
98 ASSERT (errno == EBADF);
101 struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
103 ASSERT (func (fd, ts) == -1);
104 ASSERT (errno == EINVAL);
107 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
109 ASSERT (func (fd, ts) == -1);
110 ASSERT (errno == EINVAL);
112 ASSERT (fstat (fd, &st2) == 0);
113 ASSERT (st1.st_atime == st2.st_atime);
114 ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
115 ASSERT (utimecmp (BASE "file", &st1, &st2, 0) == 0);
117 /* Set both times. */
119 struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
120 ASSERT (func (fd, ts) == 0);
121 ASSERT (fstat (fd, &st2) == 0);
122 ASSERT (st2.st_atime == Y2K);
123 ASSERT (0 <= get_stat_atime_ns (&st2));
124 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
125 ASSERT (st2.st_mtime == Y2K);
126 ASSERT (0 <= get_stat_mtime_ns (&st2));
127 ASSERT (get_stat_mtime_ns (&st2) < BILLION);
129 ASSERT (st1.st_ctime < st2.st_ctime
130 || (st1.st_ctime == st2.st_ctime
131 && get_stat_ctime_ns (&st1) < get_stat_ctime_ns (&st2)));
134 /* Play with UTIME_OMIT, UTIME_NOW. */
137 struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
139 ASSERT (func (fd, ts) == 0);
140 ASSERT (fstat (fd, &st3) == 0);
141 ASSERT (st3.st_atime == Y2K);
142 ASSERT (0 <= get_stat_atime_ns (&st3));
143 ASSERT (get_stat_atime_ns (&st3) <= BILLION / 2);
144 ASSERT (utimecmp (BASE "file", &st1, &st3, 0) <= 0);
146 ASSERT (st2.st_ctime < st3.st_ctime
147 || (st2.st_ctime == st3.st_ctime
148 && get_stat_ctime_ns (&st2) < get_stat_ctime_ns (&st3)));
151 ts[1].tv_nsec = UTIME_OMIT;
152 ASSERT (func (fd, ts) == 0);
153 ASSERT (fstat (fd, &st2) == 0);
154 ASSERT (st2.st_atime == BILLION);
155 ASSERT (get_stat_atime_ns (&st2) == 0);
156 ASSERT (st3.st_mtime == st2.st_mtime);
157 ASSERT (get_stat_mtime_ns (&st3) == get_stat_mtime_ns (&st2));
159 ASSERT (st3.st_ctime < st2.st_ctime
160 || (st3.st_ctime == st2.st_ctime
161 && get_stat_ctime_ns (&st3) < get_stat_ctime_ns (&st2)));
165 ASSERT (close (fd) == 0);
166 ASSERT (unlink (BASE "file") == 0);