Skip to content

Commit

Permalink
Use DynFilesystem trait for FS operations
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Dec 15, 2023
1 parent 278cdb5 commit 8244c61
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 175 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ test-attestation-cert-ids = []
[package.metadata.docs.rs]
features = ["serde-extensions", "virt"]
rustdoc-args = ["--cfg", "docsrs"]

[patch.crates-io]
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "ebd27e49ca321089d01d8c9b169c4aeb58ceeeca" }
18 changes: 10 additions & 8 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ pub use crate::pipe::ServiceEndpoint;
use crate::pipe::TrussedResponder;
use crate::platform::*;
pub use crate::store::{
self,
certstore::{Certstore as _, ClientCertstore},
counterstore::{ClientCounterstore, Counterstore as _},
filestore::{ClientFilestore, Filestore, ReadDirFilesState, ReadDirState},
keystore::{ClientKeystore, Keystore},
DynFilesystem,
};
use crate::types::ui::Status;
use crate::types::*;
Expand Down Expand Up @@ -329,14 +331,14 @@ impl<P: Platform> ServiceResources<P> {
Request::DebugDumpStore(_request) => {

info_now!(":: PERSISTENT");
recursively_list(self.platform.store().ifs(), path!("/"));
recursively_list(self.platform.store().fs(Location::Internal), path!("/"));

info_now!(":: VOLATILE");
recursively_list(self.platform.store().vfs(), path!("/"));
recursively_list(self.platform.store().fs(Location::Volatile), path!("/"));

fn recursively_list<S: 'static + crate::types::LfsStorage>(fs: &'static crate::store::Fs<S>, path: &Path) {
fn recursively_list(fs: &dyn DynFilesystem, path: &Path) {
// let fs = store.vfs();
fs.read_dir_and_then(path, |dir| {
fs.read_dir_and_then(path, &mut |dir| {
for (i, entry) in dir.enumerate() {
let entry = entry.unwrap();
if i < 2 {
Expand All @@ -348,7 +350,7 @@ impl<P: Platform> ServiceResources<P> {
recursively_list(fs, entry.path());
}
if entry.file_type().is_file() {
let _contents: Vec<u8, 256> = fs.read(entry.path()).unwrap();
let _contents = fs.read::<256>(entry.path()).unwrap();
// info_now!("{} ?= {}", entry.metadata().len(), contents.len()).ok();
// info_now!("{:?}", &contents).ok();
}
Expand Down Expand Up @@ -882,19 +884,19 @@ impl<P: Platform, D: Dispatch> Service<P, D> {
self.resources
.platform
.store()
.ifs()
.fs(Location::Internal)
.available_blocks()
.unwrap(),
self.resources
.platform
.store()
.efs()
.fs(Location::External)
.available_blocks()
.unwrap(),
self.resources
.platform
.store()
.vfs()
.fs(Location::Volatile)
.available_blocks()
.unwrap(),
);
Expand Down
105 changes: 43 additions & 62 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ use crate::types::*;
#[allow(unused_imports)]
#[cfg(feature = "semihosting")]
use cortex_m_semihosting::hprintln;
use littlefs2::path::Path;
use littlefs2::{
fs::{DirEntry, Metadata},
path::Path,
};

pub use littlefs2::object_safe::{DynFile, DynFilesystem, DynStorage};

pub mod certstore;
pub mod counterstore;
Expand Down Expand Up @@ -128,6 +133,13 @@ pub unsafe trait Store: Copy {
fn ifs(self) -> &'static Fs<Self::I>;
fn efs(self) -> &'static Fs<Self::E>;
fn vfs(self) -> &'static Fs<Self::V>;
fn fs(&self, location: Location) -> &dyn DynFilesystem {
match location {
Location::Internal => self.ifs().fs,
Location::External => self.efs().fs,
Location::Volatile => self.vfs().fs,
}
}
}

pub struct Fs<S: 'static + LfsStorage> {
Expand Down Expand Up @@ -480,7 +492,7 @@ macro_rules! store {
}

// TODO: replace this with "fs.create_dir_all(path.parent())"
pub fn create_directories<S: LfsStorage>(fs: &Filesystem<S>, path: &Path) -> Result<(), Error> {
pub fn create_directories(fs: &dyn DynFilesystem, path: &Path) -> Result<(), Error> {
// hprintln!("preparing {:?}", core::str::from_utf8(path).unwrap()).ok();
let path_bytes = path.as_ref().as_bytes();

Expand Down Expand Up @@ -511,13 +523,11 @@ pub fn read<const N: usize>(
path: &Path,
) -> Result<Bytes<N>, Error> {
debug_now!("reading {}", &path);
match location {
Location::Internal => store.ifs().read(path),
Location::External => store.efs().read(path),
Location::Volatile => store.vfs().read(path),
}
.map(Bytes::from)
.map_err(|_| Error::FilesystemReadFailure)
store
.fs(location)
.read(path)
.map(From::from)
.map_err(|_| Error::FilesystemReadFailure)
}

/// Writes contents to path in location of store.
Expand All @@ -529,12 +539,10 @@ pub fn write(
contents: &[u8],
) -> Result<(), Error> {
debug_now!("writing {}", &path);
match location {
Location::Internal => store.ifs().write(path, contents),
Location::External => store.efs().write(path, contents),
Location::Volatile => store.vfs().write(path, contents),
}
.map_err(|_| Error::FilesystemWriteFailure)
store
.fs(location)
.write(path, contents)
.map_err(|_| Error::FilesystemWriteFailure)
}

/// Creates parent directory if necessary, then writes.
Expand All @@ -546,33 +554,23 @@ pub fn store(
contents: &[u8],
) -> Result<(), Error> {
debug_now!("storing {}", &path);
match location {
Location::Internal => create_directories(store.ifs(), path)?,
Location::External => create_directories(store.efs(), path)?,
Location::Volatile => create_directories(store.vfs(), path)?,
}
write(store, location, path, contents)
create_directories(store.fs(location), path)?;
store
.fs(location)
.write(path, contents)
.map_err(|_| Error::FilesystemWriteFailure)
}

#[inline(never)]
pub fn delete(store: impl Store, location: Location, path: &Path) -> bool {
debug_now!("deleting {}", &path);
let outcome = match location {
Location::Internal => store.ifs().remove(path),
Location::External => store.efs().remove(path),
Location::Volatile => store.vfs().remove(path),
};
outcome.is_ok()
store.fs(location).remove(path).is_ok()
}

#[inline(never)]
pub fn exists(store: impl Store, location: Location, path: &Path) -> bool {
debug_now!("checking existence of {}", &path);
match location {
Location::Internal => path.exists(store.ifs()),
Location::External => path.exists(store.efs()),
Location::Volatile => path.exists(store.vfs()),
}
store.fs(location).exists(path)
}

#[inline(never)]
Expand All @@ -582,12 +580,7 @@ pub fn metadata(
path: &Path,
) -> Result<Option<Metadata>, Error> {
debug_now!("checking existence of {}", &path);
let result = match location {
Location::Internal => store.ifs().metadata(path),
Location::External => store.efs().metadata(path),
Location::Volatile => store.vfs().metadata(path),
};
match result {
match store.fs(location).metadata(path) {
Ok(metadata) => Ok(Some(metadata)),
Err(littlefs2::io::Error::NoSuchEntry) => Ok(None),
Err(_) => Err(Error::FilesystemReadFailure),
Expand All @@ -597,42 +590,30 @@ pub fn metadata(
#[inline(never)]
pub fn rename(store: impl Store, location: Location, from: &Path, to: &Path) -> Result<(), Error> {
debug_now!("renaming {} to {}", &from, &to);
match location {
Location::Internal => store.ifs().rename(from, to),
Location::External => store.efs().rename(from, to),
Location::Volatile => store.vfs().rename(from, to),
}
.map_err(|_| Error::FilesystemWriteFailure)
store
.fs(location)
.rename(from, to)
.map_err(|_| Error::FilesystemWriteFailure)
}

#[inline(never)]
pub fn remove_dir(store: impl Store, location: Location, path: &Path) -> bool {
debug_now!("remove_dir'ing {}", &path);
let outcome = match location {
Location::Internal => store.ifs().remove_dir(path),
Location::External => store.efs().remove_dir(path),
Location::Volatile => store.vfs().remove_dir(path),
};
outcome.is_ok()
store.fs(location).remove_dir(path).is_ok()
}

#[inline(never)]
pub fn remove_dir_all_where<P>(
pub fn remove_dir_all_where(
store: impl Store,
location: Location,
path: &Path,
predicate: P,
) -> Result<usize, Error>
where
P: Fn(&DirEntry) -> bool,
{
predicate: &dyn Fn(&DirEntry) -> bool,
) -> Result<usize, Error> {
debug_now!("remove_dir'ing {}", &path);
let outcome = match location {
Location::Internal => store.ifs().remove_dir_all_where(path, &predicate),
Location::External => store.efs().remove_dir_all_where(path, &predicate),
Location::Volatile => store.vfs().remove_dir_all_where(path, &predicate),
};
outcome.map_err(|_| Error::FilesystemWriteFailure)
store
.fs(location)
.remove_dir_all_where(path, predicate)
.map_err(|_| Error::FilesystemWriteFailure)
}

// pub fn delete_volatile(store: impl Store, handle: &ObjectHandle) -> bool {
Expand Down
5 changes: 1 addition & 4 deletions src/store/certstore.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use littlefs2::{
path,
path::{Path, PathBuf},
};
use littlefs2::{path, path::PathBuf};
use rand_chacha::ChaCha8Rng;

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

use crate::{
Expand Down
Loading

0 comments on commit 8244c61

Please sign in to comment.