Stat syscall

From Helpful
Jump to navigation Jump to search

This article/section is a stub — some half-sorted notes, not necessarily checked, not necessarily correct. Feel free to ignore, or tell me about it.

The struct used for stat() in C is defined like:

struct stat {
	dev_t     st_dev;
	ino_t     st_ino;
 	mode_t    st_mode;
	nlink_t   st_nlink;
	uid_t     st_uid;
	gid_t     st_gid;
	dev_t     st_rdev;
	off_t     st_size;
	time_t    st_atime;
	time_t    st_mtime;
	time_t    st_ctime;
	blksize_t st_blksize;
	blkcnt_t  st_blocks;
	mode_t    st_attr;
};

A stat64() variation ists, which has the same struct names, but with 64-bit inodes, offsets, and block counts.


Meanings (approximately)

st_dev - Identifier of the device on which this file resides.

st_ino - Platform dependent, but if non-zero, uniquely identifies the file for a given value of st_dev (often inode on *nix, or file index on windows)

st_mode - File mode: file type and file mode bits (permissions)

there are often some helper functions that answer questions like 'is this writable' and 'is this a regular file', based on these bits

st_nlink - Number of hard links (where applicable)

st_uid - User identifier of the file owner

st_gid - Group identifier of the file owner


st_size - size, in bytes

*nix: for (unfollowed) symlinks this is the size of the path it contains

st_atime - Time of most recent access expressed in seconds.

st_mtime - Time of most recent content modification expressed in seconds.

st_ctime - Time of most recent metadata change expressed in seconds.

not really done on windows?

Note that the meaning and resolution of atime, mtime, and ctime varies with OS and filesystem, and even for the same OS and FS, the resolution doesn't have to be the same between different fields (see e.g. FAT32).



Specific operating systems add their own fields.

Some things (e.g. Python) tries to unifies this into a single call, and the below is adapted from its documentation

'st_blocks - amount of 512-byte blocks actually used (can be rather smaller than what st_size indicates, for sparse files, compression)

mostly on *nix

st_blksize

mostly on *nix
"Preferred" blocksize for efficient file system I/O. Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.

st_rdev

mostly on *nix
Type of device if an inode device.

st_flags - user defined flags

mostly on *nix

st_gen - File generation number.

mostly just BSD?

On Solaris and derivatives, the following attributes may also be available:

st_fstype - String that uniquely identifies the type of the filesystem that contains the file.

mostly just Solaris?

st_rsize - Real size of the file.

mostly on macOS

st_creator - Creator of the file.

mostly on macOS

st_type - File type.

mostly on macOS

st_file_attributes - Windows file attributes

on Windows

st_reparse_tag

on Windows