Skip to content

Commit be37965

Browse files
committed
Use a RefCell to provide interior mutability for VolumeManager.
1 parent aec477c commit be37965

19 files changed

+453
-407
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ let sdcard = embedded_sdmmc::SdCard::new(sdmmc_spi, delay);
1717
println!("Card size is {} bytes", sdcard.num_bytes()?);
1818
// Now let's look for volumes (also known as partitions) on our block device.
1919
// To do this we need a Volume Manager. It will take ownership of the block device.
20-
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
20+
let volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
2121
// Try and access Volume 0 (i.e. the first partition).
2222
// The volume object holds information about the filesystem on that volume.
23-
let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
23+
let volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
2424
println!("Volume 0: {:?}", volume0);
2525
// Open the root directory (mutably borrows from the volume).
26-
let mut root_dir = volume0.open_root_dir()?;
26+
let root_dir = volume0.open_root_dir()?;
2727
// Open a file called "MY_FILE.TXT" in the root directory
2828
// This mutably borrows the directory.
29-
let mut my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
29+
let my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
3030
// Print the contents of the file, assuming it's in ISO-8859-1 encoding
3131
while !my_file.is_eof() {
3232
let mut buffer = [0u8; 32];
@@ -43,7 +43,7 @@ By default the `VolumeManager` will initialize with a maximum number of `4` open
4343

4444
```rust
4545
// Create a volume manager with a maximum of 6 open directories, 12 open files, and 4 volumes (or partitions)
46-
let mut cont: VolumeManager<_, _, 6, 12, 4> = VolumeManager::new_with_limits(block, time_source);
46+
let cont: VolumeManager<_, _, 6, 12, 4> = VolumeManager::new_with_limits(block, time_source);
4747
```
4848

4949
## Supported features

examples/append_file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
3030
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
3131
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
3232
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
33-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
33+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
3434
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
35-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
36-
let mut root_dir = volume.open_root_dir()?;
35+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
36+
let root_dir = volume.open_root_dir()?;
3737
println!("\nCreating file {}...", FILE_TO_APPEND);
38-
let mut f = root_dir.open_file_in_dir(FILE_TO_APPEND, Mode::ReadWriteAppend)?;
38+
let f = root_dir.open_file_in_dir(FILE_TO_APPEND, Mode::ReadWriteAppend)?;
3939
f.write(b"\r\n\r\nThis has been added to your file.\r\n")?;
4040
Ok(())
4141
}

examples/big_dir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
1111
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
1212
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
1313
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
14-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
14+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
1515
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
16-
let mut volume = volume_mgr
16+
let volume = volume_mgr
1717
.open_volume(embedded_sdmmc::VolumeIdx(1))
1818
.unwrap();
1919
println!("Volume: {:?}", volume);
20-
let mut root_dir = volume.open_root_dir().unwrap();
20+
let root_dir = volume.open_root_dir().unwrap();
2121

