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 <sys/stat.h>
10
11       int utimensat(int dirfd, const char *pathname,
12                     const struct timespec times[2], int flags);
13
14       int futimens(int fd, const struct timespec times[2]);
15
16   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18       utimensat(): _ATFILE_SOURCE
19       futimens():  _GNU_SOURCE   /*  Will  likely  change  after POSIX.1-2008
20       changes are incorporated into glibc */
21

DESCRIPTION

23       utimensat() and  futimens()  update  the  timestamps  of  a  file  with
24       nanosecond  precision.  This contrasts with the historical utime(2) and
25       utimes(2), which permit only second and microsecond precision,  respec‐
26       tively, when setting file timestamps.
27
28       With  utimensat() the file is specified via the pathname given in path‐
29       name.  With futimens() the file whose timestamps are to be  updated  is
30       specified via an open file descriptor, fd.
31
32       For  both  calls,  the  new  file timestamps are specified in the array
33       times: times[0] specifies the new "last access time" (atime);  times[1]
34       specifies  the  new "last modification time" (mtime).  Each of the ele‐
35       ments of times specifies a time in seconds and  nanoseconds  since  the
36       Epoch  (00:00:00,  1  Jan  1970,  UTC), in a structure of the following
37       form:
38
39           struct timespec {
40               time_t tv_sec;        /* seconds */
41               long   tv_nsec;       /* nanoseconds */
42           };
43
44       Updated file timestamps are set to the greatest value supported by  the
45       file system that is not greater than the specified time.
46
47       If  the tv_nsec field of one of the timespec structures has the special
48       value UTIME_NOW, then the corresponding file timestamp is  set  to  the
49       current  time.   If the tv_nsec field of one of the timespec structures
50       has the special value UTIME_OMIT, then the corresponding file timestamp
51       is  left  unchanged.   In  both of these cases, the value of the corre‐
52       sponding tv_sec field is ignored.
53
54       If times is NULL, then both timestamps are set to the current time.
55
56   Permissions requirements
57       To set both file timestamps to the current time (i.e., times  is  NULL,
58       or both tv_nsec fields specify UTIME_NOW), either:
59
60       1. the caller must have write access to the file;
61
62       2. the caller's effective user ID must match the owner of the file; or
63
64       3. the caller must have appropriate privileges.
65
66       To  make  any  change other than setting both timestamps to the current
67       time (i.e., times  is  not  NULL,  and  both  tv_nsec  fields  are  not
68       UTIME_NOW and both tv_nsec fields are not UTIME_OMIT), either condition
69       2 or 3 above must apply.
70
71       If both tv_nsec fields are specified as UTIME_OMIT, then no file owner‐
72       ship  or  permission  checks are performed, and the file timestamps are
73       not modified, but other error conditions may still be detected.
74
75   utimensat() specifics
76       If pathname is relative, then by default it is interpreted relative  to
77       the  directory  referred  to by the open file descriptor, dirfd (rather
78       than relative to the current working directory of the calling  process,
79       as is done by utimes(2) for a relative pathname).  See openat(2) for an
80       explanation of why this can be useful.
81
82       If pathname is relative and dirfd is the special value  AT_FDCWD,  then
83       pathname  is  interpreted  relative to the current working directory of
84       the calling process (like utimes(2)).
85
86       If pathname is absolute, then dirfd is ignored.
87
88       The flags field is a bit mask that may be 0, or include  the  following
89       constant, defined in <fcntl.h>:
90
91       AT_SYMLINK_NOFOLLOW
92              If  pathname  specifies  a  symbolic link, then update the time‐
93              stamps of the link, rather than the file to which it refers.
94

RETURN VALUE

96       On success, utimensat() and futimens()  return  0.   On  error,  -1  is
97       returned and errno is set to indicate the error.
98

ERRORS

100       EACCES times is NULL, or both tv_nsec values are UTIME_NOW, and:
101              * the  effective  user ID of the caller does not match the owner
102                of the file, the caller does not  have  write  access  to  the
103                file,  and  the caller is not privileged (Linux: does not have
104                either the CAP_FOWNER or the CAP_DAC_OVERRIDE capability); or,
105              * the file is marked immutable (see chattr(1)).
106
107       EBADF  (futimens()) fd is not a valid file descriptor.
108
109       EBADF  (utimensat()) pathname is a relative pathname, but dirfd is nei‐
110              ther AT_FDCWD nor a valid file descriptor.
111
112       EFAULT times pointed to an invalid address; or, dirfd was AT_FDCWD, and
113              pathname is NULL or an invalid address.
114
115       EINVAL Invalid value in flags.
116
117       EINVAL Invalid value in one of the tv_nsec fields (value outside  range
118              0  to  999,999,999,  and  not  UTIME_NOW  or  UTIME_OMIT); or an
119              invalid value in one of the tv_sec fields.
120
121       EINVAL pathname is NULL, dirfd is  not  AT_FDCWD,  and  flags  contains
122              AT_SYMLINK_NOFOLLOW.
123
124       ELOOP  (utimensat())  Too  many  symbolic  links  were  encountered  in
125              resolving pathname.
126
127       ENAMETOOLONG
128              (utimensat()) pathname is too long.
129
130       ENOENT (utimensat()) A component of  pathname  does  not  refer  to  an
131              existing directory or file, or pathname is an empty string.
132
133       ENOTDIR
134              (utimensat()) pathname is a relative pathname, but dirfd is nei‐
135              ther AT_FDCWD nor a file descriptor referring  to  a  directory;
136              or, one of the prefix components of pathname is not a directory.
137
138       EPERM  The caller attempted to change one or both timestamps to a value
139              other than the current time, or to change one of the  timestamps
140              to the current time while leaving the other timestamp unchanged,
141              (i.e., times is not NULL, both tv_nsec fields are not UTIME_NOW,
142              and both tv_nsec fields are not UTIME_OMIT) and:
143              * the  caller's  effective  user  ID does not match the owner of
144                file, and the caller is not privileged (Linux: does  not  have
145                the CAP_FOWNER capability); or,
146              * the file is marked append-only or immutable (see chattr(1)).
147
148       EROFS  The file is on a read-only file system.
149
150       ESRCH  (utimensat())  Search permission is denied for one of the prefix
151              components of pathname.
152

VERSIONS

154       utimensat() was added to Linux in  kernel  2.6.22;  glibc  support  was
155       added with version 2.6.
156
157       Support for futimens() first appeared in glibc 2.6.
158

CONFORMING TO

160       futimens() and utimensat() are specified in POSIX.1-2008.
161

NOTES

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

BUGS

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

SEE ALSO

208       chattr(1), futimesat(2),  openat(2),  stat(2),  utimes(2),  futimes(3),
209       path_resolution(7), symlink(7)
210

COLOPHON

212       This  page  is  part of release 3.22 of the Linux man-pages project.  A
213       description of the project, and information about reporting  bugs,  can
214       be found at http://www.kernel.org/doc/man-pages/.
215
216
217
218Linux                             2008-09-29                      UTIMENSAT(2)
Impressum