| 1 | /* Change access and modification times of open file. Linux version. |
| 2 | Copyright (C) 2007-2020 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. */ |
| 27 | int |
| 28 | __utimensat64_helper (int fd, const char *file, |
| 29 | const struct __timespec64 tsp64[2], int flags) |
| 30 | { |
| 31 | #ifdef __ASSUME_TIME64_SYSCALLS |
| 32 | # ifndef __NR_utimensat_time64 |
| 33 | # define __NR_utimensat_time64 __NR_utimensat |
| 34 | # endif |
| 35 | return INLINE_SYSCALL (utimensat_time64, 4, fd, file, &tsp64[0], flags); |
| 36 | #else |
| 37 | # ifdef __NR_utimensat_time64 |
| 38 | int ret = INLINE_SYSCALL (utimensat_time64, 4, fd, file, &tsp64[0], flags); |
| 39 | if (ret == 0 || errno != ENOSYS) |
| 40 | return ret; |
| 41 | # endif |
| 42 | if (tsp64 |
| 43 | && (! in_time_t_range (tsp64[0].tv_sec) |
| 44 | || ! in_time_t_range (tsp64[1].tv_sec))) |
| 45 | { |
| 46 | __set_errno (EOVERFLOW); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | struct timespec tsp32[2]; |
| 51 | if (tsp64) |
| 52 | { |
| 53 | tsp32[0] = valid_timespec64_to_timespec (tsp64[0]); |
| 54 | tsp32[1] = valid_timespec64_to_timespec (tsp64[1]); |
| 55 | } |
| 56 | |
| 57 | return INLINE_SYSCALL (utimensat, 4, fd, file, tsp64 ? &tsp32[0] : NULL, |
| 58 | flags); |
| 59 | #endif |
| 60 | |
| 61 | } |
| 62 | libc_hidden_def (__utimensat64_helper) |
| 63 | |
| 64 | /* Change the access time of FILE to TSP[0] and |
| 65 | the modification time of FILE to TSP[1]. |
| 66 | |
| 67 | Starting with 2.6.22 the Linux kernel has the utimensat syscall. */ |
| 68 | int |
| 69 | __utimensat64 (int fd, const char *file, const struct __timespec64 tsp64[2], |
| 70 | int flags) |
| 71 | { |
| 72 | if (file == NULL) |
| 73 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); |
| 74 | |
| 75 | return __utimensat64_helper (fd, file, &tsp64[0], flags); |
| 76 | } |
| 77 | |
| 78 | #if __TIMESIZE != 64 |
| 79 | libc_hidden_def (__utimensat64) |
| 80 | |
| 81 | int |
| 82 | __utimensat (int fd, const char *file, const struct timespec tsp[2], |
| 83 | int flags) |
| 84 | { |
| 85 | struct __timespec64 tsp64[2]; |
| 86 | if (tsp) |
| 87 | { |
| 88 | tsp64[0] = valid_timespec_to_timespec64 (tsp[0]); |
| 89 | tsp64[1] = valid_timespec_to_timespec64 (tsp[1]); |
| 90 | } |
| 91 | |
| 92 | return __utimensat64 (fd, file, tsp ? &tsp64[0] : NULL, flags); |
| 93 | } |
| 94 | #endif |
| 95 | weak_alias (__utimensat, utimensat) |
| 96 | |