latest version of directory
This commit is contained in:
parent
6ad3b36201
commit
00b4ed0066
@ -1,18 +1,41 @@
|
|||||||
const FILE_NAME_MAX_LEN: i32 = 80;
|
const FILE_NAME_MAX_LEN: i32 = 80;
|
||||||
const NUM_DIR_ENTRIES: i32 = 30;
|
const NUM_DIR_ENTRIES: i32 = 30;
|
||||||
|
|
||||||
|
use crate::drivers::drv_disk::DrvDisk;
|
||||||
use crate::kernel::mgerror::ErrorCode;
|
use crate::kernel::mgerror::ErrorCode;
|
||||||
use crate::kernel::mgerror::ErrorCode::{AlreadyInDirectory, NospaceInDirectory, InexistDirectoryError};
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
|
use super::openfile::OpenFile;
|
||||||
pub struct DirectoryEntry {
|
pub struct DirectoryEntry {
|
||||||
in_use: bool,
|
in_use: bool,
|
||||||
sector: i32,
|
sector: i32,
|
||||||
name: [char; FILE_NAME_MAX_LEN as usize],
|
name: [char; FILE_NAME_MAX_LEN as usize],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DirectoryEntry {
|
||||||
|
fn to_bytes(&self) -> Vec<u8> {
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
bytes.push(self.in_use as u8);
|
||||||
|
bytes.extend_from_slice(&self.sector.to_le_bytes());
|
||||||
|
for ch in &self.name {
|
||||||
|
bytes.extend_from_slice(&ch.encode_utf8(&mut [0; 4]).as_bytes());
|
||||||
|
}
|
||||||
|
bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn entries_to_bytes(entries: &Vec<DirectoryEntry>) -> Vec<u8> {
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
for entry in entries {
|
||||||
|
bytes.extend_from_slice(&entry.to_bytes());
|
||||||
|
}
|
||||||
|
bytes
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Directory {
|
pub struct Directory {
|
||||||
table_size: i32,
|
table_size: i32,
|
||||||
table: Vec<DirectoryEntry>,
|
table: Vec<DirectoryEntry>,
|
||||||
|
drv_disk: DrvDisk,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Directory {
|
impl Directory {
|
||||||
@ -23,7 +46,7 @@ impl Directory {
|
|||||||
///
|
///
|
||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **size** is the number of entries in the directory
|
/// - **size** is the number of entries in the directory
|
||||||
pub fn init_directory(size: i32) -> Directory {
|
pub fn init_directory(size: i32, drv_disk_: DrvDisk) -> Directory {
|
||||||
let tmp:Vec<DirectoryEntry> = Vec::new();
|
let tmp:Vec<DirectoryEntry> = Vec::new();
|
||||||
for i in 0..size {
|
for i in 0..size {
|
||||||
tmp[i as usize].in_use = false;
|
tmp[i as usize].in_use = false;
|
||||||
@ -31,16 +54,17 @@ impl Directory {
|
|||||||
Directory {
|
Directory {
|
||||||
table_size: size,
|
table_size: size,
|
||||||
table: tmp,
|
table: tmp,
|
||||||
|
drv_disk: drv_disk_,
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_from(&self, OpenFile file) {
|
pub fn fetch_from(&self,mut file: OpenFile) {
|
||||||
file.read_at(self.table,self.table_size * mem::size_of::<DirectoryEntry>() as i32);
|
OpenFile::read_at(&mut self.drv_disk, &mut file, &mut entries_to_bytes(&self.table), self.table_size * mem::size_of::<DirectoryEntry>() as i32, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_back(&self, OpenFile file) {
|
pub fn write_back(&self,mut file: OpenFile) {
|
||||||
file.write_at(self.table,self.table_size * mem::size_of::<DirectoryEntry>() as i32);
|
OpenFile::write_at(&mut self.drv_disk,&mut file,&mut entries_to_bytes(&self.table),self.table_size * mem::size_of::<DirectoryEntry>() as i32,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Look up file name in directory.
|
/// Look up file name in directory.
|
||||||
@ -85,19 +109,20 @@ impl Directory {
|
|||||||
/// - **new_sector** the disk sector containing the added file's header
|
/// - **new_sector** the disk sector containing the added file's header
|
||||||
///
|
///
|
||||||
/// return NO_ERROR, ALREADY_IN_DIRECTORY or NOSPACE_IN_DIRECTORY.
|
/// return NO_ERROR, ALREADY_IN_DIRECTORY or NOSPACE_IN_DIRECTORY.
|
||||||
pub fn add(&self, name: char, new_sector: i32) -> ErrorCode {
|
pub fn add(&self, name: char, new_sector: i32) -> Result<(), ErrorCode> {
|
||||||
if self.find_index(name) != -1 {
|
if self.find_index(name) != -1 {
|
||||||
return AlreadyInDirectory;
|
return Err(ErrorCode::AlreadyInDirectory);
|
||||||
}
|
}
|
||||||
for i in 0..self.table_size {
|
for i in 0..self.table_size {
|
||||||
if !self.table[i as usize].in_use {
|
if !self.table[i as usize].in_use {
|
||||||
self.table[i as usize].in_use = true;
|
self.table[i as usize].in_use = true;
|
||||||
self.table[i as usize].name.starts_with(&self.table[i as usize].name);
|
self.table[i as usize].name.starts_with(&self.table[i as usize].name);
|
||||||
self.table[i as usize].sector = new_sector;
|
self.table[i as usize].sector = new_sector;
|
||||||
return NoError;
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NospaceInDirectory;
|
return Err(ErrorCode::NospaceInDirectory);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a file name from the directory.
|
/// Remove a file name from the directory.
|
||||||
@ -106,13 +131,13 @@ impl Directory {
|
|||||||
/// - **name** the file name to be removed
|
/// - **name** the file name to be removed
|
||||||
/// return NO_ERROR, or INEXIST_DIRECTORY_ERROR
|
/// return NO_ERROR, or INEXIST_DIRECTORY_ERROR
|
||||||
|
|
||||||
pub fn remove(&self, name: char) -> ErrorCode{
|
pub fn remove(&self, name: char) -> Result<(), ErrorCode> {
|
||||||
let i = self.find_index(name);
|
let i = self.find_index(name);
|
||||||
if i == -1 {
|
if i == -1 {
|
||||||
return InexistDirectoryError;
|
return Err(ErrorCode::InexistDirectoryError);
|
||||||
}
|
}
|
||||||
self.table[i as usize].in_use = false;
|
self.table[i as usize].in_use = false;
|
||||||
return NoError;
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List all the file names in the directory.(recursive function)
|
/// List all the file names in the directory.(recursive function)
|
||||||
@ -121,7 +146,7 @@ impl Directory {
|
|||||||
/// - **name** the name of the Dir to print
|
/// - **name** the name of the Dir to print
|
||||||
/// - **depth** the depth in the recursion (to print a nice hierarchy with spaces)
|
/// - **depth** the depth in the recursion (to print a nice hierarchy with spaces)
|
||||||
pub fn list(&self, name: char, depth: i32) {
|
pub fn list(&self, name: char, depth: i32) {
|
||||||
let dir = Directory::init_directory(NUM_DIR_ENTRIES);
|
let dir = Directory::init_directory(NUM_DIR_ENTRIES, self.drv_disk);
|
||||||
|
|
||||||
for i in 0..self.table_size {
|
for i in 0..self.table_size {
|
||||||
if self.table[i as usize].in_use {
|
if self.table[i as usize].in_use {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user