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