1 /* Set the access and modification time of a file relative to directory fd.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* written by Eric Blake */
26 #include "stat-time.h"
34 /* If we have a native utimensat, but are compiling this file, then
35 utimensat was defined to rpl_utimensat by our replacement
36 sys/stat.h. We assume the native version might fail with ENOSYS,
37 or succeed without properly affecting ctime (as is the case when
38 using newer glibc but older Linux kernel). In this scenario,
39 rpl_utimensat checks whether the native version is usable, and
40 local_utimensat provides the fallback manipulation. */
42 static int local_utimensat (int, char const *, struct timespec const[2], int);
43 # define AT_FUNC_NAME local_utimensat
45 /* Like utimensat, but work around native bugs. */
48 rpl_utimensat (int fd, char const *file, struct timespec const times[2],
51 /* See comments in utimens.c for details. */
52 static int utimensat_works_really; /* 0 = unknown, 1 = yes, -1 = no. */
53 if (0 <= utimensat_works_really)
58 struct timespec ts[2];
59 /* As recently as Linux kernel 2.6.32 (Dec 2009), several file
60 systems (xfs, ntfs-3g) have bugs with a single UTIME_OMIT,
61 but work if both times are either explicitly specified or
62 UTIME_NOW. Work around it with a preparatory [l]stat prior
63 to calling utimensat; fortunately, there is not much timing
64 impact due to the extra syscall even on file systems where
65 UTIME_OMIT would have worked. FIXME: Simplify this in 2012,
66 when file system bugs are no longer common. */
67 if (times && (times[0].tv_nsec == UTIME_OMIT
68 || times[1].tv_nsec == UTIME_OMIT))
70 if (fstatat (fd, file, &st, flag))
72 if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT)
74 if (times[0].tv_nsec == UTIME_OMIT)
75 ts[0] = get_stat_atime (&st);
78 if (times[1].tv_nsec == UTIME_OMIT)
79 ts[1] = get_stat_mtime (&st);
84 # endif /* __linux__ */
85 result = utimensat (fd, file, times, flag);
86 /* Linux kernel 2.6.25 has a bug where it returns EINVAL for
87 UTIME_NOW or UTIME_OMIT with non-zero tv_sec, which
88 local_utimensat works around. Meanwhile, EINVAL for a bad
89 flag is indeterminate whether the native utimensat works, but
90 local_utimensat will also reject it. */
91 if (result == -1 && errno == EINVAL && (flag & ~AT_SYMLINK_NOFOLLOW))
93 if (result == 0 || (errno != ENOSYS && errno != EINVAL))
95 utimensat_works_really = 1;
99 /* No point in trying openat/futimens, since on Linux, futimens is
100 implemented with the same syscall as utimensat. Only avoid the
101 native utimensat due to an ENOSYS failure; an EINVAL error was
102 data-dependent, and the next caller may pass valid data. */
103 if (0 <= utimensat_works_really && errno == ENOSYS)
104 utimensat_works_really = -1;
105 return local_utimensat (fd, file, times, flag);
108 #else /* !HAVE_UTIMENSAT */
110 # define AT_FUNC_NAME utimensat
112 #endif /* !HAVE_UTIMENSAT */
114 /* Set the access and modification time stamps of FILE to be
115 TIMESPEC[0] and TIMESPEC[1], respectively; relative to directory
116 FD. If flag is AT_SYMLINK_NOFOLLOW, change the times of a symlink,
117 or fail with ENOSYS if not possible. If TIMESPEC is null, set the
118 time stamps to the current time. If possible, do it without
119 changing the working directory. Otherwise, resort to using
120 save_cwd/fchdir, then utimens/restore_cwd. If either the save_cwd
121 or the restore_cwd fails, then give a diagnostic and exit nonzero.
122 Return 0 on success, -1 (setting errno) on failure. */
124 /* AT_FUNC_NAME is now utimensat or local_utimensat. */
125 #define AT_FUNC_F1 lutimens
126 #define AT_FUNC_F2 utimens
127 #define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
128 #define AT_FUNC_POST_FILE_PARAM_DECLS , struct timespec const ts[2], int flag
129 #define AT_FUNC_POST_FILE_ARGS , ts
134 #undef AT_FUNC_USE_F1_COND
135 #undef AT_FUNC_POST_FILE_PARAM_DECLS
136 #undef AT_FUNC_POST_FILE_ARGS