@@ -11,6 +11,7 @@ use trussed::{
1111use trussed_manage:: {
1212 FactoryResetClientReply , FactoryResetClientRequest , FactoryResetDeviceReply ,
1313 FactoryResetDeviceRequest , ManageExtension , ManageReply , ManageRequest ,
14+ FACTORY_RESET_MARKER_FILE ,
1415} ;
1516
1617use crate :: StagingBackend ;
@@ -61,16 +62,34 @@ impl ExtensionImpl<ManageExtension> for StagingBackend {
6162 let store = platform. store ( ) ;
6263
6364 for location in [ Location :: Internal , Location :: External , Location :: Volatile ] {
64- store
65- . fs ( location)
66- . remove_dir_all_where (
67- path ! ( "/" ) ,
68- & callback ( self . manage . should_preserve_file , location) ,
69- )
70- . map_err ( |_err| {
71- debug ! ( "Failed to delete {location:?} fs: {_err:?}" ) ;
72- Error :: FunctionFailed
73- } ) ?;
65+ let fs = store. fs ( location) ;
66+ fs. remove_dir_all_where (
67+ path ! ( "/" ) ,
68+ & callback ( self . manage . should_preserve_file , location) ,
69+ )
70+ . map_err ( |_err| {
71+ debug ! ( "Failed to delete {location:?} fs: {_err:?}" ) ;
72+ Error :: FunctionFailed
73+ } ) ?;
74+ if location == Location :: External {
75+ let is_empty = fs
76+ . read_dir_and_then ( path ! ( "/" ) , & mut |dir| match dir. next ( ) {
77+ Some ( Ok ( _) ) => Ok ( false ) ,
78+ Some ( Err ( err) ) => Err ( err) ,
79+ None => Ok ( true ) ,
80+ } )
81+ . map_err ( |_err| {
82+ debug ! ( "Failed to check emptyness {location:?} fs: {_err:?}" ) ;
83+ Error :: FunctionFailed
84+ } ) ?;
85+ if is_empty {
86+ fs. write ( FACTORY_RESET_MARKER_FILE , & [ ] )
87+ . map_err ( |_err| {
88+ debug ! ( "Failed to write reformat instruction for {location:?} fs: {_err:?}" ) ;
89+ Error :: FunctionFailed
90+ } ) ?;
91+ }
92+ }
7493 }
7594 Ok ( ManageReply :: FactoryResetDevice ( FactoryResetDeviceReply ) )
7695 }
0 commit comments