The file system’s inode (index node) contains one inode for each file residing in the file system. An inode stores essential information, including:
o File Type: Indicates if the entry is a regular file, directory, character device, or symbolic link.
o File Size: Represents the size of the file in bytes.
o Permissions: Specifies the read, write, and execute permissions for different users.
o Owners and Groups: Stores the user and group IDs for the file’s owner and group.
o Time Stamps: Records three timestamps: the last access time, the last modification time (content), and the last status change time (metadata).
o File Modes: Includes permissions and special attributes associated with the file.
o Number of Hard Links: Indicates the number of hard links pointing to the inode.
o Pointers to Data Blocks: Stores pointers to the data blocks that contain the file’s actual content.
o Number of Blocks Allocated: Represents the number of blocks allocated to the file.
The inode acts as a central data structure within the file system, encapsulating these crucial details about each file. In Linux, the stat system call is used to retrieve information about a file or directory. When stat() is called on a file or directory, it accesses the associated inode internally to populate the struct stat data structure.
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
nlink_t st_nlink; /* Number of hard links */
uid_t st_uid; /* User ID of owner */
gid_t st_gid; /* Group ID of owner */
dev_t st_rdev; /* Device ID (if special file) */
off_t st_size; /* Total size, in bytes */
blksize_t st_blksize; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
This struct stat contains fields that correspond to various attributes and metadata of a file. By utilizing the stat system call and accessing the inode internally, we can retrieve information about files and directories in a file system.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
An Article by: Yashwanth Naidu Tikkisetty
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
