From 52929cef24703ace37859c24477140c880958b59 Mon Sep 17 00:00:00 2001 From: AmauryBrodu <60550980+AmauryBrodu@users.noreply.github.com> Date: Wed, 29 Mar 2023 13:17:21 +0200 Subject: [PATCH 1/7] added the bitmap, tests still need to be done --- src/utility/bitmap.rs | 102 ++++++++++++++++++++++++++++++++++++++++++ src/utility/mod.rs | 3 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/utility/bitmap.rs diff --git a/src/utility/bitmap.rs b/src/utility/bitmap.rs new file mode 100644 index 0000000..a34e5b4 --- /dev/null +++ b/src/utility/bitmap.rs @@ -0,0 +1,102 @@ +pub const BITS_IN_BYTE: usize = 8; +pub const BITS_IN_WORD: usize = 32; + +use std::fs::File; +use std::io::{Cursor, Write, Read}; + +pub struct BitMap { + num_bits: usize, + num_words: usize, + map: Vec, +} + +impl BitMap { + + pub fn mark(&mut self, which: usize) { + assert!(which >= 0 && which < self.num_bits); + self.map[which / BITS_IN_WORD] |= 1 << (which % BITS_IN_WORD); + } + + pub fn test(&self, which: usize) -> bool { + assert!(which < self.num_bits); + (self.map[which / BITS_IN_WORD] & (1 << (which % BITS_IN_WORD))) != 0 + } + + pub fn find(&mut self) -> i32 { + for i in 0..self.num_bits { + if !self.test(i) { + self.mark(i); + return i as i32; + } + } + -1 + } + + pub fn clear(&mut self, which: usize) { + assert!(which < self.num_bits, "index out of range"); + + let word_idx = which / BITS_IN_WORD; + let bit_idx = which % BITS_IN_WORD; + self.map[word_idx] &= !(1 << bit_idx); + } + + pub fn num_clear(&self) -> i32 { + let mut count = 0; + for i in 0..self.num_bits { + if !self.test(i) { + count += 1; + } + } + count + } + + pub fn print(&self) { + println!("Bitmap set:"); + for i in 0..self.num_bits { + if self.test(i) { + print!("{}, ", i); + } + } + println!(); + } + + + + pub fn fetch_from(&mut self, file_path: &str) -> std::io::Result<&Vec> { + // Ouvre le fichier en mode lecture seule + let mut file = File::open(file_path)?; + + // Lit les octets du fichier dans un buffer + let mut buffer = vec![0; self.num_words * std::mem::size_of::()]; + file.read_exact(&mut buffer)?; + + // Convertit les octets en vecteur de u32 + self.map = vec![0; self.num_words]; + for i in 0..self.num_words { + let j = i * std::mem::size_of::(); + let bytes = [ + buffer[j], buffer[j+1], buffer[j+2], buffer[j+3] + ]; + self.map[i] = u32::from_le_bytes(bytes); + } + + Ok(&self.map) + } + + pub fn write_back(&mut self, file_path: &str) -> std::io::Result<()> { + // Encapsule le vecteur dans un std::io::Cursor pour l'utiliser comme un std::io::Write + let mut cursor = Cursor::new(Vec::::new()); + for u32_val in self.map.as_mut_slice() { + cursor.write_all(&u32_val.to_le_bytes())?; + } + + // Ouvre le fichier en mode écriture + let mut file = File::create(file_path)?; + + // Écrit les données dans le fichier + file.write_all(&cursor.into_inner())?; + + Ok(()) + } + +} \ No newline at end of file diff --git a/src/utility/mod.rs b/src/utility/mod.rs index 651aed7..6db23f2 100644 --- a/src/utility/mod.rs +++ b/src/utility/mod.rs @@ -1 +1,2 @@ -pub mod list; \ No newline at end of file +pub mod list; +pub mod bitmap; \ No newline at end of file From 99a69c7998ef8b4c8e54fcc63dcf792c2a14eda4 Mon Sep 17 00:00:00 2001 From: AmauryBrodu <60550980+AmauryBrodu@users.noreply.github.com> Date: Wed, 29 Mar 2023 13:20:44 +0200 Subject: [PATCH 2/7] filehdr nearly done --- src/filesys/filehdr.rs | 154 +++++++++++++++++++++++++++++++++++++++++ src/filesys/mod.rs | 6 ++ 2 files changed, 160 insertions(+) create mode 100644 src/filesys/mod.rs diff --git a/src/filesys/filehdr.rs b/src/filesys/filehdr.rs index e69de29..e6dcad9 100644 --- a/src/filesys/filehdr.rs +++ b/src/filesys/filehdr.rs @@ -0,0 +1,154 @@ +use crate::simulator::disk; +use crate::{simulator::disk::SECTOR_SIZE, utility::bitmap::BitMap}; +pub const MAX_HEADER_SECTORS: i32 = 32; +pub const DATAS_IN_FIRST_SECTOR: i32 = (SECTOR_SIZE - 5 * 8) /8; +pub const DATAS_IN_SECTOR: i32 = (SECTOR_SIZE - 1 * 8) /8; +pub const MAX_DATA_SECTORS: i32 = ((MAX_HEADER_SECTORS-1) * DATAS_IN_SECTOR + DATAS_IN_FIRST_SECTOR); +pub const MAX_FILE_LENGTH: i32 = ((MAX_DATA_SECTORS) * SECTOR_SIZE); + +use crate::DrvDisk; +use crate::Disk; + +pub struct FileHdr { + is_dir: i32, + num_bytes: i32, + num_sectors: i32, + data_sectors: Vec, + num_header_sectors: i32, + header_sectors: [i32; MAX_HEADER_SECTORS as usize], +} + +impl FileHdr { + + pub fn allocate(&mut self, mut free_map: BitMap, file_size: i32) -> bool { + self.num_bytes = file_size; + if file_size > MAX_FILE_LENGTH { + panic!("file size is too long"); + } + + self.num_sectors = (file_size + SECTOR_SIZE -1) / SECTOR_SIZE; + self.num_header_sectors = ((self.num_sectors - DATAS_IN_FIRST_SECTOR)+DATAS_IN_SECTOR-1) / DATAS_IN_SECTOR; + + // Check if there is enough free sectors for both of them + if free_map.num_clear() < self.num_sectors + self.num_header_sectors { + return false; + } + + for i in 0..self.num_header_sectors { + self.header_sectors[i as usize] = free_map.find(); + } + + self.data_sectors = vec![0; MAX_DATA_SECTORS as usize]; + for i in 0..self.num_sectors { + self.data_sectors[i as usize] = free_map.find(); + } + + return true; + } + + pub fn re_allocate(&mut self, mut free_map: BitMap, old_file_size: i32, new_file_size: i32) -> bool { + let mut new_num_sectors = ((new_file_size + SECTOR_SIZE -1) / SECTOR_SIZE) - self.num_sectors; + self.num_bytes = new_file_size; + + let mut new_num_header_sectors = (((self.num_sectors - DATAS_IN_FIRST_SECTOR)+DATAS_IN_SECTOR-1) / DATAS_IN_SECTOR) - self.num_header_sectors; + + assert!(new_file_size <= MAX_FILE_LENGTH); + + if free_map.num_clear() < new_num_sectors + new_num_header_sectors { + return false; + } + + for i in 0..new_num_header_sectors { + self.header_sectors[(i + self.num_header_sectors)as usize] = free_map.find(); + } + + for i in 0..new_num_sectors { + self.data_sectors[(i + self.num_sectors) as usize] = free_map.find(); + } + + return true; + + + + } + + pub fn deallocate(&mut self, mut free_map: BitMap) { + for i in 0..self.num_sectors { + assert!(free_map.test(self.data_sectors[i as usize] as usize)); + free_map.clear(self.data_sectors[i as usize]as usize); + } + + for i in 0..self.num_header_sectors { + assert!(free_map.test(self.header_sectors[i as usize] as usize)); + free_map.clear(self.header_sectors[i as usize]as usize); + } + } + + //TODO: fetchFrom WriteBack + + + pub fn byte_to_sector(&self,offset: i32) -> i32 { + return self.data_sectors[ (offset / SECTOR_SIZE) as usize]; + } + + pub fn file_length(&self) -> i32 { + return self.num_bytes; + } + + pub fn change_file_length(&mut self, new_size: i32) { + self.num_bytes = new_size; + assert!(new_size <= MAX_FILE_LENGTH); + + } + + pub fn max_file_length(&self) -> i32 { + return self.num_sectors * SECTOR_SIZE; + } + + pub fn print(&self) { + let mut data = Vec::new(); + println!("FileHeader contents. File size: {}. File blocks:", self.num_bytes); + for i in 0..self.num_sectors { + print!("{} ", self.data_sectors[i as usize]); + } + println!("\nFile contents:"); + let mut k = 0; + for i in 0..self.num_sectors { + let disk = Disk::init_disk(); + let mut drv_disk = DrvDisk::init_drv_disk(disk); + drv_disk.read_sector(self.data_sectors[i as usize], &mut data); + for j in 0..SECTOR_SIZE.min(self.num_bytes - k) { + let c = data[j as usize]; + if c >= 0x20 && c <= 0x7E { + print!("{}", c as char); + } else { + print!("\\{:x}", c); + } + k += 1; + } + println!(""); + } + } + + pub fn is_dir(&self) -> bool{ + return self.is_dir == 1; + } + + pub fn set_file(&mut self) { + self.is_dir = 0; + } + + pub fn set_dir(&mut self) { + self.is_dir = 1; + } + +} + +#[cfg(test)] +mod test { + + #[test] + fn test_allocate() { + + } +} \ No newline at end of file diff --git a/src/filesys/mod.rs b/src/filesys/mod.rs new file mode 100644 index 0000000..14f5368 --- /dev/null +++ b/src/filesys/mod.rs @@ -0,0 +1,6 @@ +pub mod directory; +pub mod filehdr; +pub mod filesys; +pub mod fsmisc; +pub mod oftable; +pub mod openfile; \ No newline at end of file From 1c0f0765c070319e628b26ef582643f3adab3ab0 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Wed, 29 Mar 2023 13:24:12 +0200 Subject: [PATCH 3/7] init_filesys done --- src/filesys/filesys.rs | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) 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 From 1da8b8465ff033beb4ed66f8b14a571f9dbaea91 Mon Sep 17 00:00:00 2001 From: AmauryBrodu <60550980+AmauryBrodu@users.noreply.github.com> Date: Wed, 29 Mar 2023 15:02:04 +0200 Subject: [PATCH 4/7] commented the bitmap + init_bitmap + init_file_hdr --- src/filesys/filehdr.rs | 18 ++++++++++++++++ src/utility/bitmap.rs | 49 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/filesys/filehdr.rs b/src/filesys/filehdr.rs index e6dcad9..920ae1a 100644 --- a/src/filesys/filehdr.rs +++ b/src/filesys/filehdr.rs @@ -20,6 +20,19 @@ pub struct FileHdr { impl FileHdr { + + pub fn init_file_hdr() -> FileHdr { + FileHdr { + is_dir: 0, + num_bytes: 0, + num_sectors: 0, + data_sectors: Vec::new(), + num_header_sectors: 0, + header_sectors: [0;MAX_HEADER_SECTORS as usize], + } + } + + pub fn allocate(&mut self, mut free_map: BitMap, file_size: i32) -> bool { self.num_bytes = file_size; if file_size > MAX_FILE_LENGTH { @@ -87,6 +100,11 @@ impl FileHdr { //TODO: fetchFrom WriteBack + + + + + pub fn byte_to_sector(&self,offset: i32) -> i32 { return self.data_sectors[ (offset / SECTOR_SIZE) as usize]; } diff --git a/src/utility/bitmap.rs b/src/utility/bitmap.rs index a34e5b4..d66dd2e 100644 --- a/src/utility/bitmap.rs +++ b/src/utility/bitmap.rs @@ -4,6 +4,8 @@ pub const BITS_IN_WORD: usize = 32; use std::fs::File; use std::io::{Cursor, Write, Read}; +use crate::simulator::disk::SECTOR_SIZE; + pub struct BitMap { num_bits: usize, num_words: usize, @@ -12,16 +14,44 @@ pub struct BitMap { impl BitMap { + /// Initialize a bitmap with "nitems" bits, so that every bit is clear. + /// it can be added somewhere on a list. + /// + /// ### Parameters + /// - **nitems** is the number of bits in the bitmap. + ///---------------------------------------------------------------------- + pub fn init_bitmap(&self, n_items: usize) -> BitMap { + let mut tmp: Vec; + BitMap{ + num_bits: n_items, + num_words: (n_items + SECTOR_SIZE as usize -1) / SECTOR_SIZE as usize, + map: tmp, + }; + + *self + } + + /// Set the "nth" bit in a bitmap. + /// ### Parameters + /// - **which** is the number of the bit to be set. pub fn mark(&mut self, which: usize) { assert!(which >= 0 && which < self.num_bits); self.map[which / BITS_IN_WORD] |= 1 << (which % BITS_IN_WORD); } + /// return true if the "nth" bit is set. + /// + /// ### Paramenters + /// - **which** is the number of the bit to be tested. pub fn test(&self, which: usize) -> bool { assert!(which < self.num_bits); (self.map[which / BITS_IN_WORD] & (1 << (which % BITS_IN_WORD))) != 0 } + /// Return the number of the first bit which is clear. + /// As a side effect, set the bit (mark it as in use). + /// (In other words, find and allocate a bit.) + /// If no bits are clear, return ERROR pub fn find(&mut self) -> i32 { for i in 0..self.num_bits { if !self.test(i) { @@ -32,6 +62,9 @@ impl BitMap { -1 } + /// Clear the "nth" bit in a bitmap. + /// ### Parameters + /// - **which** is the number of the bit to be cleared. pub fn clear(&mut self, which: usize) { assert!(which < self.num_bits, "index out of range"); @@ -40,6 +73,8 @@ impl BitMap { self.map[word_idx] &= !(1 << bit_idx); } + /// Return the number of clear bits in the bitmap. + /// (In other words, how many bits are unallocated?) pub fn num_clear(&self) -> i32 { let mut count = 0; for i in 0..self.num_bits { @@ -50,6 +85,9 @@ impl BitMap { count } + /// Print the contents of the bitmap, for debugging. + /// Could be done in a number of ways, but we just print the #'s of + /// all the bits that are set in the bitmap. pub fn print(&self) { println!("Bitmap set:"); for i in 0..self.num_bits { @@ -60,8 +98,10 @@ impl BitMap { println!(); } - - + ///Initialize the contents of a bitmap from a Nachos file. + /// + /// ### Parameters + /// - **file** is the place to read the bitmap from pub fn fetch_from(&mut self, file_path: &str) -> std::io::Result<&Vec> { // Ouvre le fichier en mode lecture seule let mut file = File::open(file_path)?; @@ -83,6 +123,10 @@ impl BitMap { Ok(&self.map) } + /// Store the contents of a bitmap to a Nachos file. + /// + /// ### Paramenters + /// - **file** is the place to write the bitmap to pub fn write_back(&mut self, file_path: &str) -> std::io::Result<()> { // Encapsule le vecteur dans un std::io::Cursor pour l'utiliser comme un std::io::Write let mut cursor = Cursor::new(Vec::::new()); @@ -98,5 +142,4 @@ impl BitMap { Ok(()) } - } \ No newline at end of file From 60410efd1abd0815593871bc2fcea85414879055 Mon Sep 17 00:00:00 2001 From: AmauryBrodu <60550980+AmauryBrodu@users.noreply.github.com> Date: Wed, 29 Mar 2023 15:05:07 +0200 Subject: [PATCH 5/7] bug fix of bitmap constructor --- src/utility/bitmap.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/utility/bitmap.rs b/src/utility/bitmap.rs index d66dd2e..4af76a1 100644 --- a/src/utility/bitmap.rs +++ b/src/utility/bitmap.rs @@ -20,15 +20,13 @@ impl BitMap { /// ### Parameters /// - **nitems** is the number of bits in the bitmap. ///---------------------------------------------------------------------- - pub fn init_bitmap(&self, n_items: usize) -> BitMap { + pub fn init_bitmap(n_items: usize) -> BitMap { let mut tmp: Vec; BitMap{ num_bits: n_items, num_words: (n_items + SECTOR_SIZE as usize -1) / SECTOR_SIZE as usize, map: tmp, - }; - - *self + } } /// Set the "nth" bit in a bitmap. From 58e866cb2f34fd8c6ca70d26d46127ca07cf35d9 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Wed, 29 Mar 2023 15:10:11 +0200 Subject: [PATCH 6/7] filesys : open & create --- src/filesys/filesys.rs | 114 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 101 insertions(+), 13 deletions(-) diff --git a/src/filesys/filesys.rs b/src/filesys/filesys.rs index 866cc74..350ac13 100644 --- a/src/filesys/filesys.rs +++ b/src/filesys/filesys.rs @@ -1,11 +1,11 @@ -use crate::simulator::disk; - +use crate::{simulator::disk, utility::bitmap, kernel::mgerror::ErrorCode}; +const ERROR : i32 = -1; 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 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 } @@ -56,17 +56,17 @@ impl Filesys { } } - /// create a file in the Nachos file system (similar to UNIX create). + /// Create a file in the BurritOS 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 + /// 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 @@ -81,9 +81,97 @@ impl Filesys { /// /// -**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 { + pub fn create(&mut self, name : String, initial_size : i32) -> Result<(), ErrorCode> { + + lock.acquire(); + let dir_sector = find_dir(name); + if dir_sector == ERROR { + lock.release(); + return Err(ErrorCode::InexistFileError); + } + + let dir_file = openfile::init_open_file(dir_sector); + let directory = directory::init_directory(num_dir_entries); + directory.fetch_from(&dir_file); + + if directory.find(name) != ERROR { + lock.release(); + return Err(ErrorCode::AlreadyInDirectory); + } + + // Get the freemap from the disk + let free_map = bitmap::init_bitmap(disk::NUM_SECTORS); + free_map.fetch_from(self.free_map_file); + + // Find a sector to hold the file header + let sector = free_map.find(); + if sector == ERROR { + lock.release(); + return Err(ErrorCode::OutOfDisk); + } + + // Add the file in the directory + let add_result = directory.add(name, sector); + match add_result { + Err(e) => { + lock.release(); + return Err(e); + } + _ => {} + } + + // Indicate that this is a file, not a directory + let hdr = file_header::init_file_header(); + hdr.set_file(); + + if !hdr.allocate(&free_map, initial_size) { + lock.release(); + return Err(ErrorCode::OutOfDisk); + } + + // everthing worked, flush all changes back to disk + hdr.write_back(sector); + directory.write_back(&dir_file); + free_map.write_back(self.free_map_file); + + lock.release(); + + Ok(()) } + + /// Open a file for reading and writing. + /// To open a file: + /// Find the location of the file's header, using the directory + /// Bring the header into memory + /// + /// ### parameters + /// + /// -**name** the text name of the file to be opened (NOT MODIFIED) + pub fn open(name : String) -> Option { + let open_file : Open_file; + // Find the directory containing the file + let dir_sector = find_dir(name); + if dir_sector == ERROR { + return None; + } + + // Read the directory from disk + let dir_file = open_file::init_open_file(dir_sector); + let directory = directory::init_directory(num_dir_entries); + directory.fetch_from(&directory); + + // Find the file in the directory + let sector = directory.find(name); + if sector >= 0 { + open_file = openfile::init_open_file(sector); + open_file.set_name(name); + if open_file.is_dir() { + return None; + } + } + + Some(open_file) + } + } \ No newline at end of file From 4633c512bd6455f320c60bcfa7a0ecf2a19fd526 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Wed, 29 Mar 2023 16:53:08 +0200 Subject: [PATCH 7/7] added num_dir_entries & directory_file_size in filesys.rs --- src/filesys/filesys.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/filesys/filesys.rs b/src/filesys/filesys.rs index 3d31522..75a5c96 100644 --- a/src/filesys/filesys.rs +++ b/src/filesys/filesys.rs @@ -2,9 +2,11 @@ use crate::{simulator::disk, utility::bitmap::BitMap, kernel::mgerror::ErrorCode use super::filehdr::FileHdr; -const ERROR : i32 = -1; -const FREE_MAP_SECTOR : i32 = 0; -const DIRECTORY_SECTOR : i32 = 1; +pub const ERROR : i32 = -1; +pub const FREE_MAP_SECTOR : i32 = 0; +pub const DIRECTORY_SECTOR : i32 = 1; +pub const NUM_DIR_ENTRIES : i32 = 10; +pub const DIRECTORY_FILE_SIZE : i32 = 100; //std::mem::size_of() * NUM_DIR_ENTRIES; pub struct Filesys { pub free_map_file : OpenFile, //Bit map of free disk blocks, represented as a file @@ -27,7 +29,7 @@ impl Filesys { pub fn init_filesys(format : bool) -> Filesys { if format { let free_map = BitMap::init_bitmap(disk::NUM_SECTORS as usize); - let directory = Directory::init_directory(num_dir_entries); + let directory = Directory::init_directory(NUM_DIR_ENTRIES); free_map.mark(FREE_MAP_SECTOR as usize); free_map.mark(DIRECTORY_SECTOR as usize); @@ -36,7 +38,7 @@ impl Filesys { let dir_header : FileHdr; map_header.allocate(free_map, FREE_MAP_SECTOR); - dir_header.allocate(free_map, directory_file_size); + dir_header.allocate(free_map, DIRECTORY_FILE_SIZE); dir_header.set_dir(); @@ -97,7 +99,7 @@ impl Filesys { } let dir_file = OpenFile::init_open_file(dir_sector); - let directory = Directory::init_directory(num_dir_entries); + let directory = Directory::init_directory(NUM_DIR_ENTRIES); directory.fetch_from(&dir_file); if directory.find(name) != ERROR { @@ -163,7 +165,7 @@ impl Filesys { // Read the directory from disk let dir_file = OpenFile::init_open_file(dir_sector); - let directory = Directory::init_directory(num_dir_entries); + let directory = Directory::init_directory(NUM_DIR_ENTRIES); directory.fetch_from(&directory); // Find the file in the directory @@ -201,7 +203,7 @@ impl Filesys { // Fetch the directory from the disk let dir_file = OpenFile::init_open_file(dir_sector); - let directory = Directory::init_directory(num_dir_entries); + let directory = Directory::init_directory(NUM_DIR_ENTRIES); directory.fetch_from(&dir_file); // Look for the file in the directory @@ -283,7 +285,7 @@ impl Filesys { // Fetch it from disk let parent_dir_file = OpenFile::init_open_file(parent_sector); - let parent_dir = Directory::init_directory(num_dir_entries); + let parent_dir = Directory::init_directory(NUM_DIR_ENTRIES); parent_dir.fetch_from(parent_dir_file); // Check that the directory does not exit yet @@ -303,7 +305,7 @@ impl Filesys { // Allocate free sectors for the directory contents let hdr : FileHdr; - if !hdr.allocate(free_map, directory_file_size) { + if !hdr.allocate(free_map, DIRECTORY_FILE_SIZE) { return Err(ErrorCode::OutOfDisk); } @@ -322,7 +324,7 @@ impl Filesys { // New directory (initially empty) let new_dir_file = OpenFile::init_open_file(hdr_sector as usize); - let new_dir = Directory::init_directory(num_dir_entries); + let new_dir = Directory::init_directory(NUM_DIR_ENTRIES); new_dir.write_back(new_dir_file); // Parent directory @@ -354,7 +356,7 @@ impl Filesys { // Fetch it from disk let parent_dir_file = OpenFile::init_open_file(parent_sector); - let parent_dir = Directory::init_directory(num_dir_entries); + let parent_dir = Directory::init_directory(NUM_DIR_ENTRIES); parent_dir.fetch_from(parent_dir_file); // Check that the directory to be removed exist @@ -374,7 +376,7 @@ impl Filesys { // Fetch its contents from the disk let the_dir_file = OpenFile::init_open_file(the_dir_sector); - let the_dir = Directory::init_directory(num_dir_entries); + let the_dir = Directory::init_directory(NUM_DIR_ENTRIES); the_dir.fetch_from(the_dir_file); // Check that is is empty