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)
29 bool atime_supported = true;
31 if (symlink ("nowhere", BASE "link"))
34 fputs ("skipping test: symlinks not supported on this file system\n",
39 result = func (BASE "link", NULL);
40 if (result == -1 && errno == ENOSYS)
42 ASSERT (unlink (BASE "link") == 0);
44 fputs ("skipping test: "
45 "setting symlink time not supported on this file system\n",
50 ASSERT (lstat (BASE "link", &st1) == 0);
51 /* On cygwin, lstat() changes atime of symlinks, so that lutimens
52 can only effectively modify mtime. */
54 ASSERT (lstat (BASE "link", &st2) == 0);
55 if (st1.st_atime != st2.st_atime
56 || get_stat_atime_ns (&st1) != get_stat_atime_ns (&st2))
57 atime_supported = false;
59 /* Invalid arguments. */
61 ASSERT (func ("no_such", NULL) == -1);
62 ASSERT (errno == ENOENT);
64 ASSERT (func ("", NULL) == -1);
65 ASSERT (errno == ENOENT);
67 struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
69 ASSERT (func (BASE "link", ts) == -1);
70 ASSERT (errno == EINVAL);
73 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
75 ASSERT (func (BASE "link", ts) == -1);
76 ASSERT (errno == EINVAL);
78 ASSERT (lstat (BASE "link", &st2) == 0);
81 ASSERT (st1.st_atime == st2.st_atime);
82 ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
84 ASSERT (utimecmp (BASE "link", &st1, &st2, 0) == 0);
88 struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
89 ASSERT (func (BASE "link", ts) == 0);
90 ASSERT (lstat (BASE "link", &st2) == 0);
93 ASSERT (st2.st_atime == Y2K);
94 ASSERT (0 <= get_stat_atime_ns (&st2));
95 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
97 ASSERT (st2.st_mtime == Y2K);
98 ASSERT (0 <= get_stat_mtime_ns (&st2));
99 ASSERT (get_stat_mtime_ns (&st2) < BILLION);
102 /* Play with UTIME_OMIT, UTIME_NOW. */
104 struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
105 ASSERT (func (BASE "link", ts) == 0);
106 ASSERT (lstat (BASE "link", &st2) == 0);
109 ASSERT (st2.st_atime == Y2K);
110 ASSERT (0 <= get_stat_atime_ns (&st2));
111 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
113 ASSERT (utimecmp (BASE "link", &st1, &st2, 0) <= 0);
117 ASSERT (unlink (BASE "link") == 0);