UTIMENSAT(2)

πŸ’– UTIMENSAT πŸ’–

okay bestie, you're in the System Calls Manual section and the page you just pulled up is UTIMENSAT(2) β€” two little syscalls, futimens and utimensat, and they are the way to touch a file's timestamps without any of that legacy nonsense. no cap. let's get into it 🫢


✨ NAME ✨

futimens, utimensat β€” set file access and modification times

no seriously that's it, that's the whole job, they set the access time and the modification time, and they do it properly πŸ’…


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

so yeah, link -lc like a normal person and you're good 🌸


πŸŽ€ SYNOPSIS πŸŽ€

#include <sys/stat.h>

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

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

look at that times[2] β€” it's an array of two, not one, not three, two. access time first, modification time second. the way that array is literally just two timestamps holding hands πŸ₯Ή


🌈 DESCRIPTION 🌈

so here's the vibe. the access and modification times of the file β€” named by path, or referenced by fd β€” get changed to whatever you passed in times. and the inode-change-time of the file? that gets set to the current time, automatically, no input from you. periodt.

if path specifies a relative path, it's relative to the current working directory when fd is AT_FDCWD, and otherwise it's relative to the directory associated with the file descriptor fd. iykyk.

okay now the fun part. the tv_nsec field of a timespec structure can be set to one of two special values:

  • UTIME_NOW β€” sets the current time πŸ•
  • UTIME_OMIT β€” leaves the time unchanged, untouched, unbothered πŸ’…

and in either of those cases, the tv_sec field is straight up ignored. like, it's not even invited to the party.

if times is non-NULL, it's assumed to point to an array of two timespec structures. access time gets the value of the first element, modification time gets the value of the second element. and for file systems that support file birth (creation) times β€” hi UFS2 πŸ‘‹ β€” the birth time will be set to the value of the second element if that second element is older than the currently set birth time. so to set both a birth time and a modification time, you need two calls: the first to set the birth time, the second to set the (presumably newer) modification time. ideally a new system call will be added that lets you set all three times at once. until then, bestie, two calls, that's the deal 😭

if times is NULL, that's equivalent to passing a pointer to an array of two timespec structures with both tv_nsec fields set to UTIME_NOW.

if both tv_nsec fields are UTIME_OMIT, the timestamps remain unchanged and no permissions are needed for the file itself β€” though search permissions may still be required for the path prefix. and the call may or may not succeed if the named file does not exist. yeah, "may or may not", it's giving chaos πŸŒͺ️

if both tv_nsec fields are UTIME_NOW, the caller must be the owner of the file, have permission to write the file, or be the super-user. pick one, bestie πŸ‘‘

for all other values of the timestamps, the caller must be the owner of the file or be the super-user. no write permission loophole for you.


🌟 OPTIONS 🌟 (the flag argument)

the values for the flag argument of the utimensat() system call are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:

  • AT_SYMLINK_NOFOLLOW β€” if path names a symbolic link, the symbolic link's times are changed. by default, utimensat() changes the times of the file referenced by the symbolic link. so this flag is you going "no bestie, I mean the link itself, not the thing it points at" πŸ”—

πŸ”₯ RETURN VALUES πŸ”₯

upon successful completion, the value 0 is returned. otherwise, the value -1 is returned and the global variable errno is set to indicate the error. classic. zero or minus-one, no in between, no nuance, main character energy ✨


πŸ’€ COMPATIBILITY πŸ’€

here's the tea. if the running kernel does not support this system call, a wrapper emulates it using fstatat(2), futimesat(2) and lutimes(2). and as a result of that emulation:

  • timestamps will be rounded down to the nearest microsecond 😬
  • UTIME_OMIT is not atomic 😬
  • AT_SYMLINK_NOFOLLOW is not available with a path relative to a file descriptor 😬

so if you're on an old kernel and you need those exact semantics, good luck, you're on your own fr fr πŸ™ˆ


🚨 ERRORS 🚨

these system calls will fail if:

  • [EACCES] β€” the times argument is NULL, or both tv_nsec values are UTIME_NOW, and the effective user ID of the process does not match the owner of the file, and is not the super-user, and write access is denied.
  • [EFAULT] β€” the times argument points outside the process's allocated address space.
  • [EINVAL] β€” the tv_nsec component of at least one of the values specified by the times argument has a value less than 0 or greater than 999999999 and is not equal to UTIME_NOW or UTIME_OMIT.
  • [EIO] β€” an I/O error occurred while reading or writing the affected inode.
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system.
  • [EPERM] β€” the times argument is not NULL nor are both tv_nsec values UTIME_NOW, nor are both tv_nsec values UTIME_OMIT and the calling process's effective user ID does not match the owner of the file and is not the super-user.
  • [EPERM] β€” the named file has its immutable or append-only flag set, see the chflags(2) manual page for more information.
  • [EROFS] β€” the file system containing the file is mounted read-only.

the futimens() system call will fail if:

  • [EBADF] β€” the fd argument does not refer to a valid descriptor.

the utimensat() system call will fail if:

  • [EACCES] β€” search permission is denied for a component of the path prefix.
  • [EBADF] β€” the path argument does not specify an absolute path and the fd argument is neither AT_FDCWD nor a valid file descriptor.
  • [EFAULT] β€” the path argument points outside the process's allocated address space.
  • [ELOOP] β€” too many symbolic links were encountered in translating the pathname.
  • [ENAMETOOLONG] β€” a component of a pathname exceeded NAME_MAX characters, or an entire path name exceeded PATH_MAX characters.
  • [ENOENT] β€” the named file does not exist.
  • [ENOTDIR] β€” a component of the path prefix is not a directory.
  • [ENOTDIR] β€” the path argument is not an absolute path and fd is neither AT_FDCWD nor a file descriptor associated with a directory.
yes, [ENOTDIR] shows up twice. and yes, [EPERM] shows up twice. that's not me being sloppy, that's the actual list, we keep every single one πŸ’–

πŸ‘€ SEE ALSO πŸ‘€

chflags(2), stat(2), symlink(2), utimes(2), utime(3), symlink(7)

go read them, they're cute too, this is a whole friend group 🌸


⭐ STANDARDS ⭐

the futimens() and utimensat() system calls are expected to conform to IEEE Std 1003.1-2008 ("POSIX.1"). expected to. we love a confident maybe ✨


πŸ•°οΈ HISTORY πŸ•°οΈ

the futimens() and utimensat() system calls appeared in FreeBSD 10.3.


and that's the whole page bestie, you ate πŸ’…πŸ’– sending you off with love from the Debian crew, March 30, 2020 β€” go set some timestamps, you absolute icon πŸ‘‘πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from