Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Structure of Regular Files & Directories

Lesson 15 of 32 in the free Design of Unix Operating System notes on Siksha Sarovar, written by Rohit Jangra.

Introduction

Understanding how regular files and directories are structured internally is essential for comprehending UNIX file system operations. Both are represented by inodes, but their data blocks contain very different types of information.

---

Structure of Regular Files

A regular file's inode points to data blocks that contain the file's actual content (bytes of data).

How Data is Stored:

  1. Small Files (≤ 10 blocks):
  • The inode's 10 direct block pointers point directly to the data blocks.
  • No indirection is needed.
  • Example: A 5 KB file (with 1 KB blocks) uses 5 direct pointers.
  1. Medium Files:
  • When direct pointers are exhausted, the single indirect pointer is used.
  • The indirect block contains pointers to more data blocks.
  1. Large Files:
  • Double indirect and triple indirect pointers are used for very large files.
  • This multi-level indexing scheme allows files up to several gigabytes.

Example — Reading Byte at Offset 9000 (Block Size = 1024 bytes):

  • Block number = 9000 / 1024 = Block 8 (0-indexed).
  • Byte offset within block = 9000 % 1024 = 808.
  • Block 8 is within the direct pointer range (0-9), so the kernel reads the 9th direct pointer.

Holes in Files:

  • UNIX supports sparse files — files where some blocks are not allocated.
  • If a program writes to an offset beyond the current file size, intermediate blocks are not allocated.
  • Reading from a hole returns zeros.
  • The file appears large, but only allocated blocks consume disk space.

---

Structure of Directories

A directory is a special file whose data blocks contain a list of directory entries. Each entry maps a filename to an inode number.

Directory Entry Format (Traditional UNIX):

FieldSizeDescription
Inode Number2 bytesInode number of the file
Filename14 bytesName of the file (padded with null bytes)
Total16 bytes per entry
  • In traditional UNIX (System V), filenames are limited to 14 characters.
  • Modern systems (BSD, Linux) support longer filenames with variable-length directory entries.

Special Entries in Every Directory:

EntryInodeMeaning
.Current dir's inodeReference to itself
..Parent dir's inodeReference to parent directory

Example Directory Contents:

Inode  Filename
─────  ──────────
  100  .
   50  ..
  101  file1.txt
  102  file2.txt
  120  subdir
  • The directory itself has inode 100.
  • Its parent directory has inode 50.
  • file1.txt is at inode 101, file2.txt at inode 102.
  • subdir (a subdirectory) is at inode 120.

Deleting a File:

  • When a file is deleted (rm file1.txt), the kernel:
  1. Sets the inode number in the directory entry to 0 (marks it as free).
  2. Decrements the link count in the file's inode.
  3. If link count reaches 0 and no process has the file open, the inode and data blocks are freed.

How Path Resolution Works

To access /home/user1/file.txt:

  1. Start at root inode (inode 2).
  2. Read root's data blocks → find entry for home → get inode number (say 30).
  3. Read inode 30's data blocks → find user1 → get inode number (say 55).
  4. Read inode 55's data blocks → find file.txt → get inode number (say 101).
  5. Read inode 101 → access the file's data blocks.

Summary

  • Regular files store data in data blocks pointed to by the inode's block addresses.
  • Directories store (inode number, filename) pairs in their data blocks.
  • Every directory contains . and .. entries.
  • Path resolution is a step-by-step process of reading directory entries to find the target inode.
  • Sparse files (holes) are supported — unallocated blocks read as zeros.