Skip to content

Ansh2625/In-Memory-File-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

In-Memory File System

A console-based in-memory file system built using core Data Structures like Tree, Trie, HashMap and Algorithms like String Parsing, Recursion, DFS.
Simulates Unix-like file operations such as mkdir, cd, ls, touch, etc. Without using the actual file system.

Problem Statement

Design and implement a file system that:

  • Maintains a virtual folder/file structure in memory.
  • Allows users to create, delete, and navigate directories.
  • Stores file metadata and simulates basic content handling.
  • Lookup and Traversal should be fast.
  • Allows Unix-Like file operations such as mkdir, cd, cat, echo.
  • Built using OOP principle.

Project Structure

commandinvoker pacakage

  • CommandInvoker.java: Implements the invoker part of the Command Pattern. It stores and executes commands provided by the user interface (like mkdir, cd, ls, etc.).

commands packge

  • Command.java: Interface for all user commands in the system.
  • MkdirCommand.java,CdCommand.java,LsCommand.java,etc: Concrete command classes implementing the Command.java interface. Each class represents a specific file system command and delegates the logic to the corresponding FileSystemOperation.java.

filesystem package

  • core package
    • FileSystemState.java: Maintains the current state of the file system, including the root node, current working directory, undo/redo stacks, and Trie root. Acts as a shared context passed to all file system operations.
  • helper package
    • Helper.java: Provides utility functions to resolve file paths (both absolute and relative), compute absolute paths, and insert entries into the Trie for auto-completion support.
  • operations package
    • FileSystemOperation.java: Interface representing a file system operation.
    • MkdirOperation.java, CdOperation.java, TouchOperation.java, etc: Each class implements the FileSystemOperation.java interface to encapsulate a specific file system behavior (like creating directories, changing directories, file creation, content writing, deletion, etc.). These operations modify the file system state and support undo/redo functionality.
    • FileSystem.java: Core class that maintains the reference to the current file system state. Provides methods to get/set current directory, root node, and manages undo/redo stacks. Executes operations via the FileSystemOperation.java interface, acting as the central orchestrator of all file system changes.
    • Action.java: Represents a reversible operation for supporting undo/redo functionality.
    • TrieNode.java: Represents a node in a trie (prefix tree) structure used for efficient file/folder name search. Each node maintains its children and a flag to mark the end of a word. Supports auto-complete and fast lookup features within the file system.

node package

  • Node.java: Core building block of the in-memory file system. Represents both files and folders using a unified structure. Each node stores metadata such as name, content (for files), child nodes (for directories), parent reference, and optional symbolic link support.

Main.java

  • Entry point of the application. Initializes the FileSystem.java, reads user commands from input, and routes them through the CommandInvoker.java for execution. Simulates a terminal-like interface for interacting with the in-memory file system.

Class Diagram

Class Diagram

Concepts Used

DSA Algorithms & Techniques

  • KMP (Knuth-Morris-Pratt) Pattern Matching
    Used in the search command to efficiently match substrings inside file contents. Built from scratch with custom LPS (Longest Prefix Suffix) construction for optimal linear-time performance.
  • DFS (Depth-First Search)
    Used in operations like tree, find, and search to recursively traverse the file system hierarchy, exploring all nested directories and files.
  • Trie (Prefix Tree)
    Used to index file and folder names for fast autocomplete/search functionality (future extension ready). Trie insertion logic is handled during file/folder creation.
  • Stack (Undo/Redo Functionality)
    Two stacks are used to implement undo and redo operations, allowing users to reverse or reapply changes with precision.
  • HashSet / HashMap
    Used for visited tracking in DFS (to prevent infinite cycles) and fast lookups of child nodes in directories.

Object-Oriented Design Principles

  • Command Pattern
    Every command (mkdir, cd, touch, echo, etc.) implements a common interface (Command) and delegates logic to operation classes like MkdirOperation, CdOperation, etc., encapsulating request execution and promoting extensibility.
  • Separation of Concerns (SOC)
    Clear separation between command parsing (CommandInvoker), execution logic (FileSystemOperation classes), and state management (FileSystemState).
  • Single Responsibility Principle (SRP)
    Each class has one focused responsibility. e.g., Helper only deals with path resolution and trie updates, Action only represents undo/redo state, etc.
  • Open-Closed Principle (OCP)
    New commands can be added without modifying existing code – just create a new Command + corresponding Operation class.
  • Immutability & Encapsulation
    Classes like Node restrict direct external modification through proper getter/setter encapsulation.

How to Run

  1. Prerequisites:
  • Java 17 or later installed.
  • VS Code (recommended) or any Java-supported IDE.
  1. Clone the Repository:
git clone https://github.com/Ansh2625/In-Memory-File-System 
cd In-Memory-File-System
  1. Import the Project:
  • Open VS Code.
  • Select File > Open and choose the project's root folder (In-Memory-File-System).
  1. Build the Project:
  • Open Main.java file inside src.
  • Click the Run button on top, or right-click and choose Run java.
  1. Run the Application:
  • Right-click on Main.java.
  • Alternatively, use the terminal:
javac -d bin src/Main.java 
java -cp bin Main
  1. Interact with the System:
    The system simulates in-memory file system operations. Check console logs for actions like file/folder creation, navigation, content manipulation, search, and undo/redo execution.

Test Run

Features

  • In-Memory File System supporting dynamic creation and manipulation of files and directories.
  • Basic UNIX-like Shell Commands: mkdir, cd, ls, touch, echo, cat, rm, tree, pwd, etc.
  • Symbolic Link (ln) Support: Create and traverse symbolic links like in Linux.
  • Command Pattern Architecture: Each shell command is modular and follows the command design pattern for extensibility.
  • Undo/Redo Functionality: Supports stepwise rollback or re-application of actions like mkdir, touch, rm, echo.
  • Content Search using KMP: Efficient substring matching in file contents using the Knuth-Morris-Pratt algorithm.
  • Auto-Complete Support: Simulates shell-like suggestions using Trie data structure.
  • Recursive Tree Display: Visualize file structure like tree command in Linux.

Future Improvements

  • Persistence Layer: Integrate file saving/loading functionality using serialization to preserve state across sessions.
  • User & Permissions System: Add support for multiple users, authentication, and Unix-style permission control (rwx).
  • Disk Quota Simulation: Impose limits on storage size per folder/user to mimic real file system constraints.
  • Timestamps & Metadata: Store and display creation/modification timestamps for files and directories.

License

This project is open-source and available under the MIT License.

Author

Developed by Ansh Sharma. Contact, anshsharma2625@gmail.com.

About

A custom-built In-Memory File System in Java supporting shell-like commands, symbolic links, undo/redo, and autocomplete.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages