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>
47 #define ALL_MODE (mode_t)(S_IRWXU|S_IRWXG|S_IRWXO)
49 typedef struct timetest
{
52 int (*func
)(const char *pfile
);
55 static char tfile
[BUFSIZ
] = { 0 };
61 * Verify time will be changed correctly after each operation.
64 * 1. Define time test array.
65 * 2. Loop through each item in this array.
66 * 3. Verify the time is changed after each operation.
71 get_file_time(const char *pfile
, int what
, time_t *ptr
)
75 if (pfile
== NULL
|| ptr
== NULL
) {
79 if (stat(pfile
, &stat_buf
) == -1) {
85 *ptr
= stat_buf
.st_atime
;
88 *ptr
= stat_buf
.st_ctime
;
91 *ptr
= stat_buf
.st_mtime
;
99 do_read(const char *pfile
)
102 char buf
[BUFSIZ
] = { 0 };
108 if ((fd
= open(pfile
, O_RDONLY
, ALL_MODE
)) == -1) {
111 if (read(fd
, buf
, sizeof (buf
)) == -1) {
112 (void) fprintf(stderr
, "read(%d, buf, %d) failed with errno "
113 "%d\n", fd
, sizeof (buf
), errno
);
122 do_write(const char *pfile
)
125 char buf
[BUFSIZ
] = "call function do_write()";
131 if ((fd
= open(pfile
, O_WRONLY
, ALL_MODE
)) == -1) {
134 if (write(fd
, buf
, strlen(buf
)) == -1) {
135 (void) fprintf(stderr
, "write(%d, buf, %d) failed with errno "
136 "%d\n", fd
, strlen(buf
), errno
);
145 do_link(const char *pfile
)
148 char link_file
[BUFSIZ
] = { 0 };
156 * Figure out source file directory name, and create
157 * the link file in the same directory.
159 dname
= dirname(strdup(pfile
));
160 (void) snprintf(link_file
, BUFSIZ
, "%s/%s", dname
, "link_file");
162 if (link(pfile
, link_file
) == -1) {
163 (void) fprintf(stderr
, "link(%s, %s) failed with errno %d\n",
164 pfile
, link_file
, errno
);
165 free((void *)dirname
);
169 (void) unlink(link_file
);
170 free((void *)dirname
);
175 do_creat(const char *pfile
)
183 if ((fd
= creat(pfile
, ALL_MODE
)) == -1) {
184 (void) fprintf(stderr
, "creat(%s, ALL_MODE) failed with errno "
185 "%d\n", pfile
, errno
);
194 do_utime(const char *pfile
)
203 * Times of the file are set to the current time
205 if (utime(pfile
, NULL
) == -1) {
206 (void) fprintf(stderr
, "utime(%s, NULL) failed with errno "
207 "%d\n", pfile
, errno
);
215 do_chmod(const char *pfile
)
223 if (chmod(pfile
, ALL_MODE
) == -1) {
224 (void) fprintf(stderr
, "chmod(%s, ALL_MODE) failed with "
225 "errno %d\n", pfile
, errno
);
233 do_chown(const char *pfile
)
241 if (chown(pfile
, getuid(), getgid()) == -1) {
242 (void) fprintf(stderr
, "chown(%s, %d, %d) failed with errno "
243 "%d\n", pfile
, (int)getuid(), (int)getgid(), errno
);
253 if ((strlen(tfile
) != 0) && (access(tfile
, F_OK
) == 0)) {
254 (void) unlink(tfile
);
258 static timetest_t timetest_table
[] = {
259 { ST_ATIME
, "st_atime", do_read
},
260 { ST_ATIME
, "st_atime", do_utime
},
261 { ST_MTIME
, "st_mtime", do_creat
},
262 { ST_MTIME
, "st_mtime", do_write
},
263 { ST_MTIME
, "st_mtime", do_utime
},
264 { ST_CTIME
, "st_ctime", do_creat
},
265 { ST_CTIME
, "st_ctime", do_write
},
266 { ST_CTIME
, "st_ctime", do_chmod
},
267 { ST_CTIME
, "st_ctime", do_chown
},
268 { ST_CTIME
, "st_ctime", do_link
},
269 { ST_CTIME
, "st_ctime", do_utime
},
272 #define NCOMMAND (sizeof (timetest_table) / sizeof (timetest_table[0]))
276 main(int argc
, char *argv
[])
279 char *penv
[] = {"TESTDIR", "TESTFILE0"};
281 (void) fprintf(stdout
, "Verify [acm]time is modified appropriately.\n");
282 (void) atexit(cleanup
);
285 * Get the environment variable values.
287 for (i
= 0; i
< sizeof (penv
) / sizeof (char *); i
++) {
288 if ((penv
[i
] = getenv(penv
[i
])) == NULL
) {
289 (void) fprintf(stderr
, "getenv(penv[%d])\n", i
);
293 (void) snprintf(tfile
, sizeof (tfile
), "%s/%s", penv
[0], penv
[1]);
296 * If the test file is exists, remove it first.
298 if (access(tfile
, F_OK
) == 0) {
299 (void) unlink(tfile
);
302 if ((fd
= open(tfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, ALL_MODE
)) == -1) {
303 (void) fprintf(stderr
, "open(%s) failed: %d\n", tfile
, errno
);
308 for (i
= 0; i
< NCOMMAND
; i
++) {
312 * Get original time before operating.
314 ret
= get_file_time(tfile
, timetest_table
[i
].type
, &t1
);
316 (void) fprintf(stderr
, "get_file_time(%s %d) = %d\n",
317 tfile
, timetest_table
[i
].type
, ret
);
322 * Sleep 2 seconds, then invoke command on given file
325 timetest_table
[i
].func(tfile
);
328 * Get time after operating.
330 ret
= get_file_time(tfile
, timetest_table
[i
].type
, &t2
);
332 (void) fprintf(stderr
, "get_file_time(%s %d) = %d\n",
333 tfile
, timetest_table
[i
].type
, ret
);
338 (void) fprintf(stderr
, "%s: t1(%ld) == t2(%ld)\n",
339 timetest_table
[i
].name
, (long)t1
, (long)t2
);
342 (void) fprintf(stderr
, "%s: t1(%ld) != t2(%ld)\n",
343 timetest_table
[i
].name
, (long)t1
, (long)t2
);
347 (void) fprintf(stdout
, "PASS\n");