]> git.cworth.org Git - gzip/blob - lib/utimens.c
Imported Debian patch 1.3.9-1
[gzip] / lib / utimens.c
1 /* Set file access and modification times.
2
3    Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by the
7    Free Software Foundation; either version 2, or (at your option) any
8    later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
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 <unistd.h>
30
31 #if HAVE_UTIME_H
32 # include <utime.h>
33 #endif
34
35 /* Some systems (even some that do have <utime.h>) don't declare this
36    structure anywhere.  */
37 #ifndef HAVE_STRUCT_UTIMBUF
38 struct utimbuf
39 {
40   long actime;
41   long modtime;
42 };
43 #endif
44
45 /* Some systems don't have ENOSYS.  */
46 #ifndef ENOSYS
47 # ifdef ENOTSUP
48 #  define ENOSYS ENOTSUP
49 # else
50 /* Some systems don't have ENOTSUP either.  */
51 #  define ENOSYS EINVAL
52 # endif
53 #endif
54
55 #ifndef __attribute__
56 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
57 #  define __attribute__(x)
58 # endif
59 #endif
60
61 #ifndef ATTRIBUTE_UNUSED
62 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
63 #endif
64
65 /* Set the access and modification time stamps of FD (a.k.a. FILE) to be
66    TIMESPEC[0] and TIMESPEC[1], respectively.
67    FD must be either negative -- in which case it is ignored --
68    or a file descriptor that is open on FILE.
69    If FD is nonnegative, then FILE can be NULL, which means
70    use just futimes (or equivalent) instead of utimes (or equivalent),
71    and fail if on an old system without futimes (or equivalent).
72    If TIMESPEC is null, set the time stamps to the current time.
73    Return 0 on success, -1 (setting errno) on failure.  */
74
75 int
76 futimens (int fd ATTRIBUTE_UNUSED,
77           char const *file, struct timespec const timespec[2])
78 {
79   /* Some Linux-based NFS clients are buggy, and mishandle time stamps
80      of files in NFS file systems in some cases.  We have no
81      configure-time test for this, but please see
82      <http://bugs.gentoo.org/show_bug.cgi?id=132673> for references to
83      some of the problems with Linux 2.6.16.  If this affects you,
84      compile with -DHAVE_BUGGY_NFS_TIME_STAMPS; this is reported to
85      help in some cases, albeit at a cost in performance.  But you
86      really should upgrade your kernel to a fixed version, since the
87      problem affects many applications.  */
88
89 #if HAVE_BUGGY_NFS_TIME_STAMPS
90   if (fd < 0)
91     sync ();
92   else
93     fsync (fd);
94 #endif
95
96   /* There's currently no interface to set file timestamps with
97      nanosecond resolution, so do the best we can, discarding any
98      fractional part of the timestamp.  */
99 #if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
100   struct timeval timeval[2];
101   struct timeval const *t;
102   if (timespec)
103     {
104       timeval[0].tv_sec = timespec[0].tv_sec;
105       timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
106       timeval[1].tv_sec = timespec[1].tv_sec;
107       timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
108       t = timeval;
109     }
110   else
111     t = NULL;
112
113
114   if (fd < 0)
115     {
116 # if HAVE_FUTIMESAT
117       return futimesat (AT_FDCWD, file, t);
118 # endif
119     }
120   else
121     {
122       /* If futimesat or futimes fails here, don't try to speed things
123          up by returning right away.  glibc can incorrectly fail with
124          errno == ENOENT if /proc isn't mounted.  Also, Mandrake 10.0
125          in high security mode doesn't allow ordinary users to read
126          /proc/self, so glibc incorrectly fails with errno == EACCES.
127          If errno == EIO, EPERM, or EROFS, it's probably safe to fail
128          right away, but these cases are rare enough that they're not
129          worth optimizing, and who knows what other messed-up systems
130          are out there?  So play it safe and fall back on the code
131          below.  */
132 # if HAVE_FUTIMESAT
133       if (futimesat (fd, NULL, t) == 0)
134         return 0;
135 # elif HAVE_FUTIMES
136       if (futimes (fd, t) == 0)
137         return 0;
138 # endif
139     }
140 #endif
141
142   if (!file)
143     {
144 #if ! (HAVE_FUTIMESAT || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
145       errno = ENOSYS;
146 #endif
147
148       /* Prefer EBADF to ENOSYS if both error numbers apply.  */
149       if (errno == ENOSYS)
150         {
151           int fd2 = dup (fd);
152           int dup_errno = errno;
153           if (0 <= fd2)
154             close (fd2);
155           errno = (fd2 < 0 && dup_errno == EBADF ? EBADF : ENOSYS);
156         }
157
158       return -1;
159     }
160
161 #if HAVE_WORKING_UTIMES
162   return utimes (file, t);
163 #else
164   {
165     struct utimbuf utimbuf;
166     struct utimbuf const *ut;
167     if (timespec)
168       {
169         utimbuf.actime = timespec[0].tv_sec;
170         utimbuf.modtime = timespec[1].tv_sec;
171         ut = &utimbuf;
172       }
173     else
174       ut = NULL;
175
176     return utime (file, ut);
177   }
178 #endif
179 }
180
181 /* Set the access and modification time stamps of FILE to be
182    TIMESPEC[0] and TIMESPEC[1], respectively.  */
183 int
184 utimens (char const *file, struct timespec const timespec[2])
185 {
186   return futimens (-1, file, timespec);
187 }