Files
system-programming/chp4/README.md
Fabio Scotto di Santolo 7c73ac7f64 Update README.md
2025-06-27 14:18:02 +02:00

43 lines
1.6 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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 `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.