diff --git a/src/filesys/filesys.rs b/src/filesys/filesys.rs index 3130ceb..866cc74 100644 --- a/src/filesys/filesys.rs +++ b/src/filesys/filesys.rs @@ -1,6 +1,89 @@ +use crate::simulator::disk; +const FREE_MAP_SECTOR : i32 = 0; +const DIRECTORY_SECTOR : i32 = 1; + pub struct Filesys { pub free_map_file : Open_File, //Bit map of free disk blocks, represented as a file pub directory_file : Open_file //"Root" directory -- list of file names, represented as a file +} + +impl Filesys { + + /// Initialize the file system. If format = true, the disk has + /// nothing on it, and we need to initialize the disk to contain + /// an empty directory, and a bitmap of free sectors (with almost but + /// not all of the sectors marked as free). + /// + /// If format = false, we just have to open the files + /// representing the bitmap and the directory. + /// + /// ### parameters + /// + /// -**format** should we initialize the disk? + pub fn init_filesys(format : bool) -> Filesys { + if format { + let free_map = bitmap::init_bitmap(disk::NUM_SECTORS); + let directory = directory::init_directory(num_dir_entries); + + free_map.mark(FREE_MAP_SECTOR); + free_map.mark(DIRECTORY_SECTOR); + + let map_header = filehdr::allocate(&free_map, FREE_MAP_SECTOR); + let dir_header = filehdr::allocate(&free_map, directory_file_size); + + dir_header.set_dir(); + + map_header.write_back(FREE_MAP_SECTOR); + dir_header.write_back(DIRECTORY_SECTOR); + + let free_map_file = openfile::init_open_file(FREE_MAP_SECTOR); + let directory_file = openfile::init_open_file(DIRECTORY_SECTOR); + + free_map.write_back(free_map_file); + directory.write_back(directory_file); + + Filesys { + free_map_file, + directory_file + } + } else { + Filesys { + free_map_file : openfile::init_open_file(FREE_MAP_SECTOR), + directory_file : openfile::init_open_file(DIRECTORY_SECTOR) + } + } + } + + /// create a file in the Nachos file system (similar to UNIX create). + /// Since we can't increase the size of files dynamically, we have + /// to give Create the initial size of the file. + // + /// The steps to create a file are: + /// Make sure the file doesn't already exist + /// Allocate a sector for the file header + /// Allocate space on disk for the data blocks for the file + /// Add the name to the directory + /// Store the new file header on disk + /// Flush the changes to the bitmap and the directory back to disk + /// + /// Create fails if: + /// file is already in directory + /// no free space for file header + /// no free entry for file in directory + /// no free space for data blocks for the file + /// + /// Note that this implementation assumes there is no concurrent access + /// to the file system! + /// + /// ### parameters + /// + /// -**name** is the name of file to be created (NOT MODIFIED) + /// -**initialSize** is the size of file to be created + /// \return NO_ERROR if everything goes ok, otherwise, return an error + // code as define in msgerror.h + pub fn create(name : String, initial_size : i32) -> i32 { + + } } \ No newline at end of file