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():
19       Since glibc 2.10: _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
20       Before glibc 2.10: _ATFILE_SOURCE
21       futimens():
22       Since glibc 2.10: _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
23       Before glibc 2.10: _GNU_SOURCE
24

DESCRIPTION

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

RETURN VALUE

99       On  success,  utimensat()  and  futimens()  return  0.  On error, -1 is
100       returned and errno is set to indicate the error.
101

ERRORS

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

VERSIONS

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

CONFORMING TO

163       futimens() and utimensat() are specified in POSIX.1-2008.
164

NOTES

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

BUGS

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

SEE ALSO

211       chattr(1),  futimesat(2),  openat(2),  stat(2),  utimes(2), futimes(3),
212       path_resolution(7), symlink(7)
213

COLOPHON

215       This page is part of release 3.25 of the Linux  man-pages  project.   A
216       description  of  the project, and information about reporting bugs, can
217       be found at http://www.kernel.org/doc/man-pages/.
218
219
220
221Linux                             2009-12-13                      UTIMENSAT(2)
Impressum