1 /* Set file access and modification times.
3 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
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 2, or (at your option) any
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert. */
21 /* derived from a function in touch.c */
37 /* Some systems (even some that do have <utime.h>) don't declare this
38 structure anywhere. */
39 #ifndef HAVE_STRUCT_UTIMBUF
47 /* Some systems don't have ENOSYS. */
50 # define ENOSYS ENOTSUP
52 /* Some systems don't have ENOTSUP either. */
53 # define ENOSYS EINVAL
57 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
58 # define __attribute__(x)
61 #ifndef ATTRIBUTE_UNUSED
62 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
65 /* Set the access and modification time stamps of FD (a.k.a. FILE) to be
66 TIMESPEC[0] and TIMESPEC[1], respectively.
67 FD must be either negative -- in which case it is ignored --
68 or a file descriptor that is open on FILE.
69 If FD is nonnegative, then FILE can be NULL, which means
70 use just futimes (or equivalent) instead of utimes (or equivalent),
71 and fail if on an old system without futimes (or equivalent).
72 If TIMESPEC is null, set the time stamps to the current time.
73 Return 0 on success, -1 (setting errno) on failure. */
76 futimens (int fd ATTRIBUTE_UNUSED,
77 char const *file, struct timespec const timespec[2])
79 /* There's currently no interface to set file timestamps with
80 nanosecond resolution, so do the best we can, discarding any
81 fractional part of the timestamp. */
82 #if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
83 struct timeval timeval[2];
84 struct timeval const *t;
87 timeval[0].tv_sec = timespec[0].tv_sec;
88 timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
89 timeval[1].tv_sec = timespec[1].tv_sec;
90 timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
100 return futimesat (AT_FDCWD, file, t);
105 /* If futimesat or futimes fails here, don't try to speed things
106 up by returning right away. glibc can incorrectly fail with
107 errno == ENOENT if /proc isn't mounted. Also, Mandrake 10.0
108 in high security mode doesn't allow ordinary users to read
109 /proc/self, so glibc incorrectly fails with errno == EACCES.
110 If errno == EIO, EPERM, or EROFS, it's probably safe to fail
111 right away, but these cases are rare enough that they're not
112 worth optimizing, and who knows what other messed-up systems
113 are out there? So play it safe and fall back on the code
116 if (futimesat (fd, NULL, t) == 0)
119 if (futimes (fd, t) == 0)
127 #if ! (HAVE_FUTIMESAT || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
131 /* Prefer EBADF to ENOSYS if both error numbers apply. */
135 int dup_errno = errno;
138 errno = (fd2 < 0 && dup_errno == EBADF ? EBADF : ENOSYS);
144 #if HAVE_WORKING_UTIMES
145 return utimes (file, t);
148 struct utimbuf utimbuf;
149 struct utimbuf const *ut;
152 utimbuf.actime = timespec[0].tv_sec;
153 utimbuf.modtime = timespec[1].tv_sec;
159 return utime (file, ut);
164 /* Set the access and modification time stamps of FILE to be
165 TIMESPEC[0] and TIMESPEC[1], respectively. */
167 utimens (char const *file, struct timespec const timespec[2])
169 return futimens (-1, file, timespec);