Prefer new POSIX 200x interfaces over futimesat.
[gnulib.git] / 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    Return 0 on success, -1 (setting errno) on failure.  */
76
77 int
78 gl_futimens (int fd ATTRIBUTE_UNUSED,
79              char const *file, struct timespec const timespec[2])
80 {
81   /* Some Linux-based NFS clients are buggy, and mishandle time stamps
82      of files in NFS file systems in some cases.  We have no
83      configure-time test for this, but please see
84      <http://bugs.gentoo.org/show_bug.cgi?id=132673> for references to
85      some of the problems with Linux 2.6.16.  If this affects you,
86      compile with -DHAVE_BUGGY_NFS_TIME_STAMPS; this is reported to
87      help in some cases, albeit at a cost in performance.  But you
88      really should upgrade your kernel to a fixed version, since the
89      problem affects many applications.  */
90
91 #if HAVE_BUGGY_NFS_TIME_STAMPS
92   if (fd < 0)
93     sync ();
94   else
95     fsync (fd);
96 #endif
97
98   /* POSIX 200x added two interfaces to set file timestamps with
99      nanosecond resolution.  */
100 #if HAVE_UTIMENSAT
101   if (fd < 0)
102     return utimensat (AT_FDCWD, file, timespec, 0);
103 #endif
104 #if HAVE_FUTIMENS
105   return futimens (fd, timespec);
106 #else
107
108   /* The platform lacks an interface to set file timestamps with
109      nanosecond resolution, so do the best we can, discarding any
110      fractional part of the timestamp.  */
111   {
112 # if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
113     struct timeval timeval[2];
114     struct timeval const *t;
115     if (timespec)
116       {
117         timeval[0].tv_sec = timespec[0].tv_sec;
118         timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
119         timeval[1].tv_sec = timespec[1].tv_sec;
120         timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
121         t = timeval;
122       }
123     else
124       t = NULL;
125
126     if (fd < 0)
127       {
128 #  if HAVE_FUTIMESAT
129         return futimesat (AT_FDCWD, file, t);
130 #  endif
131       }
132     else
133       {
134         /* If futimesat or futimes fails here, don't try to speed things
135            up by returning right away.  glibc can incorrectly fail with
136            errno == ENOENT if /proc isn't mounted.  Also, Mandrake 10.0
137            in high security mode doesn't allow ordinary users to read
138            /proc/self, so glibc incorrectly fails with errno == EACCES.
139            If errno == EIO, EPERM, or EROFS, it's probably safe to fail
140            right away, but these cases are rare enough that they're not
141            worth optimizing, and who knows what other messed-up systems
142            are out there?  So play it safe and fall back on the code
143            below.  */
144 #  if HAVE_FUTIMESAT
145         if (futimesat (fd, NULL, t) == 0)
146           return 0;
147 #  elif HAVE_FUTIMES
148         if (futimes (fd, t) == 0)
149           return 0;
150 #  endif
151       }
152 # endif /* HAVE_FUTIMESAT || HAVE_WORKING_UTIMES */
153
154     if (!file)
155       {
156 # if ! (HAVE_FUTIMESAT || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
157         errno = ENOSYS;
158 # endif
159
160         /* Prefer EBADF to ENOSYS if both error numbers apply.  */
161         if (errno == ENOSYS)
162           {
163             int fd2 = dup (fd);
164             int dup_errno = errno;
165             if (0 <= fd2)
166               close (fd2);
167             errno = (fd2 < 0 && dup_errno == EBADF ? EBADF : ENOSYS);
168           }
169
170         return -1;
171       }
172
173 # if HAVE_WORKING_UTIMES
174     return utimes (file, t);
175 # else
176     {
177       struct utimbuf utimbuf;
178       struct utimbuf const *ut;
179       if (timespec)
180         {
181           utimbuf.actime = timespec[0].tv_sec;
182           utimbuf.modtime = timespec[1].tv_sec;
183           ut = &utimbuf;
184         }
185       else
186         ut = NULL;
187
188       return utime (file, ut);
189     }
190 # endif /* !HAVE_WORKING_UTIMES */
191   }
192 #endif /* !HAVE_FUTIMENS */
193 }
194
195 /* Set the access and modification time stamps of FILE to be
196    TIMESPEC[0] and TIMESPEC[1], respectively.  */
197 int
198 utimens (char const *file, struct timespec const timespec[2])
199 {
200   return gl_futimens (-1, file, timespec);
201 }