Manpage of UTIMENSAT

UTIMENSAT

Section: Linux Programmer's Manual (2)
Updated: 2016-03-15
Index
 

NAME

utimensat, futimens - change file timestamps with nanosecond precision  

SYNOPSIS

#include <fcntl.h>           /* Definition of AT_* constants */#include <sys/stat.h>int utimensat(int dirfd, const char *pathname,              const struct timespec times[2], int flags);int futimens(int fd, const struct timespec times[2]);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

utimensat():

Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_ATFILE_SOURCE

futimens():

Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
 

DESCRIPTION

utimensat() and futimens() update the timestamps of a file with nanosecond precision. This contrasts with the historical utime(2) and utimes(2), which permit only second and microsecond precision, respectively, when setting file timestamps.

With utimensat() the file is specified via the pathname given in pathname. With futimens() the file whose timestamps are to be updated is specified via an open file descriptor, fd.

For both calls, the new file timestamps are specified in the array times: times[0] specifies the new "last access time" (atime); times[1] specifies the new "last modification time" (mtime). Each of the elements of timesspecifies a time as the number of seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). This information is conveyed in a structure of the following form:

struct timespec {
    time_t tv_sec;        /* seconds */
    long   tv_nsec;       /* nanoseconds */
};

Updated file timestamps are set to the greatest value supported by the filesystem that is not greater than the specified time.

If the tv_nsecfield of one of the timespecstructures has the special value UTIME_NOW, then the corresponding file timestamp is set to the current time. If the tv_nsecfield of one of the timespecstructures has the special value UTIME_OMIT, then the corresponding file timestamp is left unchanged. In both of these cases, the value of the corresponding tv_secfield is ignored.

If timesis NULL, then both timestamps are set to the current time.  

Permissions requirements

To set both file timestamps to the current time (i.e., timesis NULL, or both tv_nsecfields specify UTIME_NOW), either:
1.
the caller must have write access to the file;
2.
the caller's effective user ID must match the owner of the file; or
3.
the caller must have appropriate privileges.

To make any change other than setting both timestamps to the current time (i.e., timesis not NULL, and neither tv_nsecfield is UTIME_NOWand neither tv_nsecfield is UTIME_OMIT), either condition 2 or 3 above must apply.

If both tv_nsecfields are specified as UTIME_OMIT, then no file ownership or permission checks are performed, and the file timestamps are not modified, but other error conditions may still be detected.  

utimensat() specifics

If pathnameis relative, then by default it is interpreted relative to the directory referred to by the open file descriptor, dirfd(rather than relative to the current working directory of the calling process, as is done by utimes(2) for a relative pathname). See openat(2) for an explanation of why this can be useful.

If pathnameis relative and dirfdis the special value AT_FDCWD, then pathnameis interpreted relative to the current working directory of the calling process (like utimes(2)).

If pathnameis absolute, then dirfdis ignored.

The flagsfield is a bit mask that may be 0, or include the following constant, defined in <fcntl.h>:

AT_SYMLINK_NOFOLLOW
If pathnamespecifies a symbolic link, then update the timestamps of the link, rather than the file to which it refers.
 

RETURN VALUE

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

ERRORS

