1.6 KiB
Executable File
1.6 KiB
Executable File
Chapter 4 – 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
statstructure 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()andfchown().
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 withreaddir(). - Low-level:
getdents()system call.
File Timestamps
utime(),utimes(),futimens(), andutimensat()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()anddup2().
- Set non-blocking mode (
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.