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)):
46 Since glibc 2.10: _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
53 Since glibc 2.10: _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
61 update the timestamps of a file with nanosecond precision.
62 This contrasts with the historical
66 which permit only second and microsecond precision, respectively,
67 when setting file timestamps.
71 the file is specified via the pathname given in
75 the file whose timestamps are to be updated is specified via
76 an open file descriptor,
79 For both calls, the new file timestamps are specified in the array
82 specifies the new "last access time" (\fIatime\fP);
84 specifies the new "last modification time" (\fImtime\fP).
85 Each of the elements of
87 specifies a time as the the number of seconds and nanoseconds
88 since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
89 This information is conveyed in a structure of the following form:
94 time_t tv_sec; /* seconds */
95 long tv_nsec; /* nanoseconds */
100 Updated file timestamps are set to the greatest value
101 supported by the file system that is not greater than the specified time.
107 structures has the special value
109 then the corresponding file timestamp is set to the current time.
114 structures has the special value
116 then the corresponding file timestamp is left unchanged.
117 In both of these cases, the value of the corresponding
119 .\" 2.6.22 was broken: it is not ignored
124 is NULL, then both timestamps are set to the current time.
126 .SS Permissions requirements
127 To set both file timestamps to the current time (i.e.,
135 the caller must have write access to the file;
136 .\" 2.6.22 was broken here -- for futimens() the check is
137 .\" based on whether or not the file descriptor is writable,
138 .\" not on whether the the caller's effective UID has write
139 .\" permission for the file referred to by the descriptor.
141 the caller's effective user ID must match the owner of the file; or
143 the caller must have appropriate privileges.
145 To make any change other than setting both timestamps to the
148 is not NULL, and both
152 .\" 2.6.22 was broken here:
153 .\" both must something other than *either* UTIME_OMIT *or* UTIME_NOW.
158 either condition 2 or 3 above must apply.
162 fields are specified as
164 then no file ownership or permission checks are performed,
165 and the file timestamps are not modified,
166 but other error conditions may still be detected.
169 .SS utimensat() specifics
172 is relative, then by default it is interpreted relative to the
173 directory referred to by the open file descriptor,
175 (rather than relative to the current working directory of
176 the calling process, as is done by
178 for a relative pathname).
181 for an explanation of why this can be useful.
182 .\" FIXME . Say something about O_SEARCH? (But it's not in current
183 .\" glibc (Mar 08), or kernel 2.6.25.)
193 is interpreted relative to the current working
194 directory of the calling process (like
205 field is a bit mask that may be 0, or include the following constant,
209 .B AT_SYMLINK_NOFOLLOW
212 specifies a symbolic link, then update the timestamps of the link,
213 rather than the file to which it refers.
220 On error, \-1 is returned and
222 is set to indicate the error.
236 the effective user ID of the caller does not match
237 the owner of the file,
238 the caller does not have write access to the file,
239 and the caller is not privileged
240 (Linux: does not have either the
245 .\" But Linux 2.6.22 was broken here.
246 .\" Traditionally, utime()/utimes() gives the error EACCES for the case
247 .\" where the timestamp pointer argument is NULL (i.e., set both timestamps
248 .\" to the current time), and the file is owned by a user other than the
249 .\" effective UID of the caller, and the file is not writable by the
250 .\" effective UID of the program. utimensat() also gives this error in the
251 .\" same case. However, in the same circumstances, when utimensat() is
252 .\" given a 'times' array in which both tv_nsec fields are UTIME_NOW, which
253 .\" provides equivalent functionality to specifying 'times' as NULL, the
254 .\" call succeeds. It should fail with the error EACCES in this case.
256 .\" POSIX.1-2008 has the following:
259 .\" .RB ( utimensat ())
261 .\" was not opened with
263 .\" and the permissions of the directory to which
265 .\" refers do not allow searches.
267 the file is marked immutable (see
269 .\" EXT2_IMMUTABLE_FL and similar flags for other file systems.
276 is not a valid file descriptor.
281 is a relative pathname, but
285 nor a valid file descriptor.
289 pointed to an invalid address; or,
295 is NULL or an invalid address.
302 Invalid value in one of the
304 fields (value outside range 0 to 999,999,999, and not
308 or an invalid value in one of the
313 .\" SUSv4 does not specify this error.
322 .BR AT_SYMLINK_NOFOLLOW .
326 Too many symbolic links were encountered in resolving
338 does not refer to an existing directory or file,
346 is a relative pathname, but
350 nor a file descriptor referring to a directory;
351 or, one of the prefix components of
356 The caller attempted to change one or both timestamps to a value
357 other than the current time,
358 or to change one of the timestamps to the current time while
359 leaving the other timestamp unchanged,
374 the caller's effective user ID does not match the owner of file,
375 and the caller is not privileged
376 (Linux: does not have the
380 .\" Linux 2.6.22 was broken here:
381 .\" it was not consistent with the old utimes() implementation,
382 .\" since the case when both tv_nsec fields are UTIME_NOW, was not
383 .\" treated like the (times == NULL) case.
384 the file is marked append-only or immutable (see
386 .\" EXT2_IMMUTABLE_FL EXT_APPPEND_FL and similar flags for
387 .\" other file systems.
389 .\" Why the inconsistency (which is described under NOTES) between
390 .\" EACCES and EPERM, where only EPERM tests for append-only.
391 .\" (This was also so for the older utimes() implementation.)
396 The file is on a read-only file system.
400 Search permission is denied for one of the prefix components of
404 was added to Linux in kernel 2.6.22;
405 glibc support was added with version 2.6.
409 first appeared in glibc 2.6.
414 are specified in POSIX.1-2008.
420 On Linux, timestamps cannot be changed for a file marked immutable,
421 and the only change permitted for files marked append-only is to
422 set the timestamps to the current time.
423 (This is consistent with the historical behavior of
431 is a library function implemented on top of the
434 To support this, the Linux
436 system call implements a nonstandard feature: if
438 is NULL, then the call modifies the timestamps of
439 the file referred to by the file descriptor
441 (which may refer to any type of file).
442 Using this feature, the call
443 .I "futimens(fd,\ times)"
447 utimensat(fd, NULL, times, 0);
454 on kernels before 2.6.26.
455 These bugs are either nonconformances with the POSIX.1 draft specification
456 or inconsistencies with historical Linux behavior.
458 POSIX.1 specifies that if one of the
464 then the value of the corresponding
466 field should be ignored.
467 Instead, the value of the
469 field is required to be 0 (or the error
473 Various bugs mean that for the purposes of permission checking,
478 isn't always treated the same as specifying
481 and the case where one
487 isn't treated the same as specifying
489 as a pointer to an array of structures containing arbitrary time values.
490 As a result, in some cases:
491 a) file timestamps can be updated by a process that shouldn't have
492 permission to perform updates;
493 b) file timestamps can't be updated by a process that should have
494 permission to perform updates; and
497 value is returned in case of an error.
498 .\" Below, the long description of the errors from the previous bullet
499 .\" point (abridged because it's too much detail for a man page).
509 .\" should occur if the process's effective user ID does not match
510 .\" the file owner and the process is not privileged.
511 .\" Instead, the call successfully changes one of the timestamps.
513 .\" If file is not writable by the effective user ID of the process and
514 .\" the process's effective user ID does not match the file owner and
515 .\" the process is not privileged,
518 .\" is NULL, then the error
521 .\" This error should also occur if
523 .\" points to an array of structures in which both
527 .\" Instead the call succeeds.
529 .\" If a file is marked as append-only (see
531 .\" then Linux traditionally
537 .\" argument to be used in order to update both timestamps to the current time.
542 .\" should also produce the same result when given a
544 .\" argument that points to an array of structures in which both
548 .\" Instead, the call fails with the error
551 .\" If a file is marked as immutable (see
553 .\" then Linux traditionally
566 .\" should also produce the same result when given a
568 .\" that points to an array of structures in which both
572 .\" Instead, the call fails with the error
575 POSIX.1 says that a process that has \fIwrite access to the file\fP
580 pointing to an array of structures in which both
584 in order to update both timestamps to the current time.
587 instead checks whether the
588 .IR "access mode of the file descriptor allows writing" .
589 .\" This means that a process with a file descriptor that allows
590 .\" writing could change the timestamps of a file for which it
591 .\" does not have write permission;
592 .\" conversely, a process with a read-only file descriptor won't
593 .\" be able to update the timestamps of a file,
594 .\" even if it has write permission on the file.
602 .BR path_resolution (7),