@@ -76,7 +76,12 @@ use crate::types::*;
7676#[ allow( unused_imports) ]
7777#[ cfg( feature = "semihosting" ) ]
7878use 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
8186pub mod certstore;
8287pub 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
133145pub 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) ]
558565pub 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) ]
569571pub 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) ]
598591pub 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) ]
609600pub 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 {
0 commit comments