@@ -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
@@ -1691,7 +1696,7 @@ mod tests {
1691
1696
DUMMY ,
1692
1697
DUMMY ,
1693
1698
FAT32_PARTITION0_FAT_TABLE ,
1694
- ] )
1699
+ ] ) ,
1695
1700
}
1696
1701
}
1697
1702
@@ -1707,7 +1712,7 @@ mod tests {
1707
1712
DUMMY ,
1708
1713
DUMMY ,
1709
1714
FAT32_PARTITION0_FAT_TABLE_DIRTY ,
1710
- ] )
1715
+ ] ) ,
1711
1716
}
1712
1717
}
1713
1718
@@ -1724,7 +1729,7 @@ mod tests {
1724
1729
DUMMY ,
1725
1730
DUMMY ,
1726
1731
FAT32_PARTITION0_FAT_TABLE ,
1727
- ] )
1732
+ ] ) ,
1728
1733
}
1729
1734
}
1730
1735
}
@@ -1745,8 +1750,6 @@ mod tests {
1745
1750
start_block_idx : BlockIdx ,
1746
1751
_reason : & str ,
1747
1752
) -> Result < ( ) , Self :: Error > {
1748
-
1749
-
1750
1753
println ! (
1751
1754
"Reading block {} to {}" ,
1752
1755
start_block_idx. 0 ,
@@ -1780,21 +1783,19 @@ mod tests {
1780
1783
#[ test]
1781
1784
fn partition0 ( ) {
1782
1785
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 ) ;
1788
1787
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 ( ) ;
1790
1791
let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
1791
1792
assert_eq ! ( v, expected_id) ;
1792
1793
assert_eq ! (
1793
1794
& c. open_volumes[ 0 ] ,
1794
1795
& VolumeInfo {
1795
1796
volume_id: expected_id,
1796
1797
idx: VolumeIdx ( 0 ) ,
1797
- read_only : false ,
1798
+ open_mode : VolumeOpenMode :: ReadWrite ,
1798
1799
volume_type: VolumeType :: Fat ( crate :: FatVolume {
1799
1800
lba_start: BlockIdx ( 1 ) ,
1800
1801
num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1816,23 +1817,24 @@ mod tests {
1816
1817
) ;
1817
1818
let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
1818
1819
assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
1819
- c. set_volume_status_dirty ( fat_info, true ) . unwrap ( ) ;
1820
1820
}
1821
1821
1822
1822
#[ test]
1823
1823
fn partition0_dirty ( ) {
1824
1824
let mut c: VolumeManager < DummyBlockDevice , Clock , 2 , 2 > =
1825
1825
VolumeManager :: new_with_limits ( DummyBlockDevice :: new_dirty ( ) , Clock , 0xAA00_0000 ) ;
1826
1826
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 ( ) ;
1828
1830
let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
1829
1831
assert_eq ! ( v, expected_id) ;
1830
1832
assert_eq ! (
1831
1833
& c. open_volumes[ 0 ] ,
1832
1834
& VolumeInfo {
1833
1835
volume_id: expected_id,
1834
1836
idx: VolumeIdx ( 0 ) ,
1835
- read_only : false ,
1837
+ open_mode : VolumeOpenMode :: ReadWrite ,
1836
1838
volume_type: VolumeType :: Fat ( crate :: FatVolume {
1837
1839
lba_start: BlockIdx ( 1 ) ,
1838
1840
num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1858,22 +1860,23 @@ mod tests {
1858
1860
1859
1861
#[ test]
1860
1862
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
+ ) ;
1867
1868
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 ( ) ;
1869
1872
let expected_id = RawVolume ( SearchId ( 0xAA00_0000 ) ) ;
1870
1873
assert_eq ! ( v, expected_id) ;
1871
1874
assert_eq ! (
1872
1875
& c. open_volumes[ 0 ] ,
1873
1876
& VolumeInfo {
1874
1877
volume_id: expected_id,
1875
1878
idx: VolumeIdx ( 0 ) ,
1876
- read_only : false ,
1879
+ open_mode : VolumeOpenMode :: ReadWrite ,
1877
1880
volume_type: VolumeType :: Fat ( crate :: FatVolume {
1878
1881
lba_start: BlockIdx ( 1 ) ,
1879
1882
num_blocks: BlockCount ( 0x0011_2233 ) ,
@@ -1896,7 +1899,10 @@ mod tests {
1896
1899
let VolumeType :: Fat ( fat_info) = & c. open_volumes [ 0 ] . volume_type ;
1897
1900
assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
1898
1901
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
+ ) ;
1900
1906
assert_eq ! ( c. block_device. blocks. borrow( ) [ 2 ] , FAT32_PARTITION0_FSINFO ) ;
1901
1907
assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
1902
1908
assert_eq ! ( c. block_device. blocks. borrow( ) [ 4 ] , DUMMY ) ;
@@ -1913,7 +1919,10 @@ mod tests {
1913
1919
c. set_volume_status_dirty ( fat_info, false ) . unwrap ( ) ;
1914
1920
assert_eq ! ( c. volume_status_dirty( fat_info) . unwrap( ) , false ) ;
1915
1921
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
+ ) ;
1917
1926
assert_eq ! ( c. block_device. blocks. borrow( ) [ 2 ] , FAT32_PARTITION0_FSINFO ) ;
1918
1927
assert_eq ! ( c. block_device. blocks. borrow( ) [ 3 ] . contents[ 7 ] & ( 1 << 3 ) , 8 ) ;
1919
1928
assert_eq ! ( c. block_device. blocks. borrow( ) [ 4 ] , DUMMY ) ;
0 commit comments