]>
Commit | Line | Data |
---|---|---|
5ba3f43e A |
1 | /* $NetBSD: t_utimensat.c,v 1.6 2017/01/10 15:13:56 christos Exp $ */ |
2 | ||
3 | /*- | |
4 | * Copyright (c) 2012 The NetBSD Foundation, Inc. | |
5 | * All rights reserved. | |
6 | * | |
7 | * This code is derived from software contributed to The NetBSD Foundation | |
8 | * by Emmanuel Dreyfus. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | #include <sys/cdefs.h> | |
32 | __RCSID("$NetBSD: t_utimensat.c,v 1.6 2017/01/10 15:13:56 christos Exp $"); | |
33 | ||
34 | #include <sys/param.h> | |
35 | #include <sys/stat.h> | |
36 | #include <sys/time.h> | |
37 | #include <errno.h> | |
38 | #include <fcntl.h> | |
39 | #include <limits.h> | |
40 | #include <paths.h> | |
41 | #include <stdio.h> | |
42 | #include <string.h> | |
43 | #include <unistd.h> | |
44 | ||
45 | #include <darwintest.h> | |
46 | #include <darwintest_utils.h> | |
47 | ||
a39ff7e2 A |
48 | #define DIRPATH "dir" |
49 | #define FILEPATH "dir/utimensat" | |
5ba3f43e A |
50 | #define BASEFILE "utimensat" |
51 | #define LINK "dir/symlink" | |
52 | #define BASELINK "symlink" | |
53 | #define FILEERR "dir/symlink" | |
54 | ||
55 | static const struct timespec tptr[] = { | |
56 | { 0x12345678, 987654321 }, | |
57 | { 0x15263748, 123456789 }, | |
58 | }; | |
59 | ||
60 | static void chtmpdir(void) | |
61 | { | |
62 | T_SETUPBEGIN; | |
63 | T_ASSERT_POSIX_ZERO(chdir(dt_tmpdir()), NULL); | |
64 | ||
65 | // <rdar://problem/31780295> dt_tmpdir() should guarantee a clean directory for each run | |
a39ff7e2 | 66 | unlink(FILEPATH); |
5ba3f43e | 67 | unlink(LINK); |
a39ff7e2 A |
68 | rmdir(DIRPATH); |
69 | ||
70 | // Skip the test if the current working directory is not on APFS. | |
71 | struct statfs sfs = { 0 }; | |
72 | T_QUIET; T_ASSERT_POSIX_SUCCESS(statfs(".", &sfs), NULL); | |
73 | if (memcmp(&sfs.f_fstypename[0], "apfs", strlen("apfs")) != 0) { | |
74 | T_SKIP("utimensat is APFS-only, but working directory is non-APFS"); | |
75 | } | |
5ba3f43e A |
76 | |
77 | T_SETUPEND; | |
78 | } | |
79 | ||
80 | T_DECL(netbsd_utimensat_fd, "See that utimensat works with fd") | |
81 | { | |
82 | chtmpdir(); | |
83 | ||
84 | int dfd; | |
85 | int fd; | |
86 | struct stat st; | |
87 | ||
a39ff7e2 A |
88 | T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL); |
89 | T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT|O_RDWR, 0644)), NULL); | |
5ba3f43e A |
90 | T_ASSERT_POSIX_ZERO(close(fd), NULL); |
91 | ||
a39ff7e2 | 92 | T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL); |
5ba3f43e A |
93 | T_ASSERT_POSIX_ZERO(utimensat(dfd, BASEFILE, tptr, 0), NULL); |
94 | T_ASSERT_POSIX_ZERO(close(dfd), NULL); | |
95 | ||
a39ff7e2 | 96 | T_ASSERT_POSIX_ZERO(stat(FILEPATH, &st), NULL); |
5ba3f43e A |
97 | T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL); |
98 | T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL); | |
99 | T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL); | |
100 | T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL); | |
101 | } | |
102 | ||
103 | T_DECL(netbsd_utimensat_fdcwd, "See that utimensat works with fd as AT_FDCWD") | |
104 | { | |
105 | chtmpdir(); | |
106 | ||
107 | int fd; | |
108 | struct stat st; | |
109 | ||
a39ff7e2 A |
110 | T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL); |
111 | T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT|O_RDWR, 0644)), NULL); | |
5ba3f43e A |
112 | T_ASSERT_POSIX_ZERO(close(fd), NULL); |
113 | ||
a39ff7e2 | 114 | T_ASSERT_POSIX_ZERO(chdir(DIRPATH), NULL); |
5ba3f43e A |
115 | T_ASSERT_POSIX_ZERO(utimensat(AT_FDCWD, BASEFILE, tptr, 0), NULL); |
116 | ||
117 | T_ASSERT_POSIX_ZERO(stat(BASEFILE, &st), NULL); | |
118 | T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL); | |
119 | T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL); | |
120 | T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL); | |
121 | T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL); | |
122 | } | |
123 | ||
124 | T_DECL(netbsd_utimensat_fdcwderr, "See that utimensat fails with fd as AT_FDCWD and bad path") | |
125 | { | |
126 | chtmpdir(); | |
127 | ||
a39ff7e2 | 128 | T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL); |
5ba3f43e A |
129 | T_ASSERT_EQ(utimensat(AT_FDCWD, FILEERR, tptr, 0), -1, NULL); |
130 | } | |
131 | ||
132 | T_DECL(netbsd_utimensat_fderr1, "See that utimensat fail with bad path") | |
133 | { | |
134 | chtmpdir(); | |
135 | ||
136 | int dfd; | |
137 | ||
a39ff7e2 A |
138 | T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL); |
139 | T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL); | |
5ba3f43e A |
140 | T_ASSERT_EQ(utimensat(dfd, FILEERR, tptr, 0), -1, NULL); |
141 | T_ASSERT_POSIX_ZERO(close(dfd), NULL); | |
142 | } | |
143 | ||
144 | T_DECL(netbsd_utimensat_fderr2, "See that utimensat fails with bad fdat") | |
145 | { | |
146 | chtmpdir(); | |
147 | ||
148 | int dfd; | |
149 | int fd; | |
150 | char cwd[MAXPATHLEN]; | |
151 | ||
a39ff7e2 A |
152 | T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL); |
153 | T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT|O_RDWR, 0644)), NULL); | |
5ba3f43e A |
154 | T_ASSERT_POSIX_ZERO(close(fd), NULL); |
155 | ||
156 | T_ASSERT_POSIX_SUCCESS((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)), NULL); | |
157 | T_ASSERT_EQ(utimensat(dfd, BASEFILE, tptr, 0), -1, NULL); | |
158 | T_ASSERT_POSIX_ZERO(close(dfd), NULL); | |
159 | } | |
160 | ||
161 | T_DECL(netbsd_utimensat_fderr3, "See that utimensat fails with fd as -1") | |
162 | { | |
163 | chtmpdir(); | |
164 | ||
165 | int fd; | |
166 | ||
a39ff7e2 A |
167 | T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL); |
168 | T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT|O_RDWR, 0644)), NULL); | |
5ba3f43e A |
169 | T_ASSERT_POSIX_ZERO(close(fd), NULL); |
170 | ||
a39ff7e2 | 171 | T_ASSERT_EQ(utimensat(-1, FILEPATH, tptr, 0), -1, NULL); |
5ba3f43e A |
172 | } |
173 | ||
174 | T_DECL(netbsd_utimensat_fdlink, "See that utimensat works on symlink") | |
175 | { | |
176 | chtmpdir(); | |
177 | ||
178 | int dfd; | |
179 | struct stat st; | |
180 | ||
a39ff7e2 A |
181 | T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL); |
182 | T_ASSERT_POSIX_ZERO(symlink(FILEPATH, LINK), NULL); /* NB: FILE does not exists */ | |
5ba3f43e | 183 | |
a39ff7e2 | 184 | T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL); |
5ba3f43e A |
185 | |
186 | T_ASSERT_EQ(utimensat(dfd, BASELINK, tptr, 0), -1, NULL); | |
187 | T_ASSERT_EQ(errno, ENOENT, NULL); | |
188 | ||
189 | T_ASSERT_POSIX_ZERO(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW), NULL); | |
190 | ||
191 | T_ASSERT_POSIX_ZERO(close(dfd), NULL); | |
192 | ||
193 | T_ASSERT_POSIX_ZERO(lstat(LINK, &st), NULL); | |
194 | T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL); | |
195 | T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL); | |
196 | T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL); | |
197 | T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL); | |
198 | } |