@@ -773,38 +773,18 @@ where
773773 Ok ( ( ) )
774774 }
775775
776- /// Close a file with the given full path .
776+ /// Close a file with the given raw file handle .
777777 pub fn close_file ( & mut self , file : RawFile ) -> Result < ( ) , Error < D :: Error > > {
778- let mut found_idx = None ;
779- for ( idx, info) in self . open_files . iter ( ) . enumerate ( ) {
780- if file == info. file_id {
781- found_idx = Some ( ( info, idx) ) ;
782- break ;
783- }
784- }
785-
786- let ( file_info, file_idx) = found_idx. ok_or ( Error :: BadHandle ) ?;
787-
788- if file_info. dirty {
789- let volume_idx = self . get_volume_by_id ( file_info. volume_id ) ?;
790- match self . open_volumes [ volume_idx] . volume_type {
791- VolumeType :: Fat ( ref mut fat) => {
792- debug ! ( "Updating FAT info sector" ) ;
793- fat. update_info_sector ( & self . block_device ) ?;
794- debug ! ( "Updating dir entry {:?}" , file_info. entry) ;
795- if file_info. entry . size != 0 {
796- // If you have a length, you must have a cluster
797- assert ! ( file_info. entry. cluster. 0 != 0 ) ;
798- }
799- fat. write_entry_to_disk ( & self . block_device , & file_info. entry ) ?;
800- }
801- } ;
802- }
803-
778+ let file_idx = self . flush_file_get_index ( file) ?;
804779 self . open_files . swap_remove ( file_idx) ;
805780 Ok ( ( ) )
806781 }
807782
783+ /// Flush (update the entry) for a file with the given raw file handle.
784+ pub fn flush_file ( & mut self , file : RawFile ) -> Result < ( ) , Error < D :: Error > > {
785+ self . flush_file_get_index ( file) . map ( |_| ( ) )
786+ }
787+
808788 /// Check if any files or folders are open.
809789 pub fn has_open_handles ( & self ) -> bool {
810790 !( self . open_dirs . is_empty ( ) || self . open_files . is_empty ( ) )
@@ -1074,6 +1054,38 @@ where
10741054 let available = Block :: LEN - block_offset;
10751055 Ok ( ( block_idx, block_offset, available) )
10761056 }
1057+
1058+ /// Flush (update the entry) for a file with the given raw file handle and
1059+ /// get its index.
1060+ fn flush_file_get_index ( & mut self , file : RawFile ) -> Result < usize , Error < D :: Error > > {
1061+ let mut found_idx = None ;
1062+ for ( idx, info) in self . open_files . iter ( ) . enumerate ( ) {
1063+ if file == info. file_id {
1064+ found_idx = Some ( ( info, idx) ) ;
1065+ break ;
1066+ }
1067+ }
1068+
1069+ let ( file_info, file_idx) = found_idx. ok_or ( Error :: BadHandle ) ?;
1070+
1071+ if file_info. dirty {
1072+ let volume_idx = self . get_volume_by_id ( file_info. volume_id ) ?;
1073+ match self . open_volumes [ volume_idx] . volume_type {
1074+ VolumeType :: Fat ( ref mut fat) => {
1075+ debug ! ( "Updating FAT info sector" ) ;
1076+ fat. update_info_sector ( & self . block_device ) ?;
1077+ debug ! ( "Updating dir entry {:?}" , file_info. entry) ;
1078+ if file_info. entry . size != 0 {
1079+ // If you have a length, you must have a cluster
1080+ assert ! ( file_info. entry. cluster. 0 != 0 ) ;
1081+ }
1082+ fat. write_entry_to_disk ( & self . block_device , & file_info. entry ) ?;
1083+ }
1084+ } ;
1085+ }
1086+
1087+ Ok ( file_idx)
1088+ }
10771089}
10781090
10791091/// Transform mode variants (ReadWriteCreate_Or_Append) to simple modes ReadWriteAppend or
0 commit comments