From 879ea30da4be417a3da5dab5e90ffa549607da49 Mon Sep 17 00:00:00 2001 From: Koichi Imai Date: Wed, 25 Jun 2025 08:48:42 +0900 Subject: [PATCH 1/7] add async filesystem trait Signed-off-by: Koichi Imai --- awkernel_async_lib/src/file/filesystem.rs | 151 ++++++++++++++++++---- 1 file changed, 123 insertions(+), 28 deletions(-) diff --git a/awkernel_async_lib/src/file/filesystem.rs b/awkernel_async_lib/src/file/filesystem.rs index 9a1063df0..69a1f4f00 100644 --- a/awkernel_async_lib/src/file/filesystem.rs +++ b/awkernel_async_lib/src/file/filesystem.rs @@ -1,14 +1,87 @@ //! The async filesystem trait definitions needed to implement new async virtual filesystems +use super::path::AsyncVfsPath; +use crate::time::Time; +use alloc::{boxed::Box, string::String}; +use async_trait::async_trait; +use awkernel_lib::file::{ + error::{Error, IoError}, + io::SeekFrom, + vfs::error::{VfsError, VfsErrorKind, VfsResult}, + vfs::path::VfsMetadata, +}; +use futures::stream::BoxStream; -use crate::async_vfs::{AsyncVfsPath, SeekAndRead}; -use crate::error::VfsErrorKind; -use crate::{VfsError, VfsMetadata, VfsResult}; +#[async_trait] +pub trait AsyncSeekAndRead: Send + Unpin { + async fn read(&mut self, buf: &mut [u8]) -> Result>; -use async_std::io::Write; -use async_std::stream::Stream; -use async_trait::async_trait; -use std::fmt::Debug; -use std::time::SystemTime; + async fn seek(&mut self, pos: SeekFrom) -> Result>; + + async fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), VfsError> { + while !buf.is_empty() { + match self.read(buf).await { + Ok(0) => break, + Ok(n) => { + buf = &mut buf[n..]; + } + Err(e) => return Err(e), + } + } + if !buf.is_empty() { + Err(VfsError::from(VfsErrorKind::from(Error::from( + E::new_unexpected_eof_error(), + ))) + .with_context(|| "failed to fill whole buffer")) + } else { + Ok(()) + } + } +} + +#[async_trait] +pub trait AsyncSeekAndWrite: Send + Unpin { + async fn write(&mut self, buf: &[u8]) -> Result>; + + async fn write_all(&mut self, buf: &[u8]) -> Result<(), VfsError>; + + async fn flush(&mut self) -> Result<(), VfsError>; + + async fn seek(&mut self, pos: SeekFrom) -> Result>; +} + +#[async_trait] +impl AsyncSeekAndRead for Box + Send> { + async fn read(&mut self, buf: &mut [u8]) -> Result> { + (**self).read(buf).await + } + + async fn seek(&mut self, pos: SeekFrom) -> Result> { + (**self).seek(pos).await + } + + async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), VfsError> { + (**self).read_exact(buf).await + } +} + +#[async_trait] +impl AsyncSeekAndWrite for Box + Send> { + async fn write(&mut self, buf: &[u8]) -> Result> { + (**self).write(buf).await + } + + async fn write_all(&mut self, buf: &[u8]) -> Result<(), VfsError> { + (**self).write_all(buf).await + } + + async fn flush(&mut self) -> Result<(), VfsError> { + (**self).flush().await + } + + async fn seek(&mut self, pos: SeekFrom) -> Result> { + (**self).seek(pos).await + } +} /// File system implementations must implement this trait /// All path parameters are absolute, starting with '/', except for the root directory @@ -18,58 +91,80 @@ use std::time::SystemTime; /// /// Please use the test_macros [test_macros::test_async_vfs!] and [test_macros::test_async_vfs_readonly!] #[async_trait] -pub trait AsyncFileSystem: Debug + Sync + Send + 'static { +pub trait AsyncFileSystem: Sync + Send + 'static { + /// The error type that can be returned by this file system. + type Error: IoError + Clone + Send + Sync; + /// Iterates over all direct children of this directory path /// NOTE: the returned String items denote the local bare filenames, i.e. they should not contain "/" anywhere - async fn read_dir( - &self, - path: &str, - ) -> VfsResult + Send>>; + async fn read_dir(&self, path: &str) -> VfsResult, Self::Error>; + /// Creates the directory at this path /// /// Note that the parent directory must already exist. - async fn create_dir(&self, path: &str) -> VfsResult<()>; + async fn create_dir(&self, path: &str) -> VfsResult<(), Self::Error>; + /// Opens the file at this path for reading - async fn open_file(&self, path: &str) -> VfsResult>; + async fn open_file( + &self, + path: &str, + ) -> VfsResult + Send>, Self::Error>; + /// Creates a file at this path for writing - async fn create_file(&self, path: &str) -> VfsResult>; + async fn create_file( + &self, + path: &str, + ) -> VfsResult + Send>, Self::Error>; + /// Opens the file at this path for appending - async fn append_file(&self, path: &str) -> VfsResult>; + async fn append_file( + &self, + path: &str, + ) -> VfsResult + Send>, Self::Error>; + /// Returns the file metadata for the file at this path - async fn metadata(&self, path: &str) -> VfsResult; + async fn metadata(&self, path: &str) -> VfsResult; + /// Sets the files creation timestamp, if the implementation supports it - async fn set_creation_time(&self, _path: &str, _time: SystemTime) -> VfsResult<()> { + async fn set_creation_time(&self, _path: &str, _time: Time) -> VfsResult<(), Self::Error> { Err(VfsError::from(VfsErrorKind::NotSupported)) } /// Sets the files modification timestamp, if the implementation supports it - async fn set_modification_time(&self, _path: &str, _time: SystemTime) -> VfsResult<()> { + async fn set_modification_time(&self, _path: &str, _time: Time) -> VfsResult<(), Self::Error> { Err(VfsError::from(VfsErrorKind::NotSupported)) } /// Sets the files access timestamp, if the implementation supports it - async fn set_access_time(&self, _path: &str, _time: SystemTime) -> VfsResult<()> { + async fn set_access_time(&self, _path: &str, _time: Time) -> VfsResult<(), Self::Error> { Err(VfsError::from(VfsErrorKind::NotSupported)) } /// Returns true if a file or directory at path exists, false otherwise - async fn exists(&self, path: &str) -> VfsResult; + async fn exists(&self, path: &str) -> VfsResult; + /// Removes the file at this path - async fn remove_file(&self, path: &str) -> VfsResult<()>; + async fn remove_file(&self, path: &str) -> VfsResult<(), Self::Error>; + /// Removes the directory at this path - async fn remove_dir(&self, path: &str) -> VfsResult<()>; + async fn remove_dir(&self, path: &str) -> VfsResult<(), Self::Error>; + /// Copies the src path to the destination path within the same filesystem (optional) - async fn copy_file(&self, _src: &str, _dest: &str) -> VfsResult<()> { + async fn copy_file(&self, _src: &str, _dest: &str) -> VfsResult<(), Self::Error> { Err(VfsErrorKind::NotSupported.into()) } /// Moves the src path to the destination path within the same filesystem (optional) - async fn move_file(&self, _src: &str, _dest: &str) -> VfsResult<()> { + async fn move_file(&self, _src: &str, _dest: &str) -> VfsResult<(), Self::Error> { Err(VfsErrorKind::NotSupported.into()) } /// Moves the src directory to the destination path within the same filesystem (optional) - async fn move_dir(&self, _src: &str, _dest: &str) -> VfsResult<()> { + async fn move_dir(&self, _src: &str, _dest: &str) -> VfsResult<(), Self::Error> { Err(VfsErrorKind::NotSupported.into()) } } -impl From for AsyncVfsPath { +impl From for AsyncVfsPath +where + E: IoError + Clone + Send + Sync + 'static, + T: AsyncFileSystem, +{ fn from(filesystem: T) -> Self { AsyncVfsPath::new(filesystem) } From 2ed8fd51a3d62b5ef143992463500d46490c33f2 Mon Sep 17 00:00:00 2001 From: Koichi Date: Wed, 25 Jun 2025 23:11:15 +0900 Subject: [PATCH 2/7] fix filesystem trait unpin Signed-off-by: Koichi --- awkernel_async_lib/src/file/filesystem.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/awkernel_async_lib/src/file/filesystem.rs b/awkernel_async_lib/src/file/filesystem.rs index 69a1f4f00..8faefc88c 100644 --- a/awkernel_async_lib/src/file/filesystem.rs +++ b/awkernel_async_lib/src/file/filesystem.rs @@ -97,7 +97,10 @@ pub trait AsyncFileSystem: Sync + Send + 'static { /// Iterates over all direct children of this directory path /// NOTE: the returned String items denote the local bare filenames, i.e. they should not contain "/" anywhere - async fn read_dir(&self, path: &str) -> VfsResult, Self::Error>; + async fn read_dir( + &self, + path: &str, + ) -> VfsResult + Send>, Self::Error>; /// Creates the directory at this path /// @@ -108,19 +111,19 @@ pub trait AsyncFileSystem: Sync + Send + 'static { async fn open_file( &self, path: &str, - ) -> VfsResult + Send>, Self::Error>; + ) -> VfsResult + Send + Unpin>, Self::Error>; /// Creates a file at this path for writing async fn create_file( &self, path: &str, - ) -> VfsResult + Send>, Self::Error>; + ) -> VfsResult + Send + Unpin>, Self::Error>; /// Opens the file at this path for appending async fn append_file( &self, path: &str, - ) -> VfsResult + Send>, Self::Error>; + ) -> VfsResult + Send + Unpin>, Self::Error>; /// Returns the file metadata for the file at this path async fn metadata(&self, path: &str) -> VfsResult; From 27ed5ff4d8477762617e56f22c4e91e90113e433 Mon Sep 17 00:00:00 2001 From: Koichi Date: Thu, 26 Jun 2025 11:18:02 +0900 Subject: [PATCH 3/7] add unpin and delete from for AsyncVfsPath Signed-off-by: Koichi --- awkernel_async_lib/src/file/filesystem.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/awkernel_async_lib/src/file/filesystem.rs b/awkernel_async_lib/src/file/filesystem.rs index 8faefc88c..0414c6df6 100644 --- a/awkernel_async_lib/src/file/filesystem.rs +++ b/awkernel_async_lib/src/file/filesystem.rs @@ -9,7 +9,7 @@ use awkernel_lib::file::{ vfs::error::{VfsError, VfsErrorKind, VfsResult}, vfs::path::VfsMetadata, }; -use futures::stream::BoxStream; +use futures::stream::Stream; #[async_trait] pub trait AsyncSeekAndRead: Send + Unpin { @@ -50,7 +50,7 @@ pub trait AsyncSeekAndWrite: Send + Unpin { } #[async_trait] -impl AsyncSeekAndRead for Box + Send> { +impl AsyncSeekAndRead for Box + Send + Unpin> { async fn read(&mut self, buf: &mut [u8]) -> Result> { (**self).read(buf).await } @@ -65,7 +65,7 @@ impl AsyncSeekAndRead for Box + Se } #[async_trait] -impl AsyncSeekAndWrite for Box + Send> { +impl AsyncSeekAndWrite for Box + Send + Unpin> { async fn write(&mut self, buf: &[u8]) -> Result> { (**self).write(buf).await } @@ -162,13 +162,3 @@ pub trait AsyncFileSystem: Sync + Send + 'static { Err(VfsErrorKind::NotSupported.into()) } } - -impl From for AsyncVfsPath -where - E: IoError + Clone + Send + Sync + 'static, - T: AsyncFileSystem, -{ - fn from(filesystem: T) -> Self { - AsyncVfsPath::new(filesystem) - } -} From 3aa1086fc41ccfd78c72b241bed1de6293e918d5 Mon Sep 17 00:00:00 2001 From: Koichi Date: Thu, 26 Jun 2025 11:28:24 +0900 Subject: [PATCH 4/7] add comments Signed-off-by: Koichi --- awkernel_async_lib/src/file/filesystem.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/awkernel_async_lib/src/file/filesystem.rs b/awkernel_async_lib/src/file/filesystem.rs index 0414c6df6..d5dc6637b 100644 --- a/awkernel_async_lib/src/file/filesystem.rs +++ b/awkernel_async_lib/src/file/filesystem.rs @@ -11,6 +11,8 @@ use awkernel_lib::file::{ }; use futures::stream::Stream; +// TODO(Koichi98): Currently, we provide our own AsyncSeekAndRead and AsyncSeekAndWrite traits. However, we plan to replace these with traits from embedded-io-async crate in the future. Since this migration would entail extensive modifications, we're temporarily maintaining our current, custom implementation. + #[async_trait] pub trait AsyncSeekAndRead: Send + Unpin { async fn read(&mut self, buf: &mut [u8]) -> Result>; From dcdda5d8602f02600ba8b131ac59fbe5c0f8e257 Mon Sep 17 00:00:00 2001 From: Koichi Date: Thu, 26 Jun 2025 11:45:18 +0900 Subject: [PATCH 5/7] fix comments Signed-off-by: Koichi --- awkernel_async_lib/src/file/filesystem.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/awkernel_async_lib/src/file/filesystem.rs b/awkernel_async_lib/src/file/filesystem.rs index d5dc6637b..3ce828b73 100644 --- a/awkernel_async_lib/src/file/filesystem.rs +++ b/awkernel_async_lib/src/file/filesystem.rs @@ -11,8 +11,7 @@ use awkernel_lib::file::{ }; use futures::stream::Stream; -// TODO(Koichi98): Currently, we provide our own AsyncSeekAndRead and AsyncSeekAndWrite traits. However, we plan to replace these with traits from embedded-io-async crate in the future. Since this migration would entail extensive modifications, we're temporarily maintaining our current, custom implementation. - +// 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." #[async_trait] pub trait AsyncSeekAndRead: Send + Unpin { async fn read(&mut self, buf: &mut [u8]) -> Result>; From f3d001bd8e1ea402e1f626cfba1d31e7fb19126d Mon Sep 17 00:00:00 2001 From: Koichi Date: Thu, 26 Jun 2025 11:55:12 +0900 Subject: [PATCH 6/7] fix Signed-off-by: Koichi --- awkernel_async_lib/src/file/filesystem.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/awkernel_async_lib/src/file/filesystem.rs b/awkernel_async_lib/src/file/filesystem.rs index 3ce828b73..9c69058f7 100644 --- a/awkernel_async_lib/src/file/filesystem.rs +++ b/awkernel_async_lib/src/file/filesystem.rs @@ -21,21 +21,13 @@ pub trait AsyncSeekAndRead: Send + Unpin { async fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), VfsError> { while !buf.is_empty() { match self.read(buf).await { - Ok(0) => break, + Ok(0) => return Ok(()), Ok(n) => { buf = &mut buf[n..]; } Err(e) => return Err(e), } } - if !buf.is_empty() { - Err(VfsError::from(VfsErrorKind::from(Error::from( - E::new_unexpected_eof_error(), - ))) - .with_context(|| "failed to fill whole buffer")) - } else { - Ok(()) - } } } From 244deef1dee201488a8defc00be1e3f1e02f62ab Mon Sep 17 00:00:00 2001 From: Koichi Date: Thu, 26 Jun 2025 12:00:49 +0900 Subject: [PATCH 7/7] fix Signed-off-by: Koichi --- awkernel_async_lib/src/file/filesystem.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/awkernel_async_lib/src/file/filesystem.rs b/awkernel_async_lib/src/file/filesystem.rs index 9c69058f7..d855ceb9f 100644 --- a/awkernel_async_lib/src/file/filesystem.rs +++ b/awkernel_async_lib/src/file/filesystem.rs @@ -4,7 +4,7 @@ use crate::time::Time; use alloc::{boxed::Box, string::String}; use async_trait::async_trait; use awkernel_lib::file::{ - error::{Error, IoError}, + error::IoError, io::SeekFrom, vfs::error::{VfsError, VfsErrorKind, VfsResult}, vfs::path::VfsMetadata, @@ -28,6 +28,8 @@ pub trait AsyncSeekAndRead: Send + Unpin { Err(e) => return Err(e), } } + + Ok(()) } }