futimens()

Updated: April 19, 2023

Set the access and modification times for a file

Synopsis:

#include <sys/stat.h>

int futimens( int fd,
              const struct timespec times[2] );

Arguments:

fd
The descriptor for the file whose modification time you want to set.
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.

Library:

libc

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

Description:

The futimens() function sets the access and modification times, accurate to the nanosecond, of the file 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.

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 futimens() 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 futimens() 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.
EBADF
The fd argument isn't a valid file descriptor.
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.
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