This project implements a file system simulator in Python, allowing users to perform various file system operations through a command-line interface. The simulator includes functionalities for creating directories, managing files, navigating directories, and saving/loading the file system state.
- The
FileSystemclass is defined inscript.py. - The
Nodeclass, which represents individual file system nodes, is an inner class ofFileSystem. - Error classes (
FileSystemError,FileNotFoundError,DirectoryNotFoundError,InvalidPathError) are imported from an external module namederrors.
The FileSystem class provides a set of methods for managing the file system:
mkdir: Create a new directory.cd: Changes the current directory. Support navigating to the parent directory using.., moving to the root directory using/, and navigating to a specified absolute path. Basically anything that you can do in a normal terminal. Since there is no user level implementation, ~ and / should take you to root.ls: List the contents of the current directory or a specified directory.grep: Search for a specified pattern in a file. PS: Its a bonuscat: Display the contents of a file.touch: Create a new empty file.echo: Write text to a file. ex -echo 'I am "Finding" difficult to write this to file' > file.txtmv: Move a file or directory to another location. ex -mv /data/ice_cream/cassata.txt ./data/boring/ice_cream/mississippimudpie/cp: Copy a file or directory to another location. ex -cp /data/ice_cream/cassata.txt .**. For current directory **rm: Remove a file or directory.
The main function in main.py orchestrates the execution of the file system simulator:
- It initializes a
FileSystemobject and sets up signal handlers for interrupt and termination signals (SIGINTandSIGTERM). - If the script is invoked with the
--loadargument, it attempts to load the previous state of the file system from "autosave.json". - The script enters a loop to continuously prompt the user for commands and execute them using the
execute_commandfunction. - If the user enters the "exit" command, the script saves the current state of the file system to "autosave.json" and exits gracefully.
The execute_command function parses and executes user-entered commands:
- It splits the command into tokens and determines the operation to perform based on the first token.
- Depending on the operation, it calls the corresponding method of the
FileSystemobject. - If the command is invalid, it prints an error message.
The save_state_and_exit function saves the current state of the file system to a JSON file and exits the script:
- It is called when the user interrupts the script (e.g., pressing
Ctrl+C) or when the "exit" command is entered.
The load_state function attempts to load the previous state of the file system from a JSON file:
- It catches
FileNotFoundErrorandjson.JSONDecodeErrorexceptions and prints appropriate error messages if the file is not found or has an invalid JSON format.
We need python3 installed to execute this python program
- Running the code to use file system operations
python3 main.py - Running unit tests
python3 -m unittest unit_test.py - For autosavng current state in autosave.json
ctrl c
Understanding: Initially, I carefully reviewed the project requirements to gain a clear understanding of the task at hand. This involved identifying the necessary functionalities required for a file system simulator, such as directory management and file manipulation.
Designing Structure: With a solid understanding of the requirements, I proceeded to design the class structure. I created the FileSystem class to encapsulate the file system functionality and an inner Node class to represent the nodes within the file system.
Implementing Features: Each method within the FileSystem class was then implemented to execute specific file system operations. I ensured that each method addressed a particular aspect of file system management, resulting in a comprehensive set of functionalities.
Error Handling: To enhance the robustness of the file system simulator, I incorporated error handling mechanisms. This involved importing error classes from an external module and implementing graceful error handling to handle various scenarios encountered during execution.
Main Script: To orchestrate the execution of the file system simulator, I developed the main script. This script provided a command-line interface for user interaction, enabling users to interact with the file system and manage its state effectively.
State Management: I implemented functionality within the file system to save and load its state. This feature allowed users to persist their work across sessions, enhancing the usability and convenience of the simulator.
Testing and Refinement: To ensure the reliability and correctness of the implementation, I wrote unit tests to validate the functionality of each method. I iteratively refined the implementation based on test results and feedback, ensuring that the simulator met the specified requirements.
Documentation: Finally, I provided comprehensive project documentation via a README file. This documentation offered an overview of the file system simulator and provided guidance on how to use it effectively, contributing to its usability and accessibility.
- Outcome: PASSED
- Explanation: This test verifies the functionality of the
mkdirmethod, which creates a new directory in the file system. It checks whether the directory was successfully created and if it is marked as a directory in the file system's current directory. The test passed as the directory was created, and its properties were correctly set.
- Outcome: PASSED
- Explanation: This test validates the functionality of the
cdmethod, responsible for changing the current directory in the file system. It creates a directory, changes to it usingcd, and checks if the current directory's name matches the expected name. The test passed as the current directory was successfully changed to the newly created directory.
- Outcome: PASSED
- Explanation: The
test_lsfunction tests thelsmethod, which lists the contents of the current directory in the file system. After creating a directory, the test invokeslsand verifies if the directory is listed among the current directory's contents. The test passed as the directory was correctly listed.
- Outcome: PASSED
- Explanation: This test assesses the
touchmethod's functionality, which creates a new file in the current directory of the file system. After creating a new file, the test checks if the file exists in the current directory and if it is marked as a file (not a directory). The test passed as the file was successfully created, and its properties were accurately set.
These tests passed as expected because the implemented methods behaved as intended, satisfying the specified requirements and functionality.
- we can save the current path by ctrl - c, but we cannot start from that path right now by using something like --load.
- I would work on this thing, any collaboration, help or suggestion is heartfully welcome.
https://docs.rtems.org/branches/master/filesystem/in-memory.html