2222
let mut file_num = 0;
2323
loop {
2424
file_num += 1;
2525
let file_name = format!("{}.da", file_num);
2626
println!("opening file {file_name} for writing");
27-
let mut file = root_dir
27+
let file = root_dir
2828
.open_file_in_dir(
2929
file_name.as_str(),
3030
embedded_sdmmc::Mode::ReadWriteCreateOrTruncate,

examples/create_file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
3030
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
3131
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
3232
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
33-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
33+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
3434
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
35-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
36-
let mut root_dir = volume.open_root_dir()?;
35+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
36+
let root_dir = volume.open_root_dir()?;
3737
println!("\nCreating file {}...", FILE_TO_CREATE);
3838
// This will panic if the file already exists: use ReadWriteCreateOrAppend
3939
// or ReadWriteCreateOrTruncate instead if you want to modify an existing
4040
// file.
41-
let mut f = root_dir.open_file_in_dir(FILE_TO_CREATE, Mode::ReadWriteCreate)?;
41+
let f = root_dir.open_file_in_dir(FILE_TO_CREATE, Mode::ReadWriteCreate)?;
4242
f.write(b"Hello, this is a new file on disk\r\n")?;
4343
Ok(())
4444
}

examples/delete_file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
3333
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
3434
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
3535
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
36-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
36+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
3737
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
38-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
39-
let mut root_dir = volume.open_root_dir()?;
38+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
39+
let root_dir = volume.open_root_dir()?;
4040
println!("Deleting file {}...", FILE_TO_DELETE);
4141
root_dir.delete_file_in_dir(FILE_TO_DELETE)?;
4242
println!("Deleted!");

examples/list_dir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ fn main() -> Result<(), Error> {
4747
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
4848
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
4949
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
50-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
50+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
5151
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
52-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
52+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
5353
let root_dir = volume.open_root_dir()?;
5454
list_dir(root_dir, "/")?;
5555
Ok(())
@@ -59,7 +59,7 @@ fn main() -> Result<(), Error> {
5959
///
6060
/// The path is for display purposes only.
6161
fn list_dir(
62-
mut directory: Directory<LinuxBlockDevice, Clock, 8, 8, 4>,
62+
directory: Directory<LinuxBlockDevice, Clock, 8, 8, 4>,
6363
path: &str,
6464
) -> Result<(), Error> {
6565
println!("Listing {}", path);

examples/read_file.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
4747
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
4848
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
4949
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
50-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
50+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
5151
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
52-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
53-
let mut root_dir = volume.open_root_dir()?;
52+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
53+
let root_dir = volume.open_root_dir()?;
5454
println!("\nReading file {}...", FILE_TO_READ);
55-
let mut f = root_dir.open_file_in_dir(FILE_TO_READ, Mode::ReadOnly)?;
55+
let f = root_dir.open_file_in_dir(FILE_TO_READ, Mode::ReadOnly)?;
56+
// Proves we can open two files at once now (or try to - this file doesn't exist)
57+
let f2 = root_dir.open_file_in_dir("MISSING.DAT", Mode::ReadOnly);
58+
assert!(f2.is_err());
5659
while !f.is_eof() {
5760
let mut buffer = [0u8; 16];
5861
let offset = f.offset();

examples/readme_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ fn main() -> Result<(), Error> {
125125
println!("Card size is {} bytes", sdcard.num_bytes()?);
126126
// Now let's look for volumes (also known as partitions) on our block device.
127127
// To do this we need a Volume Manager. It will take ownership of the block device.
128-
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
128+
let volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
129129
// Try and access Volume 0 (i.e. the first partition).
130130
// The volume object holds information about the filesystem on that volume.
131-
let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
131+
let volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
132132
println!("Volume 0: {:?}", volume0);
133133
// Open the root directory (mutably borrows from the volume).
134-
let mut root_dir = volume0.open_root_dir()?;
134+
let root_dir = volume0.open_root_dir()?;
135135
// Open a file called "MY_FILE.TXT" in the root directory
136136
// This mutably borrows the directory.
137-
let mut my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
137+
let my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
138138
// Print the contents of the file, assuming it's in ISO-8859-1 encoding
139139
while !my_file.is_eof() {
140140
let mut buffer = [0u8; 32];

examples/shell.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl Context {
228228
fn dir(&mut self, path: &Path) -> Result<(), Error> {
229229
println!("Directory listing of {:?}", path);
230230
let dir = self.resolve_existing_directory(path)?;
231-
let mut dir = dir.to_directory(&mut self.volume_mgr);
231+
let dir = dir.to_directory(&mut self.volume_mgr);
232232
dir.iterate_dir(|entry| {
233233
if !entry.attributes.is_volume() && !entry.attributes.is_lfn() {
234234
println!(
@@ -257,7 +257,7 @@ impl Context {
257257
///
258258
/// Will close the given directory.
259259
fn tree_dir(&mut self, dir: RawDirectory) -> Result<(), Error> {
260-
let mut dir = dir.to_directory(&mut self.volume_mgr);
260+
let dir = dir.to_directory(&mut self.volume_mgr);
261261
let mut children = Vec::new();
262262
dir.iterate_dir(|entry| {
263263
println!(
@@ -329,8 +329,8 @@ impl Context {
329329
/// print a text file
330330
fn cat(&mut self, filename: &Path) -> Result<(), Error> {
331331
let (dir, filename) = self.resolve_filename(filename)?;
332-
let mut dir = dir.to_directory(&mut self.volume_mgr);
333-
let mut f = dir.open_file_in_dir(filename, embedded_sdmmc::Mode::ReadOnly)?;
332+
let dir = dir.to_directory(&mut self.volume_mgr);
333+
let f = dir.open_file_in_dir(filename, embedded_sdmmc::Mode::ReadOnly)?;
334334
let mut data = Vec::new();
335335
while !f.is_eof() {
336336
let mut buffer = vec![0u8; 65536];
@@ -350,8 +350,8 @@ impl Context {
350350
/// print a binary file
351351
fn hexdump(&mut self, filename: &Path) -> Result<(), Error> {
352352
let (dir, filename) = self.resolve_filename(filename)?;
353-
let mut dir = dir.to_directory(&mut self.volume_mgr);
354-
let mut f = dir.open_file_in_dir(filename, embedded_sdmmc::Mode::ReadOnly)?;
353+
let dir = dir.to_directory(&mut self.volume_mgr);
354+
let f = dir.open_file_in_dir(filename, embedded_sdmmc::Mode::ReadOnly)?;
355355
let mut data = Vec::new();
356356
while !f.is_eof() {
357357
let mut buffer = vec![0u8; 65536];
@@ -387,7 +387,7 @@ impl Context {
387387
/// create a directory
388388
fn mkdir(&mut self, dir_name: &Path) -> Result<(), Error> {
389389
let (dir, filename) = self.resolve_filename(dir_name)?;
390-
let mut dir = dir.to_directory(&mut self.volume_mgr);
390+
let dir = dir.to_directory(&mut self.volume_mgr);
391391
dir.make_dir_in_dir(filename)
392392
}
393393

src/filesystem/directory.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl RawDirectory {
5757
const MAX_VOLUMES: usize,
5858
>(
5959
self,
60-
volume_mgr: &mut VolumeManager<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>,
60+
volume_mgr: &VolumeManager<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>,
6161
) -> Directory<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>
6262
where
6363
D: crate::BlockDevice,
@@ -87,7 +87,7 @@ pub struct Directory<
8787
T: crate::TimeSource,
8888
{
8989
raw_directory: RawDirectory,
90-
volume_mgr: &'a mut VolumeManager<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>,
90+
volume_mgr: &'a VolumeManager<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>,
9191
}
9292

9393
impl<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize>
@@ -99,7 +99,7 @@ where
9999
/// Create a new `Directory` from a `RawDirectory`
100100
pub fn new(
101101
raw_directory: RawDirectory,
102-
volume_mgr: &'a mut VolumeManager<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>,
102+
volume_mgr: &'a VolumeManager<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>,
103103
) -> Directory<'a, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES> {
104104
Directory {
105105
raw_directory,
@@ -111,7 +111,7 @@ where
111111
///
112112
/// You can then read the directory entries with `iterate_dir` and `open_file_in_dir`.
113113
pub fn open_dir<N>(
114-
&mut self,
114+
&self,
115115
name: N,
116116
) -> Result<Directory<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>, Error<D::Error>>
117117
where
@@ -135,7 +135,7 @@ where
135135
}
136136

137137
/// Look in a directory for a named file.
138-
pub fn find_directory_entry<N>(&mut self, name: N) -> Result<DirEntry, Error<D::Error>>
138+
pub fn find_directory_entry<N>(&self, name: N) -> Result<DirEntry, Error<D::Error>>
139139
where
140140
N: ToShortFileName,
141141
{
@@ -144,7 +144,7 @@ where
144144
}
145145

146146
/// Call a callback function for each directory entry in a directory.
147-
pub fn iterate_dir<F>(&mut self, func: F) -> Result<(), Error<D::Error>>
147+
pub fn iterate_dir<F>(&self, func: F) -> Result<(), Error<D::Error>>
148148
where
149149
F: FnMut(&DirEntry),
150150
{
@@ -153,7 +153,7 @@ where
153153

154154
/// Open a file with the given full path. A file can only be opened once.
155155
pub fn open_file_in_dir<N>(
156-
&mut self,
156+
&self,
157157
name: N,
158158
mode: crate::Mode,
159159
) -> Result<crate::File<D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>, crate::Error<D::Error>>
@@ -167,15 +167,15 @@ where
167167
}
168168

169169
/// Delete a closed file with the given filename, if it exists.
170-
pub fn delete_file_in_dir<N>(&mut self, name: N) -> Result<(), Error<D::Error>>
170+
pub fn delete_file_in_dir<N>(&self, name: N) -> Result<(), Error<D::Error>>
171171
where
172172
N: ToShortFileName,
173173
{
174174
self.volume_mgr.delete_file_in_dir(self.raw_directory, name)
175175
}
176176

177177
/// Make a directory inside this directory
178-
pub fn make_dir_in_dir<N>(&mut self, name: N) -> Result<(), Error<D::Error>>
178+
pub fn make_dir_in_dir<N>(&self, name: N) -> Result<(), Error<D::Error>>
179179
where
180180
N: ToShortFileName,
181181
{
@@ -239,9 +239,9 @@ where
239239
#[derive(Debug, Clone)]
240240
pub(crate) struct DirectoryInfo {
241241
/// The handle for this directory.
242-
pub(crate) directory_id: RawDirectory,
242+
pub(crate) raw_directory: RawDirectory,
243243
/// The handle for the volume this directory is on
244-
pub(crate) volume_id: RawVolume,
244+
pub(crate) raw_volume: RawVolume,
245245
/// The starting point of the directory listing.
246246
pub(crate) cluster: ClusterId,
247247
}

0 commit comments

Comments
 (0)