Update copyright years: This file was modified in 2005.
[gnulib.git] / 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 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include "utimens.h"
28
29 #include <errno.h>
30 #include <fcntl.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) || __STRICT_ANSI__
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 futimens (int fd ATTRIBUTE_UNUSED,
79           char const *file, struct timespec const timespec[2])
80 {
81   /* There's currently no interface to set file timestamps with
82      nanosecond resolution, so do the best we can, discarding any
83      fractional part of the timestamp.  */
84 #if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
85   struct timeval timeval[2];
86   struct timeval const *t;
87   if (timespec)
88     {
89       timeval[0].tv_sec = timespec[0].tv_sec;
90       timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
91       timeval[1].tv_sec = timespec[1].tv_sec;
92       timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
93       t = timeval;
94     }
95   else
96     t = NULL;
97
98
99   if (fd < 0)
100     {
101 # if HAVE_FUTIMESAT
102       return futimesat (AT_FDCWD, file, t);
103 # endif
104     }
105   else
106     {
107       /* If futimesat or futimes fails here, don't try to speed things
108          up by returning right away.  glibc can incorrectly fail with
109          errno == ENOENT if /proc isn't mounted.  Also, Mandrake 10.0
110          in high security mode doesn't allow ordinary users to read
111          /proc/self, so glibc incorrectly fails with errno == EACCES.
112          If errno == EIO, EPERM, or EROFS, it's probably safe to fail
113          right away, but these cases are rare enough that they're not
114          worth optimizing, and who knows what other messed-up systems
115          are out there?  So play it safe and fall back on the code
116          below.  */
117 # if HAVE_FUTIMESAT
118       if (futimesat (fd, NULL, t) == 0)
119         return 0;
120 # elif HAVE_FUTIMES
121       if (futimes (fd, t) == 0)
122         return 0;
123 # endif
124     }
125 #endif
126
127   if (!file)
128     {
129 #if ! (HAVE_FUTIMESAT || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
130       errno = ENOSYS;
131 #endif
132
133       /* Prefer EBADF to ENOSYS if both error numbers apply.  */
134       if (errno == ENOSYS)
135         {
136           int fd2 = dup (fd);
137           int dup_errno = errno;
138           if (0 <= fd2)
139             close (fd2);
140           errno = (fd2 < 0 && dup_errno == EBADF ? EBADF : ENOSYS);
141         }
142
143       return -1;
144     }
145
146 #if HAVE_WORKING_UTIMES
147   return utimes (file, t);
148 #else
149   {
150     struct utimbuf utimbuf;
151     struct utimbuf const *ut;
152     if (timespec)
153       {
154         utimbuf.actime = timespec[0].tv_sec;
155         utimbuf.modtime = timespec[1].tv_sec;
156         ut = &utimbuf;
157       }
158     else
159       ut = NULL;
160
161     return utime (file, ut);
162   }
163 #endif
164 }
165
166 /* Set the access and modification time stamps of FILE to be
167    TIMESPEC[0] and TIMESPEC[1], respectively.  */
168 int
169 utimens (char const *file, struct timespec const timespec[2])
170 {
171   return futimens (-1, file, timespec);
172 }