@@ -775,14 +775,36 @@ where
775775
776776 /// Close a file with the given raw file handle.
777777 pub fn close_file ( & mut self , file : RawFile ) -> Result < ( ) , Error < D :: Error > > {
778- let file_idx = self . flush_file_get_index ( file) ?;
778+ let flush_result = self . flush_file ( file) ;
779+ let file_idx = self . get_file_by_id ( file) ?;
779780 self . open_files . swap_remove ( file_idx) ;
780- Ok ( ( ) )
781+ flush_result
781782 }
782783
783784 /// Flush (update the entry) for a file with the given raw file handle.
784785 pub fn flush_file ( & mut self , file : RawFile ) -> Result < ( ) , Error < D :: Error > > {
785- self . flush_file_get_index ( file) . map ( |_| ( ) )
786+ let file_info = self
787+ . open_files
788+ . iter ( )
789+ . find ( |info| info. file_id == file)
790+ . ok_or ( Error :: BadHandle ) ?;
791+
792+ if file_info. dirty {
793+ let volume_idx = self . get_volume_by_id ( file_info. volume_id ) ?;
794+ match self . open_volumes [ volume_idx] . volume_type {
795+ VolumeType :: Fat ( ref mut fat) => {
796+ debug ! ( "Updating FAT info sector" ) ;
797+ fat. update_info_sector ( & self . block_device ) ?;
798+ debug ! ( "Updating dir entry {:?}" , file_info. entry) ;
799+ if file_info. entry . size != 0 {
800+ // If you have a length, you must have a cluster
801+ assert ! ( file_info. entry. cluster. 0 != 0 ) ;
802+ }
803+ fat. write_entry_to_disk ( & self . block_device , & file_info. entry ) ?;
804+ }
805+ } ;
806+ }
807+ Ok ( ( ) )
786808 }
787809
788810 /// Check if any files or folders are open.
@@ -1054,38 +1076,6 @@ where
10541076 let available = Block :: LEN - block_offset;
10551077 Ok ( ( block_idx, block_offset, available) )
10561078 }
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- }
10891079}
10901080
10911081/// Transform mode variants (ReadWriteCreate_Or_Append) to simple modes ReadWriteAppend or
0 commit comments