EACCES
timesis NULL, or both tv_nsecvalues are UTIME_NOW, and either:
*
the effective user ID of the caller does not match the owner of the file, the caller does not have write access to the file, and the caller is not privileged (Linux: does not have either the CAP_FOWNERor the CAP_DAC_OVERRIDEcapability); or,
*
the file is marked immutable (see chattr(1)).
EBADF
(futimens()) fdis not a valid file descriptor.
EBADF
(utimensat()) pathnameis a relative pathname, but dirfdis neither AT_FDCWDnor a valid file descriptor.
EFAULT
timespointed to an invalid address; or, dirfdwas AT_FDCWD, and pathnameis NULL or an invalid address.
EINVAL
Invalid value in flags.
EINVAL
Invalid value in one of the tv_nsecfields (value outside range 0 to 999,999,999, and not UTIME_NOWor UTIME_OMIT); or an invalid value in one of the tv_secfields.
EINVAL
pathnameis NULL, dirfdis not AT_FDCWD, and flagscontains AT_SYMLINK_NOFOLLOW.
ELOOP
(utimensat()) Too many symbolic links were encountered in resolving pathname.
ENAMETOOLONG
(utimensat()) pathnameis too long.
ENOENT
(utimensat()) A component of pathnamedoes not refer to an existing directory or file, or pathnameis an empty string.
ENOTDIR
(utimensat()) pathnameis a relative pathname, but dirfdis neither AT_FDCWDnor a file descriptor referring to a directory; or, one of the prefix components of pathnameis not a directory.
EPERM
The caller attempted to change one or both timestamps to a value other than the current time, or to change one of the timestamps to the current time while leaving the other timestamp unchanged, (i.e., timesis not NULL, neither tv_nsecfield is UTIME_NOW, and neither tv_nsecfield is UTIME_OMIT) and either:
*
the caller's effective user ID does not match the owner of file, and the caller is not privileged (Linux: does not have the CAP_FOWNERcapability); or,
*
the file is marked append-only or immutable (see chattr(1)).
EROFS
The file is on a read-only filesystem.
ESRCH
(utimensat()) Search permission is denied for one of the prefix components of pathname.
 

VERSIONS

utimensat() was added to Linux in kernel 2.6.22; glibc support was added with version 2.6.

Support for futimens() first appeared in glibc 2.6.  

ATTRIBUTES

For an explanation of the terms used in this section, see attributes(7).
InterfaceAttributeValue
utimensat(), futimens() Thread safetyMT-Safe

 

CONFORMING TO

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

NOTES

utimensat() obsoletes futimesat(2).

On Linux, timestamps cannot be changed for a file marked immutable, and the only change permitted for files marked append-only is to set the timestamps to the current time. (This is consistent with the historical behavior of utime(2) and utimes(2) on Linux.)

On Linux, futimens() is a library function implemented on top of the utimensat() system call. To support this, the Linux utimensat() system call implements a nonstandard feature: if pathnameis NULL, then the call modifies the timestamps of the file referred to by the file descriptor dirfd(which may refer to any type of file). Using this feature, the call futimens(fd, times)is implemented as:

    utimensat(fd, NULL, times, 0);

If both tv_nsecfields are specified as UTIME_OMIT, then the Linux implementation of utimensat() succeeds even if the file referred to by dirfdand pathnamedoes not exist.  

BUGS

Several bugs afflict utimensat() and futimens() on kernels before 2.6.26. These bugs are either nonconformances with the POSIX.1 draft specification or inconsistencies with historical Linux behavior.
*
POSIX.1 specifies that if one of the tv_nsecfields has the value UTIME_NOWor UTIME_OMIT, then the value of the corresponding tv_secfield should be ignored. Instead, the value of the tv_secfield is required to be 0 (or the error EINVALresults).
*
Various bugs mean that for the purposes of permission checking, the case where both tv_nsecfields are set to UTIME_NOWisn't always treated the same as specifying timesas NULL, and the case where one tv_nsecvalue is UTIME_NOWand the other is UTIME_OMITisn't treated the same as specifying timesas a pointer to an array of structures containing arbitrary time values. As a result, in some cases: a) file timestamps can be updated by a process that shouldn't have permission to perform updates; b) file timestamps can't be updated by a process that should have permission to perform updates; and c) the wrong errnovalue is returned in case of an error.
*
POSIX.1 says that a process that has write access to the filecan make a call with timesas NULL, or with timespointing to an array of structures in which both tv_nsecfields are UTIME_NOW, in order to update both timestamps to the current time. However, futimens() instead checks whether the access mode of the file descriptor allows writing.
 

SEE ALSO

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


 

Index

NAME
SYNOPSIS
DESCRIPTION
Permissions requirements
utimensat() specifics
RETURN VALUE
ERRORS
VERSIONS
ATTRIBUTES
CONFORMING TO
NOTES
BUGS
SEE ALSO

This document was created by man2html, using the manual pages.
Time: 22:27:58 GMT, June 20, 2016