@@ -18,6 +18,12 @@ use crate::{
1818} ;
1919use heapless:: Vec ;
2020
21+ #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
22+ pub enum VolumeOpenMode {
23+ ReadOnly ,
24+ ReadWrite ,
25+ }
26+
2127/// A `VolumeManager` wraps a block device and gives access to the FAT-formatted
2228/// volumes within it.
2329#[ derive( Debug ) ]
@@ -103,7 +109,7 @@ where
103109 & mut self ,
104110 volume_idx : VolumeIdx ,
105111 ) -> Result < Volume < D , T , MAX_DIRS , MAX_FILES , MAX_VOLUMES > , Error < D :: Error > > {
106- return self . _open_volume ( volume_idx, false ) ;
112+ return self . _open_volume ( volume_idx, VolumeOpenMode :: ReadWrite ) ;
107113 }
108114
109115 /// Get a read only volume (or partition) based on entries in the Master Boot Record.
@@ -115,20 +121,19 @@ where
115121 & mut self ,
116122 volume_idx : VolumeIdx ,
117123 ) -> Result < Volume < D , T , MAX_DIRS , MAX_FILES , MAX_VOLUMES > , Error < D :: Error > > {
118- return self . _open_volume ( volume_idx, true ) ;
124+ return self . _open_volume ( volume_idx, VolumeOpenMode :: ReadOnly ) ;
119125 }
120-
121126 /// Get a volume (or partition) based on entries in the Master Boot Record.
122127 ///
123128 /// We do not support GUID Partition Table disks. Nor do we support any
124129 /// concept of drive letters - that is for a higher layer to handle.
125130 fn _open_volume (
126131 & mut self ,
127132 volume_idx : VolumeIdx ,
128- read_only : bool ,
133+ open_mode : VolumeOpenMode ,
129134 ) -> Result < Volume < D , T , MAX_DIRS , MAX_FILES , MAX_VOLUMES > , Error < D :: Error > > {
130- let v = self . open_raw_volume ( volume_idx, read_only ) ?;
131- if !read_only {
135+ let v = self . open_raw_volume ( volume_idx, open_mode ) ?;
136+ if open_mode != VolumeOpenMode :: ReadOnly {
132137 let idx = self . get_volume_by_id ( v) ?;
133138 let VolumeType :: Fat ( volume_type) = & self . open_volumes [ idx] . volume_type ;
134139 self . set_volume_status_dirty ( volume_type, true ) ?;
@@ -146,7 +151,7 @@ where
146151 pub fn open_raw_volume (
147152 & mut self ,
148153 volume_idx : VolumeIdx ,
149- read_only : bool ,
154+ open_mode : VolumeOpenMode ,
150155 ) -> Result < RawVolume , Error < D :: Error > > {
151156 const PARTITION1_START : usize = 446 ;
152157 const PARTITION2_START : usize = PARTITION1_START + PARTITION_INFO_LENGTH ;
@@ -225,7 +230,7 @@ where
225230 volume_id : id,
226231 idx : volume_idx,
227232 volume_type : volume,
228- read_only : read_only ,
233+ open_mode : open_mode ,
229234 } ;
230235 // We already checked for space
231236 self . open_volumes . push ( info) . unwrap ( ) ;
@@ -354,7 +359,7 @@ where
354359 }
355360 }
356361 let volume_idx = self . get_volume_by_id ( volume) ?;
357- if ! self . open_volumes [ volume_idx] . read_only {
362+ if self . open_volumes [ volume_idx] . open_mode != VolumeOpenMode :: ReadOnly {
358363 let VolumeType :: Fat ( volume_type) = & self . open_volumes [ volume_idx] . volume_type ;
359364 self . set_volume_status_dirty ( volume_type, false ) ?;
360365 }
@@ -555,7 +560,7 @@ where
555560 let volume_info = & self . open_volumes [ volume_idx] ;
556561 let sfn = name. to_short_filename ( ) . map_err ( Error :: FilenameError ) ?;
557562
558- if volume_info. read_only && mode != Mode :: ReadOnly {
563+ if volume_info. open_mode == VolumeOpenMode :: ReadOnly && mode != Mode :: ReadOnly {
559564 return Err ( Error :: VolumeReadOnly ) ;
560565 }
561566
@@ -1691,7 +1696,7 @@ mod tests {
16911696 DUMMY ,
16921697 DUMMY ,
16931698 FAT32_PARTITION0_FAT_TABLE ,
1694- ] )
1699+ ] ) ,
16951700 }
16961701 }
16971702
@@ -1707,7 +1712,7 @@ mod tests {
17071712 DUMMY ,
17081713 DUMMY ,
17091714 FAT32_PARTITION0_FAT_TABLE_DIRTY ,
1710- ] )
1715+ ] ) ,
17111716 }
17121717 }
17131718
@@ -1724,7 +1729,7 @@ mod tests {
17241729 DUMMY ,
17251730 DUMMY ,
17261731 FAT32_PARTITION0_FAT_TABLE ,
1727- ] )
1732+ ] ) ,
17281733 }
17291734 }
17301735 }
@@ -1745,8 +1750,6 @@ mod tests {
17451750 start_block_idx : BlockIdx ,
17461751 _reason : & str ,
17471752 ) -> Result < ( ) , Self :: Error > {
1748-
1749-
17501753 println ! (
17511754 "Reading block {} to {}" ,
17521755 start_block_idx. 0 ,
@@ -1780,21 +1783,19 @@ mod tests {
17801783 #[ test]
17811784 fn partition0 ( ) {
17821785 let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
1783- VolumeManager :: new_with_limits (
1784- DummyBlockDevice :: new_not_dirty ( ) ,
1785- Clock ,
1786- 0xAA00_0000 ,
1787- ) ;
1786+ VolumeManager :: new_with_limits ( DummyBlockDevice :: new_not_dirty ( ) , Clock , 0xAA00_0000 ) ;
17881787
1789- let v = c. open_raw_volume ( VolumeIdx ( 0 ) , false ) . unwrap ( ) ;
1788+ let v = c
1789+ . open_raw_volume ( VolumeIdx ( 0 ) , VolumeOpenMode :: ReadWrite )
1790+ . unwrap ( ) ;
17901791 let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
17911792 assert_eq ! ( v, expected_id) ;
17921793 assert_eq ! (
17931794 & c. open_volumes[ 0 ] ,
17941795 & VolumeInfo {
17951796 volume_id: expected_id,
17961797 idx: VolumeIdx ( 0 ) ,
1797- read_only : false ,
1798+ open_mode : VolumeOpenMode :: ReadWrite ,
17981799 volume_type: VolumeType :: Fat ( crate :: FatVolume {
17991800 lba_start: BlockIdx ( 1 ) ,
18001801 num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1816,23 +1817,24 @@ mod tests {
18161817 ) ;
18171818 let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
18181819 assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
1819- c. set_volume_status_dirty ( fat_info, true ) . unwrap ( ) ;
18201820 }
18211821
18221822 #[ test]
18231823 fn partition0_dirty ( ) {
18241824 let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
18251825 VolumeManager :: new_with_limits ( DummyBlockDevice :: new_dirty ( ) , Clock , 0xAA00_0000 ) ;
18261826
1827- let v = c. open_raw_volume ( VolumeIdx ( 0 ) , false ) . unwrap ( ) ;
1827+ let v = c
1828+ . open_raw_volume ( VolumeIdx ( 0 ) , VolumeOpenMode :: ReadWrite )
1829+ . unwrap ( ) ;
18281830 let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
18291831 assert_eq ! ( v, expected_id) ;
18301832 assert_eq ! (
18311833 & c. open_volumes[ 0 ] ,
18321834 & VolumeInfo {
18331835 volume_id: expected_id,
18341836 idx: VolumeIdx ( 0 ) ,
1835- read_only : false ,
1837+ open_mode : VolumeOpenMode :: ReadWrite ,
18361838 volume_type: VolumeType :: Fat ( crate :: FatVolume {
18371839 lba_start: BlockIdx ( 1 ) ,
18381840 num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1858,22 +1860,23 @@ mod tests {
18581860
18591861 #[ test]
18601862 fn partition0_set_dirty ( ) {
1861- let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
1862- VolumeManager :: new_with_limits (
1863- DummyBlockDevice :: new_not_dirty_fattable_size_5 ( ) ,
1864- Clock ,
1865- 0xAA00_0000 ,
1866- ) ;
1863+ let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > = VolumeManager :: new_with_limits (
1864+ DummyBlockDevice :: new_not_dirty_fattable_size_5 ( ) ,
1865+ Clock ,
1866+ 0xAA00_0000 ,
1867+ ) ;
18671868
1868- let v = c. open_raw_volume ( VolumeIdx ( 0 ) , false ) . unwrap ( ) ;
1869+ let v = c
1870+ . open_raw_volume ( VolumeIdx ( 0 ) , VolumeOpenMode :: ReadWrite )
1871+ . unwrap ( ) ;
18691872 let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
18701873 assert_eq ! ( v, expected_id) ;
18711874 assert_eq ! (
18721875 & c. open_volumes[ 0 ] ,
18731876 & VolumeInfo {
18741877 volume_id: expected_id,
18751878 idx: VolumeIdx ( 0 ) ,
1876- read_only : false ,
1879+ open_mode : VolumeOpenMode :: ReadWrite ,
18771880 volume_type: VolumeType :: Fat ( crate :: FatVolume {
18781881 lba_start: BlockIdx ( 1 ) ,
18791882 num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1896,7 +1899,10 @@ mod tests {
18961899 let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
18971900 assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
18981901 assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
1899- assert_eq ! ( c. block_device. blocks. borrow( ) [ 1 ] , FAT32_PARTITION0_BOOT_FAT_TABLE_SIZE_5 ) ;
1902+ assert_eq ! (
1903+ c. block_device. blocks. borrow( ) [ 1 ] ,
1904+ FAT32_PARTITION0_BOOT_FAT_TABLE_SIZE_5
1905+ ) ;
19001906 assert_eq ! ( c. block_device. blocks. borrow( ) [ 2 ] , FAT32_PARTITION0_FSINFO ) ;
19011907 assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
19021908 assert_eq ! ( c. block_device. blocks. borrow( ) [ 4 ] , DUMMY ) ;
@@ -1913,7 +1919,10 @@ mod tests {
19131919 c. set_volume_status_dirty ( fat_info, false ) . unwrap ( ) ;
19141920 assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
19151921 assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
1916- assert_eq ! ( c. block_device. blocks. borrow( ) [ 1 ] , FAT32_PARTITION0_BOOT_FAT_TABLE_SIZE_5 ) ;
1922+ assert_eq ! (
1923+ c. block_device. blocks. borrow( ) [ 1 ] ,
1924+ FAT32_PARTITION0_BOOT_FAT_TABLE_SIZE_5
1925+ ) ;
19171926 assert_eq ! ( c. block_device. blocks. borrow( ) [ 2 ] , FAT32_PARTITION0_FSINFO ) ;
19181927 assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
19191928 assert_eq ! ( c. block_device. blocks. borrow( ) [ 4 ] , DUMMY ) ;
0 commit comments