utimensat: new module
[gnulib.git] / lib / utimensat.c
1 /* Set the access and modification time of a file relative to directory fd.
2    Copyright (C) 2009 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 <http://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 "utimens.h"
27
28 #if HAVE_UTIMENSAT
29
30 # undef utimensat
31
32 /* If we have a native utimensat, but are compiling this file, then
33    utimensat was defined to rpl_utimensat by our replacement
34    sys/stat.h.  We assume the native version might fail with ENOSYS
35    (as is the case when using newer glibc but older Linux kernel).  In
36    this scenario, rpl_utimensat checks whether the native version is
37    usable, and local_utimensat provides the fallback manipulation.  */
38
39 static int local_utimensat (int, char const *, struct timespec const[2], int);
40 # define AT_FUNC_NAME local_utimensat
41
42 /* Like utimensat, but work around native bugs.  */
43
44 int
45 rpl_utimensat (int fd, char const *file, struct timespec const times[2],
46                int flag)
47 {
48   static int utimensat_works_really; /* 0 = unknown, 1 = yes, -1 = no.  */
49   if (0 <= utimensat_works_really)
50     {
51       int result = utimensat (fd, file, times, flag);
52       /* Linux kernel 2.6.25 has a bug where it returns EINVAL for
53          UTIME_NOW or UTIME_OMIT with non-zero tv_sec, which
54          local_utimensat works around.  Meanwhile, EINVAL for a bad
55          flag is indeterminate whether the native utimensat works, but
56          local_utimensat will also reject it.  */
57       if (result == -1 && errno == EINVAL && (flag & ~AT_SYMLINK_NOFOLLOW))
58         return result;
59       if (result == 0 || (errno != ENOSYS && errno != EINVAL))
60         {
61           utimensat_works_really = 1;
62           return result;
63         }
64     }
65   /* No point in trying openat/futimens, since on Linux, futimens is
66      implemented with the same syscall as utimensat.  Only avoid the
67      native utimensat due to an ENOSYS failure; an EINVAL error was
68      data-dependent, and the next caller may pass valid data.  */
69   if (0 <= utimensat_works_really && errno == ENOSYS)
70     utimensat_works_really = -1;
71   return local_utimensat (fd, file, times, flag);
72 }
73
74 #else /* !HAVE_UTIMENSAT */
75
76 # define AT_FUNC_NAME utimensat
77
78 #endif /* !HAVE_UTIMENSAT */
79
80 /* Set the access and modification time stamps of FILE to be
81    TIMESPEC[0] and TIMESPEC[1], respectively; relative to directory
82    FD.  If flag is AT_SYMLINK_NOFOLLOW, change the times of a symlink,
83    or fail with ENOSYS if not possible.  If TIMESPEC is null, set the
84    time stamps to the current time.  If possible, do it without
85    changing the working directory.  Otherwise, resort to using
86    save_cwd/fchdir, then utimens/restore_cwd.  If either the save_cwd
87    or the restore_cwd fails, then give a diagnostic and exit nonzero.
88    Return 0 on success, -1 (setting errno) on failure.  */
89
90 /* AT_FUNC_NAME is now utimensat or local_utimensat.  */
91 #define AT_FUNC_F1 lutimens
92 #define AT_FUNC_F2 utimens
93 #define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
94 #define AT_FUNC_POST_FILE_PARAM_DECLS , struct timespec const ts[2], int flag
95 #define AT_FUNC_POST_FILE_ARGS        , ts
96 #include "at-func.c"
97 #undef AT_FUNC_NAME
98 #undef AT_FUNC_F1
99 #undef AT_FUNC_F2
100 #undef AT_FUNC_USE_F1_COND
101 #undef AT_FUNC_POST_FILE_PARAM_DECLS
102 #undef AT_FUNC_POST_FILE_ARGS