openat: don’t close (-1)
[gnulib.git] / tests / test-fdutimensat.c
blob5f8d0f6d071defb8aea018c2dc83b5da540a418b
1 /* Tests of fdutimensat.
2 Copyright (C) 2009-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
19 #include <config.h>
21 #include "utimens.h"
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "ignore-value.h"
28 #include "macros.h"
30 #define BASE "test-fdutimensat.t"
32 #include "test-futimens.h"
33 #include "test-lutimens.h"
34 #include "test-utimens.h"
36 static int dfd = AT_FDCWD;
38 /* Wrap fdutimensat to behave like futimens. */
39 static int
40 do_futimens (int fd, struct timespec const times[2])
42 return fdutimensat (fd, dfd, NULL, times, 0);
45 /* Test the use of file descriptors alongside a name. */
46 static int
47 do_fdutimens (char const *name, struct timespec const times[2])
49 int result;
50 int nofollow_result;
51 int nofollow_errno;
52 int fd = openat (dfd, name, O_WRONLY);
53 if (fd < 0)
54 fd = openat (dfd, name, O_RDONLY);
55 errno = 0;
56 nofollow_result = fdutimensat (fd, dfd, name, times, AT_SYMLINK_NOFOLLOW);
57 nofollow_errno = errno;
58 result = fdutimensat (fd, dfd, name, times, 0);
59 ASSERT (result == nofollow_result
60 || (nofollow_result == -1 && nofollow_errno == ENOSYS));
61 if (0 <= fd)
63 int saved_errno = errno;
64 close (fd);
65 errno = saved_errno;
67 return result;
70 /* Wrap lutimensat to behave like lutimens. */
71 static int
72 do_lutimens (const char *name, struct timespec const times[2])
74 return lutimensat (dfd, name, times);
77 /* Wrap fdutimensat to behave like lutimens. */
78 static int
79 do_lutimens1 (const char *name, struct timespec const times[2])
81 return fdutimensat (-1, dfd, name, times, AT_SYMLINK_NOFOLLOW);
84 /* Wrap fdutimensat to behave like utimens. */
85 static int
86 do_utimens (const char *name, struct timespec const times[2])
88 return fdutimensat (-1, dfd, name, times, 0);
91 int
92 main (void)
94 int result1; /* Skip because of no symlink support. */
95 int result2; /* Skip because of no futimens support. */
96 int result3; /* Skip because of no lutimens support. */
97 int fd;
99 /* Clean up any trash from prior testsuite runs. */
100 ignore_value (system ("rm -rf " BASE "*"));
102 /* Basic tests. */
103 result1 = test_utimens (do_utimens, true);
104 ASSERT (test_utimens (do_fdutimens, false) == result1);
105 result2 = test_futimens (do_futimens, result1 == 0);
106 result3 = test_lutimens (do_lutimens, (result1 + result2) == 0);
107 /* We expect 0/0, 0/77, or 77/77, but not 77/0. */
108 ASSERT (result1 <= result3);
109 ASSERT (test_lutimens (do_lutimens1, (result1 + result2) == 0) == result3);
110 dfd = open (".", O_RDONLY);
111 ASSERT (0 <= dfd);
112 ASSERT (test_utimens (do_utimens, false) == result1);
113 ASSERT (test_utimens (do_fdutimens, false) == result1);
114 ASSERT (test_futimens (do_futimens, false) == result2);
115 ASSERT (test_lutimens (do_lutimens, false) == result3);
116 ASSERT (test_lutimens (do_lutimens1, false) == result3);
118 /* Directory relative tests. */
119 ASSERT (mkdir (BASE "dir", 0700) == 0);
120 ASSERT (chdir (BASE "dir") == 0);
121 fd = open ("file", O_RDWR | O_CREAT | O_TRUNC, 0600);
122 ASSERT (0 <= fd);
123 errno = 0;
124 ASSERT (fdutimensat (AT_FDCWD, fd, ".", NULL, 0) == -1);
125 ASSERT (errno == ENOTDIR);
127 struct timespec ts[2];
128 struct stat st;
129 bool check_atime = checkable_atime (fd, &st);
130 ts[0].tv_sec = Y2K;
131 ts[0].tv_nsec = 0;
132 ts[1] = ts[0];
133 ASSERT (fdutimensat (fd, dfd, BASE "dir/file", ts, 0) == 0);
134 ASSERT (stat ("file", &st) == 0);
135 if (check_atime)
137 ASSERT (st.st_atime == Y2K);
138 ASSERT (get_stat_atime_ns (&st) == 0);
140 ASSERT (st.st_mtime == Y2K);
141 ASSERT (get_stat_mtime_ns (&st) == 0);
143 ASSERT (close (fd) == 0);
144 ASSERT (close (dfd) == 0);
145 errno = 0;
146 ASSERT (fdutimensat (-1, dfd, ".", NULL, 0) == -1);
147 ASSERT (errno == EBADF);
149 /* Cleanup. */
150 ASSERT (chdir ("..") == 0);
151 ASSERT (unlink (BASE "dir/file") == 0);
152 ASSERT (rmdir (BASE "dir") == 0);
153 int result = result1 | result2 | result3;
154 return (result ? result : test_exit_status);