diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c5aab73..a308c6ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Removed `Path::from_bytes_with_nul_unchecked`. Use `CStr::from_bytes_with_nul_unchecked` and `Path::from_cstr_unchecked` instead. - Removed `From for littlefs2::io::Error`. - Removed `object_safe::OpenOptionsCallback`. +- Removed `consts::ATTRBYTES_MAX_TYPE`. [#47]: https://github.com/trussed-dev/littlefs2/pull/47 [#57]: https://github.com/trussed-dev/littlefs2/pull/57 diff --git a/core/Cargo.toml b/core/Cargo.toml index 8d94d123..cf30dae9 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -10,7 +10,6 @@ repository.workspace = true [dependencies] bitflags = "2.6.0" -generic-array = "0.14" heapless = "0.7" serde = { version = "1", default-features = false, features = ["derive"], optional = true } diff --git a/core/src/consts.rs b/core/src/consts.rs index 569bc1c9..38c20268 100644 --- a/core/src/consts.rs +++ b/core/src/consts.rs @@ -1,5 +1,3 @@ pub const PATH_MAX: usize = 255; pub const PATH_MAX_PLUS_ONE: usize = PATH_MAX + 1; pub const ATTRBYTES_MAX: u32 = 1_022; -#[allow(non_camel_case_types)] -pub type ATTRBYTES_MAX_TYPE = generic_array::typenum::consts::U1022; diff --git a/core/src/fs.rs b/core/src/fs.rs index c4caa76b..df78bc8c 100644 --- a/core/src/fs.rs +++ b/core/src/fs.rs @@ -4,8 +4,6 @@ use bitflags::bitflags; use crate::path::{Path, PathBuf}; -pub type Bytes = generic_array::GenericArray; - bitflags! { /// Definition of file open flags which can be mixed and matched as appropriate. These definitions /// are reminiscent of the ones defined by POSIX. @@ -92,15 +90,16 @@ impl Metadata { /// [`Filesystem::clear_attribute`](struct.Filesystem.html#method.clear_attribute). pub struct Attribute { id: u8, - pub data: Bytes, - pub size: usize, + data: [u8; crate::consts::ATTRBYTES_MAX as _], + // invariant: size <= data.len() + size: usize, } impl Attribute { pub fn new(id: u8) -> Self { Attribute { id, - data: Default::default(), + data: [0; crate::consts::ATTRBYTES_MAX as _], size: 0, } } @@ -115,6 +114,10 @@ impl Attribute { &self.data[..len] } + pub fn size(&self) -> usize { + self.size + } + pub fn set_data(&mut self, data: &[u8]) -> &mut Self { let attr_max = crate::consts::ATTRBYTES_MAX as _; let len = cmp::min(attr_max, data.len()); @@ -125,6 +128,14 @@ impl Attribute { } self } + + pub fn buffer_mut(&mut self) -> &mut [u8] { + &mut self.data + } + + pub fn set_size(&mut self, size: usize) { + self.size = cmp::min(size, self.data.len()); + } } #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/core/src/lib.rs b/core/src/lib.rs index f9fdac86..e7e8d90a 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -12,7 +12,7 @@ mod io; mod object_safe; mod path; -pub use consts::{ATTRBYTES_MAX, ATTRBYTES_MAX_TYPE, PATH_MAX, PATH_MAX_PLUS_ONE}; +pub use consts::{ATTRBYTES_MAX, PATH_MAX, PATH_MAX_PLUS_ONE}; pub use fs::{Attribute, DirEntry, FileOpenFlags, FileType, Metadata}; pub use io::{Error, OpenSeekFrom, Read, Result, Seek, SeekFrom, Write}; pub use object_safe::{DirEntriesCallback, DynFile, DynFilesystem, FileCallback, Predicate}; diff --git a/src/consts.rs b/src/consts.rs index 5d11bb7c..554162c8 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -3,7 +3,7 @@ /// Re-export of `typenum::consts`. pub use generic_array::typenum::consts::*; -pub use littlefs2_core::{ATTRBYTES_MAX, ATTRBYTES_MAX_TYPE, PATH_MAX, PATH_MAX_PLUS_ONE}; +pub use littlefs2_core::{ATTRBYTES_MAX, PATH_MAX, PATH_MAX_PLUS_ONE}; pub const FILENAME_MAX_PLUS_ONE: u32 = 255 + 1; pub const FILEBYTES_MAX: u32 = crate::ll::LFS_FILE_MAX as _; diff --git a/src/fs.rs b/src/fs.rs index 74096d51..a182d2d1 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -5,7 +5,7 @@ use core::ptr::addr_of; use core::ptr::addr_of_mut; use core::{ cell::{RefCell, UnsafeCell}, - cmp, mem, slice, + mem, slice, }; use generic_array::typenum::marker_traits::Unsigned; use littlefs2_sys as ll; @@ -425,13 +425,13 @@ impl Filesystem<'_, Storage> { &mut self.alloc.borrow_mut().state, path.as_ptr(), id, - &mut attribute.data as *mut _ as *mut c_void, + attribute.buffer_mut() as *mut _ as *mut c_void, attr_max, ) }; if return_code >= 0 { - attribute.size = cmp::min(attr_max, return_code as u32) as usize; + attribute.set_size(return_code as usize); return Ok(Some(attribute)); } if return_code == ll::lfs_error_LFS_ERR_NOATTR { @@ -457,8 +457,8 @@ impl Filesystem<'_, Storage> { &mut self.alloc.borrow_mut().state, path.as_ptr(), attribute.id(), - &attribute.data as *const _ as *const c_void, - attribute.size as u32, + attribute.data() as *const _ as *const c_void, + attribute.size() as u32, ) };