1 /*
2 * Copyright (c) 2019 Darren Tucker
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #define TMPFILE "utimensat.tmp"
28 #define TMPFILE2 "utimensat.tmp2"
29
30 #ifndef AT_SYMLINK_NOFOLLOW
31 # define AT_SYMLINK_NOFOLLOW 0x80000000
32 #endif
33
34 int utimensat(int, const char *, const struct timespec[2], int);
35
36 static void
cleanup(void)37 cleanup(void)
38 {
39 (void)unlink(TMPFILE);
40 (void)unlink(TMPFILE2);
41 }
42
43 static void
fail(char * msg,long expect,long got)44 fail(char *msg, long expect, long got)
45 {
46 int saved_errno = errno;
47
48 if (expect == got && got == 0)
49 fprintf(stderr, "utimensat: %s: %s\n", msg,
50 strerror(saved_errno));
51 else
52 fprintf(stderr, "utimensat: %s: expected %ld got %ld\n",
53 msg, expect, got);
54 cleanup();
55 exit(1);
56 }
57
58 int
main(void)59 main(void)
60 {
61 int fd;
62 struct stat sb;
63 struct timespec ts[2];
64
65 cleanup();
66 if ((fd = open(TMPFILE, O_CREAT, 0600)) == -1)
67 fail("open", 0, 0);
68 close(fd);
69
70 ts[0].tv_sec = 12345678;
71 ts[0].tv_nsec = 23456789;
72 ts[1].tv_sec = 34567890;
73 ts[1].tv_nsec = 45678901;
74 if (utimensat(AT_FDCWD, TMPFILE, ts, AT_SYMLINK_NOFOLLOW) == -1)
75 fail("utimensat", 0, 0);
76
77 if (stat(TMPFILE, &sb) == -1)
78 fail("stat", 0, 0 );
79 if (sb.st_atime != 12345678)
80 fail("st_atime", 0, 0 );
81 if (sb.st_mtime != 34567890)
82 fail("st_mtime", 0, 0 );
83 #if 0
84 /*
85 * Results expected to be rounded to the nearest microsecond.
86 * Depends on timestamp precision in kernel and filesystem so
87 * disabled by default.
88 */
89 if (sb.st_atim.tv_nsec != 23456000)
90 fail("atim.tv_nsec", 23456000, sb.st_atim.tv_nsec);
91 if (sb.st_mtim.tv_nsec != 45678000)
92 fail("mtim.tv_nsec", 45678000, sb.st_mtim.tv_nsec);
93 #endif
94
95 /*
96 * POSIX specifies that when given a symlink, AT_SYMLINK_NOFOLLOW
97 * should update the symlink and not the destination. The compat
98 * code doesn't have a way to do this, so where possible it fails
99 * with instead of following a symlink when explicitly asked not to.
100 * Here we just test that it does not update the destination.
101 */
102 if (rename(TMPFILE, TMPFILE2) == -1)
103 fail("rename", 0, 0);
104 if (symlink(TMPFILE2, TMPFILE) == -1)
105 fail("symlink", 0, 0);
106 ts[0].tv_sec = 11223344;
107 ts[1].tv_sec = 55667788;
108 (void)utimensat(AT_FDCWD, TMPFILE, ts, AT_SYMLINK_NOFOLLOW);
109 if (stat(TMPFILE2, &sb) == -1)
110 fail("stat", 0, 0 );
111 if (sb.st_atime == 11223344)
112 fail("utimensat symlink st_atime", 0, 0 );
113 if (sb.st_mtime == 55667788)
114 fail("utimensat symlink st_mtime", 0, 0 );
115
116 cleanup();
117 exit(0);
118 }
119