1 /* Test of file timestamp modification functions.
2 Copyright (C) 2009 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 2 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 /* This file assumes that BASE and ASSERT are already defined. */
19 #ifndef GL_TEST_UTIMENS
20 # define GL_TEST_UTIMENS
27 #include "stat-time.h"
32 BILLION = 1000 * 1000 * 1000,
34 Y2K = 946684800, /* Jan 1, 2000, in seconds since epoch. */
36 /* Bogus positive and negative tv_nsec values closest to valid
37 range, but without colliding with UTIME_NOW or UTIME_OMIT. */
38 UTIME_BOGUS_POS = BILLION + ((UTIME_NOW == BILLION || UTIME_OMIT == BILLION)
39 ? (1 + (UTIME_NOW == BILLION + 1)
40 + (UTIME_OMIT == BILLION + 1))
42 UTIME_BOGUS_NEG = -1 - ((UTIME_NOW == -1 || UTIME_OMIT == -1)
43 ? (1 + (UTIME_NOW == -2) + (UTIME_OMIT == -2))
47 #endif /* GL_TEST_UTIMENS */
49 /* This function is designed to test both lutimens(a,b) and
50 utimensat(AT_FDCWD,a,b,AT_SYMLINK_NOFOLLOW). FUNC is the function
51 to test. If PRINT, warn before skipping tests with status 77. */
53 test_lutimens (int (*func) (char const *, struct timespec const *), bool print)
58 bool atime_supported = true;
60 if (symlink ("nowhere", BASE "link"))
63 fputs ("skipping test: symlinks not supported on this file system\n",
68 result = func (BASE "link", NULL);
69 if (result == -1 && errno == ENOSYS)
71 ASSERT (unlink (BASE "link") == 0);
73 fputs ("skipping test: "
74 "setting symlink time not supported on this file system\n",
79 ASSERT (lstat (BASE "link", &st1) == 0);
81 /* On Cygwin, the mere act of lstat changes symlink atime, even
82 though POSIX says that only readlink is allowed to do that.
83 Sleeping for one millisecond is enough to expose this. Platforms
84 without usleep either don't have symlinks, or are immune. */
87 ASSERT (lstat (BASE "link", &st2) == 0);
88 if (st1.st_atime != st2.st_atime
89 || get_stat_atime_ns (&st1) != get_stat_atime_ns (&st2))
90 atime_supported = false;
92 /* Invalid arguments. */
94 ASSERT (func ("no_such", NULL) == -1);
95 ASSERT (errno == ENOENT);
97 ASSERT (func ("", NULL) == -1);
98 ASSERT (errno == ENOENT);
100 struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
102 ASSERT (func (BASE "link", ts) == -1);
103 ASSERT (errno == EINVAL);
106 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
108 ASSERT (func (BASE "link", ts) == -1);
109 ASSERT (errno == EINVAL);
111 ASSERT (lstat (BASE "link", &st2) == 0);
114 ASSERT (st1.st_atime == st2.st_atime);
115 ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
117 ASSERT (utimecmp (BASE "link", &st1, &st2, 0) == 0);
119 /* Set both times. */
121 struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
122 ASSERT (func (BASE "link", ts) == 0);
123 ASSERT (lstat (BASE "link", &st2) == 0);
126 ASSERT (st2.st_atime == Y2K);
127 ASSERT (0 <= get_stat_atime_ns (&st2));
128 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
130 ASSERT (st2.st_mtime == Y2K);
131 ASSERT (0 <= get_stat_mtime_ns (&st2));
132 ASSERT (get_stat_mtime_ns (&st2) < BILLION);
135 /* Play with UTIME_OMIT, UTIME_NOW. */
137 struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
138 ASSERT (func (BASE "link", ts) == 0);
139 ASSERT (lstat (BASE "link", &st2) == 0);
142 ASSERT (st2.st_atime == Y2K);
143 ASSERT (0 <= get_stat_atime_ns (&st2));
144 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
146 ASSERT (utimecmp (BASE "link", &st1, &st2, 0) <= 0);
150 ASSERT (unlink (BASE "link") == 0);