@@ -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" ) ?;
@@ -1833,8 +1836,8 @@ mod tests {
1833
1836
} )
1834
1837
}
1835
1838
) ;
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 ) ;
1838
1841
}
1839
1842
1840
1843
#[ test]
@@ -1872,8 +1875,8 @@ mod tests {
1872
1875
} )
1873
1876
}
1874
1877
) ;
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 ) ;
1877
1880
}
1878
1881
1879
1882
#[ test]
@@ -1914,8 +1917,8 @@ mod tests {
1914
1917
} )
1915
1918
}
1916
1919
) ;
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 ) ;
1919
1922
assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
1920
1923
assert_eq ! (
1921
1924
c. block_device. blocks. borrow( ) [ 1 ] ,
@@ -1929,13 +1932,13 @@ mod tests {
1929
1932
assert_eq ! ( c. block_device. blocks. borrow( ) [ 7 ] , DUMMY ) ;
1930
1933
assert_eq ! ( c. block_device. blocks. borrow( ) [ 8 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
1931
1934
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 ) ;
1934
1937
assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 0 ) ;
1935
1938
assert_eq ! ( c. block_device. blocks. borrow( ) [ 8 ] . contents[ 7 ] & ( 1 << 3 ) , 0 ) ;
1936
1939
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 ) ;
1939
1942
assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
1940
1943
assert_eq ! (
1941
1944
c. block_device. blocks. borrow( ) [ 1 ] ,
0 commit comments