diff --git a/rcore-fs-ext2/src/lib.rs b/rcore-fs-ext2/src/lib.rs index 68cd187..d1493b3 100644 --- a/rcore-fs-ext2/src/lib.rs +++ b/rcore-fs-ext2/src/lib.rs @@ -8,15 +8,17 @@ extern crate ext2; #[cfg(test)] mod tests; +use alloc::string::String; use alloc::sync::Arc; +use alloc::vec; +use core::ops::Range; +use ext2::error::Error; use ext2::fs::sync::Synced; use ext2::fs::Ext2; -use ext2::error::Error; -use ext2::sector::{Size512, Address}; +use ext2::sector::{Address, Size512}; use ext2::volume::size::Size; use ext2::volume::{Volume, VolumeCommit, VolumeSlice}; -use core::ops::Range; -use rcore_fs::dev::Device; +use rcore_fs::dev::{DevError, Device}; use rcore_fs::vfs; #[derive(Clone)] @@ -33,21 +35,33 @@ pub struct Ext2FileSystem { /// A conversion between vfs::FsError and ext2::Error #[derive(Debug)] struct Ext2Error { - inner: Error + inner: Error, } impl core::convert::From for vfs::FsError { fn from(err: Ext2Error) -> Self { match err.inner { - _ => vfs::FsError::DeviceError + _ => vfs::FsError::DeviceError, } } } +impl core::convert::From for Error { + fn from(err: Ext2Error) -> Self { + err.inner + } +} + impl core::convert::From for Ext2Error { fn from(err: Error) -> Self { + Ext2Error { inner: err } + } +} + +impl core::convert::From for Ext2Error { + fn from(_: DevError) -> Self { Ext2Error { - inner: err + inner: Error::Other(String::from("unknown")), } } } @@ -58,34 +72,45 @@ impl Ext2FileSystem { } fn open_internal(device: Arc) -> Result, Ext2Error> { - let volume = Ext2Volume { - inner: device - }; + let volume = Ext2Volume { inner: device }; let fs = Synced::new(volume.clone())?; - Ok(Arc::new(Ext2FileSystem { - inner: fs, - volume, - })) + Ok(Arc::new(Ext2FileSystem { inner: fs, volume })) } } impl Volume for Ext2Volume { - type Error = Error; + type Error = Ext2Error; fn size(&self) -> Size { Size::Unbounded } - fn commit(&mut self, slice: Option>) -> Result<(), Self::Error> { + fn commit(&mut self, _slice: Option>) -> Result<(), Self::Error> { unimplemented!() } - unsafe fn slice_unchecked<'a>(&'a self, range: Range>) -> VolumeSlice<'a, u8, Size512> { - unimplemented!() + unsafe fn slice_unchecked<'a>( + &'a self, + range: Range>, + ) -> VolumeSlice<'a, u8, Size512> { + let index = range.start; + let len = range.end - range.start; + let mut vec = vec![0; len.into_index() as usize]; + self.inner + .read_at(index.into_index() as usize, vec.as_mut_slice()) + .unwrap(); + VolumeSlice::new_owned(vec, index) } - fn slice<'a>(&'a self, range: Range>) -> Result, Self::Error> { - unimplemented!() + fn slice<'a>( + &'a self, + range: Range>, + ) -> Result, Self::Error> { + let index = range.start; + let len = range.end - range.start; + let mut vec = vec![0; len.into_index() as usize]; + self.inner + .read_at(index.into_index() as usize, vec.as_mut_slice())?; + Ok(VolumeSlice::new_owned(vec, index)) } } - diff --git a/rcore-fs-ext2/src/tests.rs b/rcore-fs-ext2/src/tests.rs index 3e644de..3a63d30 100644 --- a/rcore-fs-ext2/src/tests.rs +++ b/rcore-fs-ext2/src/tests.rs @@ -17,5 +17,5 @@ fn open_sample_file() -> Arc { #[test] fn test_open() { - let fs = open_sample_file(); + let _ = open_sample_file(); }