chapter 39 - interlude: files and directories Flashcards
(13 cards)
what is a file
linear sequence of bytes identified by an inode number
What is a directory?
File mapping to inode numbers
What is a directory tree?
A hierarchy starting from root /, containing nested directories and files
what does int fd = open(“foo”, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR); do
- O_CREAT → create if not exist
- O_WRONLY → open for writing
- O_TRUNC → empty the file if it exists
- S_IRUSR | S_IWUSR → user can read/write
- Returns a file descriptor, placed in the process’s
ofile[NOFILE]
array
What is a file descriptor?
integer used to access an open file
starts @ 3 - bc the 0,1,2 er reserved
What does lseek(fd, offset, whence) do?
Moves the file offset
What is stored in the Open File Table?
Read/write flags, inode pointer, offset, and reference count
What happens after fork() to file descriptors?
Child gets copies of parent’s FDs, sharing the same offset and inode
What does dup(fd) do?
Duplicates fd, both share offset, inode, and permissions
What does fsync(fd) do?
Forces all dirty data for that file to disk immediately
What does rename(“old”, “new”) do?
Atomically renames a file; ensures no corruption even during crashes
What does stat() or fstat() return?
Metadata like inode, size, permissions, owner, and timestamps
What does unlink() do?
Removes a file; deletes it once all links and open FDs are gone