FSTATAT(3POSIX)
๐ธ FSTATAT(3POSIX) โ POSIX Programmer's Manual ๐ธ
okay bestie, welcome to the POSIX Programmer's Manual ๐ this one is fstatat, lstat and stat โ the trio that tells you literally everything about a file. pull up a chair, we're going deep ๐
๐ PROLOG ๐
real talk before we start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
no cap, that's the standard disclaimer and you should actually read it, it's not just decoration ๐ญ
โจ NAME โจ
fstatat, lstat, stat โ get file status ๐
๐ฆ SYNOPSIS ๐ฆ
#include <fcntl.h>
#include <sys/stat.h>
int fstatat(int fd, const char *restrict path,
struct stat *restrict buf, int flag);
int lstat(const char *restrict path, struct stat *restrict buf);
int stat(const char *restrict path, struct stat *restrict buf);
๐ DESCRIPTION ๐
so the stat() function shall obtain information about the named file and write it to the area pointed to by the buf argument. The path argument points to a pathname naming a file. Read, write, or execute permission of the named file is not required. like, you don't need to be allowed in to peek at the stats, which is giving sneaky but it's legal ๐ซถ
An implementation that provides additional or alternate file access control mechanisms may, under implementation-defined conditions, cause stat() to fail. In particular, the system may deny the existence of the file specified by path. ๐ญ
If the named file is a symbolic link, the stat() function shall continue pathname resolution using the contents of the symbolic link, and shall return information pertaining to the resulting file if the file exists. so it follows the link, it's a follower, main character energy โจ
The buf argument is a pointer to a stat structure, as defined in the <sys/stat.h> header, into which information is placed concerning the file.
The stat() function shall update any time-related fields (as described in the Base Definitions volume of POSIX.1โ2017, Section 4.9, File Times Update), before writing into the stat structure.
shared memory objects ๐ง
If the named file is a shared memory object, the implementation shall update in the stat structure pointed to by the buf argument the st_uid, st_gid, st_size, and st_mode fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file permission bits need be valid. The implementation may update other fields and flags.
typed memory objects ๐ธ
If the named file is a typed memory object, the implementation shall update in the stat structure pointed to by the buf argument the st_uid, st_gid, st_size, and st_mode fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file permission bits need be valid. The implementation may update other fields and flags.
For all other file types defined in this volume of POSIX.1โ2017, the structure members st_mode, st_ino, st_dev, st_uid, st_gid, st_atim, st_ctim, and st_mtim shall have meaningful values and the value of the member st_nlink shall be set to the number of links to the file. those fields are the ones that actually matter, bestie, memorize them fr fr
๐ฆ lstat() โ the one that doesn't follow ๐ฆ
The lstat() function shall be equivalent to stat(), except when path refers to a symbolic link. In that case lstat() shall return information about the link, while stat() shall return information about the file the link references. it's giving "I look at the link itself, not where it's going" ๐
For symbolic links, the st_mode member shall contain meaningful information when used with the file type macros. The file mode bits in st_mode are unspecified. The structure members st_ino, st_dev, st_uid, st_gid, st_atim, st_ctim, and st_mtim shall have meaningful values and the value of the st_nlink member shall be set to the number of (hard) links to the symbolic link. The value of the st_size member shall be set to the length of the pathname contained in the symbolic link not including any terminating null byte. ๐
๐ fstatat() โ the relative-path icon ๐
The fstatat() function shall be equivalent to the stat() or lstat() function, depending on the value of flag (see below), except in the case where path specifies a relative path. In this case the status shall be retrieved from a file relative to the directory associated with the file descriptor fd instead of the current working directory. If the access mode of the open file description associated with the file descriptor is not O_SEARCH, the function shall check whether directory searches are permitted using the current permissions of the directory underlying the file descriptor. If the access mode is O_SEARCH, the function shall not perform the check. ๐
Values for flag 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 status of the symbolic link is returned. (yes bestie, this is the "don't chase the link" button โจ)
If fstatat() is passed the special value AT_FDCWD in the fd parameter, the current working directory shall be used and the behavior shall be identical to a call to stat() or lstat() respectively, depending on whether or not the AT_SYMLINK_NOFOLLOW bit is set in flag. ๐ซถ
๐ฅ RETURN VALUE ๐ฅ
Upon successful completion, these functions shall return 0. Otherwise, these functions shall return -1 and set errno to indicate the error. zero means slay, minus one means we have a problem ๐
๐ ERRORS ๐
these functions shall fail if: ๐ญ
EACCESโ Search permission is denied for a component of the path prefix.EIOโ An error occurred while reading from the file system.ELOOPโ A loop exists in symbolic links encountered during resolution of thepathargument.ENAMETOOLONGโ The length of a component of a pathname is longer than{NAME_MAX}.ENOENTโ A component ofpathdoes not name an existing file orpathis an empty string.ENOTDIRโ A component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory, or thepathargument 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.EOVERFLOWโ The file size in bytes or the number of blocks allocated to the file or the file serial number cannot be represented correctly in the structure pointed to bybuf.
the fstatat() function shall fail if: ๐ซ
EACCESโ The access mode of the open file description associated withfdis notO_SEARCHand the permissions of the directory underlyingfddo not permit directory searches.EBADFโ Thepathargument does not specify an absolute path and thefdargument is neitherAT_FDCWDnor a valid file descriptor open for reading or searching.ENOTDIRโ Thepathargument is not an absolute path andfdis a file descriptor associated with a non-directory file.
these functions may fail if: ๐
ELOOPโ More than{SYMLOOP_MAX}symbolic links were encountered during resolution of thepathargument.ENAMETOOLONGโ 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}.EOVERFLOWโ A value to be stored would overflow one of the members of thestatstructure.
the fstatat() function may fail if: ๐ธ
EINVALโ The value of theflagargument is not valid.
The following sections are informative. โจ
๐ EXAMPLES ๐
Obtaining File Status Information ๐ฆ
The following example shows how to obtain file status information for a file named /home/cnd/mod1. The structure variable buffer is defined for the stat structure.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct stat buffer;
int status;
...
status = stat("/home/cnd/mod1", &buffer);
Getting Directory Information ๐
The following example fragment gets status information for each entry in a directory. The call to the stat() function stores file information in the stat structure pointed to by statbuf. The lines that follow the stat() call format the fields in the stat structure for presentation to the user of the program.
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <stdio.h>
#include <stdint.h>
struct dirent *dp;
struct stat statbuf;
struct passwd *pwd;
struct group *grp;
struct tm *tm;
char datestring[256];
...
/* Loop through directory entries. */
while ((dp = readdir(dir)) != NULL) {
/* Get entry's information. */
if (stat(dp->d_name, &statbuf) == -1)
continue;
/* Print out type, permissions, and number of links. */
printf("%10.10s", sperm (statbuf.st_mode));
printf("%4d", statbuf.st_nlink);
/* Print out owner's name if it is found using getpwuid(). */
if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
printf(" %-8.8s", pwd->pw_name);
else
printf(" %-8d", statbuf.st_uid);
/* Print out group name if it is found using getgrgid(). */
if ((grp = getgrgid(statbuf.st_gid)) != NULL)
printf(" %-8.8s", grp->gr_name);
else
printf(" %-8d", statbuf.st_gid);
/* Print size of file. */
printf(" %9jd", (intmax_t)statbuf.st_size);
tm = localtime(&statbuf.st_mtime);
/* Get localized date string. */
strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
printf(" %s %s\n", datestring, dp->d_name);
}
Obtaining Symbolic Link Status Information ๐
The following example shows how to obtain status information for a symbolic link named /modules/pass1. The structure variable buffer is defined for the stat structure. If the path argument specified the pathname for the file pointed to by the symbolic link (/home/cnd/mod1), the results of calling the function would be the same as those returned by a call to the stat() function.
#include <sys/stat.h>
struct stat buffer;
int status;
...
status = lstat("/modules/pass1", &buffer);
๐ APPLICATION USAGE ๐
None. literally nothing, bestie, we're done here ๐
๐ญ RATIONALE ๐ญ
okay so bestie, let's talk about why this whole thing exists, because the reasoning is actually lowkey iconic โจ
That little paragraph about "additional or alternate file access control mechanisms"? The tea is: it's there so a secure implementation can exist where a process whose label does not dominate the file's label just... can't do a stat() function. periodt. And ngl, this is not about read permission at all โ a process whose label does dominate the file's label doesn't need read permission either. And here's the wild part: an implementation that supports write-up operations could fail fstat() function calls even though it has a valid file descriptor open for writing. it's giving "permission said no but you literally have the fd open" ๐ญ
Now for the main character energy: the whole reason fstatat() exists ๐
Its purpose is to obtain the status of files in directories other than the current working directory without being exposed to race conditions. Because bestie, any part of the path of a file could get changed in parallel to a call to stat(), and that results in unspecified behavior ๐ no cap, that's undefined-adjacent chaos.
But the glow-up? By opening a file descriptor for the target directory and then using the fstatat() function, it can be guaranteed that the file whose status is returned is located relative to the desired directory. ๐ like, it's locked in. the vibe check passes. the directory is the directory, fr fr.
๐ฎ FUTURE DIRECTIONS ๐ฎ
None. ๐ that's it. nothing coming. the future is now and the future is nothing. slay.
๐ SEE ALSO ๐
go read these next bestie, they're all part of the friend group:
access()chmod()fdopendir()fstat()mknod()readlink()symlink()
also, from the Base Definitions volume of POSIX.1โ2017: Section 4.9, File Times Update, plus these headers โ <fcntl.h>, <sys_stat.h>, <sys_types.h> ๐ฆ
๐ COPYRIGHT ๐
okay so legal moment, but make it cute (and accurate, obviously):
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
bestie if you find a typo, don't come for me, come for the conversion process ๐ the link above is where you report it, mwah ๐ซถ
and that's the whole page, gorgeous โจ this one came from IEEE/The Open Group, dated 2017, and it's FSTATAT(3POSIX) โ go forth and fstatat() responsibly, no race conditions, no cap ๐๐ธ๐
๐ฆ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 ยท Other
- debian trixie: manpages-posix-dev 2017a-2 ยท Other
- ubuntu jammy: manpages-posix-dev 2017a-2 ยท Other
- ubuntu noble: manpages-posix-dev 2017a-2 ยท Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 ยท Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 ยท Other
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms