@@ -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 ///
@@ -601,6 +599,11 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
601599 self . flush ( )
602600 }
603601
602+ /// Updates the FS information sector if needed.
603+ ///
604+ /// # Errors
605+ ///
606+ /// `Error::Io` will be returned if the underlying storage object returned an I/O error.
604607 pub fn flush ( & mut self ) -> Result < ( ) , Error < IO :: Error > > {
605608 self . flush_fs_info ( ) ?;
606609 self . set_dirty_flag ( false ) ?;
@@ -619,10 +622,40 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
619622 Ok ( ( ) )
620623 }
621624
625+ fn write_bpb_status_flags < IO : Seek + Write > (
626+ disk : & mut IO ,
627+ fat_type : FatType ,
628+ status_flags : FsStatusFlags ,
629+ ) -> Result < ( ) , Error < IO :: Error > > {
630+ let encoded = status_flags. encode ( ) ;
631+
632+ // Note: only one field is written to avoid rewriting entire boot-sector which could be dangerous
633+ // Compute reserver_1 field offset and write new flags
634+ let offset = if fat_type == FatType :: Fat32 { 0x041 } else { 0x025 } ;
635+
636+ disk. seek ( io:: SeekFrom :: Start ( offset) ) ?;
637+ disk. write_u8 ( encoded) ?;
638+
639+ Ok ( ( ) )
640+ }
641+
622642 #[ inline]
623643 pub ( crate ) fn set_dirty_flag ( & self , dirty : bool ) -> Result < ( ) , Error < IO :: Error > > {
624644 let mut disk = self . disk . borrow_mut ( ) ;
625- set_dirty_flag ( & mut * disk, self . fat_type ( ) , & self . current_status_flags , dirty)
645+
646+ let mut status_flags = self . current_status_flags . get ( ) ;
647+
648+ if status_flags. dirty == dirty {
649+ // Dirty flag did not change.
650+ return Ok ( ( ) ) ;
651+ }
652+
653+ status_flags. dirty = dirty;
654+
655+ Self :: write_bpb_status_flags ( & mut * disk, self . fat_type ( ) , status_flags) ?;
656+ self . current_status_flags . set ( status_flags) ;
657+
658+ Ok ( ( ) )
626659 }
627660
628661 /// Returns a root directory object allowing for futher penetration of a filesystem structure.
@@ -752,44 +785,6 @@ impl<IO: ReadWriteSeek, TP, OCC> Clone for FsIoAdapter<'_, IO, TP, OCC> {
752785 }
753786}
754787
755- fn set_dirty_flag < IO : Seek + Write > (
756- disk : & mut IO ,
757- fat_type : FatType ,
758- current_status_flags : & Cell < FsStatusFlags > ,
759- dirty : bool ,
760- ) -> Result < ( ) , Error < IO :: Error > > {
761- let mut status_flags = current_status_flags. get ( ) ;
762-
763- if status_flags. dirty == dirty {
764- // Dirty flag did not change.
765- return Ok ( ( ) ) ;
766- }
767-
768- status_flags. dirty = dirty;
769-
770- write_bpb_status_flags ( disk, fat_type, status_flags) ?;
771- current_status_flags. set ( status_flags) ;
772-
773- Ok ( ( ) )
774- }
775-
776- fn write_bpb_status_flags < IO : Seek + Write > (
777- disk : & mut IO ,
778- fat_type : FatType ,
779- status_flags : FsStatusFlags ,
780- ) -> Result < ( ) , Error < IO :: Error > > {
781- let encoded = status_flags. encode ( ) ;
782-
783- // Note: only one field is written to avoid rewriting entire boot-sector which could be dangerous
784- // Compute reserver_1 field offset and write new flags
785- let offset = if fat_type == FatType :: Fat32 { 0x041 } else { 0x025 } ;
786-
787- disk. seek ( io:: SeekFrom :: Start ( offset) ) ?;
788- disk. write_u8 ( encoded) ?;
789-
790- Ok ( ( ) )
791- }
792-
793788fn fat_slice < B : BorrowMut < S > , E , S : ReadWriteSeek > ( io : B , bpb : & BiosParameterBlock ) -> DiskSlice < B , E , S > {
794789 let sectors_per_fat = bpb. sectors_per_fat ( ) ;
795790 let mirroring_enabled = bpb. mirroring_enabled ( ) ;
0 commit comments