@@ -398,14 +398,24 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
398398 let fat_type = FatType :: from_clusters ( total_clusters) ;
399399
400400 // read FSInfo sector if this is FAT32
401- let fs_info = if fat_type == FatType :: Fat32 {
401+ let mut fs_info = if fat_type == FatType :: Fat32 {
402402 disk. seek ( SeekFrom :: Start ( bpb. bytes_from_sectors ( bpb. fs_info_sector ( ) ) ) ) ?;
403403 FsInfoSector :: deserialize ( & mut disk) ?
404404 } else {
405405 FsInfoSector :: default ( )
406406 } ;
407407
408- let fs = Self {
408+ let mut bpb_status_flags = bpb. status_flags ( ) ;
409+
410+ // if dirty flag is set completly ignore free_cluster_count in FSInfo
411+ if bpb_status_flags. dirty ( ) {
412+ fs_info. free_cluster_count = None ;
413+ }
414+
415+ // Validate the numbers stored in the free_cluster_count and next_free_cluster are within bounds for volume
416+ fs_info. validate_and_fix ( total_clusters) ;
417+
418+ let mut fs = Self {
409419 disk : RefCell :: new ( disk) ,
410420 options,
411421 fat_type,
@@ -414,30 +424,29 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
414424 root_dir_sectors,
415425 total_clusters,
416426 fs_info : RefCell :: new ( fs_info) ,
417- current_status_flags : Cell :: new ( FsStatusFlags {
418- dirty : false ,
419- io_error : false ,
420- } ) ,
427+ current_status_flags : Cell :: new ( bpb_status_flags) ,
421428 } ;
422429
423- {
424- let status_flags = fs. read_status_flags ( ) ?;
425- let mut fs_info = fs. fs_info . borrow_mut ( ) ;
426-
427- if status_flags. dirty ( ) {
428- // if dirty flag is set completly ignore free_cluster_count in FSInfo
429- fs_info. free_cluster_count = None ;
430-
431- if fs. options . ignore_dirty_flag {
432- warn ! ( "File system is dirty, clearing dirty flag." ) ;
433- fs. set_dirty_flag ( false ) ?;
434- } else {
435- return Err ( Error :: DirtyFileSystem ) ;
436- }
430+ if bpb_status_flags. dirty ( ) {
431+ if fs. options . ignore_dirty_flag {
432+ warn ! ( "BPB is dirty, clearing dirty flag." ) ;
433+ bpb_status_flags. dirty = false ;
434+ fs. set_bpb_status_flags ( bpb_status_flags) ?;
435+ fs. current_status_flags . get_mut ( ) . dirty = false ;
436+ } else {
437+ return Err ( Error :: DirtyFileSystem )
437438 }
439+ }
438440
439- // Validate the numbers stored in the free_cluster_count and next_free_cluster are within bounds for volume
440- fs_info. validate_and_fix ( total_clusters) ;
441+ let mut fat_status_flags = fs. read_fat_status_flags ( ) ?;
442+ if fat_status_flags. dirty ( ) {
443+ if fs. options . ignore_dirty_flag {
444+ warn ! ( "FAT is dirty, clearing dirty flag." ) ;
445+ fat_status_flags. dirty = false ;
446+ fs. set_fat_status_flags ( fat_status_flags) ?;
447+ } else {
448+ return Err ( Error :: DirtyFileSystem ) ;
449+ }
441450 }
442451
443452 trace ! ( "FileSystem::new end" ) ;
@@ -546,7 +555,7 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
546555 /// `Error::Io` will be returned if the underlying storage object returned an I/O error.
547556 pub fn read_status_flags ( & self ) -> Result < FsStatusFlags , Error < IO :: Error > > {
548557 let bpb_status = self . bpb . status_flags ( ) ;
549- let fat_status = read_fat_flags ( & mut self . fat_slice ( ) , self . fat_type ) ?;
558+ let fat_status = self . read_fat_status_flags ( ) ?;
550559 Ok ( FsStatusFlags {
551560 dirty : bpb_status. dirty || fat_status. dirty ,
552561 io_error : bpb_status. io_error || fat_status. io_error ,
@@ -623,7 +632,6 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
623632 status_flags. dirty = dirty;
624633
625634 self . set_bpb_status_flags ( status_flags) ?;
626- self . set_fat_status_flags ( status_flags) ?;
627635
628636 self . current_status_flags . set ( status_flags) ;
629637
@@ -649,6 +657,11 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
649657 Ok ( ( ) )
650658 }
651659
660+ #[ inline]
661+ fn read_fat_status_flags ( & self ) -> Result < FsStatusFlags , Error < IO :: Error > > {
662+ read_fat_flags ( & mut self . fat_slice ( ) , self . fat_type )
663+ }
664+
652665 #[ inline]
653666 fn set_fat_status_flags ( & self , status_flags : FsStatusFlags ) -> Result < ( ) , Error < IO :: Error > > {
654667 write_fat_flags ( & mut self . fat_slice ( ) , self . fat_type , status_flags)
0 commit comments