Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove type parameters from Store #145

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,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 @@ -324,14 +326,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 @@ -343,7 +345,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 @@ -869,19 +871,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
126 changes: 46 additions & 80 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 @@ -122,12 +127,7 @@ pub mod keystore;
//
// This makes everything using it *much* more ergonomic.
pub unsafe trait Store: Copy {
type I: 'static + LfsStorage;
type E: 'static + LfsStorage;
type V: 'static + LfsStorage;
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;
}

pub struct Fs<S: 'static + LfsStorage> {
Expand Down Expand Up @@ -162,18 +162,15 @@ macro_rules! store {
}

unsafe impl $crate::store::Store for $store {
type I = $Ifs;
type E = $Efs;
type V = $Vfs;

fn ifs(self) -> &'static $crate::store::Fs<$Ifs> {
unsafe { &*Self::ifs_ptr() }
}
fn efs(self) -> &'static $crate::store::Fs<$Efs> {
unsafe { &*Self::efs_ptr() }
}
fn vfs(self) -> &'static $crate::store::Fs<$Vfs> {
unsafe { &*Self::vfs_ptr() }
fn fs(&self, location: $crate::types::Location) -> &dyn $crate::store::DynFilesystem {
use core::ops::Deref as _;
unsafe {
match location {
$crate::types::Location::Internal => (*Self::ifs_ptr()).deref(),
$crate::types::Location::External => (*Self::efs_ptr()).deref(),
$crate::types::Location::Volatile => (*Self::vfs_ptr()).deref(),
}
}
}
}

Expand Down Expand Up @@ -480,7 +477,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 +508,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 +524,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 +539,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 +565,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 +575,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
Loading