]> git.cworth.org Git - tar/blob - lib/utimens.c
Preserve timestamp of symlinks when extracting (if utimensat available)
[tar] / lib / utimens.c
1 /* Set file access and modification times.
2
3    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software
4    Foundation, Inc.
5
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
9    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 Paul Eggert.  */
20
21 /* derived from a function in touch.c */
22
23 #include <config.h>
24
25 #include "utimens.h"
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32
33 #if HAVE_UTIME_H
34 # include <utime.h>
35 #endif
36
37 /* Some systems (even some that do have <utime.h>) don't declare this
38    structure anywhere.  */
39 #ifndef HAVE_STRUCT_UTIMBUF
40 struct utimbuf
41 {
42   long actime;
43   long modtime;
44 };
45 #endif
46
47 /* Some systems don't have ENOSYS.  */
48 #ifndef ENOSYS
49 # ifdef ENOTSUP
50 #  define ENOSYS ENOTSUP
51 # else
52 /* Some systems don't have ENOTSUP either.  */
53 #  define ENOSYS EINVAL
54 # endif
55 #endif
56
57 #ifndef __attribute__
58 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
59 #  define __attribute__(x)
60 # endif
61 #endif
62
63 #ifndef ATTRIBUTE_UNUSED
64 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
65 #endif
66
67 /* Set the access and modification time stamps of FD (a.k.a. FILE) to be
68    TIMESPEC[0] and TIMESPEC[1], respectively.
69    FD must be either negative -- in which case it is ignored --
70    or a file descriptor that is open on FILE.
71    If FD is nonnegative, then FILE can be NULL, which means
72    use just futimes (or equivalent) instead of utimes (or equivalent),
73    and fail if on an old system without futimes (or equivalent).
74    If TIMESPEC is null, set the time stamps to the current time.
75    If the file is a symlink and IS_SYMLINK is set, then the
76    time stamps of the symlink itself will be updated if
77    possible, (but if not supported by the operating system
78    then no change will occur).
79    Return 0 on success, -1 (setting errno) on failure.  */
80
81 int
82 gl_futimens (int fd ATTRIBUTE_UNUSED,
83              char const *file, struct timespec const timespec[2],
84              int is_symlink)
85 {
86   /* Some Linux-based NFS clients are buggy, and mishandle time stamps
87      of files in NFS file systems in some cases.  We have no
88      configure-time test for this, but please see
89      <http://bugs.gentoo.org/show_bug.cgi?id=132673> for references to
90      some of the problems with Linux 2.6.16.  If this affects you,
91      compile with -DHAVE_BUGGY_NFS_TIME_STAMPS; this is reported to
92      help in some cases, albeit at a cost in performance.  But you
93      really should upgrade your kernel to a fixed version, since the
94      problem affects many applications.  */
95
96 #if HAVE_BUGGY_NFS_TIME_STAMPS
97   if (fd < 0)
98     sync ();
99   else
100     fsync (fd);
101 #endif
102
103   /* POSIX 200x added two interfaces to set file timestamps with
104      nanosecond resolution.  We provide a fallback for ENOSYS (for
105      example, compiling against Linux 2.6.25 kernel headers and glibc
106      2.7, but running on Linux 2.6.18 kernel).  */
107 #if HAVE_UTIMENSAT
108   if (fd < 0)
109     {
110       int flags = is_symlink ? AT_SYMLINK_NOFOLLOW : 0;
111       int result = utimensat (AT_FDCWD, file, timespec, flags);
112 # ifdef __linux__
113       /* Work around what might be a kernel bug:
114          http://bugzilla.redhat.com/442352
115          http://bugzilla.redhat.com/449910
116          It appears that utimensat can mistakenly return 280 rather
117          than -1 upon failure.
118          FIXME: remove in 2010 or whenever the offending kernels
119          are no longer in common use.  */
120       if (0 < result)
121         errno = ENOSYS;
122 # endif
123
124       if (result == 0 || errno != ENOSYS)
125         return result;
126     }
127 #endif
128
129   /* Without utimensat we have no way to update a symlink rather than
130    * the target, so just return immediately. */
131   if (is_symlink)
132       return 0;
133
134 #if HAVE_FUTIMENS
135   {
136     int result = futimens (fd, timespec);
137 # ifdef __linux__
138     /* Work around the same bug as above.  */
139     if (0 < result)
140       errno = ENOSYS;
141 # endif
142     if (result == 0 || errno != ENOSYS)
143       return result;
144   }
145 #endif
146
147   /* The platform lacks an interface to set file timestamps with
148      nanosecond resolution, so do the best we can, discarding any
149      fractional part of the timestamp.  */
150   {
151 #if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
152     struct timeval timeval[2];
153     struct timeval const *t;
154     if (timespec)
155       {
156         timeval[0].tv_sec = timespec[0].tv_sec;
157         timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
158         timeval[1].tv_sec = timespec[1].tv_sec;
159         timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
160         t = timeval;
161       }
162     else
163       t = NULL;
164
165     if (fd < 0)
166       {
167 # if HAVE_FUTIMESAT
168         return futimesat (AT_FDCWD, file, t);
169 # endif
170       }
171     else
172       {
173         /* If futimesat or futimes fails here, don't try to speed things
174            up by returning right away.  glibc can incorrectly fail with
175            errno == ENOENT if /proc isn't mounted.  Also, Mandrake 10.0
176            in high security mode doesn't allow ordinary users to read
177            /proc/self, so glibc incorrectly fails with errno == EACCES.
178            If errno == EIO, EPERM, or EROFS, it's probably safe to fail
179            right away, but these cases are rare enough that they're not
180            worth optimizing, and who knows what other messed-up systems
181            are out there?  So play it safe and fall back on the code
182            below.  */
183 # if HAVE_FUTIMESAT
184         if (futimesat (fd, NULL, t) == 0)
185           return 0;
186 # elif HAVE_FUTIMES
187         if (futimes (fd, t) == 0)
188           return 0;
189 # endif
190       }
191 #endif /* HAVE_FUTIMESAT || HAVE_WORKING_UTIMES */
192
193     if (!file)
194       {
195 #if ! (HAVE_FUTIMESAT || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
196         errno = ENOSYS;
197 #endif
198
199         /* Prefer EBADF to ENOSYS if both error numbers apply.  */
200         if (errno == ENOSYS)
201           {
202             int fd2 = dup (fd);
203             int dup_errno = errno;
204             if (0 <= fd2)
205               close (fd2);
206             errno = (fd2 < 0 && dup_errno == EBADF ? EBADF : ENOSYS);
207           }
208
209         return -1;
210       }
211
212 #if HAVE_WORKING_UTIMES
213     return utimes (file, t);
214 #else
215     {
216       struct utimbuf utimbuf;
217       struct utimbuf const *ut;
218       if (timespec)
219         {
220           utimbuf.actime = timespec[0].tv_sec;
221           utimbuf.modtime = timespec[1].tv_sec;
222           ut = &utimbuf;
223         }
224       else
225         ut = NULL;
226
227       return utime (file, ut);
228     }
229 #endif /* !HAVE_WORKING_UTIMES */
230   }
231 }
232
233 /* Set the access and modification time stamps of FILE to be
234    TIMESPEC[0] and TIMESPEC[1], respectively.
235    If the file is a symlink and is_symlink is set, then the
236    time stamps of the symlink itself will be updated if
237    possible, (but if not supported by the operating system
238    then no change will occur). */
239 int
240 utimens (char const *file, struct timespec const timespec[2], int is_symlink)
241 {
242   return gl_futimens (-1, file, timespec, is_symlink);
243 }