UTIMES(2)
π UTIMES(2) β bestie, we're setting file times π
okay so this is a page from the System Calls Manual, and it's all about utimes, lutimes, futimes, and futimesat. yes that's four whole functions. no cap, they all do the same vibe: touching a file's timestamps. let's get into it π«Ά
β¨ NAME β¨
utimes, lutimes, futimes, futimesat β set file access and modification times
π LIBRARY π
Standard C Library (libc, -lc)
π SYNOPSIS π
#include <sys/time.h>
int
utimes(const char *path, const struct timeval *times);
int
lutimes(const char *path, const struct timeval *times);
int
futimes(int fd, const struct timeval *times);
int
futimesat(int fd, const char *path, const struct timeval times[2]);
πΈ DESCRIPTION πΈ
so first things first, bestie, and I need you to hear me on this one:
These interfaces are obsoleted byfutimens(2)andutimensat(2)because they are not accurate to nanoseconds. π
like, they still work, but they're the old-school way. the new girls (futimens and utimensat) got that nanosecond precision. these ones? not so much. iykyk.
so here's the tea β β the access time and the modification time of the file named by path, or referenced by fd, get changed to whatever the times argument says.
if times is NULL β the access and modification times both get set to the current time. but! the caller has to be the owner of the file, or have permission to write the file, or be the super-user. one of those three, pick your fighter π
if times is non-NULL β it's assumed to point at an array of two timeval structures. here's the mapping:
- the access time gets set to the value of the first element
- the modification time gets set to the value of the second element
and okay this next part is kinda niche but it's giving UFS2 energy: for file systems that support file birth (creation) times (such as UFS2), the birth time will be set to the value of the second element if the second element is older than the currently set birth time. π¦
and because life is complicated, to set both a birth time and a modification time, you need two calls:
- the first call sets the birth time
- the second call sets the (presumably newer) modification time
and the manpage literally says: "Ideally a new system call will be added that allows the setting of all three times at once." π not them manifesting.
also when times is non-NULL, the caller must be the owner of the file or be the super-user.
either way, the inode-change-time of the file gets set to the current time. every time. periodt.
π§ lutimes() vs utimes() π§
the lutimes() system call is like utimes() except when the named file is a symbolic link:
lutimes()changes the access and modification times of the link itself πutimes()changes the times of the file the link references π―
subtle but iconic.
π futimesat() π
the futimesat() system call is equivalent to utimes() except when path specifies a relative path. in that case, the access and modification time is set to that of a file relative to the directory associated with the file descriptor fd β instead of the current working directory. π
and if you pass futimesat() the special value AT_FDCWD in the fd parameter? the current working directory gets used, and the behavior is identical to a call to utimes(). slay, consistency. β¨
π RETURN VALUES π
- on a successful completion, the value
0is returned π - otherwise, the value
-1is returned, and the global variableerrnois set to indicate the error π
π ERRORS π
all of the system calls will fail if:
[EACCES]β Search permission is denied for a component of thepathprefix.[EACCES]β Thetimesargument isNULLand 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]β Thepathortimesargument points outside the process's allocated address space.[EFAULT]β Thetimesargument points outside the process's allocated address space.[EINVAL]β Thetv_useccomponent of at least one of the values specified by thetimesargument has a value less than0or greater than999999.[EIO]β An I/O error occurred while reading or writing the affected inode.[EINTEGRITY]β Corrupted data was detected while reading from the file system.[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.[EPERM]β Thetimesargument is notNULLand 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 flags set. See thechflags(2)manual page for more information.[EROFS]β The file system containing the file is mounted read-only.
the futimes() system call will fail if:
[EBADF]β Thefdargument does not refer to a valid descriptor.
and in addition to the errors returned by utimes(), futimesat() may fail if:
[EBADF]β Thepathargument does not specify an absolute path and thefdargument is neitherAT_FDCWDnor a valid file descriptor open for searching.[ENOTDIR]β Thepathargument is not an absolute path andfdis neitherAT_FDCWDnor a file descriptor associated with a directory.
ngl that's a lot of error codes but honestly? knowing them is main character energy. π₯
π¦ SEE ALSO π¦
chflags(2), stat(2), utimensat(2), utime(3)
β STANDARDS β
the utimes() function is expected to conform to X/Open Portability Guide Issue 4, Version 2 ("XPG4.2"). π«
the futimesat() system call follows The Open Group Extended API Set 2 specification, but was replaced by utimensat() in IEEE Std 1003.1-2008 ("POSIX.1"). so yeah, it had its era and then it got upgraded. π
π°οΈ HISTORY π°οΈ
- the
utimes()system call appeared in 4.2BSD - the
futimes()andlutimes()system calls first appeared in FreeBSD 3.0 - the
futimesat()system call appeared in FreeBSD 8.0
look at the timeline bestie, they were all born at different times. very on brand for a manpage about times. π
and that's the whole page! this one came from the Debian folks, dated 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
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms