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β ifpathnames 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_OMITis not atomic π¬AT_SYMLINK_NOFOLLOWis 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]β thetimesargument isNULL, or bothtv_nsecvalues areUTIME_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]β thetimesargument points outside the process's allocated address space.[EINVAL]β thetv_nseccomponent of at least one of the values specified by thetimesargument has a value less than0or greater than999999999and is not equal toUTIME_NOWorUTIME_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]β thetimesargument is notNULLnor are bothtv_nsecvaluesUTIME_NOW, nor are bothtv_nsecvaluesUTIME_OMITand 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 thechflags(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]β thefdargument 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]β thepathargument does not specify an absolute path and thefdargument is neitherAT_FDCWDnor a valid file descriptor.[EFAULT]β thepathargument 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 exceededNAME_MAXcharacters, or an entire path name exceededPATH_MAXcharacters.[ENOENT]β the named file does not exist.[ENOTDIR]β a component of the path prefix is not a directory.[ENOTDIR]β thepathargument is not an absolute path andfdis neitherAT_FDCWDnor 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
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved
- Copyright (c) 2012, Jilles Tjoelker
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms