@@ -76,6 +76,7 @@ use crate::types::*;
7676#[ allow( unused_imports) ]
7777#[ cfg( feature = "semihosting" ) ]
7878use cortex_m_semihosting:: hprintln;
79+ use littlefs2:: fs:: File ;
7980use littlefs2:: path:: Path ;
8081
8182pub mod certstore;
@@ -523,6 +524,38 @@ pub fn read<const N: usize>(
523524 . map_err ( |_| Error :: FilesystemReadFailure )
524525}
525526
527+ pub fn fs_read_chunk < Storage : LfsStorage , const N : usize > (
528+ fs : & Filesystem < Storage > ,
529+ path : & Path ,
530+ pos : OpenSeekFrom ,
531+ ) -> Result < ( Bytes < N > , usize ) , Error > {
532+ let mut contents = Bytes :: default ( ) ;
533+ contents. resize_default ( contents. capacity ( ) ) . unwrap ( ) ;
534+ let file_len = File :: open_and_then ( fs, path, |file| {
535+ file. seek ( pos. into ( ) ) ?;
536+ let read_n = file. read ( & mut contents) ?;
537+ contents. truncate ( read_n) ;
538+ file. len ( )
539+ } )
540+ . map_err ( |_| Error :: FilesystemReadFailure ) ?;
541+ Ok ( ( contents, file_len) )
542+ }
543+ /// Reads contents from path in location of store.
544+ #[ inline( never) ]
545+ pub fn read_chunk < const N : usize > (
546+ store : impl Store ,
547+ location : Location ,
548+ path : & Path ,
549+ pos : OpenSeekFrom ,
550+ ) -> Result < ( Bytes < N > , usize ) , Error > {
551+ debug_now ! ( "reading chunk {},{:?}" , & path, pos) ;
552+ match location {
553+ Location :: Internal => fs_read_chunk ( store. ifs ( ) , path, pos) ,
554+ Location :: External => fs_read_chunk ( store. efs ( ) , path, pos) ,
555+ Location :: Volatile => fs_read_chunk ( store. vfs ( ) , path, pos) ,
556+ }
557+ }
558+
526559/// Writes contents to path in location of store.
527560#[ inline( never) ]
528561pub fn write (
@@ -540,6 +573,39 @@ pub fn write(
540573 . map_err ( |_| Error :: FilesystemWriteFailure )
541574}
542575
576+ pub fn fs_write_chunk < Storage : LfsStorage > (
577+ fs : & Filesystem < Storage > ,
578+ path : & Path ,
579+ contents : & [ u8 ] ,
580+ pos : OpenSeekFrom ,
581+ ) -> Result < ( ) , Error > {
582+ File :: open_and_then ( fs, path, |file| {
583+ use littlefs2:: io:: Write ;
584+ file. seek ( pos. into ( ) ) ?;
585+ file. write_all ( contents)
586+ } )
587+ . map_err ( |_| Error :: FilesystemReadFailure ) ?;
588+ Ok ( ( ) )
589+ }
590+
591+ /// Writes contents to path in location of store.
592+ #[ inline( never) ]
593+ pub fn write_chunk (
594+ store : impl Store ,
595+ location : Location ,
596+ path : & Path ,
597+ contents : & [ u8 ] ,
598+ pos : OpenSeekFrom ,
599+ ) -> Result < ( ) , Error > {
600+ debug_now ! ( "writing {}" , & path) ;
601+ match location {
602+ Location :: Internal => fs_write_chunk ( store. ifs ( ) , path, contents, pos) ,
603+ Location :: External => fs_write_chunk ( store. efs ( ) , path, contents, pos) ,
604+ Location :: Volatile => fs_write_chunk ( store. vfs ( ) , path, contents, pos) ,
605+ }
606+ . map_err ( |_| Error :: FilesystemWriteFailure )
607+ }
608+
543609/// Creates parent directory if necessary, then writes.
544610#[ inline( never) ]
545611pub fn store (
0 commit comments