1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
4 .\" <mtk.manpages@gmail.com>
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH UTIMENSAT 2 2009-12-13 "Linux" "Linux Programmer's Manual"
28 utimensat, futimens \- change file timestamps with nanosecond precision
31 .B #include <sys/stat.h>
33 .BI "int utimensat(int " dirfd ", const char *" pathname ,
34 .BI " const struct timespec " times "[2], int " flags );
36 .BI "int futimens(int " fd ", const struct timespec " times [2]);
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
50 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
60 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
71 update the timestamps of a file with nanosecond precision.
72 This contrasts with the historical
76 which permit only second and microsecond precision, respectively,
77 when setting file timestamps.
81 the file is specified via the pathname given in
85 the file whose timestamps are to be updated is specified via
86 an open file descriptor,
89 For both calls, the new file timestamps are specified in the array
92 specifies the new "last access time" (\fIatime\fP);
94 specifies the new "last modification time" (\fImtime\fP).
95 Each of the elements of
97 specifies a time as the number of seconds and nanoseconds
98 since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
99 This information is conveyed in a structure of the following form:
104 time_t tv_sec; /* seconds */
105 long tv_nsec; /* nanoseconds */
110 Updated file timestamps are set to the greatest value
111 supported by the file system that is not greater than the specified time.
117 structures has the special value
119 then the corresponding file timestamp is set to the current time.
124 structures has the special value
126 then the corresponding file timestamp is left unchanged.
127 In both of these cases, the value of the corresponding
129 .\" 2.6.22 was broken: it is not ignored
134 is NULL, then both timestamps are set to the current time.
136 .SS Permissions requirements
137 To set both file timestamps to the current time (i.e.,
145 the caller must have write access to the file;
146 .\" 2.6.22 was broken here -- for futimens() the check is
147 .\" based on whether or not the file descriptor is writable,
148 .\" not on whether the the caller's effective UID has write
149 .\" permission for the file referred to by the descriptor.
151 the caller's effective user ID must match the owner of the file; or
153 the caller must have appropriate privileges.
155 To make any change other than setting both timestamps to the
158 is not NULL, and both
162 .\" 2.6.22 was broken here:
163 .\" both must something other than *either* UTIME_OMIT *or* UTIME_NOW.
168 either condition 2 or 3 above must apply.
172 fields are specified as
174 then no file ownership or permission checks are performed,
175 and the file timestamps are not modified,
176 but other error conditions may still be detected.
179 .SS utimensat() specifics
182 is relative, then by default it is interpreted relative to the
183 directory referred to by the open file descriptor,
185 (rather than relative to the current working directory of
186 the calling process, as is done by
188 for a relative pathname).
191 for an explanation of why this can be useful.
192 .\" FIXME . Say something about O_SEARCH? (But it's not in current
193 .\" glibc (Mar 08), or kernel 2.6.25.)
203 is interpreted relative to the current working
204 directory of the calling process (like
215 field is a bit mask that may be 0, or include the following constant,
219 .B AT_SYMLINK_NOFOLLOW
222 specifies a symbolic link, then update the timestamps of the link,
223 rather than the file to which it refers.
230 On error, \-1 is returned and
232 is set to indicate the error.
246 the effective user ID of the caller does not match
247 the owner of the file,
248 the caller does not have write access to the file,
249 and the caller is not privileged
250 (Linux: does not have either the
255 .\" But Linux 2.6.22 was broken here.
256 .\" Traditionally, utime()/utimes() gives the error EACCES for the case
257 .\" where the timestamp pointer argument is NULL (i.e., set both timestamps
258 .\" to the current time), and the file is owned by a user other than the
259 .\" effective UID of the caller, and the file is not writable by the
260 .\" effective UID of the program. utimensat() also gives this error in the
261 .\" same case. However, in the same circumstances, when utimensat() is
262 .\" given a 'times' array in which both tv_nsec fields are UTIME_NOW, which
263 .\" provides equivalent functionality to specifying 'times' as NULL, the
264 .\" call succeeds. It should fail with the error EACCES in this case.
266 .\" POSIX.1-2008 has the following:
269 .\" .RB ( utimensat ())
271 .\" was not opened with
273 .\" and the permissions of the directory to which
275 .\" refers do not allow searches.
277 the file is marked immutable (see
279 .\" EXT2_IMMUTABLE_FL and similar flags for other file systems.
286 is not a valid file descriptor.
291 is a relative pathname, but
295 nor a valid file descriptor.
299 pointed to an invalid address; or,
305 is NULL or an invalid address.
312 Invalid value in one of the
314 fields (value outside range 0 to 999,999,999, and not
318 or an invalid value in one of the
323 .\" SUSv4 does not specify this error.
332 .BR AT_SYMLINK_NOFOLLOW .
336 Too many symbolic links were encountered in resolving
348 does not refer to an existing directory or file,
356 is a relative pathname, but
360 nor a file descriptor referring to a directory;
361 or, one of the prefix components of
366 The caller attempted to change one or both timestamps to a value
367 other than the current time,
368 or to change one of the timestamps to the current time while
369 leaving the other timestamp unchanged,
384 the caller's effective user ID does not match the owner of file,
385 and the caller is not privileged
386 (Linux: does not have the
390 .\" Linux 2.6.22 was broken here:
391 .\" it was not consistent with the old utimes() implementation,
392 .\" since the case when both tv_nsec fields are UTIME_NOW, was not
393 .\" treated like the (times == NULL) case.
394 the file is marked append-only or immutable (see
396 .\" EXT2_IMMUTABLE_FL EXT_APPPEND_FL and similar flags for
397 .\" other file systems.
399 .\" Why the inconsistency (which is described under NOTES) between
400 .\" EACCES and EPERM, where only EPERM tests for append-only.
401 .\" (This was also so for the older utimes() implementation.)
406 The file is on a read-only file system.
410 Search permission is denied for one of the prefix components of
414 was added to Linux in kernel 2.6.22;
415 glibc support was added with version 2.6.
419 first appeared in glibc 2.6.
424 are specified in POSIX.1-2008.
430 On Linux, timestamps cannot be changed for a file marked immutable,
431 and the only change permitted for files marked append-only is to
432 set the timestamps to the current time.
433 (This is consistent with the historical behavior of
441 is a library function implemented on top of the
444 To support this, the Linux
446 system call implements a nonstandard feature: if
448 is NULL, then the call modifies the timestamps of
449 the file referred to by the file descriptor
451 (which may refer to any type of file).
452 Using this feature, the call
453 .I "futimens(fd,\ times)"
457 utimensat(fd, NULL, times, 0);
464 on kernels before 2.6.26.
465 These bugs are either nonconformances with the POSIX.1 draft specification
466 or inconsistencies with historical Linux behavior.
468 POSIX.1 specifies that if one of the
474 then the value of the corresponding
476 field should be ignored.
477 Instead, the value of the
479 field is required to be 0 (or the error
483 Various bugs mean that for the purposes of permission checking,
488 isn't always treated the same as specifying
491 and the case where one
497 isn't treated the same as specifying
499 as a pointer to an array of structures containing arbitrary time values.
500 As a result, in some cases:
501 a) file timestamps can be updated by a process that shouldn't have
502 permission to perform updates;
503 b) file timestamps can't be updated by a process that should have
504 permission to perform updates; and
507 value is returned in case of an error.
508 .\" Below, the long description of the errors from the previous bullet
509 .\" point (abridged because it's too much detail for a man page).
519 .\" should occur if the process's effective user ID does not match
520 .\" the file owner and the process is not privileged.
521 .\" Instead, the call successfully changes one of the timestamps.
523 .\" If file is not writable by the effective user ID of the process and
524 .\" the process's effective user ID does not match the file owner and
525 .\" the process is not privileged,
528 .\" is NULL, then the error
531 .\" This error should also occur if
533 .\" points to an array of structures in which both
537 .\" Instead the call succeeds.
539 .\" If a file is marked as append-only (see
541 .\" then Linux traditionally
547 .\" argument to be used in order to update both timestamps to the current time.
552 .\" should also produce the same result when given a
554 .\" argument that points to an array of structures in which both
558 .\" Instead, the call fails with the error
561 .\" If a file is marked as immutable (see
563 .\" then Linux traditionally
576 .\" should also produce the same result when given a
578 .\" that points to an array of structures in which both
582 .\" Instead, the call fails with the error
585 POSIX.1 says that a process that has \fIwrite access to the file\fP
590 pointing to an array of structures in which both
594 in order to update both timestamps to the current time.
597 instead checks whether the
598 .IR "access mode of the file descriptor allows writing" .
599 .\" This means that a process with a file descriptor that allows
600 .\" writing could change the timestamps of a file for which it
601 .\" does not have write permission;
602 .\" conversely, a process with a read-only file descriptor won't
603 .\" be able to update the timestamps of a file,
604 .\" even if it has write permission on the file.
612 .BR path_resolution (7),