@@ -775,14 +775,36 @@ where
775
775
776
776
/// Close a file with the given raw file handle.
777
777
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) ?;
779
780
self . open_files . swap_remove ( file_idx) ;
780
- Ok ( ( ) )
781
+ flush_result
781
782
}
782
783
783
784
/// Flush (update the entry) for a file with the given raw file handle.
784
785
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 ( ( ) )
786
808
}
787
809
788
810
/// Check if any files or folders are open.
@@ -1054,38 +1076,6 @@ where
1054
1076
let available = Block :: LEN - block_offset;
1055
1077
Ok ( ( block_idx, block_offset, available) )
1056
1078
}
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
- }
1089
1079
}
1090
1080
1091
1081
/// Transform mode variants (ReadWriteCreate_Or_Append) to simple modes ReadWriteAppend or
0 commit comments