feat(libkos): add fs, display, input service client modules with newtypes
This commit is contained in:
73
crates/libkos/src/fs.rs
Normal file
73
crates/libkos/src/fs.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use types::bounded::CapSlot;
|
||||
use crate::syscall;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct FileHandle(u16);
|
||||
|
||||
impl FileHandle {
|
||||
pub const fn raw(&self) -> u16 { self.0 }
|
||||
}
|
||||
|
||||
fn pack_path(path: &[u8]) -> (usize, usize) {
|
||||
let mut w0 = [0u8; 8];
|
||||
let mut w1 = [0u8; 8];
|
||||
for (i, &b) in path.iter().enumerate() {
|
||||
if i < 8 { w0[i] = b; }
|
||||
else if i < 16 { w1[i - 8] = b; }
|
||||
}
|
||||
(usize::from_le_bytes(w0), usize::from_le_bytes(w1))
|
||||
}
|
||||
|
||||
fn ipc(ep: CapSlot, w0: usize, w1: usize, w2: usize, w3: usize) -> (usize, usize, usize, usize) {
|
||||
syscall::send(ep, w0, w1, w2, w3);
|
||||
syscall::receive(ep)
|
||||
}
|
||||
|
||||
pub fn open(ep: CapSlot, path: &[u8]) -> Option<FileHandle> {
|
||||
let (p0, p1) = pack_path(path);
|
||||
let (tag, handle, _, _) = ipc(ep, 0, p0, p1, 0);
|
||||
if tag == 0 { Some(FileHandle(handle as u16)) } else { None }
|
||||
}
|
||||
|
||||
pub fn read(ep: CapSlot, handle: FileHandle, max_bytes: usize) -> Option<usize> {
|
||||
let (tag, bytes_read, _, _) = ipc(ep, 1, handle.0 as usize, max_bytes, 0);
|
||||
if tag == 1 { Some(bytes_read) } else { None }
|
||||
}
|
||||
|
||||
pub fn write(ep: CapSlot, handle: FileHandle, data_word: usize) -> Option<usize> {
|
||||
let (tag, bytes_written, _, _) = ipc(ep, 2, handle.0 as usize, data_word, 0);
|
||||
if tag == 2 { Some(bytes_written) } else { None }
|
||||
}
|
||||
|
||||
pub fn close(ep: CapSlot, handle: FileHandle) {
|
||||
ipc(ep, 3, handle.0 as usize, 0, 0);
|
||||
}
|
||||
|
||||
pub fn list_dir(ep: CapSlot, path: &[u8]) -> Option<usize> {
|
||||
let (p0, p1) = pack_path(path);
|
||||
let (tag, count, _, _) = ipc(ep, 4, p0, p1, 0);
|
||||
if tag == 4 { Some(count) } else { None }
|
||||
}
|
||||
|
||||
pub fn create(ep: CapSlot, path: &[u8]) -> Option<FileHandle> {
|
||||
let (p0, p1) = pack_path(path);
|
||||
let (tag, handle, _, _) = ipc(ep, 5, p0, p1, 0);
|
||||
if tag == 0 { Some(FileHandle(handle as u16)) } else { None }
|
||||
}
|
||||
|
||||
pub fn delete(ep: CapSlot, path: &[u8]) -> bool {
|
||||
let (p0, p1) = pack_path(path);
|
||||
let (tag, _, _, _) = ipc(ep, 6, p0, p1, 0);
|
||||
tag == 3
|
||||
}
|
||||
|
||||
pub fn file_size(ep: CapSlot, handle: FileHandle) -> Option<usize> {
|
||||
let (tag, size, _, _) = ipc(ep, 7, handle.0 as usize, 0, 0);
|
||||
if tag == 5 { Some(size) } else { None }
|
||||
}
|
||||
|
||||
pub fn mkdir(ep: CapSlot, path: &[u8]) -> bool {
|
||||
let (p0, p1) = pack_path(path);
|
||||
let (tag, _, _, _) = ipc(ep, 8, p0, p1, 0);
|
||||
tag == 3
|
||||
}
|
||||
Reference in New Issue
Block a user