Skip to content

Commit 5907dd3

Browse files
Koichi98Copilot
andauthored
feat(filesystem):add async vfs path (#545)
* add async vfs path Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix for unpin Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * delete WalkDirIterator related Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * rename Error to IOError in AsyncFIlesystem Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix clippy Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix comment Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * minor fix Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * delete Fatfs related from path.rs Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * add trait bound for IoError Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * add VfsIoError Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix for deleting generic type E from VfsError Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix comment Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * delete unnecessary changes Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * cargo fmt Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix clippy Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * delete empty line Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * add Debug Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix simple_async_copy Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix copy_dir&move_dir Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix unnecessary changes Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix unnecessary Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix read_to_string Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * Update awkernel_async_lib/src/file/path.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * cargo fmt Signed-off-by: Koichi <koichi.imai.2@tier4.jp> --------- Signed-off-by: Koichi <koichi.imai.2@tier4.jp> Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 704fa12 commit 5907dd3

7 files changed

Lines changed: 123 additions & 1518 deletions

File tree

awkernel_async_lib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pin-project = "1"
1010
log = "0.4"
1111
array-macro = "2.1"
1212
fixedbitset = "0.5.7"
13+
async-trait = "0.1"
14+
async-recursion = "1.1"
1315

1416
[dependencies.futures]
1517
version = "0.3"

awkernel_async_lib/src/file.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod filesystem;
2+
pub mod path;

awkernel_async_lib/src/file/filesystem.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! The async filesystem trait definitions needed to implement new async virtual filesystems
22
use super::path::AsyncVfsPath;
33
use crate::time::Time;
4-
use alloc::{boxed::Box, string::String};
4+
use alloc::{boxed::Box, string::String, vec::Vec};
55
use async_trait::async_trait;
66
use awkernel_lib::file::{
7-
error::IoError,
87
io::SeekFrom,
98
vfs::error::{VfsError, VfsErrorKind, VfsResult},
109
vfs::path::VfsMetadata,
1110
};
11+
use core::fmt::Debug;
1212
use futures::stream::Stream;
1313

1414
// NOTE: We're currently using our own AsyncSeekAndRead and AsyncSeekAndWrite traits. We might replace these with traits from embedded-io-async in the future. However, that change would involve many modifications, and embedded-io-async doesn't seem stable yet, so we're sticking with our current approach for now.
@@ -31,6 +31,30 @@ pub trait AsyncSeekAndRead: Send + Unpin {
3131

3232
Ok(())
3333
}
34+
35+
async fn read_to_string(&mut self, buf: &mut String) -> Result<usize, VfsError> {
36+
let mut bytes = Vec::new();
37+
let mut chunk = [0; 8192];
38+
39+
loop {
40+
let bytes_read = self.read(&mut chunk).await?;
41+
if bytes_read == 0 {
42+
break;
43+
}
44+
bytes.extend_from_slice(&chunk[..bytes_read]);
45+
}
46+
47+
match String::from_utf8(bytes) {
48+
Ok(s) => {
49+
let len = s.len();
50+
buf.push_str(&s);
51+
Ok(len)
52+
}
53+
Err(_) => Err(VfsError::from(VfsErrorKind::Other(
54+
"Invalid UTF-8 sequence".into(),
55+
))),
56+
}
57+
}
3458
}
3559

3660
#[async_trait]
@@ -86,7 +110,7 @@ impl AsyncSeekAndWrite for Box<dyn AsyncSeekAndWrite + Send + Unpin> {
86110
///
87111
/// Please use the test_macros [test_macros::test_async_vfs!] and [test_macros::test_async_vfs_readonly!]
88112
#[async_trait]
89-
pub trait AsyncFileSystem: Sync + Send + 'static {
113+
pub trait AsyncFileSystem: Sync + Send + Debug {
90114
/// Iterates over all direct children of this directory path
91115
/// NOTE: the returned String items denote the local bare filenames, i.e. they should not contain "/" anywhere
92116
async fn read_dir(
@@ -147,3 +171,9 @@ pub trait AsyncFileSystem: Sync + Send + 'static {
147171
Err(VfsErrorKind::NotSupported.into())
148172
}
149173
}
174+
175+
impl From<Box<dyn AsyncFileSystem>> for AsyncVfsPath {
176+
fn from(filesystem: Box<dyn AsyncFileSystem>) -> Self {
177+
AsyncVfsPath::new(filesystem)
178+
}
179+
}

0 commit comments

Comments
 (0)