| 1 |
/* Set file access and modification times. |
| 2 |
|
| 3 |
Copyright (C) 2009-2023 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This program is free software: you can redistribute it and/or modify it |
| 6 |
under the terms of the GNU General Public License as published by the |
| 7 |
Free Software Foundation, either version 3 of the License, or any |
| 8 |
later version. |
| 9 |
|
| 10 |
This program 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 |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Written by Eric Blake. */ |
| 19 |
|
| 20 |
/* derived from a function in utimens.c */ |
| 21 |
|
| 22 |
#include <config.h> |
| 23 |
|
| 24 |
#include "utimens.h" |
| 25 |
|
| 26 |
#include <errno.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
|
| 30 |
/* Set the access and modification timestamps of FD (a.k.a. FILE) to be |
| 31 |
TIMESPEC[0] and TIMESPEC[1], respectively; relative to directory DIR. |
| 32 |
FD must be either negative -- in which case it is ignored -- |
| 33 |
or a file descriptor that is open on FILE. |
| 34 |
If FD is nonnegative, then FILE can be NULL, which means |
| 35 |
use just futimes (or equivalent) instead of utimes (or equivalent), |
| 36 |
and fail if on an old system without futimes (or equivalent). |
| 37 |
If TIMESPEC is null, set the timestamps to the current time. |
| 38 |
ATFLAG is passed to utimensat if FD is negative or futimens was |
| 39 |
unsupported, which can allow operation on FILE as a symlink. |
| 40 |
Return 0 on success, -1 (setting errno) on failure. */ |
| 41 |
|
| 42 |
int |
| 43 |
fdutimensat (int fd, int dir, char const *file, struct timespec const ts[2], |
| 44 |
int atflag) |
| 45 |
{ |
| 46 |
int result = 1; |
| 47 |
if (0 <= fd) |
| 48 |
result = futimens (fd, ts); |
| 49 |
if (file && (fd < 0 || (result == -1 && errno == ENOSYS))) |
| 50 |
result = utimensat (dir, file, ts, atflag); |
| 51 |
if (result == 1) |
| 52 |
{ |
| 53 |
errno = EBADF; |
| 54 |
result = -1; |
| 55 |
} |
| 56 |
return result; |
| 57 |
} |