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 "includes.h"
18 
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #define TMPFILE "utimensat.tmp"
30 #define TMPFILE2 "utimensat.tmp2"
31 
32 #ifndef AT_SYMLINK_NOFOLLOW
33 # define AT_SYMLINK_NOFOLLOW 0x80000000
34 #endif
35 
36 int utimensat(int, const char *, const struct timespec[2], int);
37 
38 static void
cleanup(void)39 cleanup(void)
40 {
41 	(void)unlink(TMPFILE);
42 	(void)unlink(TMPFILE2);
43 }
44 
45 static void
fail(char * msg,long expect,long got)46 fail(char *msg, long expect, long got)
47 {
48 	int saved_errno = errno;
49 
50 	if (expect == got && got == 0)
51 		fprintf(stderr, "utimensat: %s: %s\n", msg,
52 		     strerror(saved_errno));
53 	else
54 		fprintf(stderr, "utimensat: %s: expected %ld got %ld\n",
55 		     msg, expect, got);
56 	cleanup();
57 	exit(1);
58 }
59 
60 int
main(void)61 main(void)
62 {
63 	int fd;
64 	struct stat sb;
65 	struct timespec ts[2];
66 
67 	cleanup();
68 	if ((fd = open(TMPFILE, O_CREAT, 0600)) == -1)
69 		fail("open", 0, 0);
70 	close(fd);
71 
72 	ts[0].tv_sec = 12345678;
73 	ts[0].tv_nsec = 23456789;
74 	ts[1].tv_sec = 34567890;
75 	ts[1].tv_nsec = 45678901;
76 	if (utimensat(AT_FDCWD, TMPFILE, ts, AT_SYMLINK_NOFOLLOW) == -1)
77 		fail("utimensat", 0, 0);
78 
79 	if (stat(TMPFILE, &sb) == -1)
80 		fail("stat", 0, 0 );
81 	if (sb.st_atime != 12345678)
82 		fail("st_atime", 0, 0 );
83 	if (sb.st_mtime != 34567890)
84 		fail("st_mtime", 0, 0 );
85 #if 0
86 	/*
87 	 * Results expected to be rounded to the nearest microsecond.
88 	 * Depends on timestamp precision in kernel and filesystem so
89 	 * disabled by default.
90 	 */
91 	if (sb.st_atim.tv_nsec != 23456000)
92 		fail("atim.tv_nsec", 23456000, sb.st_atim.tv_nsec);
93 	if (sb.st_mtim.tv_nsec != 45678000)
94 		fail("mtim.tv_nsec", 45678000, sb.st_mtim.tv_nsec);
95 #endif
96 
97 	/*
98 	 * POSIX specifies that when given a symlink, AT_SYMLINK_NOFOLLOW
99 	 * should update the symlink and not the destination.  The compat
100 	 * code doesn't have a way to do this, so where possible it fails
101 	 * with instead of following a symlink when explicitly asked not to.
102 	 * Here we just test that it does not update the destination.
103 	 */
104 	if (rename(TMPFILE, TMPFILE2) == -1)
105 		fail("rename", 0, 0);
106 	if (symlink(TMPFILE2, TMPFILE) == -1)
107 		fail("symlink", 0, 0);
108 	ts[0].tv_sec = 11223344;
109 	ts[1].tv_sec = 55667788;
110 	(void)utimensat(AT_FDCWD, TMPFILE, ts, AT_SYMLINK_NOFOLLOW);
111 	if (stat(TMPFILE2, &sb) == -1)
112 		fail("stat", 0, 0 );
113 	if (sb.st_atime == 11223344)
114 		fail("utimensat symlink st_atime", 0, 0 );
115 	if (sb.st_mtime == 55667788)
116 		fail("utimensat symlink st_mtime", 0, 0 );
117 
118 	cleanup();
119 	exit(0);
120 }
121