From 030a5812f73da0dde98094825b2fb1967bc4a960 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sun, 7 Aug 2022 20:39:42 +0300 Subject: [PATCH] make stuff use the new VFS interface --- ableos/src/filesystem/ext2.rs | 15 ++-- ableos/src/filesystem/mod.rs | 6 +- ableos/src/filesystem/vfs.rs | 18 +++-- ableos/src/scratchpad.rs | 129 +++++++++++++----------------- ableos/src/wasm_jumploader/mod.rs | 7 +- 5 files changed, 85 insertions(+), 90 deletions(-) diff --git a/ableos/src/filesystem/ext2.rs b/ableos/src/filesystem/ext2.rs index 82f93bd4..76dc14f2 100644 --- a/ableos/src/filesystem/ext2.rs +++ b/ableos/src/filesystem/ext2.rs @@ -43,7 +43,7 @@ where Weak::new(), )); - let mut vfs = VFS.write(); + let mut vfs = VFS.lock(); let root_handle = vfs.add_fs_node(root.clone()); Ok(Self { @@ -94,13 +94,13 @@ where .fs .inode_nth(entry.inode) .ok_or_else(|| FsError::InodeNotFound)?; - let mut vfs = VFS.write(); + let mut vfs = VFS.lock(); let entry_node_handle = vfs .find_fs_node(entry.inode, self.device_handle) .unwrap_or_else(|| { vfs.add_fs_node(Arc::new(FsNode::new( ext2_type_to_fs_flags(entry_inode.type_perm()), - inode.size(), + entry_inode.size(), entry.inode, self.device_handle, Weak::new(), @@ -126,13 +126,17 @@ where } let entry = entry.unwrap(); + let entry_inode = self + .fs + .inode_nth(entry.inode as usize) + .ok_or_else(|| FsError::InodeNotFound)?; if entry.file_name_string() == name { found_node = Ok(vfs .find_fs_node(entry.inode, self.device_handle) .unwrap_or_else(|| { vfs.add_fs_node(Arc::new(FsNode::new( - ext2_type_to_fs_flags(inode.type_perm()), - inode.size(), + ext2_type_to_fs_flags(entry_inode.type_perm()), + entry_inode.size(), entry.inode, self.device_handle, Weak::new(), @@ -155,6 +159,7 @@ where } fn ext2_type_to_fs_flags(type_perm: TypePerm) -> FsFlags { + trace!("{type_perm:?}"); let is_directory = type_perm & TypePerm::DIRECTORY == TypePerm::DIRECTORY; let is_symlink = type_perm & TypePerm::SYMLINK == TypePerm::SYMLINK; let t = if is_directory { diff --git a/ableos/src/filesystem/mod.rs b/ableos/src/filesystem/mod.rs index bbd076a3..c7350646 100644 --- a/ableos/src/filesystem/mod.rs +++ b/ableos/src/filesystem/mod.rs @@ -40,7 +40,7 @@ where pub fn init() -> Result<()> { let mut state = KERNEL_STATE.lock(); let fs = load_fs()?; - let mut vfs = VFS.write(); + let mut vfs = VFS.lock(); vfs.set_root(fs.root())?; state.add_storage_device(fs); Ok(()) @@ -55,7 +55,7 @@ fn load_fs() -> Result>> { pub fn tree(path: &str) -> Result<()> { let dir = { - let mut vfs = VFS.write(); + let mut vfs = VFS.lock(); let handle = vfs.resolve(path)?; vfs.fs_node(handle).ok_or_else(|| FsError::NotFound)? }; @@ -76,7 +76,7 @@ fn tree_inner>(dir: Arc, path: S) { if let Some(dir) = dir.directory() { for entry in dir { let fs_node = { - let vfs = VFS.read(); + let vfs = VFS.lock(); vfs.fs_node(entry.node()).unwrap() }; diff --git a/ableos/src/filesystem/vfs.rs b/ableos/src/filesystem/vfs.rs index a7e22032..5a8fdeb7 100644 --- a/ableos/src/filesystem/vfs.rs +++ b/ableos/src/filesystem/vfs.rs @@ -9,7 +9,7 @@ use core::cmp; use alloc::sync::{Arc, Weak}; use hashbrown::HashMap; use lazy_static::lazy_static; -use spin::RwLock; +use spin::Mutex; use super::{errors::FsError, FsResult as Result}; use crate::{ @@ -21,7 +21,7 @@ use crate::{ const SYMLINK_RECURSION_LIMIT: u8 = 8; lazy_static! { - pub static ref VFS: RwLock = Default::default(); + pub static ref VFS: Mutex = Default::default(); } pub struct VirtualFileSystem { @@ -134,7 +134,7 @@ impl Default for VirtualFileSystem { #[derive(Debug)] pub struct FsNode { flags: FsFlags, - length: usize, // in bytes + size: usize, // in bytes inode: usize, // implementation specific identifier for the node device_handle: Handle, // uniquely assigned device handle ptr: Weak, // used by mountpoints and symlinks @@ -145,14 +145,14 @@ pub struct FsNode { impl FsNode { pub fn new( flags: FsFlags, - length: usize, + size: usize, inode: usize, device_handle: Handle, ptr: Weak, ) -> Self { Self { flags, - length, + size, inode, device_handle, ptr, @@ -181,11 +181,11 @@ impl FsNode { Err(FsError::IsDirectory)?; } - if offset > self.length { + if offset > self.size { Err(FsError::EndOfFile)?; } - let readable_size = cmp::min(size, self.length - offset); + let readable_size = cmp::min(size, self.size - offset); device.read(self, offset, readable_size, buffer)?; Ok(readable_size) } @@ -235,6 +235,10 @@ impl FsNode { pub fn inode(&self) -> usize { self.inode } + + pub fn size(&self) -> usize { + self.size + } } impl Drop for FsNode { diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index 288d2b72..2ce80e70 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -8,6 +8,7 @@ use crate::arch::drivers::sysinfo::master; use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2}; use crate::devices::pci::brute_force_scan; use crate::filesystem; +use crate::filesystem::vfs::VFS; use crate::systeminfo::{KERNEL_VERSION, RELEASE_TYPE}; use crate::time::fetch_time; use crate::{ @@ -15,14 +16,11 @@ use crate::{ rhai_shell::KEYBUFF, vterm::VTerm, KERNEL_STATE, - // wasm_jumploader::run_program, + wasm_jumploader::run_program, }; use acpi::{AcpiTables, PlatformInfo}; use cpuio::{inb, outb}; -use ext2::fs::sync::{DirectoryEntry, Synced}; -use ext2::{fs::Ext2, sector::Size1024}; -use genfs::{Fs, OpenOptions}; use kernel::allocator::ALLOCATOR; use spin::Lazy; use x86_64::instructions::interrupts::{disable, enable}; @@ -196,18 +194,16 @@ pub fn real_shell() { } pub fn command_parser(user: String, command: String) { - // let fs = &*FILE_SYSTEM.lock(); let mut iter = command.split_whitespace(); - // TODO: update the open() function to take either a ableOS path or a b"/" type path let current_path = Path::new("/home/able".to_string()); trace!("Current path: {:?}", current_path); - let current_path = b"/home/able/"; + let current_path = "/home/able/"; let bin_name = iter.next().unwrap(); let mut strin = String::new(); - for stri in iter { + for stri in iter.clone() { trace!("{}", stri); strin.push_str(stri); } @@ -225,55 +221,59 @@ pub fn command_parser(user: String, command: String) { // note: able asked for rhaish to stay in the repo but will be removed // in the future so just comment it out for now // "rhai" => { - // drop(fs); // shell(); // } - // "list" | "ls" => { - // for dir_entry in list_files_in_dir(fs, current_path) { - // println!("{}", dir_entry.file_name_string()); - // } - // } + "list" | "ls" => { + let mut vfs = VFS.lock(); + let handle = vfs.resolve(current_path).unwrap(); + let dir = vfs.fs_node(handle).unwrap(); + drop(vfs); + for dir_entry in dir.directory().unwrap() { + println!("{}", dir_entry.name()); + } + } - // "echo" => match conf_args.1.arguments.get("p") { - // Some(path) => echo_file(path.to_string(), fs), - - // None => println!("No path provided"), - // } + "echo" => match conf_args.1.arguments.get("p") { + Some(path) => echo_file(path.to_string()), + None => println!("No path provided"), + } "test" => {} "quit" => shutdown(), "tree" => filesystem::tree("/").unwrap(), _ => { - // let mut options = OpenOptions::new(); - // options.read(true); - // let file = { - // let path = format!("/home/{user}/bins/{bin_name}.wasm"); - // if let Ok(file) = fs.open(&path.as_bytes(), &options) { - // file - // } else { - // let path = format!("/shared/bins/{bin_name}.wasm"); - // if let Ok(file) = fs.open(&path.as_bytes(), &options) { - // file - // } else { - // let path = format!("/system/bins/{bin_name}.wasm"); - // match fs.open(&path.as_bytes(), &options) { - // Ok(file) => file, - // Err(error) => { - // trace!("{:?}", error); - println!("No such binary: {}", bin_name); - error!("No such binary: {}", bin_name); - // } - // } - // } - // } - // }; + let file = { + let mut vfs = VFS.lock(); + let path = format!("/home/{user}/bins/{bin_name}.wasm"); + let handle = if let Ok(file) = vfs.resolve(path) { + file + } else { + let path = format!("/shared/bins/{bin_name}.wasm"); + if let Ok(file) = vfs.resolve(path) { + file + } else { + let path = format!("/system/bins/{bin_name}.wasm"); + match vfs.resolve(path) { + Ok(file) => file, + Err(error) => { + trace!("{:?}", error); + println!("No such binary: {}", bin_name); + error!("No such binary: {}", bin_name); + return; + } + } + } + }; - // let mut binary = vec![]; - // file.read_to_end(&mut binary).unwrap(); + vfs.fs_node(handle).unwrap() + }; - // let args = iter.collect::>(); - // println!("{:?}", args); - // run_program(&binary); + let mut binary = vec![]; + file.read(0, file.size(), &mut binary).unwrap(); + + let args = iter.collect::>(); + println!("{:?}", args); + run_program(&binary); } } } @@ -304,40 +304,21 @@ pub fn sound_off() { reset_pit_for_cpu(); } -pub fn list_files_in_dir( - fs: &Synced>>, - _path: &[u8], -) -> Vec { - let mut entry_list = vec![]; - - let dirr = fs.read_dir(b"/").unwrap(); - for dir_entry in dirr { - entry_list.push(dir_entry.unwrap()); - } - - entry_list -} - -pub static CURRENT_DIR: Lazy> = Lazy::new(|| spin::Mutex::new("/".to_string())); - -pub fn echo_file(path: String, fs: &Synced>>) { - let mut current_dir = CURRENT_DIR.lock(); - - current_dir.push_str(&path); - - let file = fs - .open(current_dir.as_bytes(), OpenOptions::new().read(true)) +pub fn echo_file(path: String) { + let mut current_path = String::from("/"); + current_path.push_str(&path); + let mut vfs = VFS.lock(); + let handle = vfs + .resolve(¤t_path) .unwrap(); + let file = vfs.fs_node(handle).unwrap(); if file.is_dir() { // println!("{} is a directory", path); } else { let mut file_contents = Vec::new(); - - let _ret = file.read_to_end(&mut file_contents).unwrap(); - + file.read(0, file.size(), &mut file_contents).unwrap(); let file_contents_str = String::from_utf8_lossy(&file_contents); - println!("{}", file_contents_str); } } diff --git a/ableos/src/wasm_jumploader/mod.rs b/ableos/src/wasm_jumploader/mod.rs index 06177dca..21e7df91 100644 --- a/ableos/src/wasm_jumploader/mod.rs +++ b/ableos/src/wasm_jumploader/mod.rs @@ -1,7 +1,12 @@ +/* + * Copyright (c) 2022, Umut İnan Erdoğan + * + * SPDX-License-Identifier: MPL-2.0 + */ + pub mod host_functions; use crate::wasm_jumploader::host_functions::HostExternals; -// use genfs::{Fs, OpenOptions}; use wasmi::{ImportsBuilder, ModuleInstance}; pub fn interp() {