Skip to content

Commit

Permalink
Update littlefs2 to v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Oct 25, 2024
1 parent 1a54da2 commit 046478b
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 91 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ delog = "0.1.0"
cbor-smol = { version = "0.5", features = ["heapless-bytes-v0-3"] }
heapless-bytes = { version = "0.3.0" }
interchange = "0.3.0"
littlefs2 = "0.4.0"
littlefs2 = "0.5.0"
littlefs2-core = { version = "0.1", features = ["heapless-bytes03"] }
p256-cortex-m4 = { version = "0.1.0-alpha.6", features = ["prehash", "sec1-signatures"] }
salty = { version = "0.3.0", features = ["cose"] }
serde-indexed = "0.1.0"
Expand Down Expand Up @@ -145,6 +146,3 @@ test-attestation-cert-ids = []
[package.metadata.docs.rs]
features = ["serde-extensions", "virt"]
rustdoc-args = ["--cfg", "docsrs"]

[patch.crates-io]
littlefs2 = { git = "https://github.com/sosthene-nitrokey/littlefs2.git", rev = "2b45a7559ff44260c6dd693e4cb61f54ae5efc53" }
18 changes: 7 additions & 11 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use littlefs2::{
object_safe::DynFilesystem,
path,
path::{Path, PathBuf},
};
use littlefs2_core::{path, DynFilesystem, Path, PathBuf};
use rand_chacha::ChaCha8Rng;
pub use rand_core::{RngCore, SeedableRng};

Expand Down Expand Up @@ -122,7 +118,7 @@ impl<P: Platform> ServiceResources<P> {
}

pub fn trussed_filestore(&mut self) -> ClientFilestore<P::S> {
ClientFilestore::new(PathBuf::from("trussed"), self.platform.store())
ClientFilestore::new(PathBuf::from(path!("trussed")), self.platform.store())
}

