2 * Simple test for validating mtime on a file create and set via utime.
15 void usage(char *prog)
17 fprintf(stderr, "usage: %s <filename>\n", prog);
21 int main(int argc, char *argv[])
23 long before_mknod, after_mknod;
24 long before_utime, after_utime;
25 const char *prog = argv[0];
26 const char *filename = argv[1];
33 /* Adjust the before time back one second, because the kernel's
34 * CURRENT_TIME (lockless clock reading, used to set inode times)
35 * may drift against the do_gettimeofday() time (TSC-corrected and
36 * locked clock reading, used to return timestamps to user space).
37 * This means that the mknod time could be a second older than the
38 * before time, even for a local filesystem such as ext3.
40 before_mknod = time(0) - 1;
41 rc = mknod(filename, 0700, S_IFREG);
42 after_mknod = time(0);
43 if (rc && errno != EEXIST) {
44 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
45 prog, filename, errno, strerror(errno));
48 rc = stat(filename, &st);
50 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
51 prog, filename, errno, strerror(errno));
55 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
57 "%s: bad mknod times %lu <= %lu <= %lu false\n",
58 prog, before_mknod, st.st_mtime, after_mknod);
62 printf("%s: good mknod times %lu%s <= %lu <= %lu\n",
63 prog, before_mknod, before_mknod == st.st_mtime ? "*":"",
64 st.st_mtime, after_mknod);
70 before_utime = time(0) - 1;
71 rc = utime(filename, NULL);
72 after_utime = time(0);
74 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
75 prog, filename, errno, strerror(errno));
79 rc = stat(filename, &st);
81 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
82 prog, filename, errno, strerror(errno));
86 if (st.st_mtime < before_utime || st.st_mtime > after_utime) {
87 fprintf(stderr, "%s: bad utime times %lu <= %lu <= %lu false\n",
88 prog, before_utime, st.st_mtime, after_utime);
92 printf("%s: good utime times %lu%s <= %lu <= %lu\n",
93 prog, before_utime, before_utime == st.st_mtime ? "*" : "",
94 st.st_mtime, after_utime);