@@ -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
@@ -1708,7 +1713,7 @@ mod tests {
17081713 DUMMY ,
17091714 DUMMY ,
17101715 FAT32_PARTITION0_FAT_TABLE ,
1711- ] )
1716+ ] ) ,
17121717 }
17131718 }
17141719
@@ -1724,7 +1729,7 @@ mod tests {
17241729 DUMMY ,
17251730 DUMMY ,
17261731 FAT32_PARTITION0_FAT_TABLE_DIRTY ,
1727- ] )
1732+ ] ) ,
17281733 }
17291734 }
17301735
@@ -1741,7 +1746,7 @@ mod tests {
17411746 DUMMY ,
17421747 DUMMY ,
17431748 FAT32_PARTITION0_FAT_TABLE ,
1744- ] )
1749+ ] ) ,
17451750 }
17461751 }
17471752 }
@@ -1762,8 +1767,6 @@ mod tests {
17621767 start_block_idx : BlockIdx ,
17631768 _reason : & str ,
17641769 ) -> Result < ( ) , Self :: Error > {
1765-
1766-
17671770 println ! (
17681771 "Reading block {} to {}" ,
17691772 start_block_idx. 0 ,
@@ -1797,21 +1800,19 @@ mod tests {
17971800 #[ test]
17981801 fn partition0 ( ) {
17991802 let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
1800- VolumeManager :: new_with_limits (
1801- DummyBlockDevice :: new_not_dirty ( ) ,
1802- Clock ,
1803- 0xAA00_0000 ,
1804- ) ;
1803+ VolumeManager :: new_with_limits ( DummyBlockDevice :: new_not_dirty ( ) , Clock , 0xAA00_0000 ) ;
18051804
1806- let v = c. open_raw_volume ( VolumeIdx ( 0 ) , false ) . unwrap ( ) ;
1805+ let v = c
1806+ . open_raw_volume ( VolumeIdx ( 0 ) , VolumeOpenMode :: ReadWrite )
1807+ . unwrap ( ) ;
18071808 let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
18081809 assert_eq ! ( v, expected_id) ;
18091810 assert_eq ! (
18101811 & c. open_volumes[ 0 ] ,
18111812 & VolumeInfo {
18121813 volume_id: expected_id,
18131814 idx: VolumeIdx ( 0 ) ,
1814- read_only : false ,
1815+ open_mode : VolumeOpenMode :: ReadWrite ,
18151816 volume_type: VolumeType :: Fat ( crate :: FatVolume {
18161817 lba_start: BlockIdx ( 1 ) ,
18171818 num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1833,23 +1834,24 @@ mod tests {
18331834 ) ;
18341835 let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
18351836 assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
1836- c. set_volume_status_dirty ( fat_info, true ) . unwrap ( ) ;
18371837 }
18381838
18391839 #[ test]
18401840 fn partition0_dirty ( ) {
18411841 let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
18421842 VolumeManager :: new_with_limits ( DummyBlockDevice :: new_dirty ( ) , Clock , 0xAA00_0000 ) ;
18431843
1844- let v = c. open_raw_volume ( VolumeIdx ( 0 ) , false ) . unwrap ( ) ;
1844+ let v = c
1845+ . open_raw_volume ( VolumeIdx ( 0 ) , VolumeOpenMode :: ReadWrite )
1846+ . unwrap ( ) ;
18451847 let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
18461848 assert_eq ! ( v, expected_id) ;
18471849 assert_eq ! (
18481850 & c. open_volumes[ 0 ] ,
18491851 & VolumeInfo {
18501852 volume_id: expected_id,
18511853 idx: VolumeIdx ( 0 ) ,
1852- read_only : false ,
1854+ open_mode : VolumeOpenMode :: ReadWrite ,
18531855 volume_type: VolumeType :: Fat ( crate :: FatVolume {
18541856 lba_start: BlockIdx ( 1 ) ,
18551857 num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1875,22 +1877,23 @@ mod tests {
18751877
18761878 #[ test]
18771879 fn partition0_set_dirty ( ) {
1878- let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
1879- VolumeManager :: new_with_limits (
1880- DummyBlockDevice :: new_not_dirty_fattable_size_5 ( ) ,
1881- Clock ,
1882- 0xAA00_0000 ,
1883- ) ;
1880+ let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > = VolumeManager :: new_with_limits (
1881+ DummyBlockDevice :: new_not_dirty_fattable_size_5 ( ) ,
1882+ Clock ,
1883+ 0xAA00_0000 ,
1884+ ) ;
18841885
1885- let v = c. open_raw_volume ( VolumeIdx ( 0 ) , false ) . unwrap ( ) ;
1886+ let v = c
1887+ . open_raw_volume ( VolumeIdx ( 0 ) , VolumeOpenMode :: ReadWrite )
1888+ . unwrap ( ) ;
18861889 let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
18871890 assert_eq ! ( v, expected_id) ;
18881891 assert_eq ! (
18891892 & c. open_volumes[ 0 ] ,
18901893 & VolumeInfo {
18911894 volume_id: expected_id,
18921895 idx: VolumeIdx ( 0 ) ,
1893- read_only : false ,
1896+ open_mode : VolumeOpenMode :: ReadWrite ,
18941897 volume_type: VolumeType :: Fat ( crate :: FatVolume {
18951898 lba_start: BlockIdx ( 1 ) ,
18961899 num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1913,7 +1916,10 @@ mod tests {
19131916 let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
19141917 assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
19151918 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 ) ;
1919+ assert_eq ! (
1920+ c. block_device. blocks. borrow( ) [ 1 ] ,
1921+ FAT32_PARTITION0_BOOT_FAT_TABLE_SIZE_5
1922+ ) ;
19171923 assert_eq ! ( c. block_device. blocks. borrow( ) [ 2 ] , FAT32_PARTITION0_FSINFO ) ;
19181924 assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
19191925 assert_eq ! ( c. block_device. blocks. borrow( ) [ 4 ] , DUMMY ) ;
@@ -1930,7 +1936,10 @@ mod tests {
19301936 c. set_volume_status_dirty ( fat_info, false ) . unwrap ( ) ;
19311937 assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
19321938 assert_eq ! ( c. block_device. blocks. borrow( ) [ 0 ] , MBR_BLOCK ) ;
1933- assert_eq ! ( c. block_device. blocks. borrow( ) [ 1 ] , FAT32_PARTITION0_BOOT_FAT_TABLE_SIZE_5 ) ;
1939+ assert_eq ! (
1940+ c. block_device. blocks. borrow( ) [ 1 ] ,
1941+ FAT32_PARTITION0_BOOT_FAT_TABLE_SIZE_5
1942+ ) ;
19341943 assert_eq ! ( c. block_device. blocks. borrow( ) [ 2 ] , FAT32_PARTITION0_FSINFO ) ;
19351944 assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
19361945 assert_eq ! ( c. block_device. blocks. borrow( ) [ 4 ] , DUMMY ) ;
0 commit comments