@@ -134,9 +134,7 @@ where
134
134
) -> Result < Volume < D , T , MAX_DIRS , MAX_FILES , MAX_VOLUMES > , Error < D :: Error > > {
135
135
let v = self . open_raw_volume ( volume_idx, open_mode) ?;
136
136
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 ) ?;
140
138
}
141
139
Ok ( v. to_volume ( self ) )
142
140
}
@@ -360,8 +358,7 @@ where
360
358
}
361
359
let volume_idx = self . get_volume_by_id ( volume) ?;
362
360
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 ) ?;
365
362
}
366
363
367
364
self . open_volumes . swap_remove ( volume_idx) ;
@@ -372,32 +369,38 @@ where
372
369
/// Sets the volume status dirty to dirty if true, to not dirty if false
373
370
fn set_volume_status_dirty (
374
371
& self ,
375
- volume : & FatVolume ,
372
+ volume : RawVolume ,
376
373
dirty : bool ,
377
374
) -> Result < ( ) , Error < D :: Error > > {
378
375
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 ;
379
378
let fat_table1_start = volume. lba_start + volume. fat_start ;
380
379
self . block_device
381
380
. read ( & mut blocks, fat_table1_start, "reading fat table" ) ?;
382
381
let block = & mut blocks[ 0 ] ;
383
382
let mut fat_table =
384
383
fat:: FatTable :: create_from_bytes ( & mut block. contents , volume. get_fat_type ( ) )
385
384
. 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
+ }
393
394
}
394
395
}
395
396
Ok ( ( ) )
396
397
}
397
398
398
399
/// 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 > > {
400
401
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 ;
401
404
let fat_table1_start = volume. lba_start + volume. fat_start ;
402
405
self . block_device
403
406
. read ( & mut blocks, fat_table1_start, "reading fat table" ) ?;
@@ -1816,8 +1819,8 @@ mod tests {
1816
1819
} )
1817
1820
}
1818
1821
) ;
1819
- let VolumeType :: Fat ( fat_info ) = & c. open_volumes [ 0 ] . volume_type ;
1820
- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , false ) ;
1822
+ let volume = c. open_volumes [ 0 ] . volume_id ;
1823
+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , false ) ;
1821
1824
}
1822
1825
1823
1826
#[ test]
@@ -1855,8 +1858,8 @@ mod tests {
1855
1858
} )
1856
1859
}
1857
1860
) ;
1858
- let VolumeType :: Fat ( fat_info ) = & c. open_volumes [ 0 ] . volume_type ;
1859
- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , true ) ;
1861
+ let volume = c. open_volumes [ 0 ] . volume_id ;
1862
+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , true ) ;
1860
1863
}
1861
1864
1862
1865
#[ test]
@@ -1897,8 +1900,8 @@ mod tests {
1897
1900
} )
1898
1901
}
1899
1902
) ;
1900
- let VolumeType :: Fat ( fat_info ) = & c. open_volumes [ 0 ] . volume_type ;
1901
- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , false ) ;
1903
+ let volume = c. open_volumes [ 0 ] . volume_id ;
1904
+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , false ) ;
1902
1905
assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
1903
1906
assert_eq ! (
1904
1907
c. block_device. blocks. borrow( ) [ 1 ] ,
@@ -1912,13 +1915,13 @@ mod tests {
1912
1915
assert_eq ! ( c. block_device. blocks. borrow( ) [ 7 ] , DUMMY ) ;
1913
1916
assert_eq ! ( c. block_device. blocks. borrow( ) [ 8 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
1914
1917
1915
- c. set_volume_status_dirty ( fat_info , true ) . unwrap ( ) ;
1916
- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , true ) ;
1918
+ c. set_volume_status_dirty ( volume , true ) . unwrap ( ) ;
1919
+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , true ) ;
1917
1920
assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 0 ) ;
1918
1921
assert_eq ! ( c. block_device. blocks. borrow( ) [ 8 ] . contents[ 7 ] & ( 1 << 3 ) , 0 ) ;
1919
1922
1920
- c. set_volume_status_dirty ( fat_info , false ) . unwrap ( ) ;
1921
- assert_eq ! ( c. volume_status_dirty( fat_info ) . unwrap( ) , false ) ;
1923
+ c. set_volume_status_dirty ( volume , false ) . unwrap ( ) ;
1924
+ assert_eq ! ( c. volume_status_dirty( volume ) . unwrap( ) , false ) ;
1922
1925
assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
1923
1926
assert_eq ! (
1924
1927
c. block_device. blocks. borrow( ) [ 1 ] ,
0 commit comments