Print this page
10076 make usr/src/test smatch clean
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/test/zfs-tests/tests/functional/ctime/ctime_001_pos.c
+++ new/usr/src/test/zfs-tests/tests/functional/ctime/ctime_001_pos.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
21 21
22 22 /*
23 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 28 * Copyright (c) 2013 by Delphix. All rights reserved.
29 29 */
30 30
31 +/*
32 + * Copyright (c) 2018, Joyent, Inc.
33 + */
31 34
32 35 #include <sys/types.h>
33 36 #include <sys/stat.h>
34 37 #include <utime.h>
35 38 #include <stdio.h>
36 39 #include <stdlib.h>
37 40 #include <unistd.h>
38 41 #include <strings.h>
39 42 #include <errno.h>
40 43 #include <fcntl.h>
41 44 #include <libgen.h>
42 45
43 46 #define ST_ATIME 0
44 47 #define ST_CTIME 1
45 48 #define ST_MTIME 2
46 49
47 50 #define ALL_MODE (mode_t)(S_IRWXU|S_IRWXG|S_IRWXO)
48 51
49 52 typedef struct timetest {
50 53 int type;
51 54 char *name;
52 55 int (*func)(const char *pfile);
53 56 } timetest_t;
54 57
55 58 static char tfile[BUFSIZ] = { 0 };
56 59
57 60 extern int errno;
58 61
59 62 /*
60 63 * DESCRIPTION:
61 64 * Verify time will be changed correctly after each operation.
62 65 *
63 66 * STRATEGY:
64 67 * 1. Define time test array.
65 68 * 2. Loop through each item in this array.
66 69 * 3. Verify the time is changed after each operation.
67 70 *
68 71 */
69 72
70 73 static int
71 74 get_file_time(const char *pfile, int what, time_t *ptr)
72 75 {
73 76 struct stat stat_buf;
74 77
75 78 if (pfile == NULL || ptr == NULL) {
76 79 return (-1);
77 80 }
78 81
79 82 if (stat(pfile, &stat_buf) == -1) {
80 83 return (-1);
81 84 }
82 85
83 86 switch (what) {
84 87 case ST_ATIME:
85 88 *ptr = stat_buf.st_atime;
86 89 return (0);
87 90 case ST_CTIME:
88 91 *ptr = stat_buf.st_ctime;
89 92 return (0);
90 93 case ST_MTIME:
91 94 *ptr = stat_buf.st_mtime;
92 95 return (0);
93 96 default:
94 97 return (-1);
95 98 }
96 99 }
97 100
98 101 static int
99 102 do_read(const char *pfile)
100 103 {
101 104 int fd, ret = 0;
102 105 char buf[BUFSIZ] = { 0 };
103 106
104 107 if (pfile == NULL) {
105 108 return (-1);
106 109 }
107 110
108 111 if ((fd = open(pfile, O_RDONLY, ALL_MODE)) == -1) {
109 112 return (-1);
110 113 }
111 114 if (read(fd, buf, sizeof (buf)) == -1) {
112 115 (void) fprintf(stderr, "read(%d, buf, %d) failed with errno "
113 116 "%d\n", fd, sizeof (buf), errno);
114 117 return (1);
115 118 }
116 119 (void) close(fd);
117 120
118 121 return (ret);
119 122 }
120 123
121 124 static int
122 125 do_write(const char *pfile)
123 126 {
124 127 int fd, ret = 0;
125 128 char buf[BUFSIZ] = "call function do_write()";
126 129
127 130 if (pfile == NULL) {
128 131 return (-1);
129 132 }
130 133
131 134 if ((fd = open(pfile, O_WRONLY, ALL_MODE)) == -1) {
132 135 return (-1);
133 136 }
134 137 if (write(fd, buf, strlen(buf)) == -1) {
135 138 (void) fprintf(stderr, "write(%d, buf, %d) failed with errno "
136 139 "%d\n", fd, strlen(buf), errno);
137 140 return (1);
138 141 }
139 142 (void) close(fd);
140 143
141 144 return (ret);
142 145 }
143 146
144 147 static int
145 148 do_link(const char *pfile)
146 149 {
147 150 int ret = 0;
148 151 char link_file[BUFSIZ] = { 0 };
149 152 char *dname;
150 153
151 154 if (pfile == NULL) {
152 155 return (-1);
153 156 }
154 157
155 158 /*
156 159 * Figure out source file directory name, and create
157 160 * the link file in the same directory.
158 161 */
159 162 dname = dirname(strdup(pfile));
160 163 (void) snprintf(link_file, BUFSIZ, "%s/%s", dname, "link_file");
161 164
162 165 if (link(pfile, link_file) == -1) {
163 166 (void) fprintf(stderr, "link(%s, %s) failed with errno %d\n",
164 167 pfile, link_file, errno);
165 168 free((void *)dirname);
166 169 return (1);
167 170 }
168 171
169 172 (void) unlink(link_file);
170 173 free((void *)dirname);
171 174 return (ret);
172 175 }
173 176
174 177 static int
175 178 do_creat(const char *pfile)
176 179 {
177 180 int fd, ret = 0;
178 181
179 182 if (pfile == NULL) {
180 183 return (-1);
181 184 }
182 185
183 186 if ((fd = creat(pfile, ALL_MODE)) == -1) {
184 187 (void) fprintf(stderr, "creat(%s, ALL_MODE) failed with errno "
185 188 "%d\n", pfile, errno);
186 189 return (1);
187 190 }
188 191 (void) close(fd);
189 192
190 193 return (ret);
191 194 }
192 195
193 196 static int
194 197 do_utime(const char *pfile)
195 198 {
196 199 int ret = 0;
197 200
198 201 if (pfile == NULL) {
199 202 return (-1);
200 203 }
201 204
202 205 /*
203 206 * Times of the file are set to the current time
204 207 */
205 208 if (utime(pfile, NULL) == -1) {
206 209 (void) fprintf(stderr, "utime(%s, NULL) failed with errno "
207 210 "%d\n", pfile, errno);
208 211 return (1);
209 212 }
210 213
211 214 return (ret);
212 215 }
213 216
214 217 static int
215 218 do_chmod(const char *pfile)
216 219 {
217 220 int ret = 0;
218 221
219 222 if (pfile == NULL) {
220 223 return (-1);
221 224 }
222 225
223 226 if (chmod(pfile, ALL_MODE) == -1) {
224 227 (void) fprintf(stderr, "chmod(%s, ALL_MODE) failed with "
225 228 "errno %d\n", pfile, errno);
226 229 return (1);
227 230 }
228 231
229 232 return (ret);
230 233 }
231 234
232 235 static int
233 236 do_chown(const char *pfile)
234 237 {
235 238 int ret = 0;
236 239
237 240 if (pfile == NULL) {
238 241 return (-1);
239 242 }
240 243
241 244 if (chown(pfile, getuid(), getgid()) == -1) {
242 245 (void) fprintf(stderr, "chown(%s, %d, %d) failed with errno "
243 246 "%d\n", pfile, (int)getuid(), (int)getgid(), errno);
244 247 return (1);
245 248 }
246 249
247 250 return (ret);
248 251 }
249 252
250 253 static void
251 254 cleanup(void)
252 255 {
253 256 if ((strlen(tfile) != 0) && (access(tfile, F_OK) == 0)) {
254 257 (void) unlink(tfile);
255 258 }
256 259 }
257 260
258 261 static timetest_t timetest_table[] = {
259 262 { ST_ATIME, "st_atime", do_read },
260 263 { ST_ATIME, "st_atime", do_utime },
261 264 { ST_MTIME, "st_mtime", do_creat },
262 265 { ST_MTIME, "st_mtime", do_write },
263 266 { ST_MTIME, "st_mtime", do_utime },
264 267 { ST_CTIME, "st_ctime", do_creat },
265 268 { ST_CTIME, "st_ctime", do_write },
266 269 { ST_CTIME, "st_ctime", do_chmod },
267 270 { ST_CTIME, "st_ctime", do_chown },
268 271 { ST_CTIME, "st_ctime", do_link },
269 272 { ST_CTIME, "st_ctime", do_utime },
270 273 };
271 274
272 275 #define NCOMMAND (sizeof (timetest_table) / sizeof (timetest_table[0]))
273 276
274 277 /* ARGSUSED */
275 278 int
276 279 main(int argc, char *argv[])
277 280 {
278 281 int i, ret, fd;
279 282 char *penv[] = {"TESTDIR", "TESTFILE0"};
280 283
281 284 (void) fprintf(stdout, "Verify [acm]time is modified appropriately.\n");
282 285 (void) atexit(cleanup);
283 286
284 287 /*
285 288 * Get the environment variable values.
286 289 */
287 290 for (i = 0; i < sizeof (penv) / sizeof (char *); i++) {
288 291 if ((penv[i] = getenv(penv[i])) == NULL) {
289 292 (void) fprintf(stderr, "getenv(penv[%d])\n", i);
290 293 return (1);
291 294 }
292 295 }
293 296 (void) snprintf(tfile, sizeof (tfile), "%s/%s", penv[0], penv[1]);
294 297
295 298 /*
296 299 * If the test file is exists, remove it first.
297 300 */
298 301 if (access(tfile, F_OK) == 0) {
299 302 (void) unlink(tfile);
300 303 }
301 304 ret = 0;
302 305 if ((fd = open(tfile, O_WRONLY | O_CREAT | O_TRUNC, ALL_MODE)) == -1) {
303 306 (void) fprintf(stderr, "open(%s) failed: %d\n", tfile, errno);
304 307 return (1);
305 308 }
306 309 (void) close(fd);
307 310
308 311 for (i = 0; i < NCOMMAND; i++) {
309 312 time_t t1, t2;
310 313
311 314 /*
312 315 * Get original time before operating.
313 316 */
314 317 ret = get_file_time(tfile, timetest_table[i].type, &t1);
↓ open down ↓ |
274 lines elided |
↑ open up ↑ |
315 318 if (ret != 0) {
316 319 (void) fprintf(stderr, "get_file_time(%s %d) = %d\n",
317 320 tfile, timetest_table[i].type, ret);
318 321 return (1);
319 322 }
320 323
321 324 /*
322 325 * Sleep 2 seconds, then invoke command on given file
323 326 */
324 327 (void) sleep(2);
325 - timetest_table[i].func(tfile);
328 + (void) timetest_table[i].func(tfile);
326 329
327 330 /*
328 331 * Get time after operating.
329 332 */
330 333 ret = get_file_time(tfile, timetest_table[i].type, &t2);
331 334 if (ret != 0) {
332 335 (void) fprintf(stderr, "get_file_time(%s %d) = %d\n",
333 336 tfile, timetest_table[i].type, ret);
334 337 return (1);
335 338 }
336 339
337 340 if (t1 == t2) {
338 341 (void) fprintf(stderr, "%s: t1(%ld) == t2(%ld)\n",
339 342 timetest_table[i].name, (long)t1, (long)t2);
340 343 return (1);
341 344 } else {
342 345 (void) fprintf(stderr, "%s: t1(%ld) != t2(%ld)\n",
343 346 timetest_table[i].name, (long)t1, (long)t2);
344 347 }
345 348 }
346 349
347 350 (void) fprintf(stdout, "PASS\n");
348 351 return (0);
349 352 }
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX