Part 2 of 7 · 2 min read
What a file is
Think of a library book. The book has its contents, and a catalogue card that lists its size, its owner and its shelf. The card is not the title on the spine, and a book can have more than one title on different lists. The analogy stops in one place: a book is on one shelf, while a file's bytes may sit in many separate places on the disk.
In plain words: To a program a file is a sequence of bytes that can be read and written at any offset, and that stays after the program ends. To the OS it is more: a record of metadata (the owner, the permissions, the size, the times of last use and change, and the places of its data), plus the data itself. The record has a number, the inode number, which is unique inside one file system. Programs use names, and the OS uses the number.
A worked example. The call stat("a.txt", &st) fills a structure with the metadata. For the file of section 3 it reported inode 12, one link and a size of 6 bytes. The name a.txt is not part of the record. A program reads the six bytes with open, read and close, and the file offset (where the next read or write starts) is kept for each open file, not in the file.
The key lines. The fields that matter on this page are st_ino (inode number), st_nlink (the number of names that point to the file), st_size (bytes) and st_mode (the type and the permission bits). Level 10 uses the permission bits.
File
A file is a named, persistent sequence of bytes that the OS stores together with metadata about it, such as its size, owner, permissions and times. Inside the file system the file is identified by a number (the inode number), and not by the names that point to it.
Example: a text file with 6 bytes, owner root and mode 0644 is one file with inode number 12 on a given file system.
Check yourself:
1. Is the name of a file stored in the metadata of the file?
2. Which stat field says how many names point to a file?
3. Where is the current offset of a file kept?