pub fn keystore(&mut self, client_id: PathBuf) -> Result<ClientKeystore<P::S>> {
Expand Down Expand Up @@ -182,7 +178,7 @@ impl<P: Platform> ServiceResources<P> {
#[cfg(feature = "crypto-client-attest")]
Request::Attest(request) => {
let mut attn_keystore: ClientKeystore<P::S> = ClientKeystore::new(
PathBuf::from("attn"),
PathBuf::from(path!("attn")),
self.rng().map_err(|_| Error::EntropyMalfunction)?,
full_store,
);
Expand Down Expand Up @@ -419,7 +415,7 @@ impl<P: Platform> ServiceResources<P> {
recursively_list(fs, entry.path());
}
if entry.file_type().is_file() {
let _contents = fs.read::<256>(entry.path()).unwrap();
let _contents = fs.read::<Bytes<256>>(entry.path()).unwrap();
// info_now!("{} ?= {}", entry.metadata().len(), contents.len()).ok();
// info_now!("{:?}", &contents).ok();
}
Expand Down Expand Up @@ -858,7 +854,7 @@ impl<P: Platform> Service<P> {
syscall: S,
interrupt: Option<&'static InterruptFlag>,
) -> Result<ClientImplementation<S>, Error> {
ClientBuilder::new(client_id)
ClientBuilder::new(PathBuf::try_from(client_id).unwrap())
.interrupt(interrupt)
.prepare(self)
.map(|p| p.build(syscall))
Expand All @@ -872,7 +868,7 @@ impl<P: Platform> Service<P> {
client_id: &str,
interrupt: Option<&'static InterruptFlag>,
) -> Result<ClientImplementation<&mut Self>, Error> {
ClientBuilder::new(client_id)
ClientBuilder::new(PathBuf::try_from(client_id).unwrap())
.interrupt(interrupt)
.prepare(self)
.map(|p| p.build(self))
Expand All @@ -885,7 +881,7 @@ impl<P: Platform> Service<P> {
client_id: &str,
interrupt: Option<&'static InterruptFlag>,
) -> Result<ClientImplementation<Self>, Error> {
ClientBuilder::new(client_id)
ClientBuilder::new(PathBuf::try_from(client_id).unwrap())
.interrupt(interrupt)
.prepare(&mut self)
.map(|p| p.build(self))
Expand Down
3 changes: 1 addition & 2 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ pub fn read<const N: usize>(
store
.fs(location)
.read(path)
.map(From::from)
.map_err(|_| Error::FilesystemReadFailure)
}

Expand Down Expand Up @@ -567,7 +566,7 @@ pub fn metadata(
debug_now!("checking existence of {}", &path);
match store.fs(location).metadata(path) {
Ok(metadata) => Ok(Some(metadata)),
Err(littlefs2::io::Error::NoSuchEntry) => Ok(None),
Err(littlefs2::io::Error::NO_SUCH_ENTRY) => Ok(None),
Err(_) => Err(Error::FilesystemReadFailure),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/certstore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use littlefs2::{path, path::PathBuf};
use littlefs2_core::{path, PathBuf};
use rand_chacha::ChaCha8Rng;

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/store/counterstore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use littlefs2::{path, path::PathBuf};
use littlefs2_core::{path, PathBuf};
use rand_chacha::ChaCha8Rng;

use crate::{
Expand Down
33 changes: 17 additions & 16 deletions src/store/filestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
types::{Location, Message, UserAttribute},
Bytes,
};
use littlefs2::path;
use littlefs2_core::{path, DirEntry, Metadata, Path, PathBuf};

#[derive(Clone)]
pub struct ReadDirState {
Expand All @@ -25,11 +25,6 @@ pub struct ReadDirFilesState {
user_attribute: Option<UserAttribute>,
}

use littlefs2::{
fs::{DirEntry, Metadata},
path::{Path, PathBuf},
};

pub struct ClientFilestore<S>
where
S: Store,
Expand Down Expand Up @@ -81,7 +76,7 @@ impl<S: Store> ClientFilestore<S> {
// oh oh oh
.unwrap();
let dat_offset = "/dat/".len();
PathBuf::from(&bytes[end_of_namespace + 1 + offset + dat_offset..])
PathBuf::try_from(&bytes[end_of_namespace + 1 + offset + dat_offset..]).unwrap()
}
}

Expand Down Expand Up @@ -204,7 +199,7 @@ impl<S: Store> ClientFilestore<S> {
// `read_dir_and_then` wants to see Results (although we naturally have an Option
// at this point)
})
.ok_or(littlefs2::io::Error::Io)
.ok_or(littlefs2_core::Error::IO)
})
.ok())
}
Expand Down Expand Up @@ -241,7 +236,7 @@ impl<S: Store> ClientFilestore<S> {

(entry, read_dir_state)
})
.ok_or(littlefs2::io::Error::Io)
.ok_or(littlefs2_core::Error::IO)
})
.ok())
}
Expand All @@ -268,14 +263,17 @@ impl<S: Store> ClientFilestore<S> {
// take first entry that meets requirements
.find(|(_, entry)| {
if let Some(user_attribute) = user_attribute.as_ref() {
let mut buffer = UserAttribute::new();
buffer.resize_to_capacity();
let mut path = dir.clone();
path.push(entry.file_name());
let attribute = fs
.attribute(&path, crate::config::USER_ATTRIBUTE_NUMBER)
.attribute(&path, crate::config::USER_ATTRIBUTE_NUMBER, &mut buffer)
.unwrap();

if let Some(attribute) = attribute {
user_attribute == attribute.data()
user_attribute.len() == attribute.total_size()
&& user_attribute == attribute.data()
} else {
false
}
Expand All @@ -295,7 +293,7 @@ impl<S: Store> ClientFilestore<S> {
// `read_dir_and_then` wants to see Results (although we naturally have an Option
// at this point)
})
.ok_or(littlefs2::io::Error::Io)
.ok_or(littlefs2_core::Error::IO)
})
.ok()
.map(|(i, data)| {
Expand Down Expand Up @@ -335,13 +333,16 @@ impl<S: Store> ClientFilestore<S> {
// take first entry that meets requirements
.find(|(_, entry)| {
if let Some(user_attribute) = user_attribute.as_ref() {
let mut buffer = UserAttribute::new();
buffer.resize_to_capacity();
let mut path = real_dir.clone();
path.push(entry.file_name());
let attribute = fs
.attribute(&path, crate::config::USER_ATTRIBUTE_NUMBER)
.attribute(&path, crate::config::USER_ATTRIBUTE_NUMBER, &mut buffer)
.unwrap();
if let Some(attribute) = attribute {
user_attribute == attribute.data()
user_attribute.len() == attribute.total_size()
&& user_attribute == attribute.data()
} else {
false
}
Expand All @@ -355,7 +356,7 @@ impl<S: Store> ClientFilestore<S> {
(i, data)
})
// convert Option into Result, again because `read_dir_and_then` expects this
.ok_or(littlefs2::io::Error::Io)
.ok_or(littlefs2_core::Error::IO)
})
.ok()
.map(|(i, data)| {
Expand Down Expand Up @@ -503,7 +504,7 @@ impl<S: Store> Filestore for ClientFilestore<S> {
}
})
.next()
.ok_or(littlefs2::io::Error::Io)
.ok_or(littlefs2_core::Error::IO)
})
.ok()
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/keystore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use littlefs2::{path, path::PathBuf};
use littlefs2_core::{path, PathBuf};
use rand_chacha::ChaCha8Rng;

use crate::{
Expand Down
25 changes: 11 additions & 14 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use littlefs2::const_ram_storage;
use littlefs2::driver::Storage as LfsStorage;
use littlefs2::fs::{Allocation, Filesystem};
use littlefs2::io::Result as LfsResult;
use littlefs2_core::path;
use rand_core::{CryptoRng, RngCore};

#[cfg(any(feature = "p256", feature = "p384", feature = "p521",))]
Expand Down Expand Up @@ -172,7 +173,7 @@ macro_rules! setup {
let (test_trussed_requester, test_trussed_responder) = crate::pipe::TRUSSED_INTERCHANGE
.claim()
.expect("could not setup TEST TrussedInterchange");
let test_client_id = "TEST";
let test_client_id = path!("TEST");

assert!(trussed
.add_endpoint(test_trussed_responder, test_client_id, &[], None)
Expand Down Expand Up @@ -873,35 +874,31 @@ fn rng() {
#[test]
#[serial]
fn filesystem() {
let path = PathBuf::from(path!("test_file"));
setup!(client);

assert!(block!(client
.entry_metadata(Location::Internal, PathBuf::from("test_file"))
.entry_metadata(Location::Internal, path.clone())
.expect("no client error"))
.expect("no errors")
.metadata
.is_none(),);

let data = Bytes::from_slice(&[0; 20]).unwrap();
block!(client
.write_file(
Location::Internal,
PathBuf::from("test_file"),
data.clone(),
None,
)
.write_file(Location::Internal, path.clone(), data.clone(), None,)
.expect("no client error"))
.expect("no errors");

let recv_data = block!(client
.read_file(Location::Internal, PathBuf::from("test_file"))
.read_file(Location::Internal, path.clone())
.expect("no client error"))
.expect("no errors")
.data;
assert_eq!(data, recv_data);

let metadata = block!(client
.entry_metadata(Location::Internal, PathBuf::from("test_file"))
.entry_metadata(Location::Internal, path.clone())
.expect("no client error"))
.expect("no errors")
.metadata
Expand All @@ -910,23 +907,23 @@ fn filesystem() {

// This returns an error because the name doesn't exist
block!(client
.remove_file(Location::Internal, PathBuf::from("bad_name"))
.remove_file(Location::Internal, path!("bad_name").into())
.expect("no client error"))
.ok();
let metadata = block!(client
.entry_metadata(Location::Internal, PathBuf::from("test_file"))
.entry_metadata(Location::Internal, path.clone())
.expect("no client error"))
.expect("no errors")
.metadata
.unwrap();
assert!(metadata.is_file());

block!(client
.remove_file(Location::Internal, PathBuf::from("test_file"))
.remove_file(Location::Internal, path.clone())
.expect("no client error"))
.expect("no errors");
assert!(block!(client
.entry_metadata(Location::Internal, PathBuf::from("test_file"))
.entry_metadata(Location::Internal, path.clone())
.expect("no client error"))
.expect("no errors")
.metadata
Expand Down
12 changes: 4 additions & 8 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ pub use heapless::{String, Vec};

pub use crate::Bytes;

pub use littlefs2::{
driver::Storage as LfsStorage,
fs::{DirEntry, Filesystem, Metadata},
io::Result as LfsResult,
path::{Path, PathBuf},
};
pub use littlefs2::{driver::Storage as LfsStorage, fs::Filesystem};
pub use littlefs2_core::{DirEntry, Metadata, Path, PathBuf, Result as LfsResult};

use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -86,7 +82,7 @@ impl Id {
}

pub fn hex_path(&self) -> PathBuf {
PathBuf::from(self.hex().as_slice())
PathBuf::try_from(self.hex().as_slice()).unwrap()
}

/// skips leading zeros
Expand Down Expand Up @@ -289,7 +285,7 @@ impl From<PathBuf> for CoreContext {

impl From<&str> for CoreContext {
fn from(s: &str) -> Self {
Self::new(s.into())
Self::new(s.try_into().unwrap())
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<S: StoreProvider> Platform<S> {
test: impl FnOnce(Client<S, D>) -> R,
) -> R {
let mut service = Service::with_dispatch(self, dispatch);
let client = ClientBuilder::new(client_id)
let client = ClientBuilder::new(littlefs2::path::PathBuf::try_from(client_id).unwrap())
.backends(backends)
.prepare(&mut service)
.unwrap()
Expand All @@ -157,8 +157,11 @@ impl<S: StoreProvider> Platform<S> {
test: impl FnOnce([MultiClient<S>; N]) -> R,
) -> R {
let mut service = Service::new(self);
let prepared_clients =
client_ids.map(|id| ClientBuilder::new(id).prepare(&mut service).unwrap());
let prepared_clients = client_ids.map(|id| {
ClientBuilder::new(littlefs2::path::PathBuf::try_from(id).unwrap())
.prepare(&mut service)
.unwrap()
});
let service = Arc::new(Mutex::new(service));
test(prepared_clients.map(|builder| builder.build(service.clone())))
}
Expand All @@ -172,7 +175,7 @@ impl<S: StoreProvider> Platform<S> {
) -> R {
let mut service = Service::with_dispatch(self, dispatch);
let prepared_clients = client_ids.map(|(id, backends)| {
ClientBuilder::new(id)
ClientBuilder::new(littlefs2::path::PathBuf::try_from(id).unwrap())
.backends(backends)
.prepare(&mut service)
.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions src/virt/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Storage for FilesystemStorage {
fn write(&mut self, offset: usize, data: &[u8]) -> LfsResult<usize> {
debug!("write: offset: {}, len: {}", offset, data.len());
if offset + data.len() > STORAGE_SIZE {
return Err(littlefs2::io::Error::NoSpace);
return Err(littlefs2::io::Error::NO_SPACE);
}
let mut file = OpenOptions::new().write(true).open(&self.0).unwrap();
file.seek(SeekFrom::Start(offset as _)).unwrap();
Expand All @@ -79,7 +79,7 @@ impl Storage for FilesystemStorage {
fn erase(&mut self, offset: usize, len: usize) -> LfsResult<usize> {
debug!("erase: offset: {}, len: {}", offset, len);
if offset + len > STORAGE_SIZE {
return Err(littlefs2::io::Error::NoSpace);
return Err(littlefs2::io::Error::NO_SPACE);
}
let mut file = OpenOptions::new().write(true).open(&self.0).unwrap();
file.seek(SeekFrom::Start(offset as _)).unwrap();
Expand Down
Loading

0 comments on commit 046478b

Please sign in to comment.