feat(storage): add mkdir to FileSystem trait with stubs

This commit is contained in:
2026-10-18 23:06:26 +02:00
parent 25824a01fe
commit d85cb5b55d
8 changed files with 33 additions and 0 deletions

View File

@@ -116,4 +116,6 @@ impl FileSystem for MockFileSystem {
fn file_size(&mut self, handle: FileHandle) -> Option<usize> {
self.files[handle.raw() as usize].as_ref().map(|e| e.size)
}
fn mkdir(&mut self, _path: &[u8]) -> bool { true }
}

View File

@@ -73,4 +73,5 @@ pub trait FileSystem {
fn create(&mut self, path: &[u8]) -> Option<FileHandle>;
fn delete(&mut self, path: &[u8]) -> bool;
fn file_size(&mut self, handle: FileHandle) -> Option<usize>;
fn mkdir(&mut self, path: &[u8]) -> bool;
}

View File

@@ -336,6 +336,8 @@ impl<B: BlockDevice> FileSystem for Ext4<B> {
fn file_size(&mut self, handle: FileHandle) -> Option<usize> {
self.open_files[handle.raw() as usize].as_ref().map(|f| f.size)
}
fn mkdir(&mut self, _path: &[u8]) -> bool { false }
}
fn read_u32(data: &[u8], offset: usize) -> u32 {

View File

@@ -368,6 +368,8 @@ impl<B: BlockDevice> FileSystem for Fat16<B> {
fn file_size(&mut self, handle: FileHandle) -> Option<usize> {
self.open_files[handle.raw() as usize].as_ref().map(|f| f.size)
}
fn mkdir(&mut self, _path: &[u8]) -> bool { false }
}
fn to_fat_name(name: &[u8]) -> [u8; 11] {

View File

@@ -46,6 +46,7 @@ pub enum StorageRequest {
Create { path: PackedPath },
Delete { path: PackedPath },
FileSize { handle: FileHandle },
Mkdir { path: PackedPath },
}
#[derive(Debug, PartialEq)]
@@ -71,6 +72,7 @@ impl Encode for StorageRequest {
StorageRequest::Create { path } => [5, path.word0(), path.word1(), 0],
StorageRequest::Delete { path } => [6, path.word0(), path.word1(), 0],
StorageRequest::FileSize { handle } => [7, handle.raw() as usize, 0, 0],
StorageRequest::Mkdir { path } => [8, path.word0(), path.word1(), 0],
}
}
}
@@ -86,6 +88,7 @@ impl Decode for StorageRequest {
5 => Some(StorageRequest::Create { path: PackedPath::from_words(words[1], words[2]) }),
6 => Some(StorageRequest::Delete { path: PackedPath::from_words(words[1], words[2]) }),
7 => Some(StorageRequest::FileSize { handle: FileHandle::new(words[1] as u16) }),
8 => Some(StorageRequest::Mkdir { path: PackedPath::from_words(words[1], words[2]) }),
_ => None,
}
}

View File

@@ -82,6 +82,15 @@ impl<F: FileSystem> Server for StorageServer<F> {
None => StorageResponse::Error,
}
}
StorageRequest::Mkdir { path } => {
let buf = path.to_bytes();
let len = buf.iter().position(|&b| b == 0).unwrap_or(16);
if self.fs.mkdir(&buf[..len]) {
StorageResponse::Ok
} else {
StorageResponse::Error
}
}
}
}
}

View File

@@ -59,6 +59,13 @@ fn all_response_variants() {
}
}
#[test]
fn mkdir_roundtrip() {
let path = PackedPath::from_bytes(b"mydir");
let req = StorageRequest::Mkdir { path };
assert_eq!(StorageRequest::decode(req.encode()), Some(req));
}
#[test]
fn invalid_request_returns_none() {
assert!(StorageRequest::decode([99, 0, 0, 0]).is_none());

View File

@@ -68,6 +68,13 @@ fn list_dir() {
assert_eq!(resp, StorageResponse::DirEntries { count: 2 });
}
#[test]
fn mkdir_returns_ok() {
let mut server = test_server();
let resp = server.handle(StorageRequest::Mkdir { path: path(b"subdir") });
assert_eq!(resp, StorageResponse::Ok);
}
#[test]
fn close_succeeds() {
let mut server = test_server();