1 /* Set file access and modification times.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free
4 Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3 of the License, or any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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 /* Set the access and modification time stamps of FD (a.k.a. FILE) to be
48 TIMESPEC[0] and TIMESPEC[1], respectively.
49 FD must be either negative -- in which case it is ignored --
50 or a file descriptor that is open on FILE.
51 If FD is nonnegative, then FILE can be NULL, which means
52 use just futimes (or equivalent) instead of utimes (or equivalent),
53 and fail if on an old system without futimes (or equivalent).
54 If TIMESPEC is null, set the time stamps to the current time.
55 Return 0 on success, -1 (setting errno) on failure. */
58 gl_futimens (int fd _UNUSED_PARAMETER_,
59 char const *file, struct timespec const timespec[2])
61 /* Some Linux-based NFS clients are buggy, and mishandle time stamps
62 of files in NFS file systems in some cases. We have no
63 configure-time test for this, but please see
64 <http://bugs.gentoo.org/show_bug.cgi?id=132673> for references to
65 some of the problems with Linux 2.6.16. If this affects you,
66 compile with -DHAVE_BUGGY_NFS_TIME_STAMPS; this is reported to
67 help in some cases, albeit at a cost in performance. But you
68 really should upgrade your kernel to a fixed version, since the
69 problem affects many applications. */
71 #if HAVE_BUGGY_NFS_TIME_STAMPS
78 /* POSIX 200x added two interfaces to set file timestamps with
79 nanosecond resolution. We provide a fallback for ENOSYS (for
80 example, compiling against Linux 2.6.25 kernel headers and glibc
81 2.7, but running on Linux 2.6.18 kernel). */
85 int result = utimensat (AT_FDCWD, file, timespec, 0);
87 /* Work around what might be a kernel bug:
88 http://bugzilla.redhat.com/442352
89 http://bugzilla.redhat.com/449910
90 It appears that utimensat can mistakenly return 280 rather
92 FIXME: remove in 2010 or whenever the offending kernels
93 are no longer in common use. */
98 if (result == 0 || errno != ENOSYS)
104 int result = futimens (fd, timespec);
106 /* Work around the same bug as above. */
110 if (result == 0 || errno != ENOSYS)
115 /* The platform lacks an interface to set file timestamps with
116 nanosecond resolution, so do the best we can, discarding any
117 fractional part of the timestamp. */
119 #if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
120 struct timeval timeval[2];
121 struct timeval const *t;
124 timeval[0].tv_sec = timespec[0].tv_sec;
125 timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
126 timeval[1].tv_sec = timespec[1].tv_sec;
127 timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
136 return futimesat (AT_FDCWD, file, t);
141 /* If futimesat or futimes fails here, don't try to speed things
142 up by returning right away. glibc can incorrectly fail with
143 errno == ENOENT if /proc isn't mounted. Also, Mandrake 10.0
144 in high security mode doesn't allow ordinary users to read
145 /proc/self, so glibc incorrectly fails with errno == EACCES.
146 If errno == EIO, EPERM, or EROFS, it's probably safe to fail
147 right away, but these cases are rare enough that they're not
148 worth optimizing, and who knows what other messed-up systems
149 are out there? So play it safe and fall back on the code
152 if (futimesat (fd, NULL, t) == 0)
155 if (futimes (fd, t) == 0)
159 #endif /* HAVE_FUTIMESAT || HAVE_WORKING_UTIMES */
163 #if ! (HAVE_FUTIMESAT || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
167 /* Prefer EBADF to ENOSYS if both error numbers apply. */
171 int dup_errno = errno;
174 errno = (fd2 < 0 && dup_errno == EBADF ? EBADF : ENOSYS);
180 #if HAVE_WORKING_UTIMES
181 return utimes (file, t);
184 struct utimbuf utimbuf;
185 struct utimbuf const *ut;
188 utimbuf.actime = timespec[0].tv_sec;
189 utimbuf.modtime = timespec[1].tv_sec;
195 return utime (file, ut);
197 #endif /* !HAVE_WORKING_UTIMES */
201 /* Set the access and modification time stamps of FILE to be
202 TIMESPEC[0] and TIMESPEC[1], respectively. */
204 utimens (char const *file, struct timespec const timespec[2])
206 return gl_futimens (-1, file, timespec);