xref: /xnu-11215.1.10/libsyscall/wrappers/utimensat.c (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1 /*
2  * Copyright (c) 2006, 2017 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/attr.h>
27 #include <sys/time.h>
28 #include <sys/fcntl.h>
29 #include <unistd.h>
30 #include <strings.h>
31 
32 extern int __gettimeofday(struct timeval *, struct timezone *);
33 extern int __commpage_gettimeofday(struct timeval *);
34 
35 static struct timespec times_now[2] = {
36 	{ .tv_nsec = UTIME_NOW },
37 	{ .tv_nsec = UTIME_NOW }
38 };
39 
40 /*
41  * Resolve any UTIME_NOW or UTIME_OMIT and return the attributes buffer and
42  * attributes to pass.  Assumes times_in is writable.
43  */
44 static int
prepare_times_array_and_attrs(struct timespec times_in[2],struct timespec times_out[2],size_t * times_out_size,int * flags)45 prepare_times_array_and_attrs(struct timespec times_in[2],
46     struct timespec times_out[2], size_t *times_out_size, int *flags)
47 {
48 	if (times_in[0].tv_nsec == UTIME_OMIT &&
49 	    times_in[1].tv_nsec == UTIME_OMIT) {
50 		return 0;
51 	}
52 
53 	if (times_in[0].tv_nsec == UTIME_NOW ||
54 	    times_in[1].tv_nsec == UTIME_NOW) {
55 		if (times_in[0].tv_nsec == UTIME_NOW &&
56 		    times_in[1].tv_nsec == UTIME_NOW) {
57 			*flags |= FSOPT_UTIMES_NULL;
58 		}
59 		struct timespec now = {};
60 		{
61 			/*
62 			 * TODO: Replace with nanosecond time when available
63 			 */
64 			struct timeval tv;
65 			if (__commpage_gettimeofday(&tv) != 0) {
66 				__gettimeofday(&tv, NULL);
67 			}
68 			TIMEVAL_TO_TIMESPEC(&tv, &now);
69 		}
70 
71 		if (times_in[0].tv_nsec == UTIME_NOW) {
72 			times_in[0] = now;
73 		}
74 		if (times_in[1].tv_nsec == UTIME_NOW) {
75 			times_in[1] = now;
76 		}
77 	}
78 
79 	int attrs = 0;
80 	*times_out_size = 0;
81 	struct timespec *times_cursor = times_out;
82 	if (times_in[1].tv_nsec != UTIME_OMIT) {
83 		attrs |= ATTR_CMN_MODTIME;
84 		*times_cursor++ = times_in[1];
85 		*times_out_size += sizeof(struct timespec);
86 	}
87 	if (times_in[0].tv_nsec != UTIME_OMIT) {
88 		attrs |= ATTR_CMN_ACCTIME;
89 		*times_cursor = times_in[0];
90 		*times_out_size += sizeof(struct timespec);
91 	}
92 	return attrs;
93 }
94 
95 int
futimens(int fd,const struct timespec _times_in[2])96 futimens(int fd, const struct timespec _times_in[2])
97 {
98 	struct timespec times_in[2];
99 	int flags_out;
100 
101 	if (_times_in != NULL) {
102 		memcpy(&times_in, _times_in, sizeof(times_in));
103 	} else {
104 		memcpy(&times_in, times_now, sizeof(times_in));
105 	}
106 
107 	size_t attrbuf_size = 0;
108 	struct timespec times_out[2] = {};
109 	struct attrlist a = {
110 		.bitmapcount = ATTR_BIT_MAP_COUNT
111 	};
112 	a.commonattr = prepare_times_array_and_attrs(times_in, times_out, &attrbuf_size, &flags_out);
113 
114 	return fsetattrlist(fd, &a, &times_out, attrbuf_size, 0);
115 }
116 
117 int
utimensat(int fd,const char * path,const struct timespec _times_in[2],int flags)118 utimensat(int fd, const char *path, const struct timespec _times_in[2], int flags)
119 {
120 	struct timespec times_in[2];
121 	int flags_out = 0;
122 
123 	if (_times_in != NULL) {
124 		memcpy(&times_in, _times_in, sizeof(times_in));
125 	} else {
126 		memcpy(&times_in, times_now, sizeof(times_in));
127 	}
128 
129 	size_t attrbuf_size = 0;
130 	struct timespec times_out[2] = {};
131 	struct attrlist a = {
132 		.bitmapcount = ATTR_BIT_MAP_COUNT
133 	};
134 	a.commonattr = prepare_times_array_and_attrs(times_in, times_out, &attrbuf_size, &flags_out);
135 
136 	if (flags & AT_SYMLINK_NOFOLLOW) {
137 		flags_out |= FSOPT_NOFOLLOW;
138 	}
139 	if (flags & AT_SYMLINK_NOFOLLOW_ANY) {
140 		flags_out |= FSOPT_NOFOLLOW_ANY;
141 	}
142 
143 	return setattrlistat(fd, path, &a, &times_out, attrbuf_size, flags_out);
144 }
145