1UTIMENSAT(2) Linux Programmer's Manual UTIMENSAT(2)
2
3
4
6 utimensat, futimens - change file timestamps with nanosecond precision
7
9 #include <fcntl.h> /* Definition of AT_* constants */
10 #include <sys/stat.h>
11
12 int utimensat(int dirfd, const char *pathname,
13 const struct timespec times[2], int flags);
14
15 int futimens(int fd, const struct timespec times[2]);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 utimensat():
20 Since glibc 2.10:
21 _POSIX_C_SOURCE >= 200809L
22 Before glibc 2.10:
23 _ATFILE_SOURCE
24 futimens():
25 Since glibc 2.10:
26 _POSIX_C_SOURCE >= 200809L
27 Before glibc 2.10:
28 _GNU_SOURCE
29
31 utimensat() and futimens() update the timestamps of a file with
32 nanosecond precision. This contrasts with the historical utime(2) and
33 utimes(2), which permit only second and microsecond precision, respec‐
34 tively, when setting file timestamps.
35
36 With utimensat() the file is specified via the pathname given in path‐
37 name. With futimens() the file whose timestamps are to be updated is
38 specified via an open file descriptor, fd.
39
40 For both calls, the new file timestamps are specified in the array
41 times: times[0] specifies the new "last access time" (atime); times[1]
42 specifies the new "last modification time" (mtime). Each of the ele‐
43 ments of times specifies a time as the number of seconds and nanosec‐
44 onds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). This informa‐
45 tion is conveyed in a structure of the following form:
46
47 struct timespec {
48 time_t tv_sec; /* seconds */
49 long tv_nsec; /* nanoseconds */
50 };
51
52 Updated file timestamps are set to the greatest value supported by the
53 filesystem that is not greater than the specified time.
54
55 If the tv_nsec field of one of the timespec structures has the special
56 value UTIME_NOW, then the corresponding file timestamp is set to the
57 current time. If the tv_nsec field of one of the timespec structures
58 has the special value UTIME_OMIT, then the corresponding file timestamp
59 is left unchanged. In both of these cases, the value of the corre‐
60 sponding tv_sec field is ignored.
61
62 If times is NULL, then both timestamps are set to the current time.
63
64 Permissions requirements
65 To set both file timestamps to the current time (i.e., times is NULL,
66 or both tv_nsec fields specify UTIME_NOW), either:
67
68 1. the caller must have write access to the file;
69
70 2. the caller's effective user ID must match the owner of the file; or
71
72 3. the caller must have appropriate privileges.
73
74 To make any change other than setting both timestamps to the current
75 time (i.e., times is not NULL, and neither tv_nsec field is UTIME_NOW
76 and neither tv_nsec field is UTIME_OMIT), either condition 2 or 3 above
77 must apply.
78
79 If both tv_nsec fields are specified as UTIME_OMIT, then no file owner‐
80 ship or permission checks are performed, and the file timestamps are
81 not modified, but other error conditions may still be detected.
82
83 utimensat() specifics
84 If pathname is relative, then by default it is interpreted relative to
85 the directory referred to by the open file descriptor, dirfd (rather
86 than relative to the current working directory of the calling process,
87 as is done by utimes(2) for a relative pathname). See openat(2) for an
88 explanation of why this can be useful.
89
90 If pathname is relative and dirfd is the special value AT_FDCWD, then
91 pathname is interpreted relative to the current working directory of
92 the calling process (like utimes(2)).
93
94 If pathname is absolute, then dirfd is ignored.
95
96 The flags field is a bit mask that may be 0, or include the following
97 constant, defined in <fcntl.h>:
98
99 AT_SYMLINK_NOFOLLOW
100 If pathname specifies a symbolic link, then update the time‐
101 stamps of the link, rather than the file to which it refers.
102
104 On success, utimensat() and futimens() return 0. On error, -1 is
105 returned and errno is set to indicate the error.
106
108 EACCES times is NULL, or both tv_nsec values are UTIME_NOW, and either:
109
110 * the effective user ID of the caller does not match the owner
111 of the file, the caller does not have write access to the
112 file, and the caller is not privileged (Linux: does not have
113 either the CAP_FOWNER or the CAP_DAC_OVERRIDE capability);
114 or,
115
116 * the file is marked immutable (see chattr(1)).
117
118 EBADF (futimens()) fd is not a valid file descriptor.
119
120 EBADF (utimensat()) pathname is a relative pathname, but dirfd is nei‐
121 ther AT_FDCWD nor a valid file descriptor.
122
123 EFAULT times pointed to an invalid address; or, dirfd was AT_FDCWD, and
124 pathname is NULL or an invalid address.
125
126 EINVAL Invalid value in flags.
127
128 EINVAL Invalid value in one of the tv_nsec fields (value outside range
129 0 to 999,999,999, and not UTIME_NOW or UTIME_OMIT); or an
130 invalid value in one of the tv_sec fields.
131
132 EINVAL pathname is NULL, dirfd is not AT_FDCWD, and flags contains
133 AT_SYMLINK_NOFOLLOW.
134
135 ELOOP (utimensat()) Too many symbolic links were encountered in
136 resolving pathname.
137
138 ENAMETOOLONG
139 (utimensat()) pathname is too long.
140
141 ENOENT (utimensat()) A component of pathname does not refer to an
142 existing directory or file, or pathname is an empty string.
143
144 ENOTDIR
145 (utimensat()) pathname is a relative pathname, but dirfd is nei‐
146 ther AT_FDCWD nor a file descriptor referring to a directory;
147 or, one of the prefix components of pathname is not a directory.
148
149 EPERM The caller attempted to change one or both timestamps to a value
150 other than the current time, or to change one of the timestamps
151 to the current time while leaving the other timestamp unchanged,
152 (i.e., times is not NULL, neither tv_nsec field is UTIME_NOW,
153 and neither tv_nsec field is UTIME_OMIT) and either:
154
155 * the caller's effective user ID does not match the owner of
156 file, and the caller is not privileged (Linux: does not have
157 the CAP_FOWNER capability); or,
158
159 * the file is marked append-only or immutable (see chattr(1)).
160
161 EROFS The file is on a read-only filesystem.
162
163 ESRCH (utimensat()) Search permission is denied for one of the prefix
164 components of pathname.
165
167 utimensat() was added to Linux in kernel 2.6.22; glibc support was
168 added with version 2.6.
169
170 Support for futimens() first appeared in glibc 2.6.
171
173 For an explanation of the terms used in this section, see
174 attributes(7).
175
176 ┌────────────────────────┬───────────────┬─────────┐
177 │Interface │ Attribute │ Value │
178 ├────────────────────────┼───────────────┼─────────┤
179 │utimensat(), futimens() │ Thread safety │ MT-Safe │
180 └────────────────────────┴───────────────┴─────────┘
181
183 futimens() and utimensat() are specified in POSIX.1-2008.
184
186 utimensat() obsoletes futimesat(2).
187
188 On Linux, timestamps cannot be changed for a file marked immutable, and
189 the only change permitted for files marked append-only is to set the
190 timestamps to the current time. (This is consistent with the histori‐
191 cal behavior of utime(2) and utimes(2) on Linux.)
192
193 If both tv_nsec fields are specified as UTIME_OMIT, then the Linux
194 implementation of utimensat() succeeds even if the file referred to by
195 dirfd and pathname does not exist.
196
197 C library/kernel ABI differences
198 On Linux, futimens() is a library function implemented on top of the
199 utimensat() system call. To support this, the Linux utimensat() system
200 call implements a nonstandard feature: if pathname is NULL, then the
201 call modifies the timestamps of the file referred to by the file
202 descriptor dirfd (which may refer to any type of file). Using this
203 feature, the call futimens(fd, times) is implemented as:
204
205 utimensat(fd, NULL, times, 0);
206
207 Note, however, that the glibc wrapper for utimensat() disallows passing
208 NULL as the value for pathname: the wrapper function returns the error
209 EINVAL in this case.
210
212 Several bugs afflict utimensat() and futimens() on kernels before
213 2.6.26. These bugs are either nonconformances with the POSIX.1 draft
214 specification or inconsistencies with historical Linux behavior.
215
216 * POSIX.1 specifies that if one of the tv_nsec fields has the value
217 UTIME_NOW or UTIME_OMIT, then the value of the corresponding tv_sec
218 field should be ignored. Instead, the value of the tv_sec field is
219 required to be 0 (or the error EINVAL results).
220
221 * Various bugs mean that for the purposes of permission checking, the
222 case where both tv_nsec fields are set to UTIME_NOW isn't always
223 treated the same as specifying times as NULL, and the case where one
224 tv_nsec value is UTIME_NOW and the other is UTIME_OMIT isn't treated
225 the same as specifying times as a pointer to an array of structures
226 containing arbitrary time values. As a result, in some cases: a)
227 file timestamps can be updated by a process that shouldn't have per‐
228 mission to perform updates; b) file timestamps can't be updated by a
229 process that should have permission to perform updates; and c) the
230 wrong errno value is returned in case of an error.
231
232 * POSIX.1 says that a process that has write access to the file can
233 make a call with times as NULL, or with times pointing to an array
234 of structures in which both tv_nsec fields are UTIME_NOW, in order
235 to update both timestamps to the current time. However, futimens()
236 instead checks whether the access mode of the file descriptor allows
237 writing.
238
240 chattr(1), touch(1), futimesat(2), openat(2), stat(2), utimes(2),
241 futimes(3), inode(7), path_resolution(7), symlink(7)
242
244 This page is part of release 4.16 of the Linux man-pages project. A
245 description of the project, information about reporting bugs, and the
246 latest version of this page, can be found at
247 https://www.kernel.org/doc/man-pages/.
248
249
250
251Linux 2017-09-15 UTIMENSAT(2)