diff options
author | RaindropsSys <raindrops@equestria.dev> | 2023-12-02 16:02:58 +0100 |
---|---|---|
committer | RaindropsSys <raindrops@equestria.dev> | 2023-12-02 16:02:58 +0100 |
commit | d5aa820492349ee62eac26f0408ea09e13709386 (patch) | |
tree | fdce32edb936a138e9ae2736c1ed2f6f98e8f399 /toolkit | |
parent | ac3c8079a1205ed8bc3e6b1f1bad2e575c444ebb (diff) | |
download | cometos-d5aa820492349ee62eac26f0408ea09e13709386.tar.gz cometos-d5aa820492349ee62eac26f0408ea09e13709386.tar.bz2 cometos-d5aa820492349ee62eac26f0408ea09e13709386.zip |
Updated 24 files, added 17 files and renamed 2 files (automated)
Diffstat (limited to 'toolkit')
-rw-r--r-- | toolkit/.DS_Store | bin | 6148 -> 6148 bytes | |||
-rw-r--r-- | toolkit/Cargo.toml | 3 | ||||
-rw-r--r-- | toolkit/src/error.rs | 24 | ||||
-rw-r--r-- | toolkit/src/lib.rs | 125 |
4 files changed, 130 insertions, 22 deletions
diff --git a/toolkit/.DS_Store b/toolkit/.DS_Store Binary files differindex eee283d..b7cc4e5 100644 --- a/toolkit/.DS_Store +++ b/toolkit/.DS_Store diff --git a/toolkit/Cargo.toml b/toolkit/Cargo.toml index 19c0c62..216ed08 100644 --- a/toolkit/Cargo.toml +++ b/toolkit/Cargo.toml @@ -6,9 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -log = "0.4.20" uefi = { version = "0.26.0", features = ["alloc"] } -uefi-services = { version = "0.23.0", features = ["logger"], default-features = false } +uefi-services = { version = "0.23.0", features = [], default-features = false } build-info = { version = "0.0.34", features = [], default-features = false } [build-dependencies] diff --git a/toolkit/src/error.rs b/toolkit/src/error.rs index 823dcd6..db5c313 100644 --- a/toolkit/src/error.rs +++ b/toolkit/src/error.rs @@ -4,21 +4,37 @@ use crate::Toolkit; #[derive(Copy, Clone)] pub enum Error { - //E01, + E01, E02, E03, E04, - E05 + E05, + E06, + E07, + E08, + E09, + E10, + E11, + E12, + E13 } impl Error { fn description(&self) -> &str { match self { - //Error::E01 => "An internal undocumented system error has occurred.", + Error::E01 => "An internal undocumented system error has occurred.", Error::E02 => "The requested command could not be found.", - Error::E03 => "A required paramater for this command was not provided.", + Error::E03 => "Too few arguments were provided to this command.", Error::E04 => "The requested file or directory could not be found.", Error::E05 => "Unable to go up one level from the root directory.", + Error::E06 => "No command name could be found in the input.", + Error::E07 => "Unexpected end of line: quotes do not match.", + Error::E08 => "Too many arguments were provided to this command.", + Error::E09 => "The requested file is not a directory.", + Error::E10 => "The requested file already exists.", + Error::E11 => "The directory to affect is not empty.", + Error::E12 => "The requested file is a directory.", + Error::E13 => "Input/output error occurred while reading the file." //_ => "Unknown error." } } diff --git a/toolkit/src/lib.rs b/toolkit/src/lib.rs index d0d64bb..b083cac 100644 --- a/toolkit/src/lib.rs +++ b/toolkit/src/lib.rs @@ -4,6 +4,7 @@ extern crate alloc; use alloc::string::{String, ToString}; use alloc::{format, vec}; +use alloc::collections::BTreeMap; use alloc::vec::Vec; use uefi::{Char16, CStr16}; use uefi::prelude::*; @@ -23,13 +24,15 @@ pub struct ToolkitWorkingPath { pub struct ToolkitVersionInfo { pub version: &'static str, pub timestamp: &'static str, - pub compiler: &'static str + pub compiler: &'static str, + pub profile: &'static str } pub struct Toolkit { system_table: SystemTable<Boot>, pub current_directory: ToolkitWorkingPath, - pub version: ToolkitVersionInfo + pub version: ToolkitVersionInfo, + pub globals: BTreeMap<String, String> } impl Toolkit { @@ -51,12 +54,19 @@ impl Toolkit { current_directory: ToolkitWorkingPath { name: String::from("") }, - version + version, + globals: BTreeMap::new() } } } impl Toolkit { + fn get_fs(&self) -> FileSystem { + let handle = self.system_table.boot_services().image_handle(); + let fs = self.system_table.boot_services().get_image_file_system(handle).expect("Failed to start up filesystem"); + FileSystem::new(fs) + } + pub fn color(&mut self, fg: Color, bg: Color) { self.system_table.stdout().set_color(fg, bg).expect("Failed to set screen color"); } @@ -120,10 +130,6 @@ impl Toolkit { stdout.reset(false).expect("Failed to clear screen buffer"); } - /*pub fn sleep(&mut self, time: usize) { - self.system_table.boot_services().stall(time * 1000); - }*/ - pub fn get_cwd(&self) -> &str { &self.current_directory.name } @@ -179,9 +185,7 @@ impl Toolkit { if path == "" { return true; } - let handle = self.system_table.boot_services().image_handle(); - let fs = self.system_table.boot_services().get_image_file_system(handle).expect("Failed to start up filesystem"); - let mut filesystem = FileSystem::new(fs); + let mut filesystem = self.get_fs(); let mut buf: Vec<u16> = vec![0; path.len() + 1]; return match filesystem.try_exists(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())) { Ok(b) => b, @@ -189,10 +193,53 @@ impl Toolkit { }; } + pub fn is_dir(&self, path: &str) -> bool { + if path == "" { + return true; + } + let mut filesystem = self.get_fs(); + let mut buf: Vec<u16> = vec![0; path.len() + 1]; + return match filesystem.metadata(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())) { + Ok(b) => b.is_directory(), + Err(_) => false + }; + } + + pub fn is_file(&self, path: &str) -> bool { + if path == "" { + return true; + } + let mut filesystem = self.get_fs(); + let mut buf: Vec<u16> = vec![0; path.len() + 1]; + return match filesystem.metadata(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())) { + Ok(b) => b.is_regular_file(), + Err(_) => false + }; + } + + pub fn read_file(&self, path: &str) -> Option<String> { + if path == "" { + return None; + } + let mut filesystem = self.get_fs(); + let mut buf: Vec<u16> = vec![0; path.len() + 1]; + return match filesystem.read_to_string(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())) { + Ok(b) => Some(b), + Err(_) => None + }; + } + + pub fn write_file(&self, path: &str, text: &str) { + if path == "" { + return; + } + let mut filesystem = self.get_fs(); + let mut buf: Vec<u16> = vec![0; path.len() + 1]; + filesystem.write(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap()), text.as_bytes()).unwrap(); + } + pub fn scandir(&self, path: &str) -> UefiDirectoryIter { - let handle = self.system_table.boot_services().image_handle(); - let fs = self.system_table.boot_services().get_image_file_system(handle).expect("Failed to start up filesystem"); - let mut filesystem = FileSystem::new(fs); + let mut filesystem = self.get_fs(); let mut buf: Vec<u16> = vec![0; path.len() + 1]; return match filesystem.read_dir(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())) { Ok(b) => b, @@ -200,6 +247,36 @@ impl Toolkit { }; } + pub fn resolve_path(&self, orig_og_path: &str) -> String { + let og_path = orig_og_path.replace("\\", "/"); + let mut final_path = self.get_cwd().to_string().replace("/", "\\"); + let path = &og_path.replace("/", "\\"); + let mut buf = vec![0; path.len() + 1]; + let cstr = CStr16::from_str_with_buf(path, &mut buf).unwrap(); + let path = Path::new(cstr); + + if og_path.starts_with("/") { + final_path = String::from(""); + } + + for i in path.components() { + if i.to_string() == ".." { + let parts = final_path.split("\\").collect::<Vec<&str>>(); + let len = parts.len() - 1; + + final_path = parts[..len].join("\\"); + } else if i.to_string() != "." { + final_path = format!("{}\\{}", final_path, i).replace("/", "\\"); + } + } + + if final_path.len() > 0 { + final_path[1..].to_string().replace("\\\\", "\\") + } else { + final_path.to_string().replace("\\\\", "\\") + } + } + pub fn make_readable(&self, _size: u64) -> String { let size = _size as f64; @@ -217,13 +294,29 @@ impl Toolkit { } pub fn mkdir(&self, path: &str) { - let handle = self.system_table.boot_services().image_handle(); - let fs = self.system_table.boot_services().get_image_file_system(handle).expect("Failed to start up filesystem"); - let mut filesystem = FileSystem::new(fs); + let mut filesystem = self.get_fs(); let mut buf: Vec<u16> = vec![0; path.len() + 1]; filesystem.create_dir(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())).unwrap(); } + pub fn rmdir(&mut self, path: &str) { + let mut filesystem = self.get_fs(); + let mut buf: Vec<u16> = vec![0; path.len() + 1]; + filesystem.remove_dir(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())).unwrap(); + } + + pub fn unlink(&mut self, path: &str) { + let mut filesystem = self.get_fs(); + let mut buf: Vec<u16> = vec![0; path.len() + 1]; + filesystem.remove_file(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())).unwrap(); + } + + pub fn recursive_rmdir(&mut self, path: &str) { + let mut filesystem = self.get_fs(); + let mut buf: Vec<u16> = vec![0; path.len() + 1]; + filesystem.remove_dir_all(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())).unwrap(); + } + pub fn print(&mut self, str: &str) { let stdout = self.system_table.stdout(); let mut buf = vec![0; str.len() + 1]; |