utimensat()

Updated: May 06, 2022

Set the access and modification times for a file

Synopsis:

#include <sys/stat.h>

int utimensat( int fd,
               const char *path,
               const struct timespec times[2],
               int flag);

Arguments:

fd
The descriptor for the directory that path is relative to, or AT_FDCWD to use the current working directory.
Note: In QNX Neutrino, this argument must currently be AT_FDCWD.
path
The path for the file whose modification time you want to set. If the path isn't an absolute path, it's relative to the directory specified by fd.
times
NULL, or an array of two timespec structures. The first array member represents the date and time of last access, and the second represents the date and time of last modification. The times in the timespec structure are measured in seconds and nanoseconds since the Epoch.
flag
A bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:
  • AT_SYMLINK_NOFOLLOW — if path names a symbolic link, then change the access and modification times of the symbolic link.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The utimensat() function changes the access and modification times, accurate to the nanosecond, of the file pointed to by the path argument, relative to the directory associated with the file descriptor fd.

If the tv_nsec field of a timespec structure is UTIME_NOW, the file's relevant timestamp is set to the current time. If the tv_nsec field is UTIME_OMIT, the file's relevant timestamp isn't changed. In either case, the tv_sec field is ignored.

If the times argument is NULL, both the access and modification timestamps are set to the current time.

Note: The time that's actually recorded depends on the resolution in the filesystem's data structures. For example, no matter what you put in the tv_nsec field, the time that a FAT filesystem records uses a 2-second granularity. The recorded time is the greatest value supported by the filesystem that isn't greater than the specified time.

The path argument is a relative path, the file to be used is relative to the directory associated with the file descriptor fd instead of the current working directory. The function checks whether directory searches are permitted using the current permissions of the directory underlying the file descriptor.

Only a process with the effective user ID equal to the user ID of the file, or with write access to the file, or with appropriate privileges may use utimensat() with a NULL pointer as the times argument or with both tv_nsec fields set to UTIME_NOW.

Only a process with the effective user ID equal to the user ID of the file or with appropriate privileges may use utimensat() with a non-NULL times argument that doesn't have both tv_nsec fields set to UTIME_NOW and doesn't have both tv_nsec fields set to UTIME_OMIT. If both tv_nsec fields are set to UTIME_OMIT, no ownership or permissions check is performed for the file, but other error conditions may still be detected (including EACCES errors related to the path prefix).

Upon completion, this function marks the last file status change timestamp for update.

Returns:

0
Success.
-1
An error occurred; errno is set, and the file times aren't affected.

Errors:

EACCES
  • The times argument is NULL or both tv_nsec values are UTIME_NOW, and the effective user ID of the process doesn't match the owner of the file and write access is denied.
  • Search permission is denied by a component of the path prefix.
EBADF
The path argument doesn't specify an absolute path, and the fd argument is neither AT_FDCWD nor a valid file descriptor that's open for reading or searching.
EINVAL
  • Either of the times argument structures specified a tv_nsec value that was neither UTIME_NOW nor UTIME_OMIT, and was a value less than zero or greater than or equal to 1000 million.
  • A new file timestamp would be a value whose tv_sec component isn't a value supported by the filesystem.
  • The value of the flag argument isn't valid.
ELOOP
  • A loop exists in symbolic links encountered during resolution of the path argument.
  • More than SYMLOOP_MAX symbolic links were encountered during resolution of the path argument.
ENAMETOOLONG
  • The length of a component of a pathname is longer than NAME_MAX.
  • The length of a pathname exceeds PATH_MAX, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds PATH_MAX.
ENOENT
A component of path doesn't name an existing file or path is an empty string.
ENOSYS
(QNX Neutrino extension) The fd argument wasn't AT_FDCWD.
ENOTDIR
  • The path argument isn't an absolute path, and fd is a file descriptor associated with a non-directory file.
  • A component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory, or the path argument contains at least one non-slash character and ends with one or more trailing slash characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.
EPERM
The times argument isn't NULL, doesn't have both tv_nsec fields set to UTIME_NOW, doesn't have both tv_nsec fields set to UTIME_OMIT, the calling process's effective user ID doesn't match the owner of the file, and the calling process doesn't have appropriate privileges.
EROFS
The filesystem containing the file is read-only.

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes