source: trunk/coreutils/lib/utimens.c@ 2560

Last change on this file since 2560 was 2560, checked in by bird, 20 years ago

Initial porting.

File size: 4.5 KB
Line 
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
38struct 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#ifdef __INNOTEK_LIBC__ /* fs doesn't to better than seconds, but we must use futimes! */
64# define HAVE_WORKING_UTIMES 1
65#endif
66
67
68/* Set the access and modification time stamps of FD (a.k.a. FILE) to be
69 TIMESPEC[0] and TIMESPEC[1], respectively.
70 FD must be either negative -- in which case it is ignored --
71 or a file descriptor that is open on FILE.
72 If FD is nonnegative, then FILE can be NULL, which means
73 use just futimes (or equivalent) instead of utimes (or equivalent),
74 and fail if on an old system without futimes (or equivalent).
75 If TIMESPEC is null, set the time stamps to the current time.
76 Return 0 on success, -1 (setting errno) on failure. */
77
78int
79futimens (int fd ATTRIBUTE_UNUSED,
80 char const *file, struct timespec const timespec[2])
81{
82 /* There's currently no interface to set file timestamps with
83 nanosecond resolution, so do the best we can, discarding any
84 fractional part of the timestamp. */
85#if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
86 struct timeval timeval[2];
87 struct timeval const *t;
88 if (timespec)
89 {
90 timeval[0].tv_sec = timespec[0].tv_sec;
91 timeval[0].tv_usec = timespec[0].tv_nsec / 1000;
92 timeval[1].tv_sec = timespec[1].tv_sec;
93 timeval[1].tv_usec = timespec[1].tv_nsec / 1000;
94 t = timeval;
95 }
96 else
97 t = NULL;
98
99# if HAVE_FUTIMESAT
100 return fd < 0 ? futimesat (AT_FDCWD, file, t) : futimesat (fd, NULL, t);
101# elif HAVE_FUTIMES
102 if (0 <= fd)
103 {
104 if (futimes (fd, t) == 0)
105 return 0;
106
107 /* Don't worry about trying to speed things up by returning right
108 away here. glibc futimes can incorrectly fail with errno ==
109 ENOENT if /proc isn't mounted. Also, Mandrake 10.0 in high
110 security mode doesn't allow ordinary users to read /proc/self, so
111 glibc futimes incorrectly fails with errno == EACCES. If futimes
112 fails with errno == EIO, EPERM, or EROFS, it's probably safe to
113 fail right away, but these cases are rare enough that they're not
114 worth optimizing, and who knows what other messed-up systems are
115 out there? So play it safe and fall back on the code below. */
116 }
117# endif
118#endif
119
120#if ! HAVE_FUTIMESAT
121
122 if (!file)
123 {
124# if ! (HAVE_WORKING_UTIMES && HAVE_FUTIMES)
125 errno = ENOSYS;
126# endif
127
128 /* Prefer EBADF to ENOSYS if both error numbers apply. */
129 if (errno == ENOSYS)
130 {
131 int fd2 = dup (fd);
132 int dup_errno = errno;
133 if (0 <= fd2)
134 close (fd2);
135 errno = (fd2 < 0 && dup_errno == EBADF ? EBADF : ENOSYS);
136 }
137
138 return -1;
139 }
140
141# if HAVE_WORKING_UTIMES
142 return utimes (file, t);
143# else
144 {
145 struct utimbuf utimbuf;
146 struct utimbuf const *ut;
147 if (timespec)
148 {
149 utimbuf.actime = timespec[0].tv_sec;
150 utimbuf.modtime = timespec[1].tv_sec;
151 ut = &utimbuf;
152 }
153 else
154 ut = NULL;
155
156 return utime (file, ut);
157 }
158# endif
159
160#endif
161}
162
163/* Set the access and modification time stamps of FILE to be
164 TIMESPEC[0] and TIMESPEC[1], respectively. */
165int
166utimens (char const *file, struct timespec const timespec[2])
167{
168 return futimens (-1, file, timespec);
169}
Note: See TracBrowser for help on using the repository browser.