# Chapter 3 – Advanced File I/O ## File Metadata and the `stat` Family - Use `stat()`, `fstat()`, `lstat()` to retrieve file metadata: - File type, size, inode number, permissions, ownership, timestamps (atime, mtime, ctime). - The `stat` structure includes all these details. ## File Types - Regular file, directory, symbolic link, character/block device, FIFO, socket. - Use macros like `S_ISREG()`, `S_ISDIR()` to test file types. ## File Permissions and Modes - Represented by 12 bits (e.g., `rwxr-xr--`), plus sticky/SUID/SGID bits. - Manipulate with `chmod()`, `fchmod()`, `umask()`. - Ownership changed with `chown()` and `fchown()`. ## Links - **Hard links**: multiple filenames point to the same inode. - **Symbolic links**: separate files that reference other paths; can be broken. - Use `link()`, `unlink()`, `symlink()`, `readlink()`. ## Directories - Open with `opendir()`, read entries with `readdir()`. - Low-level: `getdents()` system call. ## File Timestamps - `utime()`, `utimes()`, `futimens()`, and `utimensat()` to update access/modification times. ## File Descriptors and Flags - Manipulate with `fcntl()`: - Set non-blocking mode (`O_NONBLOCK`), FD_CLOEXEC flag, file locks. - Duplicate with `dup()` and `dup2()`. ## File Locking - `flock()` for advisory locking (whole file). - `fcntl()` for POSIX record locking (ranges). ## Temporary Files - `tmpfile()`, `mkstemp()` are safe methods to create temp files. ## Summary - Chapter 3 focuses on extended file handling: metadata, links, directories, file modes, and advanced descriptor manipulation.