1277610Sjilles/*-
2277610Sjilles * Copyright (c) 2015 Jilles Tjoelker
3277610Sjilles * All rights reserved.
4277610Sjilles *
5277610Sjilles * Redistribution and use in source and binary forms, with or without
6277610Sjilles * modification, are permitted provided that the following conditions
7277610Sjilles * are met:
8277610Sjilles * 1. Redistributions of source code must retain the above copyright
9277610Sjilles *    notice, this list of conditions and the following disclaimer.
10277610Sjilles * 2. Redistributions in binary form must reproduce the above copyright
11277610Sjilles *    notice, this list of conditions and the following disclaimer in the
12277610Sjilles *    documentation and/or other materials provided with the distribution.
13277610Sjilles *
14277610Sjilles * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15277610Sjilles * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16277610Sjilles * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17277610Sjilles * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18277610Sjilles * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19277610Sjilles * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20277610Sjilles * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21277610Sjilles * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22277610Sjilles * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23277610Sjilles * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24277610Sjilles * SUCH DAMAGE.
25277610Sjilles */
26277610Sjilles
27277610Sjilles#include <sys/cdefs.h>
28277610Sjilles__FBSDID("$FreeBSD$");
29277610Sjilles
30277610Sjilles#include "namespace.h"
31277610Sjilles#include <sys/stat.h>
32277610Sjilles
33277610Sjilles#include <errno.h>
34277610Sjilles#include <fcntl.h>
35277610Sjilles#include <time.h>
36277610Sjilles#include "un-namespace.h"
37277610Sjilles
38277610Sjilles#include "libc_private.h"
39277610Sjilles
40277610Sjillesint
41277610Sjillesfutimens(int fd, const struct timespec times[2])
42277610Sjilles{
43277610Sjilles	struct timeval now, tv[2], *tvp;
44277610Sjilles	struct stat sb;
45293783Sjilles	int osreldate;
46277610Sjilles
47293783Sjilles	osreldate = __getosreldate();
48293783Sjilles	if (osreldate >= 1100056 ||
49293783Sjilles	    (osreldate >= 1002506 && osreldate < 1100000))
50277610Sjilles		return (__sys_futimens(fd, times));
51277610Sjilles
52277610Sjilles	if (times == NULL || (times[0].tv_nsec == UTIME_NOW &&
53277610Sjilles	    times[1].tv_nsec == UTIME_NOW))
54277610Sjilles		tvp = NULL;
55277610Sjilles	else if (times[0].tv_nsec == UTIME_OMIT &&
56277610Sjilles	    times[1].tv_nsec == UTIME_OMIT)
57277610Sjilles		return (0);
58277610Sjilles	else {
59277610Sjilles		if ((times[0].tv_nsec < 0 || times[0].tv_nsec > 999999999) &&
60277610Sjilles		    times[0].tv_nsec != UTIME_NOW &&
61277610Sjilles		    times[0].tv_nsec != UTIME_OMIT) {
62277610Sjilles			errno = EINVAL;
63277610Sjilles			return (-1);
64277610Sjilles		}
65277610Sjilles		if ((times[1].tv_nsec < 0 || times[1].tv_nsec > 999999999) &&
66277610Sjilles		    times[1].tv_nsec != UTIME_NOW &&
67277610Sjilles		    times[1].tv_nsec != UTIME_OMIT) {
68277610Sjilles			errno = EINVAL;
69277610Sjilles			return (-1);
70277610Sjilles		}
71277610Sjilles		tv[0].tv_sec = times[0].tv_sec;
72277610Sjilles		tv[0].tv_usec = times[0].tv_nsec / 1000;
73277610Sjilles		tv[1].tv_sec = times[1].tv_sec;
74277610Sjilles		tv[1].tv_usec = times[1].tv_nsec / 1000;
75277610Sjilles		tvp = tv;
76277610Sjilles		if (times[0].tv_nsec == UTIME_OMIT ||
77277610Sjilles		    times[1].tv_nsec == UTIME_OMIT) {
78277610Sjilles			if (_fstat(fd, &sb) == -1)
79277610Sjilles				return (-1);
80277610Sjilles			if (times[0].tv_nsec == UTIME_OMIT) {
81277610Sjilles				tv[0].tv_sec = sb.st_atim.tv_sec;
82277610Sjilles				tv[0].tv_usec = sb.st_atim.tv_nsec / 1000;
83277610Sjilles			}
84277610Sjilles			if (times[1].tv_nsec == UTIME_OMIT) {
85277610Sjilles				tv[1].tv_sec = sb.st_mtim.tv_sec;
86277610Sjilles				tv[1].tv_usec = sb.st_mtim.tv_nsec / 1000;
87277610Sjilles			}
88277610Sjilles		}
89277610Sjilles		if (times[0].tv_nsec == UTIME_NOW ||
90277610Sjilles		    times[1].tv_nsec == UTIME_NOW) {
91277610Sjilles			if (gettimeofday(&now, NULL) == -1)
92277610Sjilles				return (-1);
93277610Sjilles			if (times[0].tv_nsec == UTIME_NOW)
94277610Sjilles				tv[0] = now;
95277610Sjilles			if (times[1].tv_nsec == UTIME_NOW)
96277610Sjilles				tv[1] = now;
97277610Sjilles		}
98277610Sjilles	}
99277610Sjilles	return (futimes(fd, tvp));
100277610Sjilles}
101