Sub topic 11
Directory Structures
This chapter assumes you understand files, system calls, and the difference between user mode and kernel mode. It also assumes basic binary arithmetic and familiarity with storage devices.
A directory is a file-system structure that maps a name to a file or another directory. The mapped object may be represented by an inode or a similar metadata record. A directory usually stores names and references, while the referenced metadata stores information such as size, ownership, permissions, and block locations.
Single-level and two-level directories
A single-level directory stores every file in one directory. It is simple to implement, but every filename must be unique across the whole system.
A two-level directory gives each user a separate directory. Two users can therefore create files with the same name. A system-wide master directory maps user names to their personal directories.
| Structure | Main advantage | Main limitation |
|---|---|---|
| Single-level | Very simple lookup and management | Filename conflicts and no grouping |
| Two-level | Separates users and permits repeated filenames | Limited grouping within one user directory |
| Tree | Supports natural grouping and scalable paths | Requires path traversal and more management |
| Acyclic graph | Allows shared files or directories | Sharing and deletion need reference tracking |
Tree directories
Most general-purpose systems use a tree structure. The root directory is the tree's starting point. Each directory can contain files and child directories. A pathname identifies an object by listing directory names in order.
An absolute path starts at the root, such as /home/lee/report.txt. A relative path starts at the process's current working directory, such as notes/report.txt. The operating system resolves a path one component at a time. At each step, it checks that the current object is a directory and searches for the next name.
A directory entry does not usually contain the entire file. It contains a filename and a reference to the file's metadata. This separation allows multiple directory entries to refer to the same file.
Acyclic-graph directories
An acyclic graph directory structure extends a tree by allowing sharing. A file can have directory entries in several directories. A shared subdirectory can also be reachable from multiple parents.
Hard links are an example of shared file references. The file remains allocated while at least one directory entry refers to it. A reference count in the file's metadata can track this condition. Removing one name decreases the count. The storage can be released when the count reaches zero and no process still has the file open.
The graph must remain acyclic. If directories could point back to their ancestors, path traversal and recursive deletion could loop forever. Systems may prevent directory hard links or require special checks to preserve this property.
Common errors
- Treating a pathname as file data rather than a sequence of directory lookups.
- Assuming filenames are globally unique in a tree.
- Confusing a hard link, which adds another directory entry, with a symbolic link, which stores a path to another object.
- Forgetting that deleting a name does not necessarily delete the underlying file immediately.
Exercise
In a tree directory, /a/b/c is an absolute path. What does the operating system do first when resolving it?
Show answer
It starts at the root directory / and looks up the entry named a, then continues with b and c.