@@ -360,27 +360,36 @@ where
360360 dirty : bool ,
361361 ) -> Result < ( ) , Error < D :: Error > > {
362362 let mut blocks = [ Block :: new ( ) ] ;
363- self . block_device . read (
364- & mut blocks,
365- volume. lba_start + volume. fat_start ,
366- "reading fat table" ,
367- ) ?;
363+ let fat_table1_start = volume. lba_start + volume. fat_start ;
364+ self . block_device
365+ . read ( & mut blocks, fat_table1_start, "reading fat table" ) ?;
368366 let block = & mut blocks[ 0 ] ;
369367 let mut fat_table =
370368 fat:: FatTable :: create_from_bytes ( & mut block. contents , volume. get_fat_type ( ) )
371369 . map_err ( Error :: FormatError ) ?;
372370 fat_table. set_dirty ( dirty) ;
373- let fat_table_start = volume. lba_start + volume. fat_start ;
374371 if volume. fat_nums == 1 || volume. fat_nums == 2 {
375- self . block_device . write ( & blocks, fat_table_start ) ?;
372+ self . block_device . write ( & blocks, fat_table1_start ) ?;
376373 // Synchronize also backup fat table
377374 if volume. fat_nums == 2 {
378375 self . block_device
379- . write ( & blocks, fat_table_start + volume. fat_size ) ?
376+ . write ( & blocks, fat_table1_start + volume. fat_size ) ?
380377 }
381378 }
382379 Ok ( ( ) )
383380 }
381+ #[ cfg( test) ]
382+ fn volume_status_dirty ( & self , volume : & FatVolume ) -> Result < bool , Error < D :: Error > > {
383+ let mut blocks = [ Block :: new ( ) ] ;
384+ let fat_table1_start = volume. lba_start + volume. fat_start ;
385+ self . block_device
386+ . read ( & mut blocks, fat_table1_start, "reading fat table" ) ?;
387+ let block = & mut blocks[ 0 ] ;
388+ let fat_table =
389+ fat:: FatTable :: create_from_bytes ( & mut block. contents , volume. get_fat_type ( ) )
390+ . map_err ( Error :: FormatError ) ?;
391+ Ok ( fat_table. dirty ( ) )
392+ }
384393
385394 /// Look in a directory for a named file.
386395 pub fn find_directory_entry < N > (
@@ -1186,6 +1195,7 @@ mod tests {
11861195 use super :: * ;
11871196 use crate :: filesystem:: SearchId ;
11881197 use crate :: Timestamp ;
1198+ use crate :: VolumeType ;
11891199
11901200 struct DummyBlockDevice ;
11911201
@@ -1221,7 +1231,8 @@ mod tests {
12211231 _reason : & str ,
12221232 ) -> Result < ( ) , Self :: Error > {
12231233 // Actual blocks taken from an SD card, except I've changed the start and length of partition 0.
1224- static BLOCKS : [ Block ; 3 ] = [
1234+ static BLOCKS : [ Block ; 4 ] = [
1235+ // Block 0: MBR
12251236 Block {
12261237 contents : [
12271238 0xfa , 0xb8 , 0x00 , 0x10 , 0x8e , 0xd0 , 0xbc , 0x00 , 0xb0 , 0xb8 , 0x00 , 0x00 ,
@@ -1290,10 +1301,11 @@ mod tests {
12901301 0x00 , 0x00 , 0x55 , 0xaa , // 0x1F0
12911302 ] ,
12921303 } ,
1304+ // Block 1: Partition 0 Boot block
12931305 Block {
12941306 contents : [
12951307 0xeb , 0x58 , 0x90 , 0x6d , 0x6b , 0x66 , 0x73 , 0x2e , 0x66 , 0x61 , 0x74 , 0x00 ,
1296- 0x02 , 0x08 , 0x20 , 0x00 , // 0x000
1308+ 0x02 , 0x08 , 0x02 , 0x00 , // 0x000
12971309 0x02 , 0x00 , 0x00 , 0x00 , 0x00 , 0xf8 , 0x00 , 0x00 , 0x10 , 0x00 , 0x04 , 0x00 ,
12981310 0x00 , 0x08 , 0x00 , 0x00 , // 0x010
12991311 0x00 , 0x20 , 0x76 , 0x00 , 0x80 , 0x1d , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
@@ -1358,6 +1370,7 @@ mod tests {
13581370 0x00 , 0x00 , 0x55 , 0xaa , // 0x1F0
13591371 ] ,
13601372 } ,
1373+ // Partition 0 info sector (BPB_FSInfo)
13611374 Block {
13621375 contents : hex ! (
13631376 "52 52 61 41 00 00 00 00 00 00 00 00 00 00 00 00
@@ -1394,6 +1407,75 @@ mod tests {
13941407 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA"
13951408 ) ,
13961409 } ,
1410+ // Partition 0 FAT table
1411+ Block {
1412+ contents : [
1413+ 0xF0 , 0xFF , 0xFF , 0x0F , 0xFF , 0xFF , 0xFF , 0x0F , 0x00 , 0x00 , 0x00 , 0x00 ,
1414+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x000
1415+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1416+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x010
1417+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1418+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x020
1419+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1420+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x030
1421+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1422+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x040
1423+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1424+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x050
1425+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1426+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x060
1427+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1428+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x070
1429+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1430+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x080
1431+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1432+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x090
1433+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1434+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x0A0
1435+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1436+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x0B0
1437+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1438+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x0C0
1439+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1440+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x0D0
1441+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1442+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x0E0
1443+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1444+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x0F0
1445+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1446+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x100
1447+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1448+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x110
1449+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1450+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x120
1451+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1452+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x130
1453+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1454+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x140
1455+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1456+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x150
1457+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1458+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x160
1459+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1460+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x170
1461+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1462+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x180
1463+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1464+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x190
1465+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1466+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x1A0
1467+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1468+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x1B0
1469+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1470+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x1C0
1471+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1472+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x1D0
1473+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1474+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x1E0
1475+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
1476+ 0x00 , 0x00 , 0x00 , 0x00 , // 0x1F0
1477+ ] ,
1478+ } ,
13971479 ] ;
13981480 println ! (
13991481 "Reading block {} to {}" ,
@@ -1427,31 +1509,36 @@ mod tests {
14271509 let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
14281510 VolumeManager :: new_with_limits ( DummyBlockDevice , Clock , 0xAA00_0000 ) ;
14291511
1430- let v = c. open_raw_volume ( VolumeIdx ( 0 ) ) . unwrap ( ) ;
1512+ let v = c. open_raw_volume ( VolumeIdx ( 0 ) , false ) . unwrap ( ) ;
14311513 let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
14321514 assert_eq ! ( v, expected_id) ;
14331515 assert_eq ! (
14341516 & c. open_volumes[ 0 ] ,
14351517 & VolumeInfo {
14361518 volume_id: expected_id,
14371519 idx: VolumeIdx ( 0 ) ,
1520+ read_only: false ,
14381521 volume_type: VolumeType :: Fat ( crate :: FatVolume {
14391522 lba_start: BlockIdx ( 1 ) ,
14401523 num_blocks: BlockCount ( 0x0011_2233 ) ,
14411524 blocks_per_cluster: 8 ,
1442- first_data_block: BlockCount ( 15136 ) ,
1443- fat_start: BlockCount ( 32 ) ,
1525+ first_data_block: BlockCount ( 15106 ) ,
1526+ fat_start: BlockCount ( 2 ) ,
1527+ fat_size: BlockCount ( 7552 ) ,
1528+ fat_nums: 2 ,
14441529 name: fat:: VolumeName :: new( * b"Pictures " ) ,
14451530 free_clusters_count: None ,
14461531 next_free_cluster: None ,
1447- cluster_count: 965_788 ,
1532+ cluster_count: 965_791 ,
14481533 fat_specific_info: fat:: FatSpecificInfo :: Fat32 ( fat:: Fat32Info {
14491534 first_root_dir_cluster: ClusterId ( 2 ) ,
14501535 info_location: BlockIdx ( 1 ) + BlockCount ( 1 ) ,
14511536 } )
14521537 } )
14531538 }
14541539 ) ;
1540+ let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
1541+ assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
14551542 }
14561543}
14571544
0 commit comments