Skip to content

Commit 45ed62b

Browse files
Use DynFilesystem trait for FS operations
1 parent 278cdb5 commit 45ed62b

File tree

8 files changed

+113
-175
lines changed

8 files changed

+113
-175
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4141
- Changed `&PathBuf` to `&Path` where possible.
4242
- Put `CounterClient` and `CryptoClient::attest` behind feature flags (enabled
4343
by default).
44+
- Change store implementations to use littlefs2’s `DynFilesystem` trait instead
45+
of being generic over the storage implementation.
4446

4547
### Fixed
4648

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,6 @@ test-attestation-cert-ids = []
131131
[package.metadata.docs.rs]
132132
features = ["serde-extensions", "virt"]
133133
rustdoc-args = ["--cfg", "docsrs"]
134+
135+
[patch.crates-io]
136+
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "ebd27e49ca321089d01d8c9b169c4aeb58ceeeca" }

src/service.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ pub use crate::pipe::ServiceEndpoint;
1515
use crate::pipe::TrussedResponder;
1616
use crate::platform::*;
1717
pub use crate::store::{
18+
self,
1819
certstore::{Certstore as _, ClientCertstore},
1920
counterstore::{ClientCounterstore, Counterstore as _},
2021
filestore::{ClientFilestore, Filestore, ReadDirFilesState, ReadDirState},
2122
keystore::{ClientKeystore, Keystore},
23+
DynFilesystem,
2224
};
2325
use crate::types::ui::Status;
2426
use crate::types::*;
@@ -329,14 +331,14 @@ impl<P: Platform> ServiceResources<P> {
329331
Request::DebugDumpStore(_request) => {
330332

331333
info_now!(":: PERSISTENT");
332-
recursively_list(self.platform.store().ifs(), path!("/"));
334+
recursively_list(self.platform.store().fs(Location::Internal), path!("/"));
333335

334336
info_now!(":: VOLATILE");
335-
recursively_list(self.platform.store().vfs(), path!("/"));
337+
recursively_list(self.platform.store().fs(Location::Volatile), path!("/"));
336338

337-
fn recursively_list<S: 'static + crate::types::LfsStorage>(fs: &'static crate::store::Fs<S>, path: &Path) {
339+
fn recursively_list(fs: &dyn DynFilesystem, path: &Path) {
338340
// let fs = store.vfs();
339-
fs.read_dir_and_then(path, |dir| {
341+
fs.read_dir_and_then(path, &mut |dir| {
340342
for (i, entry) in dir.enumerate() {
341343
let entry = entry.unwrap();
342344
if i < 2 {
@@ -348,7 +350,7 @@ impl<P: Platform> ServiceResources<P> {
348350
recursively_list(fs, entry.path());
349351
}
350352
if entry.file_type().is_file() {
351-
let _contents: Vec<u8, 256> = fs.read(entry.path()).unwrap();
353+
let _contents = fs.read::<256>(entry.path()).unwrap();
352354
// info_now!("{} ?= {}", entry.metadata().len(), contents.len()).ok();
353355
// info_now!("{:?}", &contents).ok();
354356
}
@@ -882,19 +884,19 @@ impl<P: Platform, D: Dispatch> Service<P, D> {
882884
self.resources
883885
.platform
884886
.store()
885-
.ifs()
887+
.fs(Location::Internal)
886888
.available_blocks()
887889
.unwrap(),
888890
self.resources
889891
.platform
890892
.store()
891-
.efs()
893+
.fs(Location::External)
892894
.available_blocks()
893895
.unwrap(),
894896
self.resources
895897
.platform
896898
.store()
897-
.vfs()
899+
.fs(Location::Volatile)
898900
.available_blocks()
899901
.unwrap(),
900902
);

src/store.rs

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ use crate::types::*;
7676
#[allow(unused_imports)]
7777
#[cfg(feature = "semihosting")]
7878
use cortex_m_semihosting::hprintln;
79-
use littlefs2::path::Path;
79+
use littlefs2::{
80+
fs::{DirEntry, Metadata},
81+
path::Path,
82+
};
83+
84+
pub use littlefs2::object_safe::{DynFile, DynFilesystem, DynStorage};
8085

8186
pub mod certstore;
8287
pub mod counterstore;
@@ -128,6 +133,13 @@ pub unsafe trait Store: Copy {
128133
fn ifs(self) -> &'static Fs<Self::I>;
129134
fn efs(self) -> &'static Fs<Self::E>;
130135
fn vfs(self) -> &'static Fs<Self::V>;
136+
fn fs(&self, location: Location) -> &dyn DynFilesystem {
137+
match location {
138+
Location::Internal => self.ifs().fs,
139+
Location::External => self.efs().fs,
140+
Location::Volatile => self.vfs().fs,
141+
}
142+
}
131143
}
132144

133145
pub struct Fs<S: 'static + LfsStorage> {
@@ -480,7 +492,7 @@ macro_rules! store {
480492
}
481493

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

@@ -511,13 +523,11 @@ pub fn read<const N: usize>(
511523
path: &Path,
512524
) -> Result<Bytes<N>, Error> {
513525
debug_now!("reading {}", &path);
514-
match location {
515-
Location::Internal => store.ifs().read(path),
516-
Location::External => store.efs().read(path),
517-
Location::Volatile => store.vfs().read(path),
518-
}
519-
.map(Bytes::from)
520-
.map_err(|_| Error::FilesystemReadFailure)
526+
store
527+
.fs(location)
528+
.read(path)
529+
.map(From::from)
530+
.map_err(|_| Error::FilesystemReadFailure)
521531
}
522532

523533
/// Writes contents to path in location of store.
@@ -529,12 +539,10 @@ pub fn write(
529539
contents: &[u8],
530540
) -> Result<(), Error> {
531541
debug_now!("writing {}", &path);
532-
match location {
533-
Location::Internal => store.ifs().write(path, contents),
534-
Location::External => store.efs().write(path, contents),
535-
Location::Volatile => store.vfs().write(path, contents),
536-
}
537-
.map_err(|_| Error::FilesystemWriteFailure)
542+
store
543+
.fs(location)
544+
.write(path, contents)
545+
.map_err(|_| Error::FilesystemWriteFailure)
538546
}
539547

540548
/// Creates parent directory if necessary, then writes.
@@ -546,33 +554,23 @@ pub fn store(
546554
contents: &[u8],
547555
) -> Result<(), Error> {
548556
debug_now!("storing {}", &path);
549-
match location {
550-
Location::Internal => create_directories(store.ifs(), path)?,
551-
Location::External => create_directories(store.efs(), path)?,
552-
Location::Volatile => create_directories(store.vfs(), path)?,
553-
}
554-
write(store, location, path, contents)
557+
create_directories(store.fs(location), path)?;
558+
store
559+
.fs(location)
560+
.write(path, contents)
561+
.map_err(|_| Error::FilesystemWriteFailure)
555562
}
556563

557564
#[inline(never)]
558565
pub fn delete(store: impl Store, location: Location, path: &Path) -> bool {
559566
debug_now!("deleting {}", &path);
560-
let outcome = match location {
561-
Location::Internal => store.ifs().remove(path),
562-
Location::External => store.efs().remove(path),
563-
Location::Volatile => store.vfs().remove(path),
564-
};
565-
outcome.is_ok()
567+
store.fs(location).remove(path).is_ok()
566568
}
567569

568570
#[inline(never)]
569571
pub fn exists(store: impl Store, location: Location, path: &Path) -> bool {
570572
debug_now!("checking existence of {}", &path);
571-
match location {
572-
Location::Internal => path.exists(store.ifs()),
573-
Location::External => path.exists(store.efs()),
574-
Location::Volatile => path.exists(store.vfs()),
575-
}
573+
store.fs(location).exists(path)
576574
}
577575

578576
#[inline(never)]
@@ -582,12 +580,7 @@ pub fn metadata(
582580
path: &Path,
583581
) -> Result<Option<Metadata>, Error> {
584582
debug_now!("checking existence of {}", &path);
585-
let result = match location {
586-
Location::Internal => store.ifs().metadata(path),
587-
Location::External => store.efs().metadata(path),
588-
Location::Volatile => store.vfs().metadata(path),
589-
};
590-
match result {
583+
match store.fs(location).metadata(path) {
591584
Ok(metadata) => Ok(Some(metadata)),
592585
Err(littlefs2::io::Error::NoSuchEntry) => Ok(None),
593586
Err(_) => Err(Error::FilesystemReadFailure),
@@ -597,42 +590,30 @@ pub fn metadata(
597590
#[inline(never)]
598591
pub fn rename(store: impl Store, location: Location, from: &Path, to: &Path) -> Result<(), Error> {
599592
debug_now!("renaming {} to {}", &from, &to);
600-
match location {
601-
Location::Internal => store.ifs().rename(from, to),
602-
Location::External => store.efs().rename(from, to),
603-
Location::Volatile => store.vfs().rename(from, to),
604-
}
605-
.map_err(|_| Error::FilesystemWriteFailure)
593+
store
594+
.fs(location)
595+
.rename(from, to)
596+
.map_err(|_| Error::FilesystemWriteFailure)
606597
}
607598

608599
#[inline(never)]
609600
pub fn remove_dir(store: impl Store, location: Location, path: &Path) -> bool {
610601
debug_now!("remove_dir'ing {}", &path);
611-
let outcome = match location {
612-
Location::Internal => store.ifs().remove_dir(path),
613-
Location::External => store.efs().remove_dir(path),
614-
Location::Volatile => store.vfs().remove_dir(path),
615-
};
616-
outcome.is_ok()
602+
store.fs(location).remove_dir(path).is_ok()
617603
}
618604

619605
#[inline(never)]
620-
pub fn remove_dir_all_where<P>(
606+
pub fn remove_dir_all_where(
621607
store: impl Store,
622608
location: Location,
623609
path: &Path,
624-
predicate: P,
625-
) -> Result<usize, Error>
626-
where
627-
P: Fn(&DirEntry) -> bool,
628-
{
610+
predicate: &dyn Fn(&DirEntry) -> bool,
611+
) -> Result<usize, Error> {
629612
debug_now!("remove_dir'ing {}", &path);
630-
let outcome = match location {
631-
Location::Internal => store.ifs().remove_dir_all_where(path, &predicate),
632-
Location::External => store.efs().remove_dir_all_where(path, &predicate),
633-
Location::Volatile => store.vfs().remove_dir_all_where(path, &predicate),
634-
};
635-
outcome.map_err(|_| Error::FilesystemWriteFailure)
613+
store
614+
.fs(location)
615+
.remove_dir_all_where(path, predicate)
616+
.map_err(|_| Error::FilesystemWriteFailure)
636617
}
637618

638619
// pub fn delete_volatile(store: impl Store, handle: &ObjectHandle) -> bool {

src/store/certstore.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use littlefs2::{
2-
path,
3-
path::{Path, PathBuf},
4-
};
1+
use littlefs2::{path, path::PathBuf};
52
use rand_chacha::ChaCha8Rng;
63

74
use crate::{

src/store/counterstore.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use littlefs2::{
2-
path,
3-
path::{Path, PathBuf},
4-
};
1+
use littlefs2::{path, path::PathBuf};
52
use rand_chacha::ChaCha8Rng;
63

74
use crate::{

0 commit comments

Comments
 (0)