make stuff use the new VFS interface

This commit is contained in:
TheOddGarlic 2022-08-07 20:39:42 +03:00
parent 8e487e1697
commit 030a5812f7
5 changed files with 85 additions and 90 deletions

View file

@ -43,7 +43,7 @@ where
Weak::new(), Weak::new(),
)); ));
let mut vfs = VFS.write(); let mut vfs = VFS.lock();
let root_handle = vfs.add_fs_node(root.clone()); let root_handle = vfs.add_fs_node(root.clone());
Ok(Self { Ok(Self {
@ -94,13 +94,13 @@ where
.fs .fs
.inode_nth(entry.inode) .inode_nth(entry.inode)
.ok_or_else(|| FsError::InodeNotFound)?; .ok_or_else(|| FsError::InodeNotFound)?;
let mut vfs = VFS.write(); let mut vfs = VFS.lock();
let entry_node_handle = vfs let entry_node_handle = vfs
.find_fs_node(entry.inode, self.device_handle) .find_fs_node(entry.inode, self.device_handle)
.unwrap_or_else(|| { .unwrap_or_else(|| {
vfs.add_fs_node(Arc::new(FsNode::new( vfs.add_fs_node(Arc::new(FsNode::new(
ext2_type_to_fs_flags(entry_inode.type_perm()), ext2_type_to_fs_flags(entry_inode.type_perm()),
inode.size(), entry_inode.size(),
entry.inode, entry.inode,
self.device_handle, self.device_handle,
Weak::new(), Weak::new(),
@ -126,13 +126,17 @@ where
} }
let entry = entry.unwrap(); 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 { if entry.file_name_string() == name {
found_node = Ok(vfs found_node = Ok(vfs
.find_fs_node(entry.inode, self.device_handle) .find_fs_node(entry.inode, self.device_handle)
.unwrap_or_else(|| { .unwrap_or_else(|| {
vfs.add_fs_node(Arc::new(FsNode::new( vfs.add_fs_node(Arc::new(FsNode::new(
ext2_type_to_fs_flags(inode.type_perm()), ext2_type_to_fs_flags(entry_inode.type_perm()),
inode.size(), entry_inode.size(),
entry.inode, entry.inode,
self.device_handle, self.device_handle,
Weak::new(), Weak::new(),
@ -155,6 +159,7 @@ where
} }
fn ext2_type_to_fs_flags(type_perm: TypePerm) -> FsFlags { fn ext2_type_to_fs_flags(type_perm: TypePerm) -> FsFlags {
trace!("{type_perm:?}");
let is_directory = type_perm & TypePerm::DIRECTORY == TypePerm::DIRECTORY; let is_directory = type_perm & TypePerm::DIRECTORY == TypePerm::DIRECTORY;
let is_symlink = type_perm & TypePerm::SYMLINK == TypePerm::SYMLINK; let is_symlink = type_perm & TypePerm::SYMLINK == TypePerm::SYMLINK;
let t = if is_directory { let t = if is_directory {

View file

@ -40,7 +40,7 @@ where
pub fn init() -> Result<()> { pub fn init() -> Result<()> {
let mut state = KERNEL_STATE.lock(); let mut state = KERNEL_STATE.lock();
let fs = load_fs()?; let fs = load_fs()?;
let mut vfs = VFS.write(); let mut vfs = VFS.lock();
vfs.set_root(fs.root())?; vfs.set_root(fs.root())?;
state.add_storage_device(fs); state.add_storage_device(fs);
Ok(()) Ok(())
@ -55,7 +55,7 @@ fn load_fs() -> Result<Ext2StorageDevice<Size1024, Vec<u8>>> {
pub fn tree(path: &str) -> Result<()> { pub fn tree(path: &str) -> Result<()> {
let dir = { let dir = {
let mut vfs = VFS.write(); let mut vfs = VFS.lock();
let handle = vfs.resolve(path)?; let handle = vfs.resolve(path)?;
vfs.fs_node(handle).ok_or_else(|| FsError::NotFound)? vfs.fs_node(handle).ok_or_else(|| FsError::NotFound)?
}; };
@ -76,7 +76,7 @@ fn tree_inner<S: Into<String>>(dir: Arc<FsNode>, path: S) {
if let Some(dir) = dir.directory() { if let Some(dir) = dir.directory() {
for entry in dir { for entry in dir {
let fs_node = { let fs_node = {
let vfs = VFS.read(); let vfs = VFS.lock();
vfs.fs_node(entry.node()).unwrap() vfs.fs_node(entry.node()).unwrap()
}; };

View file

@ -9,7 +9,7 @@ use core::cmp;
use alloc::sync::{Arc, Weak}; use alloc::sync::{Arc, Weak};
use hashbrown::HashMap; use hashbrown::HashMap;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use spin::RwLock; use spin::Mutex;
use super::{errors::FsError, FsResult as Result}; use super::{errors::FsError, FsResult as Result};
use crate::{ use crate::{
@ -21,7 +21,7 @@ use crate::{
const SYMLINK_RECURSION_LIMIT: u8 = 8; const SYMLINK_RECURSION_LIMIT: u8 = 8;
lazy_static! { lazy_static! {
pub static ref VFS: RwLock<VirtualFileSystem> = Default::default(); pub static ref VFS: Mutex<VirtualFileSystem> = Default::default();
} }
pub struct VirtualFileSystem { pub struct VirtualFileSystem {
@ -134,7 +134,7 @@ impl Default for VirtualFileSystem {
#[derive(Debug)] #[derive(Debug)]
pub struct FsNode { pub struct FsNode {
flags: FsFlags, flags: FsFlags,
length: usize, // in bytes size: usize, // in bytes
inode: usize, // implementation specific identifier for the node inode: usize, // implementation specific identifier for the node
device_handle: Handle, // uniquely assigned device handle device_handle: Handle, // uniquely assigned device handle
ptr: Weak<FsNode>, // used by mountpoints and symlinks ptr: Weak<FsNode>, // used by mountpoints and symlinks
@ -145,14 +145,14 @@ pub struct FsNode {
impl FsNode { impl FsNode {
pub fn new( pub fn new(
flags: FsFlags, flags: FsFlags,
length: usize, size: usize,
inode: usize, inode: usize,
device_handle: Handle, device_handle: Handle,
ptr: Weak<FsNode>, ptr: Weak<FsNode>,
) -> Self { ) -> Self {
Self { Self {
flags, flags,
length, size,
inode, inode,
device_handle, device_handle,
ptr, ptr,
@ -181,11 +181,11 @@ impl FsNode {
Err(FsError::IsDirectory)?; Err(FsError::IsDirectory)?;
} }
if offset > self.length { if offset > self.size {
Err(FsError::EndOfFile)?; 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)?; device.read(self, offset, readable_size, buffer)?;
Ok(readable_size) Ok(readable_size)
} }
@ -235,6 +235,10 @@ impl FsNode {
pub fn inode(&self) -> usize { pub fn inode(&self) -> usize {
self.inode self.inode
} }
pub fn size(&self) -> usize {
self.size
}
} }
impl Drop for FsNode { impl Drop for FsNode {

View file

@ -8,6 +8,7 @@ use crate::arch::drivers::sysinfo::master;
use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2}; use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2};
use crate::devices::pci::brute_force_scan; use crate::devices::pci::brute_force_scan;
use crate::filesystem; use crate::filesystem;
use crate::filesystem::vfs::VFS;
use crate::systeminfo::{KERNEL_VERSION, RELEASE_TYPE}; use crate::systeminfo::{KERNEL_VERSION, RELEASE_TYPE};
use crate::time::fetch_time; use crate::time::fetch_time;
use crate::{ use crate::{
@ -15,14 +16,11 @@ use crate::{
rhai_shell::KEYBUFF, rhai_shell::KEYBUFF,
vterm::VTerm, vterm::VTerm,
KERNEL_STATE, KERNEL_STATE,
// wasm_jumploader::run_program, wasm_jumploader::run_program,
}; };
use acpi::{AcpiTables, PlatformInfo}; use acpi::{AcpiTables, PlatformInfo};
use cpuio::{inb, outb}; 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 kernel::allocator::ALLOCATOR;
use spin::Lazy; use spin::Lazy;
use x86_64::instructions::interrupts::{disable, enable}; use x86_64::instructions::interrupts::{disable, enable};
@ -196,18 +194,16 @@ pub fn real_shell() {
} }
pub fn command_parser(user: String, command: String) { pub fn command_parser(user: String, command: String) {
// let fs = &*FILE_SYSTEM.lock();
let mut iter = command.split_whitespace(); 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()); let current_path = Path::new("/home/able".to_string());
trace!("Current path: {:?}", current_path); trace!("Current path: {:?}", current_path);
let current_path = b"/home/able/"; let current_path = "/home/able/";
let bin_name = iter.next().unwrap(); let bin_name = iter.next().unwrap();
let mut strin = String::new(); let mut strin = String::new();
for stri in iter { for stri in iter.clone() {
trace!("{}", stri); trace!("{}", stri);
strin.push_str(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 // note: able asked for rhaish to stay in the repo but will be removed
// in the future so just comment it out for now // in the future so just comment it out for now
// "rhai" => { // "rhai" => {
// drop(fs);
// shell(); // shell();
// } // }
// "list" | "ls" => { "list" | "ls" => {
// for dir_entry in list_files_in_dir(fs, current_path) { let mut vfs = VFS.lock();
// println!("{}", dir_entry.file_name_string()); 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") { "echo" => match conf_args.1.arguments.get("p") {
// Some(path) => echo_file(path.to_string(), fs), Some(path) => echo_file(path.to_string()),
None => println!("No path provided"),
// None => println!("No path provided"), }
// }
"test" => {} "test" => {}
"quit" => shutdown(), "quit" => shutdown(),
"tree" => filesystem::tree("/").unwrap(), "tree" => filesystem::tree("/").unwrap(),
_ => { _ => {
// let mut options = OpenOptions::new(); let file = {
// options.read(true); let mut vfs = VFS.lock();
// let file = { let path = format!("/home/{user}/bins/{bin_name}.wasm");
// let path = format!("/home/{user}/bins/{bin_name}.wasm"); let handle = if let Ok(file) = vfs.resolve(path) {
// if let Ok(file) = fs.open(&path.as_bytes(), &options) { file
// file } else {
// } else { let path = format!("/shared/bins/{bin_name}.wasm");
// let path = format!("/shared/bins/{bin_name}.wasm"); if let Ok(file) = vfs.resolve(path) {
// if let Ok(file) = fs.open(&path.as_bytes(), &options) { file
// file } else {
// } else { let path = format!("/system/bins/{bin_name}.wasm");
// let path = format!("/system/bins/{bin_name}.wasm"); match vfs.resolve(path) {
// match fs.open(&path.as_bytes(), &options) { Ok(file) => file,
// Ok(file) => file, Err(error) => {
// Err(error) => { trace!("{:?}", error);
// trace!("{:?}", error); println!("No such binary: {}", bin_name);
println!("No such binary: {}", bin_name); error!("No such binary: {}", bin_name);
error!("No such binary: {}", bin_name); return;
// } }
// } }
// } }
// } };
// };
// let mut binary = vec![]; vfs.fs_node(handle).unwrap()
// file.read_to_end(&mut binary).unwrap(); };
// let args = iter.collect::<Vec<&str>>(); let mut binary = vec![];
// println!("{:?}", args); file.read(0, file.size(), &mut binary).unwrap();
// run_program(&binary);
let args = iter.collect::<Vec<&str>>();
println!("{:?}", args);
run_program(&binary);
} }
} }
} }
@ -304,40 +304,21 @@ pub fn sound_off() {
reset_pit_for_cpu(); reset_pit_for_cpu();
} }
pub fn list_files_in_dir( pub fn echo_file(path: String) {
fs: &Synced<Ext2<Size1024, Vec<u8>>>, let mut current_path = String::from("/");
_path: &[u8], current_path.push_str(&path);
) -> Vec<DirectoryEntry> { let mut vfs = VFS.lock();
let mut entry_list = vec![]; let handle = vfs
.resolve(&current_path)
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<spin::Mutex<String>> = Lazy::new(|| spin::Mutex::new("/".to_string()));
pub fn echo_file(path: String, fs: &Synced<Ext2<Size1024, Vec<u8>>>) {
let mut current_dir = CURRENT_DIR.lock();
current_dir.push_str(&path);
let file = fs
.open(current_dir.as_bytes(), OpenOptions::new().read(true))
.unwrap(); .unwrap();
let file = vfs.fs_node(handle).unwrap();
if file.is_dir() { if file.is_dir() {
// println!("{} is a directory", path); // println!("{} is a directory", path);
} else { } else {
let mut file_contents = Vec::new(); let mut file_contents = Vec::new();
file.read(0, file.size(), &mut file_contents).unwrap();
let _ret = file.read_to_end(&mut file_contents).unwrap();
let file_contents_str = String::from_utf8_lossy(&file_contents); let file_contents_str = String::from_utf8_lossy(&file_contents);
println!("{}", file_contents_str); println!("{}", file_contents_str);
} }
} }

View file

@ -1,7 +1,12 @@
/*
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
*
* SPDX-License-Identifier: MPL-2.0
*/
pub mod host_functions; pub mod host_functions;
use crate::wasm_jumploader::host_functions::HostExternals; use crate::wasm_jumploader::host_functions::HostExternals;
// use genfs::{Fs, OpenOptions};
use wasmi::{ImportsBuilder, ModuleInstance}; use wasmi::{ImportsBuilder, ModuleInstance};
pub fn interp() { pub fn interp() {