1/* Change access and modification times of open file. Linux version.
2 Copyright (C) 2007-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <sys/stat.h>
21#include <sysdep.h>
22#include <time.h>
23#include <kernel-features.h>
24
25/* Helper function defined for easy reusage of the code which calls utimensat
26 and utimensat_time64 syscall. */
27int
28__utimensat64_helper (int fd, const char *file,
29 const struct __timespec64 tsp64[2], int flags)
30{
31#ifndef __NR_utimensat_time64
32# define __NR_utimensat_time64 __NR_utimensat
33#endif
34 int ret = INLINE_SYSCALL_CALL (utimensat_time64, fd, file, &tsp64[0], flags);
35#ifndef __ASSUME_TIME64_SYSCALLS
36 if (ret == 0 || errno != ENOSYS)
37 return ret;
38
39 /* For UTIME_NOW and UTIME_OMIT the value of tv_sec field is ignored. */
40# define TS_VALID(ns) \
41 ((((ns).tv_nsec == UTIME_NOW || (ns).tv_nsec == UTIME_OMIT) \
42 || in_time_t_range ((ns).tv_sec)))
43
44 if (tsp64 != NULL
45 && (!TS_VALID (tsp64[0]) || !TS_VALID (tsp64[1])))
46 {
47 __set_errno (EOVERFLOW);
48 return -1;
49 }
50
51 struct timespec tsp32[2];
52 if (tsp64)
53 {
54 tsp32[0] = valid_timespec64_to_timespec (tsp64[0]);
55 tsp32[1] = valid_timespec64_to_timespec (tsp64[1]);
56 }
57
58 ret = INLINE_SYSCALL_CALL (utimensat, fd, file, tsp64 ? &tsp32[0] : NULL,
59 flags);
60#endif
61 return ret;
62}
63libc_hidden_def (__utimensat64_helper)
64
65/* Change the access time of FILE to TSP[0] and
66 the modification time of FILE to TSP[1].
67
68 Starting with 2.6.22 the Linux kernel has the utimensat syscall. */
69int
70__utimensat64 (int fd, const char *file, const struct __timespec64 tsp64[2],
71 int flags)
72{
73 if (file == NULL)
74 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
75
76 return __utimensat64_helper (fd, file, &tsp64[0], flags);
77}
78
79#if __TIMESIZE != 64
80libc_hidden_def (__utimensat64)
81
82int
83__utimensat (int fd, const char *file, const struct timespec tsp[2],
84 int flags)
85{
86 struct __timespec64 tsp64[2];
87 if (tsp)
88 {
89 tsp64[0] = valid_timespec_to_timespec64 (tsp[0]);
90 tsp64[1] = valid_timespec_to_timespec64 (tsp[1]);
91 }
92
93 return __utimensat64 (fd, file, tsp ? &tsp64[0] : NULL, flags);
94}
95#endif
96weak_alias (__utimensat, utimensat)
97