@@ -18,6 +18,12 @@ use crate::{
18
18
} ;
19
19
use heapless:: Vec ;
20
20
21
+ #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
22
+ pub enum VolumeOpenMode {
23
+ ReadOnly ,
24
+ ReadWrite ,
25
+ }
26
+
21
27
/// A `VolumeManager` wraps a block device and gives access to the FAT-formatted
22
28
/// volumes within it.
23
29
#[ derive( Debug ) ]
@@ -103,7 +109,7 @@ where
103
109
& mut self ,
104
110
volume_idx : VolumeIdx ,
105
111
) -> 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 ) ;
107
113
}
108
114
109
115
/// Get a read only volume (or partition) based on entries in the Master Boot Record.
@@ -115,20 +121,19 @@ where
115
121
& mut self ,
116
122
volume_idx : VolumeIdx ,
117
123
) -> 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 ) ;
119
125
}
120
-
121
126
/// Get a volume (or partition) based on entries in the Master Boot Record.
122
127
///
123
128
/// We do not support GUID Partition Table disks. Nor do we support any
124
129
/// concept of drive letters - that is for a higher layer to handle.
125
130
fn _open_volume (
126
131
& mut self ,
127
132
volume_idx : VolumeIdx ,
128
- read_only : bool ,
133
+ open_mode : VolumeOpenMode ,
129
134
) -> 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 {
132
137
let idx = self . get_volume_by_id ( v) ?;
133
138
let VolumeType :: Fat ( volume_type) = & self . open_volumes [ idx] . volume_type ;
134
139
self . set_volume_status_dirty ( volume_type, true ) ?;
@@ -146,7 +151,7 @@ where
146
151
pub fn open_raw_volume (
147
152
& mut self ,
148
153
volume_idx : VolumeIdx ,
149
- read_only : bool ,
154
+ open_mode : VolumeOpenMode ,
150
155
) -> Result < RawVolume , Error < D :: Error > > {
151
156
const PARTITION1_START : usize = 446 ;
152
157
const PARTITION2_START : usize = PARTITION1_START + PARTITION_INFO_LENGTH ;
@@ -225,7 +230,7 @@ where
225
230
volume_id : id,
226
231
idx : volume_idx,
227
232
volume_type : volume,
228
- read_only : read_only ,
233
+ open_mode : open_mode ,
229
234
} ;
230
235
// We already checked for space
231
236
self . open_volumes . push ( info) . unwrap ( ) ;
@@ -354,7 +359,7 @@ where
354
359
}
355
360
}
356
361
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 {
358
363
let VolumeType :: Fat ( volume_type) = & self . open_volumes [ volume_idx] . volume_type ;
359
364
self . set_volume_status_dirty ( volume_type, false ) ?;
360
365
}
@@ -555,7 +560,7 @@ where
555
560
let volume_info = & self . open_volumes [ volume_idx] ;
556
561
let sfn = name. to_short_filename ( ) . map_err ( Error :: FilenameError ) ?;
557
562
558
- if volume_info. read_only && mode != Mode :: ReadOnly {
563
+ if volume_info. open_mode == VolumeOpenMode :: ReadOnly && mode != Mode :: ReadOnly {
559
564
return Err ( Error :: VolumeReadOnly ) ;
560
565
}
561
566
@@ -1708,7 +1713,7 @@ mod tests {
1708
1713
DUMMY ,
1709
1714
DUMMY ,
1710
1715
FAT32_PARTITION0_FAT_TABLE ,
1711
- ] )
1716
+ ] ) ,
1712
1717
}
1713
1718
}
1714
1719
@@ -1724,7 +1729,7 @@ mod tests {
1724
1729
DUMMY ,
1725
1730
DUMMY ,
1726
1731
FAT32_PARTITION0_FAT_TABLE_DIRTY ,
1727
- ] )
1732
+ ] ) ,
1728
1733
}
1729
1734
}
1730
1735
@@ -1741,7 +1746,7 @@ mod tests {
1741
1746
DUMMY ,
1742
1747
DUMMY ,
1743
1748
FAT32_PARTITION0_FAT_TABLE ,
1744
- ] )
1749
+ ] ) ,
1745
1750
}
1746
1751
}
1747
1752
}
@@ -1762,8 +1767,6 @@ mod tests {
1762
1767
start_block_idx : BlockIdx ,
1763
1768
_reason : & str ,
1764
1769
) -> Result < ( ) , Self :: Error > {
1765
-
1766
-
1767
1770
println ! (
1768
1771
"Reading block {} to {}" ,
1769
1772
start_block_idx. 0 ,
@@ -1797,21 +1800,19 @@ mod tests {
1797
1800
#[ test]
1798
1801
fn partition0 ( ) {
1799
1802
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 ) ;
1805
1804
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 ( ) ;
1807
1808
let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
1808
1809
assert_eq ! ( v, expected_id) ;
1809
1810
assert_eq ! (
1810
1811
& c. open_volumes[ 0 ] ,
1811
1812
& VolumeInfo {
1812
1813
volume_id: expected_id,
1813
1814
idx: VolumeIdx ( 0 ) ,
1814
- read_only : false ,
1815
+ open_mode : VolumeOpenMode :: ReadWrite ,
1815
1816
volume_type: VolumeType :: Fat ( crate :: FatVolume {
1816
1817
lba_start: BlockIdx ( 1 ) ,
1817
1818
num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1833,23 +1834,24 @@ mod tests {
1833
1834
) ;
1834
1835
let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
1835
1836
assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
1836
- c. set_volume_status_dirty ( fat_info, true ) . unwrap ( ) ;
1837
1837
}
1838
1838
1839
1839
#[ test]
1840
1840
fn partition0_dirty ( ) {
1841
1841
let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
1842
1842
VolumeManager :: new_with_limits ( DummyBlockDevice :: new_dirty ( ) , Clock , 0xAA00_0000 ) ;
1843
1843
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 ( ) ;
1845
1847
let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
1846
1848
assert_eq ! ( v, expected_id) ;
1847
1849
assert_eq ! (
1848
1850
& c. open_volumes[ 0 ] ,
1849
1851
& VolumeInfo {
1850
1852
volume_id: expected_id,
1851
1853
idx: VolumeIdx ( 0 ) ,
1852
- read_only : false ,
1854
+ open_mode : VolumeOpenMode :: ReadWrite ,
1853
1855
volume_type: VolumeType :: Fat ( crate :: FatVolume {
1854
1856
lba_start: BlockIdx ( 1 ) ,
1855
1857
num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1875,22 +1877,23 @@ mod tests {
1875
1877
1876
1878
#[ test]
1877
1879
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
+ ) ;
1884
1885
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 ( ) ;
1886
1889
let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
1887
1890
assert_eq ! ( v, expected_id) ;
1888
1891
assert_eq ! (
1889
1892
& c. open_volumes[ 0 ] ,
1890
1893
& VolumeInfo {
1891
1894
volume_id: expected_id,
1892
1895
idx: VolumeIdx ( 0 ) ,
1893
- read_only : false ,
1896
+ open_mode : VolumeOpenMode :: ReadWrite ,
1894
1897
volume_type: VolumeType :: Fat ( crate :: FatVolume {
1895
1898
lba_start: BlockIdx ( 1 ) ,
1896
1899
num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1913,7 +1916,10 @@ mod tests {
1913
1916
let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
1914
1917
assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
1915
1918
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
+ ) ;
1917
1923
assert_eq ! ( c. block_device. blocks. borrow( ) [ 2 ] , FAT32_PARTITION0_FSINFO ) ;
1918
1924
assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
1919
1925
assert_eq ! ( c. block_device. blocks. borrow( ) [ 4 ] , DUMMY ) ;
@@ -1930,7 +1936,10 @@ mod tests {
1930
1936
c. set_volume_status_dirty ( fat_info, false ) . unwrap ( ) ;
1931
1937
assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
1932
1938
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
+ ) ;
1934
1943
assert_eq ! ( c. block_device. blocks. borrow( ) [ 2 ] , FAT32_PARTITION0_FSINFO ) ;
1935
1944
assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
1936
1945
assert_eq ! ( c. block_device. blocks. borrow( ) [ 4 ] , DUMMY ) ;
0 commit comments