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 *flags |= FSOPT_UTIMES_NULL;
56 struct timespec now = {};
57 {
58 /*
59 * TODO: Replace with nanosecond time when available
60 */
61 struct timeval tv;
62 if (__commpage_gettimeofday(&tv) != 0) {
63 __gettimeofday(&tv, NULL);
64 }
65 TIMEVAL_TO_TIMESPEC(&tv, &now);
66 }
67
68 if (times_in[0].tv_nsec == UTIME_NOW) {
69 times_in[0] = now;
70 }
71 if (times_in[1].tv_nsec == UTIME_NOW) {
72 times_in[1] = now;
73 }
74 }
75
76 int attrs = 0;
77 *times_out_size = 0;
78 struct timespec *times_cursor = times_out;
79 if (times_in[1].tv_nsec != UTIME_OMIT) {
80 attrs |= ATTR_CMN_MODTIME;
81 *times_cursor++ = times_in[1];
82 *times_out_size += sizeof(struct timespec);
83 }
84 if (times_in[0].tv_nsec != UTIME_OMIT) {
85 attrs |= ATTR_CMN_ACCTIME;
86 *times_cursor = times_in[0];
87 *times_out_size += sizeof(struct timespec);
88 }
89 return attrs;
90 }
91
92 int
futimens(int fd,const struct timespec _times_in[2])93 futimens(int fd, const struct timespec _times_in[2])
94 {
95 struct timespec times_in[2];
96 int flags_out;
97
98 if (_times_in != NULL) {
99 memcpy(×_in, _times_in, sizeof(times_in));
100 } else {
101 memcpy(×_in, times_now, sizeof(times_in));
102 }
103
104 size_t attrbuf_size = 0;
105 struct timespec times_out[2] = {};
106 struct attrlist a = {
107 .bitmapcount = ATTR_BIT_MAP_COUNT
108 };
109 a.commonattr = prepare_times_array_and_attrs(times_in, times_out, &attrbuf_size, &flags_out);
110
111 return fsetattrlist(fd, &a, ×_out, attrbuf_size, 0);
112 }
113
114 int
utimensat(int fd,const char * path,const struct timespec _times_in[2],int flags)115 utimensat(int fd, const char *path, const struct timespec _times_in[2], int flags)
116 {
117 struct timespec times_in[2];
118 int flags_out = 0;
119
120 if (_times_in != NULL) {
121 memcpy(×_in, _times_in, sizeof(times_in));
122 } else {
123 memcpy(×_in, times_now, sizeof(times_in));
124 }
125
126 size_t attrbuf_size = 0;
127 struct timespec times_out[2] = {};
128 struct attrlist a = {
129 .bitmapcount = ATTR_BIT_MAP_COUNT
130 };
131 a.commonattr = prepare_times_array_and_attrs(times_in, times_out, &attrbuf_size, &flags_out);
132
133 if (flags & AT_SYMLINK_NOFOLLOW) {
134 flags_out |= FSOPT_NOFOLLOW;
135 }
136 if (flags & AT_SYMLINK_NOFOLLOW_ANY) {
137 flags_out |= FSOPT_NOFOLLOW_ANY;
138 }
139
140 return setattrlistat(fd, path, &a, ×_out, attrbuf_size, flags_out);
141 }
142