1 /* Tests of fdutimensat.
2 Copyright (C) 2009, 2010 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 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
28 #include "ignore-value.h"
31 #define BASE "test-fdutimensat.t"
33 #include "test-futimens.h"
34 #include "test-lutimens.h"
35 #include "test-utimens.h"
37 static int dfd = AT_FDCWD;
39 /* Wrap fdutimensat to behave like futimens. */
41 do_futimens (int fd, struct timespec const times[2])
43 return fdutimensat (dfd, NULL, fd, times, 0);
46 /* Test the use of file descriptors alongside a name. */
48 do_fdutimens (char const *name, struct timespec const times[2])
51 int fd = openat (dfd, name, O_WRONLY);
53 fd = openat (dfd, name, O_RDONLY);
55 result = fdutimensat (dfd, name, fd, times, 0);
58 int saved_errno = errno;
65 /* Wrap lutimensat to behave like lutimens. */
67 do_lutimens (const char *name, struct timespec const times[2])
69 return lutimensat (dfd, name, times);
72 /* Wrap fdutimensat to behave like lutimens. */
74 do_lutimens1 (const char *name, struct timespec const times[2])
76 return fdutimensat (dfd, name, -1, times, AT_SYMLINK_NOFOLLOW);
79 /* Wrap fdutimensat to behave like utimens. */
81 do_utimens (const char *name, struct timespec const times[2])
83 return fdutimensat (dfd, name, -1, times, 0);
89 int result1; /* Skip because of no symlink support. */
90 int result2; /* Skip because of no futimens support. */
91 int result3; /* Skip because of no lutimens support. */
94 /* Clean up any trash from prior testsuite runs. */
95 ignore_value (system ("rm -rf " BASE "*"));
98 result1 = test_utimens (do_utimens, true);
99 ASSERT (test_utimens (do_fdutimens, false) == result1);
100 result2 = test_futimens (do_futimens, result1 == 0);
101 result3 = test_lutimens (do_lutimens, (result1 + result2) == 0);
102 /* We expect 0/0, 0/77, or 77/77, but not 77/0. */
103 ASSERT (result1 <= result3);
104 ASSERT (test_lutimens (do_lutimens1, (result1 + result2) == 0) == result3);
105 dfd = open (".", O_RDONLY);
107 ASSERT (test_utimens (do_utimens, false) == result1);
108 ASSERT (test_utimens (do_fdutimens, false) == result1);
109 ASSERT (test_futimens (do_futimens, false) == result2);
110 ASSERT (test_lutimens (do_lutimens, false) == result3);
111 ASSERT (test_lutimens (do_lutimens1, false) == result3);
113 /* Directory relative tests. */
114 ASSERT (mkdir (BASE "dir", 0700) == 0);
115 ASSERT (chdir (BASE "dir") == 0);
116 fd = creat ("file", 0600);
119 ASSERT (fdutimensat (fd, ".", fd, NULL, AT_SYMLINK_NOFOLLOW) == -1);
120 ASSERT (errno == EINVAL);
122 ASSERT (fdutimensat (fd, ".", AT_FDCWD, NULL, 0) == -1);
123 ASSERT (errno == ENOTDIR);
125 struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } };
127 ASSERT (fdutimensat (dfd, BASE "dir/file", fd, ts, 0) == 0);
128 ASSERT (stat ("file", &st) == 0);
129 ASSERT (st.st_atime == Y2K);
130 ASSERT (get_stat_atime_ns (&st) == 0);
131 ASSERT (st.st_mtime == Y2K);
132 ASSERT (get_stat_mtime_ns (&st) == 0);
134 ASSERT (close (fd) == 0);
135 ASSERT (close (dfd) == 0);
137 ASSERT (fdutimensat (dfd, ".", -1, NULL, 0) == -1);
138 ASSERT (errno == EBADF);
141 ASSERT (chdir ("..") == 0);
142 ASSERT (unlink (BASE "dir/file") == 0);
143 ASSERT (rmdir (BASE "dir") == 0);
144 return result1 | result2 | result3;