@@ -773,38 +773,18 @@ where
773
773
Ok ( ( ) )
774
774
}
775
775
776
- /// Close a file with the given full path .
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 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) ?;
804
779
self . open_files . swap_remove ( file_idx) ;
805
780
Ok ( ( ) )
806
781
}
807
782
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
+
808
788
/// Check if any files or folders are open.
809
789
pub fn has_open_handles ( & self ) -> bool {
810
790
!( self . open_dirs . is_empty ( ) || self . open_files . is_empty ( ) )
@@ -1074,6 +1054,38 @@ where
1074
1054
let available = Block :: LEN - block_offset;
1075
1055
Ok ( ( block_idx, block_offset, available) )
1076
1056
}
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
+ }
1077
1089
}
1078
1090
1079
1091
/// Transform mode variants (ReadWriteCreate_Or_Append) to simple modes ReadWriteAppend or
0 commit comments