1UTIMENSAT(2)               Linux Programmer's Manual              UTIMENSAT(2)
2
3
4

NAME

6       utimensat, futimens - change file timestamps with nanosecond precision
7

SYNOPSIS

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
15       int futimens(int fd, const struct timespec times[2]);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       utimensat():
20           Since glibc 2.10:
21               _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
22           Before glibc 2.10:
23               _ATFILE_SOURCE
24       futimens():
25           Since glibc 2.10:
26                  _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
27           Before glibc 2.10:
28                  _GNU_SOURCE
29

DESCRIPTION

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       file system 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  both  tv_nsec  fields are not
76       UTIME_NOW and both tv_nsec fields are not UTIME_OMIT), either condition
77       2 or 3 above 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

RETURN VALUE

104       On  success,  utimensat()  and  futimens()  return  0.  On error, -1 is
105       returned and errno is set to indicate the error.
106

ERRORS

108       EACCES times is NULL, or both tv_nsec values are UTIME_NOW, and:
109              * the effective user ID of the caller does not match  the  owner
110                of  the  file,  the  caller  does not have write access to the
111                file, and the caller is not privileged (Linux: does  not  have
112                either the CAP_FOWNER or the CAP_DAC_OVERRIDE capability); or,
113              * the file is marked immutable (see chattr(1)).
114
115       EBADF  (futimens()) fd is not a valid file descriptor.
116
117       EBADF  (utimensat()) pathname is a relative pathname, but dirfd is nei‐
118              ther AT_FDCWD 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 to 999,999,999,  and  not  UTIME_NOW  or  UTIME_OMIT);  or  an
127              invalid 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
133              resolving pathname.
134
135       ENAMETOOLONG
136              (utimensat()) pathname is too long.
137
138       ENOENT (utimensat())  A  component  of  pathname  does  not refer to an
139              existing 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, both tv_nsec fields are not UTIME_NOW,
150              and both tv_nsec fields are not UTIME_OMIT) and:
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              * the file is marked append-only or immutable (see chattr(1)).
155
156       EROFS  The file is on a read-only file system.
157
158       ESRCH  (utimensat()) Search permission is denied for one of the  prefix
159              components of pathname.
160

VERSIONS

162       utimensat()  was  added  to  Linux  in kernel 2.6.22; glibc support was
163       added with version 2.6.
164
165       Support for futimens() first appeared in glibc 2.6.
166

CONFORMING TO

168       futimens() and utimensat() are specified in POSIX.1-2008.
169

NOTES

171       utimensat() obsoletes futimesat(2).
172
173       On Linux, timestamps cannot be changed for a file marked immutable, and
174       the  only  change  permitted for files marked append-only is to set the
175       timestamps to the current time.  (This is consistent with the  histori‐
176       cal behavior of utime(2) and utimes(2) on Linux.)
177
178       On  Linux,  futimens()  is a library function implemented on top of the
179       utimensat() system call.  To support this, the Linux utimensat() system
180       call  implements  a  nonstandard feature: if pathname is NULL, then the
181       call modifies the timestamps of  the  file  referred  to  by  the  file
182       descriptor  dirfd  (which  may  refer to any type of file).  Using this
183       feature, the call futimens(fd, times) is implemented as:
184
185           utimensat(fd, NULL, times, 0);
186

BUGS

188       Several bugs afflict  utimensat()  and  futimens()  on  kernels  before
189       2.6.26.   These  bugs are either nonconformances with the POSIX.1 draft
190       specification or inconsistencies with historical Linux behavior.
191
192       * POSIX.1 specifies that if one of the tv_nsec  fields  has  the  value
193         UTIME_NOW  or  UTIME_OMIT, then the value of the corresponding tv_sec
194         field should be ignored.  Instead, the value of the tv_sec  field  is
195         required to be 0 (or the error EINVAL results).
196
197       * Various  bugs  mean that for the purposes of permission checking, the
198         case where both tv_nsec fields are  set  to  UTIME_NOW  isn't  always
199         treated  the same as specifying times as NULL, and the case where one
200         tv_nsec value is UTIME_NOW and the other is UTIME_OMIT isn't  treated
201         the  same  as specifying times as a pointer to an array of structures
202         containing arbitrary time values.  As a result,  in  some  cases:  a)
203         file  timestamps can be updated by a process that shouldn't have per‐
204         mission to perform updates; b) file timestamps can't be updated by  a
205         process  that  should  have permission to perform updates; and c) the
206         wrong errno value is returned in case of an error.
207
208       * POSIX.1 says that a process that has write access  to  the  file  can
209         make a call with times as NULL, or with times pointing to an array of
210         structures in which both tv_nsec fields are UTIME_NOW,  in  order  to
211         update  both  timestamps  to  the  current time.  However, futimens()
212         instead checks whether the access mode of the file descriptor  allows
213         writing.
214

SEE ALSO

216       chattr(1),  futimesat(2),  openat(2),  stat(2),  utimes(2), futimes(3),
217       path_resolution(7), symlink(7)
218

COLOPHON

220       This page is part of release 3.53 of the Linux  man-pages  project.   A
221       description  of  the project, and information about reporting bugs, can
222       be found at http://www.kernel.org/doc/man-pages/.
223
224
225
226Linux                             2012-03-25                      UTIMENSAT(2)
Impressum