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 #include "test-utimens-common.h"
19 /* This file is designed to test both lutimens(a,b) and
20 utimensat(AT_FDCWD,a,b,AT_SYMLINK_NOFOLLOW). FUNC is the function
21 to test. Assumes that BASE and ASSERT are already defined. If
22 PRINT, warn before skipping tests with status 77. */
24 test_lutimens (int (*func) (char const *, struct timespec const *), bool print)
30 bool atime_supported = true;
32 /* Non-symlinks should be handled just like utimens. */
34 ASSERT (func ("no_such", NULL) == -1);
35 ASSERT (errno == ENOENT);
37 ASSERT (func ("no_such/", NULL) == -1);
38 ASSERT (errno == ENOENT || errno == ENOTDIR);
40 ASSERT (func ("", NULL) == -1);
41 ASSERT (errno == ENOENT);
42 ASSERT (close (creat (BASE "file", 0600)) == 0);
43 ASSERT (stat (BASE "file", &st1) == 0);
44 ASSERT (st1.st_atime != Y2K);
45 ASSERT (st1.st_mtime != Y2K);
47 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
49 ASSERT (func (BASE "file/", ts) == -1);
50 ASSERT (errno == ENOTDIR);
51 ASSERT (stat (BASE "file", &st2) == 0);
52 ASSERT (st1.st_atime == st2.st_atime);
53 ASSERT (st1.st_mtime == st2.st_mtime);
56 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
57 ASSERT (func (BASE "file", ts) == 0);
59 ASSERT (stat (BASE "file", &st1) == 0);
60 ASSERT (st1.st_atime == Y2K);
61 ASSERT (st1.st_mtime == Y2K);
63 /* Play with symlink timestamps. */
64 if (symlink (BASE "file", BASE "link"))
66 ASSERT (unlink (BASE "file") == 0);
68 fputs ("skipping test: symlinks not supported on this file system\n",
73 result = func (BASE "link", NULL);
75 /* Make sure we did not reference through link by accident. */
76 ASSERT (stat (BASE "file", &st1) == 0);
77 ASSERT (st1.st_atime == Y2K);
78 ASSERT (st1.st_mtime == Y2K);
79 ASSERT (lstat (BASE "link", &st1) == 0);
80 ASSERT (st1.st_atime != Y2K);
81 ASSERT (st1.st_mtime != Y2K);
82 ASSERT (unlink (BASE "file") == 0);
83 if (result == -1 && saved_errno == ENOSYS)
85 ASSERT (unlink (BASE "link") == 0);
87 fputs ("skipping test: "
88 "setting symlink time not supported on this file system\n",
93 ASSERT (lstat (BASE "link", &st1) == 0);
94 /* On cygwin, lstat() changes atime of symlinks, so that lutimens
95 can only effectively modify mtime. */
97 ASSERT (lstat (BASE "link", &st2) == 0);
98 if (st1.st_atime != st2.st_atime
99 || get_stat_atime_ns (&st1) != get_stat_atime_ns (&st2))
100 atime_supported = false;
102 /* Invalid arguments. */
104 struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
106 ASSERT (func (BASE "link", ts) == -1);
107 ASSERT (errno == EINVAL);
110 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
112 ASSERT (func (BASE "link", ts) == -1);
113 ASSERT (errno == EINVAL);
115 ASSERT (lstat (BASE "link", &st2) == 0);
118 ASSERT (st1.st_atime == st2.st_atime);
119 ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
121 ASSERT (utimecmp (BASE "link", &st1, &st2, 0) == 0);
123 /* Set both times. */
125 struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
126 ASSERT (func (BASE "link", ts) == 0);
127 ASSERT (lstat (BASE "link", &st2) == 0);
130 ASSERT (st2.st_atime == Y2K);
131 ASSERT (0 <= get_stat_atime_ns (&st2));
132 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
134 ASSERT (st2.st_mtime == Y2K);
135 ASSERT (0 <= get_stat_mtime_ns (&st2));
136 ASSERT (get_stat_mtime_ns (&st2) < BILLION);
139 /* Play with UTIME_OMIT, UTIME_NOW. */
141 struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
142 ASSERT (func (BASE "link", ts) == 0);
143 ASSERT (lstat (BASE "link", &st2) == 0);
146 ASSERT (st2.st_atime == Y2K);
147 ASSERT (0 <= get_stat_atime_ns (&st2));
148 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
150 ASSERT (utimecmp (BASE "link", &st1, &st2, 0) <= 0);
153 /* Symlink to directory. */
154 ASSERT (unlink (BASE "link") == 0);
155 ASSERT (symlink (BASE "dir", BASE "link") == 0);
156 ASSERT (mkdir (BASE "dir", 0700) == 0);
158 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
159 ASSERT (func (BASE "link/", ts) == 0);
161 /* On cygwin 1.5, stat() changes atime of directories, so only check
163 ASSERT (stat (BASE "dir", &st1) == 0);
164 ASSERT (st1.st_mtime == Y2K);
165 ASSERT (lstat (BASE "link", &st1) == 0);
166 ASSERT (st1.st_atime != Y2K);
167 ASSERT (st1.st_mtime != Y2K);
168 ASSERT (func (BASE "link", NULL) == 0);
169 ASSERT (stat (BASE "dir", &st1) == 0);
170 ASSERT (st1.st_mtime == Y2K);
171 ASSERT (lstat (BASE "link", &st1) == 0);
172 ASSERT (st1.st_atime != Y2K);
173 ASSERT (st1.st_mtime != Y2K);
176 ASSERT (rmdir (BASE "dir") == 0);
177 ASSERT (unlink (BASE "link") == 0);