@@ -134,9 +134,7 @@ where
134134 ) -> Result < Volume < D , T , MAX_DIRS , MAX_FILES , MAX_VOLUMES > , Error < D :: Error > > {
135135 let v = self . open_raw_volume ( volume_idx, open_mode) ?;
136136 if open_mode != VolumeOpenMode :: ReadOnly {
137- let idx = self . get_volume_by_id ( v) ?;
138- let VolumeType :: Fat ( volume_type) = & self . open_volumes [ idx] . volume_type ;
139- self . set_volume_status_dirty ( volume_type, true ) ?;
137+ self . set_volume_status_dirty ( v, true ) ?;
140138 }
141139 Ok ( v. to_volume ( self ) )
142140 }
@@ -360,8 +358,7 @@ where
360358 }
361359 let volume_idx = self . get_volume_by_id ( volume) ?;
362360 if self . open_volumes [ volume_idx] . open_mode != VolumeOpenMode :: ReadOnly {
363- let VolumeType :: Fat ( volume_type) = & self . open_volumes [ volume_idx] . volume_type ;
364- self . set_volume_status_dirty ( volume_type, false ) ?;
361+ self . set_volume_status_dirty ( volume, false ) ?;
365362 }
366363
367364 self . open_volumes . swap_remove ( volume_idx) ;
@@ -372,32 +369,38 @@ where
372369 /// Sets the volume status dirty to dirty if true, to not dirty if false
373370 fn set_volume_status_dirty (
374371 & self ,
375- volume : & FatVolume ,
372+ volume : RawVolume ,
376373 dirty : bool ,
377374 ) -> Result < ( ) , Error < D :: Error > > {
378375 let mut blocks = [ Block :: new ( ) ] ;
376+ let idx = self . get_volume_by_id ( volume) ?;
377+ let VolumeType :: Fat ( volume) = & self . open_volumes [ idx] . volume_type ;
379378 let fat_table1_start = volume. lba_start + volume. fat_start ;
380379 self . block_device
381380 . read ( & mut blocks, fat_table1_start, "reading fat table" ) ?;
382381 let block = & mut blocks[ 0 ] ;
383382 let mut fat_table =
384383 fat:: FatTable :: create_from_bytes ( & mut block. contents , volume. get_fat_type ( ) )
385384 . map_err ( Error :: FormatError ) ?;
386- fat_table. set_dirty ( dirty) ;
387- if volume. fat_nums == 1 || volume. fat_nums == 2 {
388- self . block_device . write ( & blocks, fat_table1_start) ?;
389- // Synchronize also backup fat table
390- if volume. fat_nums == 2 {
391- self . block_device
392- . write ( & blocks, fat_table1_start + volume. fat_size ) ?
385+ if !fat_table. dirty ( ) {
386+ fat_table. set_dirty ( dirty) ;
387+ if volume. fat_nums == 1 || volume. fat_nums == 2 {
388+ self . block_device . write ( & blocks, fat_table1_start) ?;
389+ // Synchronize also backup fat table
390+ if volume. fat_nums == 2 {
391+ self . block_device
392+ . write ( & blocks, fat_table1_start + volume. fat_size ) ?
393+ }
393394 }
394395 }
395396 Ok ( ( ) )
396397 }
397398
398399 /// Checking if the volume is dirty or was unmounted correctly in a previous usage
399- pub fn volume_status_dirty ( & self , volume : & FatVolume ) -> Result < bool , Error < D :: Error > > {
400+ pub fn volume_status_dirty ( & self , volume : RawVolume ) -> Result < bool , Error < D :: Error > > {
400401 let mut blocks = [ Block :: new ( ) ] ;
402+ let volume_idx = self . get_volume_by_id ( volume) ?;
403+ let VolumeType :: Fat ( volume) = & self . open_volumes [ volume_idx] . volume_type ;
401404 let fat_table1_start = volume. lba_start + volume. fat_start ;
402405 self . block_device
403406 . read ( & mut blocks, fat_table1_start, "reading fat table" ) ?;
@@ -1833,8 +1836,8 @@ mod tests {
18331836 } )
18341837 }
18351838 ) ;
1836- let VolumeType :: Fat ( fat_info ) = & c. open_volumes [ 0 ] . volume_type ;
1837- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , false ) ;
1839+ let volume = c. open_volumes [ 0 ] . volume_id ;
1840+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , false ) ;
18381841 }
18391842
18401843 #[ test]
@@ -1872,8 +1875,8 @@ mod tests {
18721875 } )
18731876 }
18741877 ) ;
1875- let VolumeType :: Fat ( fat_info ) = & c. open_volumes [ 0 ] . volume_type ;
1876- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , true ) ;
1878+ let volume = c. open_volumes [ 0 ] . volume_id ;
1879+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , true ) ;
18771880 }
18781881
18791882 #[ test]
@@ -1914,8 +1917,8 @@ mod tests {
19141917 } )
19151918 }
19161919 ) ;
1917- let VolumeType :: Fat ( fat_info ) = & c. open_volumes [ 0 ] . volume_type ;
1918- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , false ) ;
1920+ let volume = c. open_volumes [ 0 ] . volume_id ;
1921+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , false ) ;
19191922 assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
19201923 assert_eq ! (
19211924 c. block_device. blocks. borrow( ) [ 1 ] ,
@@ -1929,13 +1932,13 @@ mod tests {
19291932 assert_eq ! ( c. block_device. blocks. borrow( ) [ 7 ] , DUMMY ) ;
19301933 assert_eq ! ( c. block_device. blocks. borrow( ) [ 8 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
19311934
1932- c. set_volume_status_dirty ( fat_info , true ) . unwrap ( ) ;
1933- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , true ) ;
1935+ c. set_volume_status_dirty ( volume , true ) . unwrap ( ) ;
1936+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , true ) ;
19341937 assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 0 ) ;
19351938 assert_eq ! ( c. block_device. blocks. borrow( ) [ 8 ] . contents[ 7 ] & ( 1 << 3 ) , 0 ) ;
19361939
1937- c. set_volume_status_dirty ( fat_info , false ) . unwrap ( ) ;
1938- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , false ) ;
1940+ c. set_volume_status_dirty ( volume , false ) . unwrap ( ) ;
1941+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , false ) ;
19391942 assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
19401943 assert_eq ! (
19411944 c. block_device. blocks. borrow( ) [ 1 ] ,
0 commit comments