4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2013 by Delphix. All rights reserved.
32 #include <sys/types.h>
35 #include <sys/xattr.h>
51 #define ALL_MODE (mode_t)(S_IRWXU|S_IRWXG|S_IRWXO)
53 typedef struct timetest {
56 int (*func)(const char *pfile);
59 static char tfile[BUFSIZ] = { 0 };
63 * Verify time will be changed correctly after each operation.
66 * 1. Define time test array.
67 * 2. Loop through each item in this array.
68 * 3. Verify the time is changed after each operation.
73 get_file_time(const char *pfile, int what, time_t *ptr)
77 if (pfile == NULL || ptr == NULL) {
81 if (stat(pfile, &stat_buf) == -1) {
87 *ptr = stat_buf.st_atime;
90 *ptr = stat_buf.st_ctime;
93 *ptr = stat_buf.st_mtime;
101 do_read(const char *pfile)
104 char buf[BUFSIZ] = { 0 };
110 if ((fd = open(pfile, O_RDONLY, ALL_MODE)) == -1) {
113 if (read(fd, buf, sizeof (buf)) == -1) {
114 (void) fprintf(stderr, "read(%d, buf, %zd) failed with errno "
115 "%d\n", fd, sizeof (buf), errno);
125 do_write(const char *pfile)
128 char buf[BUFSIZ] = "call function do_write()";
134 if ((fd = open(pfile, O_WRONLY, ALL_MODE)) == -1) {
137 if (write(fd, buf, strlen(buf)) == -1) {
138 (void) fprintf(stderr, "write(%d, buf, %d) failed with errno "
139 "%d\n", fd, (int)strlen(buf), errno);
149 do_link(const char *pfile)
152 char link_file[BUFSIZ] = { 0 };
153 char pfile_copy[BUFSIZ] = { 0 };
160 strncpy(pfile_copy, pfile, sizeof (pfile_copy)-1);
161 pfile_copy[sizeof (pfile_copy) - 1] = '\0';
163 * Figure out source file directory name, and create
164 * the link file in the same directory.
166 dname = dirname((char *)pfile_copy);
167 (void) snprintf(link_file, BUFSIZ, "%s/%s", dname, "link_file");
169 if (link(pfile, link_file) == -1) {
170 (void) fprintf(stderr, "link(%s, %s) failed with errno %d\n",
171 pfile, link_file, errno);
175 (void) unlink(link_file);
181 do_creat(const char *pfile)
189 if ((fd = creat(pfile, ALL_MODE)) == -1) {
190 (void) fprintf(stderr, "creat(%s, ALL_MODE) failed with errno "
191 "%d\n", pfile, errno);
200 do_utime(const char *pfile)
209 * Times of the file are set to the current time
211 if (utime(pfile, NULL) == -1) {
212 (void) fprintf(stderr, "utime(%s, NULL) failed with errno "
213 "%d\n", pfile, errno);
221 do_chmod(const char *pfile)
229 if (chmod(pfile, ALL_MODE) == -1) {
230 (void) fprintf(stderr, "chmod(%s, ALL_MODE) failed with "
231 "errno %d\n", pfile, errno);
239 do_chown(const char *pfile)
247 if (chown(pfile, getuid(), getgid()) == -1) {
248 (void) fprintf(stderr, "chown(%s, %d, %d) failed with errno "
249 "%d\n", pfile, (int)getuid(), (int)getgid(), errno);
258 do_xattr(const char *pfile)
261 char *value = "user.value";
267 if (setxattr(pfile, "user.x", value, strlen(value), 0) == -1) {
268 (void) fprintf(stderr, "setxattr(%s, %d, %d) failed with errno "
269 "%d\n", pfile, (int)getuid(), (int)getgid(), errno);
279 if ((strlen(tfile) != 0) && (access(tfile, F_OK) == 0)) {
280 (void) unlink(tfile);
284 static timetest_t timetest_table[] = {
285 { ST_ATIME, "st_atime", do_read },
286 { ST_ATIME, "st_atime", do_utime },
287 { ST_MTIME, "st_mtime", do_creat },
288 { ST_MTIME, "st_mtime", do_write },
289 { ST_MTIME, "st_mtime", do_utime },
290 { ST_CTIME, "st_ctime", do_creat },
291 { ST_CTIME, "st_ctime", do_write },
292 { ST_CTIME, "st_ctime", do_chmod },
293 { ST_CTIME, "st_ctime", do_chown },
294 { ST_CTIME, "st_ctime", do_link },
295 { ST_CTIME, "st_ctime", do_utime },
297 { ST_CTIME, "st_ctime", do_xattr },
301 #define NCOMMAND (sizeof (timetest_table) / sizeof (timetest_table[0]))
305 main(int argc, char *argv[])
308 char *penv[] = {"TESTDIR", "TESTFILE0"};
310 (void) atexit(cleanup);
313 * Get the environment variable values.
315 for (i = 0; i < sizeof (penv) / sizeof (char *); i++) {
316 if ((penv[i] = getenv(penv[i])) == NULL) {
317 (void) fprintf(stderr, "getenv(penv[%d])\n", i);
321 (void) snprintf(tfile, sizeof (tfile), "%s/%s", penv[0], penv[1]);
324 * If the test file is exists, remove it first.
326 if (access(tfile, F_OK) == 0) {
327 (void) unlink(tfile);
330 if ((fd = open(tfile, O_WRONLY | O_CREAT | O_TRUNC, ALL_MODE)) == -1) {
331 (void) fprintf(stderr, "open(%s) failed: %d\n", tfile, errno);
336 for (i = 0; i < NCOMMAND; i++) {
340 * Get original time before operating.
342 ret = get_file_time(tfile, timetest_table[i].type, &t1);
344 (void) fprintf(stderr, "get_file_time(%s %d) = %d\n",
345 tfile, timetest_table[i].type, ret);
350 * Sleep 2 seconds, then invoke command on given file
353 timetest_table[i].func(tfile);
356 * Get time after operating.
358 ret = get_file_time(tfile, timetest_table[i].type, &t2);
360 (void) fprintf(stderr, "get_file_time(%s %d) = %d\n",
361 tfile, timetest_table[i].type, ret);
366 (void) fprintf(stderr, "%s: t1(%ld) == t2(%ld)\n",
367 timetest_table[i].name, (long)t1, (long)t2);
370 (void) fprintf(stderr, "%s: t1(%ld) != t2(%ld)\n",
371 timetest_table[i].name, (long)t1, (long)t2);