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