Sync computers

This commit is contained in:
2026-03-01 16:12:02 +01:00
parent 392af94345
commit 041e544330
7 changed files with 59 additions and 41 deletions

View File

@@ -44,31 +44,29 @@ impl<'a, T> Dir<'a, T> {
} }
} }
impl<'a, T: ReadSeek> Dir<'a, T> { impl<'a, T: ReadSeek> Dir<'a, T> {
pub fn open_entry<'b, P: Into<Path<'b>>>( pub fn open_entry<P: AsRef<Path>>(
&self, &self,
path: P, path: P,
) -> Result<DirEntry<'a, T>, Error<T::Error>> { ) -> Result<DirEntry<'a, T>, Error<T::Error>> {
let path = path.into(); if path.as_ref().is_absolute() {
if path.is_absolute() {
return self.fs.open_entry(path); return self.fs.open_entry(path);
} }
for file in self.iter() { for file in self.iter() {
let f = file?; let f = file?;
if f.name_is(path.as_str()) { if f.name_is(path.as_ref().as_str()) {
return Ok(f); return Ok(f);
} }
} }
Err(Error::NotFound) Err(Error::NotFound)
} }
pub fn open_file<'b, P: Into<Path<'b>>>( pub fn open_file<P: AsRef<Path>>(
&self, &self,
path: P, path: P,
) -> Result<File<'a, T>, Error<T::Error>> { ) -> Result<File<'a, T>, Error<T::Error>> {
let path = path.into(); if path.as_ref().is_absolute() {
if path.is_absolute() {
return self.fs.open_file(path); return self.fs.open_file(path);
} }
let (start, dirname) = path.split_path(); let (start, dirname) = path.as_ref().split_path();
let entry = self.open_entry(start)?; let entry = self.open_entry(start)?;
match dirname { match dirname {
Some(name) => { Some(name) => {
@@ -85,8 +83,8 @@ impl<'a, T: ReadSeek> Dir<'a, T> {
} }
} }
} }
pub fn open_dir<'b, P: Into<Path<'b>>>(&self, path: P) -> Result<Self, Error<T::Error>> { pub fn open_dir<P: AsRef<Path>>(&self, path: P) -> Result<Self, Error<T::Error>> {
let path = path.into(); let path = path.as_ref();
if path.is_absolute() { if path.is_absolute() {
return self.fs.open_dir(path); return self.fs.open_dir(path);
} }

View File

@@ -136,22 +136,22 @@ impl<T> Fat32FileSystem<T> {
} }
} }
impl<T: ReadSeek> Fat32FileSystem<T> { impl<T: ReadSeek> Fat32FileSystem<T> {
pub fn open_entry<'b, P: Into<Path<'b>>>( pub fn open_entry<P: AsRef<Path>>(
&self, &self,
path: P, path: P,
) -> Result<DirEntry<'_, T>, Error<T::Error>> { ) -> Result<DirEntry<'_, T>, Error<T::Error>> {
let path = path.into().as_str().trim_start_matches("/"); let path = path.as_ref().as_str().trim_start_matches("/");
self.root_directory().open_entry(path) self.root_directory().open_entry(path)
} }
pub fn open_dir<'b, P: Into<Path<'b>>>(&self, path: P) -> Result<Dir<'_, T>, Error<T::Error>> { pub fn open_dir<P: AsRef<Path>>(&self, path: P) -> Result<Dir<'_, T>, Error<T::Error>> {
let path = path.into().as_str().trim_start_matches("/"); let path = path.as_ref().as_str().trim_start_matches("/");
self.root_directory().open_dir(path) self.root_directory().open_dir(path)
} }
pub fn open_file<'b, P: Into<Path<'b>>>( pub fn open_file<P: AsRef<Path>>(
&self, &self,
path: P, path: P,
) -> Result<File<'_, T>, Error<T::Error>> { ) -> Result<File<'_, T>, Error<T::Error>> {
let path = path.into().as_str().trim_start_matches("/"); let path = path.as_ref().as_str().trim_start_matches("/");
self.root_directory().open_file(path) self.root_directory().open_file(path)
} }
} }

View File

@@ -1,33 +1,46 @@
use core::ops::Deref; use core::ops::Deref;
#[cfg(feature = "alloc")]
use alloc::string::String; use alloc::string::String;
#[repr(transparent)] #[repr(transparent)]
pub struct Path<'a> { pub struct Path {
inner: &'a str, inner: str,
} }
impl<'a> From<&'a str> for Path<'a> { impl From<&str> for &Path {
fn from(value: &'a str) -> Self { fn from(value: &str) -> Self {
Self { inner: value } unsafe { &*(value as *const str as *const Path) }
} }
} }
impl<'a> AsRef<str> for Path<'a> { impl AsRef<Path> for str {
fn as_ref(&self) -> &Path {
unsafe { &*(self as *const str as *const Path) }
}
}
impl AsRef<Path> for Path {
fn as_ref(&self) -> &Path {
self
}
}
impl AsRef<str> for Path {
fn as_ref(&self) -> &str { fn as_ref(&self) -> &str {
self.inner self.as_str()
} }
} }
impl<'a> Path<'a> { impl Path {
pub fn split_path(&self) -> (&'a str, Option<Path<'a>>) { pub fn split_path(&self) -> (&str, Option<&Path>) {
if let Some((start, end)) = self.inner.split_once("/") { if let Some((start, end)) = self.inner.split_once("/") {
(start, Some(end.into())) (start, Some(end.into()))
} else { } else {
(self.inner, None) (&self.inner, None)
} }
} }
pub fn as_str(&self) -> &'a str { pub fn as_str(&self) -> &str {
self.inner &self.inner
} }
pub fn is_absolute(&self) -> bool { pub fn is_absolute(&self) -> bool {
self.inner.starts_with("/") self.inner.starts_with("/")
@@ -37,12 +50,16 @@ impl<'a> Path<'a> {
} }
} }
#[cfg(feature = "alloc")]
pub struct PathBuf { pub struct PathBuf {
inner: String, inner: String,
} }
impl<'a> Deref for PathBuf { #[cfg(feature = "alloc")]
type Target = Path<'a>; impl Deref for PathBuf {
type Target = Path;
fn deref(&self) -> &Self::Target {} fn deref(&self) -> &Self::Target {
self.inner.as_str().into()
}
} }

