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 ("", NULL) == -1);
38 ASSERT (errno == ENOENT);
39 ASSERT (close (creat (BASE "file", 0600)) == 0);
40 ASSERT (stat (BASE "file", &st1) == 0);
41 ASSERT (st1.st_atime != Y2K);
42 ASSERT (st1.st_mtime != Y2K);
44 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
45 ASSERT (func (BASE "file", ts) == 0);
47 ASSERT (stat (BASE "file", &st1) == 0);
48 ASSERT (st1.st_atime == Y2K);
49 ASSERT (st1.st_mtime == Y2K);
51 /* Play with symlink timestamps. */
52 if (symlink (BASE "file", BASE "link"))
54 ASSERT (unlink (BASE "file") == 0);
56 fputs ("skipping test: symlinks not supported on this file system\n",
61 result = func (BASE "link", NULL);
63 /* Make sure we did not reference through link by accident. */
64 ASSERT (stat (BASE "file", &st1) == 0);
65 ASSERT (st1.st_atime == Y2K);
66 ASSERT (st1.st_mtime == Y2K);
67 ASSERT (lstat (BASE "link", &st1) == 0);
68 ASSERT (st1.st_atime != Y2K);
69 ASSERT (st1.st_mtime != Y2K);
70 ASSERT (unlink (BASE "file") == 0);
71 if (result == -1 && saved_errno == ENOSYS)
73 ASSERT (unlink (BASE "link") == 0);
75 fputs ("skipping test: "
76 "setting symlink time not supported on this file system\n",
81 ASSERT (lstat (BASE "link", &st1) == 0);
82 /* On cygwin, lstat() changes atime of symlinks, so that lutimens
83 can only effectively modify mtime. */
85 ASSERT (lstat (BASE "link", &st2) == 0);
86 if (st1.st_atime != st2.st_atime
87 || get_stat_atime_ns (&st1) != get_stat_atime_ns (&st2))
88 atime_supported = false;
90 /* Invalid arguments. */
92 struct timespec ts[2] = { { Y2K, UTIME_BOGUS_POS }, { Y2K, 0 } };
94 ASSERT (func (BASE "link", ts) == -1);
95 ASSERT (errno == EINVAL);
98 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, UTIME_BOGUS_NEG } };
100 ASSERT (func (BASE "link", ts) == -1);
101 ASSERT (errno == EINVAL);
103 ASSERT (lstat (BASE "link", &st2) == 0);
106 ASSERT (st1.st_atime == st2.st_atime);
107 ASSERT (get_stat_atime_ns (&st1) == get_stat_atime_ns (&st2));
109 ASSERT (utimecmp (BASE "link", &st1, &st2, 0) == 0);
111 /* Set both times. */
113 struct timespec ts[2] = { { Y2K, BILLION / 2 - 1 }, { Y2K, BILLION - 1 } };
114 ASSERT (func (BASE "link", ts) == 0);
115 ASSERT (lstat (BASE "link", &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);
127 /* Play with UTIME_OMIT, UTIME_NOW. */
129 struct timespec ts[2] = { { BILLION, UTIME_OMIT }, { 0, UTIME_NOW } };
130 ASSERT (func (BASE "link", ts) == 0);
131 ASSERT (lstat (BASE "link", &st2) == 0);
134 ASSERT (st2.st_atime == Y2K);
135 ASSERT (0 <= get_stat_atime_ns (&st2));
136 ASSERT (get_stat_atime_ns (&st2) < BILLION / 2);
138 ASSERT (utimecmp (BASE "link", &st1, &st2, 0) <= 0);
142 ASSERT (unlink (BASE "link") == 0);