1
0
Fork 0
forked from AbleOS/ableos

CAPS: Adding in capabilities

This commit is contained in:
able 2023-07-10 22:54:05 -05:00
parent a2a2b509bb
commit ae321a9923
3 changed files with 179 additions and 0 deletions

177
kernel/src/capabilities.rs Normal file
View file

@ -0,0 +1,177 @@
//! AbleOS capability tree implementation
use {
alloc::{
format,
string::{String, ToString},
vec,
},
core::fmt,
};
// Seperate
use alloc::vec::Vec;
struct Argument {
name: String,
type_: String,
}
struct Function {
name: String,
args: Vec<Argument>,
ret: Option<String>,
}
struct Capability {
name: String,
functions: Vec<Function>,
sub_capabilities: Vec<Capability>,
}
impl Capability {
fn new(name: String) -> Capability {
Capability {
name,
functions: Vec::new(),
sub_capabilities: Vec::new(),
}
}
}
impl fmt::Display for Capability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Capability: {}\n", self.name)?;
for function in &self.functions {
write!(f, " Function: {}\n", function.name)?;
for arg in &function.args {
write!(f, " Argument: {} (Type: {})\n", arg.name, arg.type_)?;
}
if let Some(ret_type) = &function.ret {
write!(f, " Return Type: {}\n", ret_type)?;
} else {
write!(f, " Return Type: None\n")?;
}
}
for sub_capability in &self.sub_capabilities {
write!(f, "{}", sub_capability.to_string_with_indentation(2))?;
}
Ok(())
}
}
impl Capability {
fn to_string_with_indentation(&self, level: usize) -> String {
let indent = " ".repeat(level);
let mut result = format!("{}Capability: {}\n", indent, self.name);
for function in &self.functions {
result.push_str(&format!("{} Function: {}\n", indent, function.name));
for arg in &function.args {
result.push_str(&format!(
"{} Argument: {} (Type: {})\n",
indent, arg.name, arg.type_
));
}
if let Some(ret_type) = &function.ret {
result.push_str(&format!("{} Return Type: {}\n", indent, ret_type));
} else {
result.push_str(&format!("{} Return Type: None\n", indent));
}
}
for sub_capability in &self.sub_capabilities {
result.push_str(&sub_capability.to_string_with_indentation(level + 1));
}
result
}
}
struct CapabilityTree {
capabilities: Vec<Capability>,
}
impl CapabilityTree {
fn new() -> CapabilityTree {
CapabilityTree {
capabilities: Vec::new(),
}
}
}
fn example() {
let mut capability_tree = Capability::new("VFS".to_string());
let mut file_management_capability = Capability::new("File Management".to_string());
file_management_capability.functions.push(Function {
name: "OpenFile".to_string(),
args: vec![
Argument {
name: "path".to_string(),
type_: "String".to_string(),
},
Argument {
name: "mode".to_string(),
type_: "String".to_string(),
},
],
ret: Some("FileHandle".to_string()),
});
file_management_capability.functions.push(Function {
name: "ReadFile".to_string(),
args: vec![
Argument {
name: "file".to_string(),
type_: "FileHandle".to_string(),
},
Argument {
name: "buffer".to_string(),
type_: "&mut [u8]".to_string(),
},
Argument {
name: "length".to_string(),
type_: "usize".to_string(),
},
],
ret: Some("usize".to_string()),
});
file_management_capability.functions.push(Function {
name: "WriteFile".to_string(),
args: vec![
Argument {
name: "file".to_string(),
type_: "FileHandle".to_string(),
},
Argument {
name: "buffer".to_string(),
type_: "&[u8]".to_string(),
},
],
ret: None,
});
let mut directory_management_capability = Capability::new("Directory Management".to_string());
directory_management_capability.functions.push(Function {
name: "CreateDirectory".to_string(),
args: vec![Argument {
name: "path".to_string(),
type_: "String".to_string(),
}],
ret: Some("bool".to_string()),
});
directory_management_capability.functions.push(Function {
name: "ListDirectory".to_string(),
args: vec![Argument {
name: "path".to_string(),
type_: "String".to_string(),
}],
ret: Some("Vec<String>".to_string()),
});
capability_tree
.sub_capabilities
.push(file_management_capability);
capability_tree
.sub_capabilities
.push(directory_management_capability);
log::debug!("{}", capability_tree);
}

View file

@ -26,6 +26,7 @@ impl OSHandle {
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
pub struct Handle {
id: OSHandle,
// TODO: Update this to be indexes into the caps
perms: Permissions,
}

View file

@ -22,6 +22,7 @@ extern crate alloc;
mod allocator;
mod arch;
mod bootmodules;
pub mod capabilities;
pub mod device_tree;
pub mod handle;
pub mod host;