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