View File

@@ -128,9 +128,9 @@ pub fn dealloc(ptr: *mut u8, layout: core::alloc::Layout) {
syscall!(SysCall::Dealloc, ptr as u64, size as u64, align as u64); syscall!(SysCall::Dealloc, ptr as u64, size as u64, align as u64);
} }
} }
pub fn open<'a, P: Into<Path<'a>>>(path: P) -> u64 { pub fn open<P: AsRef<Path>>(path: P) -> u64 {
unsafe { unsafe {
let path_str = path.into().as_str(); let path_str = path.as_ref().as_str();
let ptr = path_str.as_ptr(); let ptr = path_str.as_ptr();
let size = path_str.len(); let size = path_str.len();
let (fd, ..) = syscall!(SysCall::Open, ptr as u64, size as u64); let (fd, ..) = syscall!(SysCall::Open, ptr as u64, size as u64);

View File

@@ -159,8 +159,8 @@ impl Scheduler {
/// Attempts to open `path`, load its contents into memory and create a new /// Attempts to open `path`, load its contents into memory and create a new
/// kernel process that will execute the loaded binary. Returns the PID of the /// kernel process that will execute the loaded binary. Returns the PID of the
/// created process, or -1 on failure. /// created process, or -1 on failure.
pub fn create_process_from_file<'a, T: Into<Path<'a>>>(&mut self, path: T) -> i64 { pub fn create_process_from_file<T: AsRef<Path>>(&mut self, path: T) -> i64 {
let path = path.into(); let path = path.as_ref();
let name = path.as_str(); let name = path.as_str();
// Open and read the binary file // Open and read the binary file

View File

@@ -16,11 +16,11 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: core::alloc::Layout) {
unsafe { alloc::alloc::dealloc(ptr, layout) } unsafe { alloc::alloc::dealloc(ptr, layout) }
} }
pub fn open<'a, P: Into<Path<'a>>>( pub fn open<P: AsRef<Path>>(
path: P, path: P,
in_kernel: bool, in_kernel: bool,
) -> Result<Box<dyn VirtualNode + Send>, Error<<Disk as bffs::io::IoBase>::Error>> { ) -> Result<Box<dyn VirtualNode + Send>, Error<<Disk as bffs::io::IoBase>::Error>> {
let path = path.into(); let path = path.as_ref();
let file = match path.split_path() { let file = match path.split_path() {
("dev", path) => { ("dev", path) => {
todo!() todo!()

View File

@@ -1,5 +1,5 @@
use alloc::boxed::Box; use alloc::boxed::Box;
use bffs::path::Path; use bffs::path::{Path, PathBuf};
use hashbrown::HashMap; use hashbrown::HashMap;
pub trait VirtualNode { pub trait VirtualNode {
@@ -7,15 +7,18 @@ pub trait VirtualNode {
} }
pub trait VirtualFileSystem { pub trait VirtualFileSystem {
fn open<'a, P: Into<Path<'a>>>(path: P) -> Result<Box<dyn VirtualNode + Send>, ()>; fn open(&mut self, path: &Path) -> Result<Box<dyn VirtualNode + Send>, ()>;
} }
pub struct MainFileSystem { pub struct MainFileSystem {
mounts: HashMap<PathBuf> mounts: HashMap<PathBuf, Box<dyn VirtualFileSystem>>,
} }
impl VirtualFileSystem for MainFileSystem { impl VirtualFileSystem for MainFileSystem {
fn open<'a, P: Into<Path<'a>>>(path: P) -> Result<Box<dyn VirtualNode + Send>, ()> { fn open(&mut self, path: &Path) -> Result<Box<dyn VirtualNode + Send>, ()> {
for mount in self.mounts.iter() {
}
todo!() todo!()
} }
} }