Skip to content

Commit

Permalink
allows the creation of a file at any position of the fs
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Jan 8, 2024
1 parent c91f514 commit d1d5b75
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
41 changes: 24 additions & 17 deletions src/fs/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,6 @@ impl MemDirectory {
},
}
}

pub fn create_file(
&self,
name: &str,
ptr: *const u8,
length: usize,
mode: AccessPermission,
) -> Result<(), IoError> {
let name = name.trim();
if name.find('/').is_none() {
let file = unsafe { RomFile::new(ptr, length, mode) };
self.inner.write().insert(name.to_string(), Box::new(file));
Ok(())
} else {
Err(IoError::EBADF)
}
}
}

impl VfsNode for MemDirectory {
Expand Down Expand Up @@ -548,4 +531,28 @@ impl VfsNode for MemDirectory {

Err(IoError::ENOENT)
}

fn traverse_create_file(
&self,
components: &mut Vec<&str>,
ptr: *const u8,
length: usize,
mode: AccessPermission,
) -> Result<(), IoError> {
if let Some(component) = components.pop() {
let name = String::from(component);

if components.is_empty() {
let file = unsafe { RomFile::new(ptr, length, mode) };
self.inner.write().insert(name.to_string(), Box::new(file));
return Ok(());
}

if let Some(directory) = self.inner.read().get(&name) {
return directory.traverse_create_file(components, ptr, length, mode);
}
}

Err(IoError::ENOENT)
}
}
25 changes: 22 additions & 3 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ pub(crate) trait VfsNode: core::fmt::Debug {
) -> Result<Arc<dyn ObjectInterface>, IoError> {
Err(IoError::ENOSYS)
}

/// Helper function to create a read-only file
fn traverse_create_file(
&self,
_components: &mut Vec<&str>,
_ptr: *const u8,
_length: usize,
_mode: AccessPermission,
) -> Result<(), IoError> {
Err(IoError::ENOSYS)
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -290,15 +301,23 @@ impl Filesystem {
self.root.traverse_mount(&mut components, obj)
}

/// Create file from ROM
/// Create read-only file
pub unsafe fn create_file(
&self,
name: &str,
path: &str,
ptr: *const u8,
length: usize,
mode: AccessPermission,
) -> Result<(), IoError> {
self.root.create_file(name, ptr, length, mode)
debug!("Create read-only file {}", path);

let mut components: Vec<&str> = path.split('/').collect();

components.reverse();
components.pop();

self.root
.traverse_create_file(&mut components, ptr, length, mode)
}
}

Expand Down

0 comments on commit d1d5b75

Please sign in to comment.