Skip to content

AnishMulay/unix-fs-defragmenter

Repository files navigation

C Build License: MIT

Unix File System Defragmenter

Unix File System Defragmenter is a standalone C utility for parsing binary disk images of a custom Unix-like file system and rewriting fragmented file layouts into contiguous block runs. The tool preserves the on-disk metadata model while rebuilding file data placement and the free-block chain into a denser, sequential layout.

Architecture & Core Systems

Disk Image Parsing

The utility performs binary file I/O directly against a raw disk image. It copies the boot block, reads the superblock from the fixed metadata region, and derives the inode and data-region boundaries from the superblock offsets. The inode table is then loaded as a packed array of on-disk inode records for traversal and rewrite.

Inode & Pointer Resolution

Each active inode is resolved through its direct data pointers, followed by single-indirect, double-indirect, and triple-indirect pointer trees. Indirect metadata blocks are relocated ahead of the data blocks they reference so the rewritten image maintains a valid, fully connected pointer graph after block renumbering.

The Defragmentation Engine

The defragmentation pass walks active inodes in inode-number order, copies each referenced block into the next contiguous sector of the output image, and rewrites every block pointer to match the new layout. After all reachable file data and pointer blocks are packed, the remaining data-region blocks are reinitialized into a monotonically increasing free-block linked list. The optimized disk image is then written back to disk as disk_defrag.

Building and Execution

Build the utility from the repository root:

make

Run the defragmenter against a disk image:

./defrag <disk_image>

The command emits a rewritten image named disk_defrag in the current working directory.

About

A standalone C utility that parses Unix-style binary disk images and optimizes file layouts by relocating data blocks contiguously to eliminate fragmentation.

Topics

Resources

License

Stars

Watchers

Forks

Contributors