Skip to content
/ fs_sim Public

UNIX file system simulator managing a virtual disk with mounting, CRUD operations, defragmentation, and consistency checking capabilities

Notifications You must be signed in to change notification settings

n5q/fs_sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Design Choices

The file system simulator is designed around a rigid block-based structure where the disk is treated as an array of 128 blocks (1024 bytes each).

Consistency Checking (fsck) Consistency checking is implemented directly within the mount process. Before a file system is successfully mounted, fsck iterates through the superblock to verify the six consistency rules. If any check fails, the mount is aborted, so never operate on a corrupted image.

Defragmentation Strategy The defragmentation algorithm (fs_defrag) was chosen to minimize data movement.

  1. It scans the inode table to identify all files (not directories).
  2. It sorts these files based on their current start_block index.
  3. It iterates through the sorted list, reading the file data into a buffer and writing it to the disk starting immediately after the superblock.
  4. Finally, it reconstructs the free block bitmap to match the new compacted layout.

Recursive Deletion The fs_delete function utilizes a helper function rmrf. This implements a recursive strategy: it first identifies children of the target node. If a child is a directory, it recurses, if it is a file, it frees the associated blocks. This ensures that deleting a directory leaves no orphaned inodes.

Command Parsing The main function in fs.c uses a dispatch pattern. It reads input line-by-line using getline and tokenizes it using strtok. Specialized handler functions (handler_0args, handler_1args, handler_2args, handler_bargs) are used to validate argument counts and types before passing control to the filesystem logic.


System Calls

disk.c

  • disk_open: Uses open() to acquire a file descriptor for the emulated disk file.
  • disk_close: Uses close() to release the file descriptor.
  • disk_bread, disk_bwrite, disk_sbwrite:
    • Use lseek() to move the file offset to the specific block location.
    • Use read() and write() respectively to transfer 1024-byte blocks between memory and the disk file.

fs_sim.c

  • fs_mount: Uses close() to ensure file descriptors are not leaked if the mount fails due to fsck errors.

fs.c

  • main:
    • Uses fopen() and fclose() for handling the script input file.

Testing Implementation

Testing was conducted using an automated Python-based test suite (test.py and test_valgrind.py) that implements both functional and memory safety verification.

Functional Testing (test.py) The testing framework operates on a "black box" basis with state verification: After execution, the script compares the binary disk files generated by the simulator against "expected" disk images. This ensures that even if the console output is correct, the actual bytes written to the emulated disk are accurate.

Memory Safety Testing (test_valgrind.py) A separate test suite wraps the execution in Valgrind (--tool=memcheck --leak-check=yes).

  • It parses the Valgrind log file to ensure "ERROR SUMMARY: 0 errors" is present.
  • This verifies that there are no memory leaks or invalid read/write operations.

About

UNIX file system simulator managing a virtual disk with mounting, CRUD operations, defragmentation, and consistency checking capabilities

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •