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 int futimens(int fd, const struct timespec times[2]);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 utimensat():
19 Since glibc 2.10:
20 _POSIX_C_SOURCE >= 200809L
21 Before glibc 2.10:
22 _ATFILE_SOURCE
23
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 re‐
105 turned and errno is set to indicate the error.
106
108 EACCES times is NULL, or both tv_nsec values are UTIME_NOW, and the ef‐
109 fective user ID of the caller does not match the owner of the
110 file, the caller does not have write access to the file, and the
111 caller is not privileged (Linux: does not have either the
112 CAP_FOWNER or the CAP_DAC_OVERRIDE capability).
113
114 EBADF (futimens()) fd is not a valid file descriptor.
115
116 EBADF (utimensat()) pathname is relative but dirfd is neither AT_FDCWD
117 nor a valid file descriptor.
118
119 EFAULT times pointed to an invalid address; or, dirfd was AT_FDCWD, and
120 pathname is NULL or an invalid address.
121
122 EINVAL Invalid value in flags.
123
124 EINVAL Invalid value in one of the tv_nsec fields (value outside range
125 0 to 999,999,999, and not UTIME_NOW or UTIME_OMIT); or an in‐
126 valid value in one of the tv_sec fields.
127
128 EINVAL pathname is NULL, dirfd is not AT_FDCWD, and flags contains
129 AT_SYMLINK_NOFOLLOW.
130
131 ELOOP (utimensat()) Too many symbolic links were encountered in re‐
132 solving pathname.
133
134 ENAMETOOLONG
135 (utimensat()) pathname is too long.
136
137 ENOENT (utimensat()) A component of pathname does not refer to an ex‐
138 isting directory or file, or pathname is an empty string.
139
140 ENOTDIR
141 (utimensat()) pathname is a relative pathname, but dirfd is nei‐
142 ther AT_FDCWD nor a file descriptor referring to a directory;
143 or, one of the prefix components of pathname is not a directory.
144
145 EPERM The caller attempted to change one or both timestamps to a value
146 other than the current time, or to change one of the timestamps
147 to the current time while leaving the other timestamp unchanged,
148 (i.e., times is not NULL, neither tv_nsec field is UTIME_NOW,
149 and neither tv_nsec field is UTIME_OMIT) and either:
150
151 * the caller's effective user ID does not match the owner of
152 file, and the caller is not privileged (Linux: does not have
153 the CAP_FOWNER capability); or,
154
155 * the file is marked append-only or immutable (see chattr(1)).
156
157 EROFS The file is on a read-only filesystem.
158
159 ESRCH (utimensat()) Search permission is denied for one of the prefix
160 components of pathname.
161
163 utimensat() was added to Linux in kernel 2.6.22; glibc support was
164 added with version 2.6.
165
166 Support for futimens() first appeared in glibc 2.6.
167
169 For an explanation of the terms used in this section, see at‐
170 tributes(7).
171
172 ┌────────────────────────────────────────────┬───────────────┬─────────┐
173 │Interface │ Attribute │ Value │
174 ├────────────────────────────────────────────┼───────────────┼─────────┤
175 │utimensat(), futimens() │ Thread safety │ MT-Safe │
176 └────────────────────────────────────────────┴───────────────┴─────────┘
177
179 futimens() and utimensat() are specified in POSIX.1-2008.
180
182 utimensat() obsoletes futimesat(2).
183
184 On Linux, timestamps cannot be changed for a file marked immutable, and
185 the only change permitted for files marked append-only is to set the
186 timestamps to the current time. (This is consistent with the histori‐
187 cal behavior of utime(2) and utimes(2) on Linux.)
188
189 If both tv_nsec fields are specified as UTIME_OMIT, then the Linux im‐
190 plementation of utimensat() succeeds even if the file referred to by
191 dirfd and pathname does not exist.
192
193 C library/kernel ABI differences
194 On Linux, futimens() is a library function implemented on top of the
195 utimensat() system call. To support this, the Linux utimensat() system
196 call implements a nonstandard feature: if pathname is NULL, then the
197 call modifies the timestamps of the file referred to by the file de‐
198 scriptor dirfd (which may refer to any type of file). Using this fea‐
199 ture, the call futimens(fd, times) is implemented as:
200
201 utimensat(fd, NULL, times, 0);
202
203 Note, however, that the glibc wrapper for utimensat() disallows passing
204 NULL as the value for pathname: the wrapper function returns the error
205 EINVAL in this case.
206
208 Several bugs afflict utimensat() and futimens() on kernels before
209 2.6.26. These bugs are either nonconformances with the POSIX.1 draft
210 specification or inconsistencies with historical Linux behavior.
211
212 * POSIX.1 specifies that if one of the tv_nsec fields has the value
213 UTIME_NOW or UTIME_OMIT, then the value of the corresponding tv_sec
214 field should be ignored. Instead, the value of the tv_sec field is
215 required to be 0 (or the error EINVAL results).
216
217 * Various bugs mean that for the purposes of permission checking, the
218 case where both tv_nsec fields are set to UTIME_NOW isn't always
219 treated the same as specifying times as NULL, and the case where one
220 tv_nsec value is UTIME_NOW and the other is UTIME_OMIT isn't treated
221 the same as specifying times as a pointer to an array of structures
222 containing arbitrary time values. As a result, in some cases: a)
223 file timestamps can be updated by a process that shouldn't have per‐
224 mission to perform updates; b) file timestamps can't be updated by a
225 process that should have permission to perform updates; and c) the
226 wrong errno value is returned in case of an error.
227
228 * POSIX.1 says that a process that has write access to the file can
229 make a call with times as NULL, or with times pointing to an array
230 of structures in which both tv_nsec fields are UTIME_NOW, in order
231 to update both timestamps to the current time. However, futimens()
232 instead checks whether the access mode of the file descriptor allows
233 writing.
234
236 chattr(1), touch(1), futimesat(2), openat(2), stat(2), utimes(2), fu‐
237 times(3), inode(7), path_resolution(7), symlink(7)
238
240 This page is part of release 5.13 of the Linux man-pages project. A
241 description of the project, information about reporting bugs, and the
242 latest version of this page, can be found at
243 https://www.kernel.org/doc/man-pages/.
244
245
246
247Linux 2021-08-27 UTIMENSAT(2)