@@ -410,7 +410,7 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
410410            if  options. ignore_dirty_flag  { 
411411                warn ! ( "BPB is dirty, clearing dirty flag." ) ; 
412412                bpb_status_flags. dirty  = false ; 
413-                 write_bpb_status_flags ( & mut  disk,  fat_type,  bpb_status_flags) ?; 
413+                 Self :: write_bpb_status_flags ( & mut  disk,  fat_type,  bpb_status_flags) ?; 
414414                bpb. set_status_flags ( bpb_status_flags) ; 
415415            }  else  { 
416416                return  Err ( Error :: DirtyFileSystem ) ; 
@@ -590,9 +590,7 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
590590        Ok ( free_cluster_count) 
591591    } 
592592
593-     /// Unmounts the filesystem. 
594-      /// 
595-      /// Updates the FS Information Sector if needed. 
593+     /// Updates the FS Information Sector if needed and unmounts the filesystem. 
596594     /// 
597595     /// # Errors 
598596     /// 
@@ -606,6 +604,11 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
606604        Ok ( disk) 
607605    } 
608606
607+     /// Updates the FS information sector if needed. 
608+      /// 
609+      /// # Errors 
610+      /// 
611+      /// `Error::Io` will be returned if the underlying storage object returned an I/O error. 
609612     pub  fn  flush ( & mut  self )  -> Result < ( ) ,  Error < IO :: Error > >  { 
610613        self . flush_fs_info ( ) ?; 
611614        self . set_dirty_flag ( false ) ?; 
@@ -624,10 +627,40 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
624627        Ok ( ( ) ) 
625628    } 
626629
630+     fn  write_bpb_status_flags < IO :  Seek  + Write > ( 
631+         disk :  & mut  IO , 
632+         fat_type :  FatType , 
633+         status_flags :  FsStatusFlags , 
634+     )  -> Result < ( ) ,  Error < IO :: Error > >  { 
635+         let  encoded = status_flags. encode ( ) ; 
636+ 
637+         // Note: only one field is written to avoid rewriting entire boot-sector which could be dangerous 
638+         // Compute reserver_1 field offset and write new flags 
639+         let  offset = if  fat_type == FatType :: Fat32  {  0x041  }  else  {  0x025  } ; 
640+ 
641+         disk. seek ( io:: SeekFrom :: Start ( offset) ) ?; 
642+         disk. write_u8 ( encoded) ?; 
643+ 
644+         Ok ( ( ) ) 
645+     } 
646+ 
627647    #[ inline]  
628648    pub ( crate )  fn  set_dirty_flag ( & self ,  dirty :  bool )  -> Result < ( ) ,  Error < IO :: Error > >  { 
629649        let  mut  disk = self . disk . borrow_mut ( ) ; 
630-         set_dirty_flag ( & mut  * disk,  self . fat_type ( ) ,  & self . current_status_flags ,  dirty) 
650+ 
651+         let  mut  status_flags = self . current_status_flags . get ( ) ; 
652+ 
653+         if  status_flags. dirty  == dirty { 
654+             // Dirty flag did not change. 
655+             return  Ok ( ( ) ) ; 
656+         } 
657+ 
658+         status_flags. dirty  = dirty; 
659+ 
660+         Self :: write_bpb_status_flags ( & mut  * disk,  self . fat_type ( ) ,  status_flags) ?; 
661+         self . current_status_flags . set ( status_flags) ; 
662+ 
663+         Ok ( ( ) ) 
631664    } 
632665
633666    /// Returns a root directory object allowing for futher penetration of a filesystem structure. 
@@ -757,44 +790,6 @@ impl<IO: ReadWriteSeek, TP, OCC> Clone for FsIoAdapter<'_, IO, TP, OCC> {
757790    } 
758791} 
759792
760- fn  set_dirty_flag < IO :  Seek  + Write > ( 
761-     disk :  & mut  IO , 
762-     fat_type :  FatType , 
763-     current_status_flags :  & Cell < FsStatusFlags > , 
764-     dirty :  bool , 
765- )  -> Result < ( ) ,  Error < IO :: Error > >  { 
766-     let  mut  status_flags = current_status_flags. get ( ) ; 
767- 
768-     if  status_flags. dirty  == dirty { 
769-         // Dirty flag did not change. 
770-         return  Ok ( ( ) ) ; 
771-     } 
772- 
773-     status_flags. dirty  = dirty; 
774- 
775-     write_bpb_status_flags ( disk,  fat_type,  status_flags) ?; 
776-     current_status_flags. set ( status_flags) ; 
777- 
778-     Ok ( ( ) ) 
779- } 
780- 
781- fn  write_bpb_status_flags < IO :  Seek  + Write > ( 
782-     disk :  & mut  IO , 
783-     fat_type :  FatType , 
784-     status_flags :  FsStatusFlags , 
785- )  -> Result < ( ) ,  Error < IO :: Error > >  { 
786-     let  encoded = status_flags. encode ( ) ; 
787- 
788-     // Note: only one field is written to avoid rewriting entire boot-sector which could be dangerous 
789-     // Compute reserver_1 field offset and write new flags 
790-     let  offset = if  fat_type == FatType :: Fat32  {  0x041  }  else  {  0x025  } ; 
791- 
792-     disk. seek ( io:: SeekFrom :: Start ( offset) ) ?; 
793-     disk. write_u8 ( encoded) ?; 
794- 
795-     Ok ( ( ) ) 
796- } 
797- 
798793fn  fat_slice < B :  BorrowMut < S > ,  E ,  S :  ReadWriteSeek > ( io :  B ,  bpb :  & BiosParameterBlock )  -> DiskSlice < B ,  E ,  S >  { 
799794    let  sectors_per_fat = bpb. sectors_per_fat ( ) ; 
800795    let  mirroring_enabled = bpb. mirroring_enabled ( ) ; 
0 commit comments