Add, forgotten in last commit.
[gnulib.git] / lib / utimens.c
1 /* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
2
3    This program is free software; you can redistribute it and/or modify it
4    under the terms of the GNU General Public License as published by the
5    Free Software Foundation; either version 2, or (at your option) any
6    later version.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software Foundation,
15    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
16
17 /* Written by Paul Eggert.  */
18
19 /* derived from a function in touch.c */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
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 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
56 # define __attribute__(x)
57 #endif
58
59 #ifndef ATTRIBUTE_UNUSED
60 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
61 #endif
62
63 /* Set the access and modification time stamps of FD (a.k.a. FILE) to be
64    TIMESPEC[0] and TIMESPEC[1], respectively.
65    FD must be either negative -- in which case it is ignored --
66    or a file descriptor that is open on FILE.
67    If FD is nonnegative, then FILE can be NULL, which means
68    use just futimes (or equivalent) instead of utimes (or equivalent),
69    and fail if on an old system without futimes (or equivalent).
70    If TIMESPEC is null, set the time stamps to the current time.
71    Return 0 on success, -1 (setting errno) on failure.  */
72
73 int
74 futimens (int fd ATTRIBUTE_UNUSED,
75           char const *file, struct timespec const timespec[2])
76 {
77   /* There's currently no interface to set file timestamps with
78      nanosecond resolution, so do the best we can, discarding any
79      fractional part of the timestamp.  */
80 #if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
81   struct timeval timeval[2];
82   struct timeval const *t;
83   if (timespec)
84     {
85       timeval[0].tv_sec = timespec[0].tv_sec;
86       timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
87       timeval[1].tv_sec = timespec[1].tv_sec;
88       timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
89       t = timeval;
90     }
91   else
92     t = NULL;
93
94 # if HAVE_FUTIMESAT
95   return fd < 0 ? futimesat (AT_FDCWD, file, t) : futimesat (fd, NULL, t);
96 # elif HAVE_FUTIMES
97   if (0 <= fd)
98     {
99       if (futimes (fd, t) == 0)
100         return 0;
101
102       /* On GNU/Linux without the futimes syscall and without /proc
103          mounted, glibc futimes fails with errno == ENOENT.  Fall back
104          on utimes if we get a weird error number like that.  */
105       switch (errno)
106         {
107         case EACCES:
108         case EIO:
109         case EPERM:
110         case EROFS:
111           return -1;
112         }
113     }
114 # endif
115 #endif
116
117 #if ! HAVE_FUTIMESAT
118
119   if (!file)
120     {
121 # if ! (HAVE_WORKING_UTIMES && HAVE_FUTIMES)
122       errno = ENOSYS;
123 # endif
124
125       /* Prefer EBADF to ENOSYS if both error numbers apply.  */
126       if (errno == ENOSYS)
127         {
128           int fd2 = dup (fd);
129           int dup_errno = errno;
130           if (0 <= fd2)
131             close (fd2);
132           errno = (fd2 < 0 && dup_errno == EBADF ? EBADF : ENOSYS);
133         }
134
135       return -1;
136     }
137
138 # if HAVE_WORKING_UTIMES
139   return utimes (file, t);
140 # else
141   {
142     struct utimbuf utimbuf;
143     struct utimbuf const *ut;
144     if (timespec)
145       {
146         utimbuf.actime = timespec[0].tv_sec;
147         utimbuf.modtime = timespec[1].tv_sec;
148         ut = &utimbuf;
149       }
150     else
151       ut = NULL;
152
153     return utime (file, ut);
154   }
155 # endif
156
157 #endif
158 }
159
160 /* Set the access and modification time stamps of FILE to be
161    TIMESPEC[0] and TIMESPEC[1], respectively.  */
162 int
163 utimens (char const *file, struct timespec const timespec[2])
164 {
165   return futimens (-1, file, timespec);
166 }