1 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH UTIMENSAT 2 2014-01-24 "Linux" "Linux Programmer's Manual"
28 utimensat, futimens \- change file timestamps with nanosecond precision
31 .B #include <fcntl.h> /* Definition of AT_* constants */
32 .B #include <sys/stat.h>
34 .BI "int utimensat(int " dirfd ", const char *" pathname ,
35 .BI " const struct timespec " times "[2], int " flags );
37 .BI "int futimens(int " fd ", const struct timespec " times [2]);
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
51 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
61 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
72 update the timestamps of a file with nanosecond precision.
73 This contrasts with the historical
77 which permit only second and microsecond precision, respectively,
78 when setting file timestamps.
82 the file is specified via the pathname given in
86 the file whose timestamps are to be updated is specified via
87 an open file descriptor,
90 For both calls, the new file timestamps are specified in the array
93 specifies the new "last access time" (\fIatime\fP);
95 specifies the new "last modification time" (\fImtime\fP).
96 Each of the elements of
98 specifies a time as the number of seconds and nanoseconds
99 since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
100 This information is conveyed in a structure of the following form:
105 time_t tv_sec; /* seconds */
106 long tv_nsec; /* nanoseconds */
111 Updated file timestamps are set to the greatest value
112 supported by the filesystem that is not greater than the specified time.
118 structures has the special value
120 then the corresponding file timestamp is set to the current time.
125 structures has the special value
127 then the corresponding file timestamp is left unchanged.
128 In both of these cases, the value of the corresponding
130 .\" 2.6.22 was broken: it is not ignored
135 is NULL, then both timestamps are set to the current time.
137 .SS Permissions requirements
138 To set both file timestamps to the current time (i.e.,
146 the caller must have write access to the file;
147 .\" 2.6.22 was broken here -- for futimens() the check is
148 .\" based on whether or not the file descriptor is writable,
149 .\" not on whether the caller's effective UID has write
150 .\" permission for the file referred to by the descriptor.
152 the caller's effective user ID must match the owner of the file; or
154 the caller must have appropriate privileges.
156 To make any change other than setting both timestamps to the
159 is not NULL, and neither
163 .\" 2.6.22 was broken here:
164 .\" both must be something other than *either* UTIME_OMIT *or* UTIME_NOW.
169 either condition 2 or 3 above must apply.
173 fields are specified as
175 then no file ownership or permission checks are performed,
176 and the file timestamps are not modified,
177 but other error conditions may still be detected.
180 .SS utimensat() specifics
183 is relative, then by default it is interpreted relative to the
184 directory referred to by the open file descriptor,
186 (rather than relative to the current working directory of
187 the calling process, as is done by
189 for a relative pathname).
192 for an explanation of why this can be useful.
193 .\" FIXME . Say something about O_SEARCH? (But it's not in current
194 .\" glibc (Mar 08), or kernel 2.6.25.)
204 is interpreted relative to the current working
205 directory of the calling process (like
216 field is a bit mask that may be 0, or include the following constant,
220 .B AT_SYMLINK_NOFOLLOW
223 specifies a symbolic link, then update the timestamps of the link,
224 rather than the file to which it refers.
231 On error, \-1 is returned and
233 is set to indicate the error.
247 the effective user ID of the caller does not match
248 the owner of the file,
249 the caller does not have write access to the file,
250 and the caller is not privileged
251 (Linux: does not have either the
256 .\" But Linux 2.6.22 was broken here.
257 .\" Traditionally, utime()/utimes() gives the error EACCES for the case
258 .\" where the timestamp pointer argument is NULL (i.e., set both timestamps
259 .\" to the current time), and the file is owned by a user other than the
260 .\" effective UID of the caller, and the file is not writable by the
261 .\" effective UID of the program. utimensat() also gives this error in the
262 .\" same case. However, in the same circumstances, when utimensat() is
263 .\" given a 'times' array in which both tv_nsec fields are UTIME_NOW, which
264 .\" provides equivalent functionality to specifying 'times' as NULL, the
265 .\" call succeeds. It should fail with the error EACCES in this case.
267 .\" POSIX.1-2008 has the following:
270 .\" .RB ( utimensat ())
272 .\" was not opened with
274 .\" and the permissions of the directory to which
276 .\" refers do not allow searches.
278 the file is marked immutable (see
280 .\" EXT2_IMMUTABLE_FL and similar flags for other filesystems.
287 is not a valid file descriptor.
292 is a relative pathname, but
296 nor a valid file descriptor.
300 pointed to an invalid address; or,
306 is NULL or an invalid address.
313 Invalid value in one of the
315 fields (value outside range 0 to 999,999,999, and not
319 or an invalid value in one of the
324 .\" SUSv4 does not specify this error.
333 .BR AT_SYMLINK_NOFOLLOW .
337 Too many symbolic links were encountered in resolving
349 does not refer to an existing directory or file,
357 is a relative pathname, but
361 nor a file descriptor referring to a directory;
362 or, one of the prefix components of
367 The caller attempted to change one or both timestamps to a value
368 other than the current time,
369 or to change one of the timestamps to the current time while
370 leaving the other timestamp unchanged,
385 the caller's effective user ID does not match the owner of file,
386 and the caller is not privileged
387 (Linux: does not have the
391 .\" Linux 2.6.22 was broken here:
392 .\" it was not consistent with the old utimes() implementation,
393 .\" since the case when both tv_nsec fields are UTIME_NOW, was not
394 .\" treated like the (times == NULL) case.
395 the file is marked append-only or immutable (see
397 .\" EXT2_IMMUTABLE_FL EXT_APPPEND_FL and similar flags for
398 .\" other filesystems.
400 .\" Why the inconsistency (which is described under NOTES) between
401 .\" EACCES and EPERM, where only EPERM tests for append-only.
402 .\" (This was also so for the older utimes() implementation.)
407 The file is on a read-only filesystem.
411 Search permission is denied for one of the prefix components of
415 was added to Linux in kernel 2.6.22;
416 glibc support was added with version 2.6.
420 first appeared in glibc 2.6.
425 are specified in POSIX.1-2008.
431 On Linux, timestamps cannot be changed for a file marked immutable,
432 and the only change permitted for files marked append-only is to
433 set the timestamps to the current time.
434 (This is consistent with the historical behavior of
442 is a library function implemented on top of the
445 To support this, the Linux
447 system call implements a nonstandard feature: if
449 is NULL, then the call modifies the timestamps of
450 the file referred to by the file descriptor
452 (which may refer to any type of file).
453 Using this feature, the call
454 .I "futimens(fd,\ times)"
458 utimensat(fd, NULL, times, 0);
465 on kernels before 2.6.26.
466 These bugs are either nonconformances with the POSIX.1 draft specification
467 or inconsistencies with historical Linux behavior.
469 POSIX.1 specifies that if one of the
475 then the value of the corresponding
477 field should be ignored.
478 Instead, the value of the
480 field is required to be 0 (or the error
484 Various bugs mean that for the purposes of permission checking,
489 isn't always treated the same as specifying
492 and the case where one
498 isn't treated the same as specifying
500 as a pointer to an array of structures containing arbitrary time values.
501 As a result, in some cases:
502 a) file timestamps can be updated by a process that shouldn't have
503 permission to perform updates;
504 b) file timestamps can't be updated by a process that should have
505 permission to perform updates; and
508 value is returned in case of an error.
509 .\" Below, the long description of the errors from the previous bullet
510 .\" point (abridged because it's too much detail for a man page).
520 .\" should occur if the process's effective user ID does not match
521 .\" the file owner and the process is not privileged.
522 .\" Instead, the call successfully changes one of the timestamps.
524 .\" If file is not writable by the effective user ID of the process and
525 .\" the process's effective user ID does not match the file owner and
526 .\" the process is not privileged,
529 .\" is NULL, then the error
532 .\" This error should also occur if
534 .\" points to an array of structures in which both
538 .\" Instead the call succeeds.
540 .\" If a file is marked as append-only (see
542 .\" then Linux traditionally
548 .\" argument to be used in order to update both timestamps to the current time.
553 .\" should also produce the same result when given a
555 .\" argument that points to an array of structures in which both
559 .\" Instead, the call fails with the error
562 .\" If a file is marked as immutable (see
564 .\" then Linux traditionally
577 .\" should also produce the same result when given a
579 .\" that points to an array of structures in which both
583 .\" Instead, the call fails with the error
586 POSIX.1 says that a process that has \fIwrite access to the file\fP
591 pointing to an array of structures in which both
595 in order to update both timestamps to the current time.
598 instead checks whether the
599 .IR "access mode of the file descriptor allows writing" .
600 .\" This means that a process with a file descriptor that allows
601 .\" writing could change the timestamps of a file for which it
602 .\" does not have write permission;
603 .\" conversely, a process with a read-only file descriptor won't
604 .\" be able to update the timestamps of a file,
605 .\" even if it has write permission on the file.
613 .BR path_resolution (7),
616 This page is part of release 3.68 of the Linux
619 A description of the project,
620 information about reporting bugs,
621 and the latest version of this page,
623 \%http://www.kernel.org/doc/man\-pages/.