1
0
mirror of https://github.com/sgmarz/osblog.git synced 2024-11-24 02:16:19 +04:00

Use global cache. fs::init will now check to see if the global cache is good or not. If it is already good, do nothing, otherwise cache the inodes.

This commit is contained in:
Stephen Marz 2020-05-15 12:29:59 -04:00
parent 8e414c62be
commit 3dd49c615e
2 changed files with 32 additions and 16 deletions

View File

@ -68,9 +68,17 @@ pub struct DirEntry {
}
/// The MinixFileSystem implements the FileSystem trait for the VFS.
pub struct MinixFileSystem {
inode_cache: BTreeMap<String, Inode>
}
pub struct MinixFileSystem;
static mut MFS_INODE_CACHE: [Option<BTreeMap<String, Inode>>; 8] = [
None,
None,
None,
None,
None,
None,
None,
None
];
impl MinixFileSystem {
/// Inodes are the meta-data of a file, including the mode (permissions and type) and
@ -165,24 +173,32 @@ impl MinixFileSystem {
}
}
// Run this ONLY in a process!
pub fn init(bdev: usize) -> Self {
let mut btm = BTreeMap::new();
let cwd = String::from("/");
pub fn init(bdev: usize) {
if unsafe {MFS_INODE_CACHE[bdev-1].is_none() } {
let mut btm = BTreeMap::new();
let cwd = String::from("/");
// Let's look at the root (inode #1)
Self::cache_at(&mut btm, &cwd, 1, bdev);
Self {
inode_cache: btm
// Let's look at the root (inode #1)
Self::cache_at(&mut btm, &cwd, 1, bdev);
unsafe {
MFS_INODE_CACHE[bdev-1] = Some(btm);
}
}
}
/// The goal of open is to traverse the path given by path. If we cache the inodes
/// in RAM, it might make this much quicker. For now, this doesn't do anything since
/// we're just testing read based on if we know the Inode we're looking for.
pub fn open(&self, path: &String) -> Result<Inode, FsError> {
if let Some(inode) = self.inode_cache.get(path) {
Ok(*inode)
pub fn open(bdev: usize, path: &String) -> Result<Inode, FsError> {
if let Some(cache) = unsafe { MFS_INODE_CACHE[bdev-1].take() } {
let ret;
if let Some(inode) = cache.get(path) {
ret = Ok(*inode);
}
else {
ret = Err(FsError::FileNotFound);
}
ret
}
else {
Err(FsError::FileNotFound)

View File

@ -12,9 +12,9 @@ use alloc::string::String;
pub fn test_elf() {
// This won't be necessary after we connect this to the VFS, but for now, we need it.
const BDEV: usize = 8;
let mfs = MinixFileSystem::init(BDEV);
MinixFileSystem::init(BDEV);
let file_to_read = String::from("/helloworld.elf");
let desc = mfs.open(&file_to_read).ok();
let desc = MinixFileSystem::open(BDEV, &file_to_read).ok();
if desc.is_none() {
println!("Error reading {}", file_to_read);
return;