added num_dir_entries & directory_file_size in filesys.rs
This commit is contained in:
parent
d620822d97
commit
4633c512bd
@ -2,9 +2,11 @@ use crate::{simulator::disk, utility::bitmap::BitMap, kernel::mgerror::ErrorCode
|
|||||||
|
|
||||||
use super::filehdr::FileHdr;
|
use super::filehdr::FileHdr;
|
||||||
|
|
||||||
const ERROR : i32 = -1;
|
pub const ERROR : i32 = -1;
|
||||||
const FREE_MAP_SECTOR : i32 = 0;
|
pub const FREE_MAP_SECTOR : i32 = 0;
|
||||||
const DIRECTORY_SECTOR : i32 = 1;
|
pub const DIRECTORY_SECTOR : i32 = 1;
|
||||||
|
pub const NUM_DIR_ENTRIES : i32 = 10;
|
||||||
|
pub const DIRECTORY_FILE_SIZE : i32 = 100; //std::mem::size_of<Directory>() * NUM_DIR_ENTRIES;
|
||||||
|
|
||||||
pub struct Filesys {
|
pub struct Filesys {
|
||||||
pub free_map_file : OpenFile, //Bit map of free disk blocks, represented as a file
|
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 {
|
pub fn init_filesys(format : bool) -> Filesys {
|
||||||
if format {
|
if format {
|
||||||
let free_map = BitMap::init_bitmap(disk::NUM_SECTORS as usize);
|
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(FREE_MAP_SECTOR as usize);
|
||||||
free_map.mark(DIRECTORY_SECTOR as usize);
|
free_map.mark(DIRECTORY_SECTOR as usize);
|
||||||
@ -36,7 +38,7 @@ impl Filesys {
|
|||||||
let dir_header : FileHdr;
|
let dir_header : FileHdr;
|
||||||
|
|
||||||
map_header.allocate(free_map, FREE_MAP_SECTOR);
|
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();
|
dir_header.set_dir();
|
||||||
|
|
||||||
@ -97,7 +99,7 @@ impl Filesys {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let dir_file = OpenFile::init_open_file(dir_sector);
|
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);
|
directory.fetch_from(&dir_file);
|
||||||
|
|
||||||
if directory.find(name) != ERROR {
|
if directory.find(name) != ERROR {
|
||||||
@ -163,7 +165,7 @@ impl Filesys {
|
|||||||
|
|
||||||
// Read the directory from disk
|
// Read the directory from disk
|
||||||
let dir_file = OpenFile::init_open_file(dir_sector);
|
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);
|
directory.fetch_from(&directory);
|
||||||
|
|
||||||
// Find the file in the directory
|
// Find the file in the directory
|
||||||
@ -201,7 +203,7 @@ impl Filesys {
|
|||||||
|
|
||||||
// Fetch the directory from the disk
|
// Fetch the directory from the disk
|
||||||
let dir_file = OpenFile::init_open_file(dir_sector);
|
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);
|
directory.fetch_from(&dir_file);
|
||||||
|
|
||||||
// Look for the file in the directory
|
// Look for the file in the directory
|
||||||
@ -283,7 +285,7 @@ impl Filesys {
|
|||||||
|
|
||||||
// Fetch it from disk
|
// Fetch it from disk
|
||||||
let parent_dir_file = OpenFile::init_open_file(parent_sector);
|
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);
|
parent_dir.fetch_from(parent_dir_file);
|
||||||
|
|
||||||
// Check that the directory does not exit yet
|
// Check that the directory does not exit yet
|
||||||
@ -303,7 +305,7 @@ impl Filesys {
|
|||||||
|
|
||||||
// Allocate free sectors for the directory contents
|
// Allocate free sectors for the directory contents
|
||||||
let hdr : FileHdr;
|
let hdr : FileHdr;
|
||||||
if !hdr.allocate(free_map, directory_file_size) {
|
if !hdr.allocate(free_map, DIRECTORY_FILE_SIZE) {
|
||||||
return Err(ErrorCode::OutOfDisk);
|
return Err(ErrorCode::OutOfDisk);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -322,7 +324,7 @@ impl Filesys {
|
|||||||
|
|
||||||
// New directory (initially empty)
|
// New directory (initially empty)
|
||||||
let new_dir_file = OpenFile::init_open_file(hdr_sector as usize);
|
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);
|
new_dir.write_back(new_dir_file);
|
||||||
|
|
||||||
// Parent directory
|
// Parent directory
|
||||||
@ -354,7 +356,7 @@ impl Filesys {
|
|||||||
|
|
||||||
// Fetch it from disk
|
// Fetch it from disk
|
||||||
let parent_dir_file = OpenFile::init_open_file(parent_sector);
|
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);
|
parent_dir.fetch_from(parent_dir_file);
|
||||||
|
|
||||||
// Check that the directory to be removed exist
|
// Check that the directory to be removed exist
|
||||||
@ -374,7 +376,7 @@ impl Filesys {
|
|||||||
|
|
||||||
// Fetch its contents from the disk
|
// Fetch its contents from the disk
|
||||||
let the_dir_file = OpenFile::init_open_file(the_dir_sector);
|
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);
|
the_dir.fetch_from(the_dir_file);
|
||||||
|
|
||||||
// Check that is is empty
|
// Check that is is empty
|
||||||
|
Loading…
x
Reference in New Issue
Block a user