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