1 /* Set the access and modification time of a file relative to directory fd.
2    Copyright (C) 2009-2018 Free Software Foundation, Inc.
3 
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.
8 
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.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 /* written by Eric Blake */
18 
19 #include <config.h>
20 
21 #include <sys/stat.h>
22 
23 #include <errno.h>
24 #include <fcntl.h>
25 
26 #include "stat-time.h"
27 #include "timespec.h"
28 #include "utimens.h"
29 
30 #if HAVE_UTIMENSAT
31 
32 # undef utimensat
33 
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.  */
41 
42 static int local_utimensat (int, char const *, struct timespec const[2], int);
43 # define AT_FUNC_NAME local_utimensat
44 
45 /* Like utimensat, but work around native bugs.  */
46 
47 int
rpl_utimensat(int fd,char const * file,struct timespec const times[2],int flag)48 rpl_utimensat (int fd, char const *file, struct timespec const times[2],
49                int flag)
50 {
51 # if defined __linux__ || defined __sun
52   struct timespec ts[2];
53 # endif
54 
55   /* See comments in utimens.c for details.  */
56   static int utimensat_works_really; /* 0 = unknown, 1 = yes, -1 = no.  */
57   if (0 <= utimensat_works_really)
58     {
59       int result;
60 # if defined __linux__ || defined __sun
61       struct stat st;
62       /* As recently as Linux kernel 2.6.32 (Dec 2009), several file
63          systems (xfs, ntfs-3g) have bugs with a single UTIME_OMIT,
64          but work if both times are either explicitly specified or
65          UTIME_NOW.  Work around it with a preparatory [l]stat prior
66          to calling utimensat; fortunately, there is not much timing
67          impact due to the extra syscall even on file systems where
68          UTIME_OMIT would have worked.
69 
70          The same bug occurs in Solaris 11.1 (Apr 2013).
71 
72          FIXME: Simplify this in 2024, when these file system bugs are
73          no longer common on Gnulib target platforms.  */
74       if (times && (times[0].tv_nsec == UTIME_OMIT
75                     || times[1].tv_nsec == UTIME_OMIT))
76         {
77           if (fstatat (fd, file, &st, flag))
78             return -1;
79           if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT)
80             return 0;
81           if (times[0].tv_nsec == UTIME_OMIT)
82             ts[0] = get_stat_atime (&st);
83           else
84             ts[0] = times[0];
85           if (times[1].tv_nsec == UTIME_OMIT)
86             ts[1] = get_stat_mtime (&st);
87           else
88             ts[1] = times[1];
89           times = ts;
90         }
91 #  ifdef __hppa__
92       /* Linux kernel 2.6.22.19 on hppa does not reject invalid tv_nsec
93          values.  */
94       else if (times
95                && ((times[0].tv_nsec != UTIME_NOW
96                     && ! (0 <= times[0].tv_nsec
97                           && times[0].tv_nsec < TIMESPEC_RESOLUTION))
98                    || (times[1].tv_nsec != UTIME_NOW
99                        && ! (0 <= times[1].tv_nsec
100                              && times[1].tv_nsec < TIMESPEC_RESOLUTION))))
101         {
102           errno = EINVAL;
103           return -1;
104         }
105 #  endif
106 # endif
107       result = utimensat (fd, file, times, flag);
108       /* Linux kernel 2.6.25 has a bug where it returns EINVAL for
109          UTIME_NOW or UTIME_OMIT with non-zero tv_sec, which
110          local_utimensat works around.  Meanwhile, EINVAL for a bad
111          flag is indeterminate whether the native utimensat works, but
112          local_utimensat will also reject it.  */
113       if (result == -1 && errno == EINVAL && (flag & ~AT_SYMLINK_NOFOLLOW))
114         return result;
115       if (result == 0 || (errno != ENOSYS && errno != EINVAL))
116         {
117           utimensat_works_really = 1;
118           return result;
119         }
120     }
121   /* No point in trying openat/futimens, since on Linux, futimens is
122      implemented with the same syscall as utimensat.  Only avoid the
123      native utimensat due to an ENOSYS failure; an EINVAL error was
124      data-dependent, and the next caller may pass valid data.  */
125   if (0 <= utimensat_works_really && errno == ENOSYS)
126     utimensat_works_really = -1;
127   return local_utimensat (fd, file, times, flag);
128 }
129 
130 #else /* !HAVE_UTIMENSAT */
131 
132 # define AT_FUNC_NAME utimensat
133 
134 #endif /* !HAVE_UTIMENSAT */
135 
136 /* Set the access and modification timestamps of FILE to be
137    TIMESPEC[0] and TIMESPEC[1], respectively; relative to directory
138    FD.  If flag is AT_SYMLINK_NOFOLLOW, change the times of a symlink,
139    or fail with ENOSYS if not possible.  If TIMESPEC is null, set the
140    timestamps to the current time.  If possible, do it without
141    changing the working directory.  Otherwise, resort to using
142    save_cwd/fchdir, then utimens/restore_cwd.  If either the save_cwd
143    or the restore_cwd fails, then give a diagnostic and exit nonzero.
144    Return 0 on success, -1 (setting errno) on failure.  */
145 
146 /* AT_FUNC_NAME is now utimensat or local_utimensat.  */
147 #define AT_FUNC_F1 lutimens
148 #define AT_FUNC_F2 utimens
149 #define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
150 #define AT_FUNC_POST_FILE_PARAM_DECLS , struct timespec const ts[2], int flag
151 #define AT_FUNC_POST_FILE_ARGS        , ts
152 #include "at-func.c"
153 #undef AT_FUNC_NAME
154 #undef AT_FUNC_F1
155 #undef AT_FUNC_F2
156 #undef AT_FUNC_USE_F1_COND
157 #undef AT_FUNC_POST_FILE_PARAM_DECLS
158 #undef AT_FUNC_POST_FILE_ARGS
159