diff --git a/examples/shell.rs b/examples/shell.rs index 0e41ce1..030e008 100644 --- a/examples/shell.rs +++ b/examples/shell.rs @@ -73,6 +73,7 @@ use std::io::prelude::*; use embedded_sdmmc::{ Error as EsError, RawDirectory, RawVolume, ShortFileName, VolumeIdx, VolumeManager, + VolumeOpenMode, }; use crate::linux::{Clock, LinuxBlockDevice}; @@ -531,7 +532,10 @@ fn main() -> Result<(), Error> { let mut current_volume = None; for volume_no in 0..4 { - match ctx.volume_mgr.open_raw_volume(VolumeIdx(volume_no)) { + match ctx + .volume_mgr + .open_raw_volume(VolumeIdx(volume_no), VolumeOpenMode::ReadWrite) + { Ok(volume) => { println!("Volume # {}: found", Context::volume_to_letter(volume_no)); match ctx.volume_mgr.open_root_dir(volume) { diff --git a/src/lib.rs b/src/lib.rs index aa98fb6..18b3ad1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,7 +75,7 @@ pub mod filesystem; pub mod sdcard; use filesystem::SearchId; -use volume_mgr::VolumeOpenMode; +pub use volume_mgr::VolumeOpenMode; #[doc(inline)] pub use crate::blockdevice::{Block, BlockCount, BlockDevice, BlockIdx}; diff --git a/src/volume_mgr.rs b/src/volume_mgr.rs index 3f25568..aa21d85 100644 --- a/src/volume_mgr.rs +++ b/src/volume_mgr.rs @@ -18,9 +18,12 @@ use crate::{ }; use heapless::Vec; +/// Opening volume mode #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum VolumeOpenMode { + /// Opening volume read only. This is faster than ReadWrite if only readings are done ReadOnly, + /// Opening volume for reading and writing. ReadWrite, } diff --git a/tests/directories.rs b/tests/directories.rs index df9154f..363d18a 100644 --- a/tests/directories.rs +++ b/tests/directories.rs @@ -1,6 +1,6 @@ //! Directory related tests -use embedded_sdmmc::{Mode, ShortFileName}; +use embedded_sdmmc::{Mode, ShortFileName, VolumeOpenMode}; mod utils; @@ -41,7 +41,7 @@ fn fat16_root_directory_listing() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat16_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); let root_dir = volume_mgr .open_root_dir(fat16_volume) @@ -103,7 +103,7 @@ fn fat16_sub_directory_listing() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat16_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); let root_dir = volume_mgr .open_root_dir(fat16_volume) @@ -168,7 +168,7 @@ fn fat32_root_directory_listing() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat32_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(1)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(1), VolumeOpenMode::ReadWrite) .expect("open volume 1"); let root_dir = volume_mgr .open_root_dir(fat32_volume) @@ -230,7 +230,7 @@ fn open_dir_twice() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat32_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(1)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(1), VolumeOpenMode::ReadWrite) .expect("open volume 1"); let root_dir = volume_mgr @@ -276,7 +276,7 @@ fn open_too_many_dirs() { > = embedded_sdmmc::VolumeManager::new_with_limits(disk, time_source, 0x1000_0000); let fat32_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(1)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(1), VolumeOpenMode::ReadWrite) .expect("open volume 1"); let root_dir = volume_mgr .open_root_dir(fat32_volume) @@ -295,7 +295,7 @@ fn find_dir_entry() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat32_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(1)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(1), VolumeOpenMode::ReadWrite) .expect("open volume 1"); let root_dir = volume_mgr @@ -325,7 +325,7 @@ fn delete_file() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat32_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(1)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(1), VolumeOpenMode::ReadWrite) .expect("open volume 1"); let root_dir = volume_mgr @@ -370,7 +370,7 @@ fn make_directory() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat32_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(1)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(1), VolumeOpenMode::ReadWrite) .expect("open volume 1"); let root_dir = volume_mgr diff --git a/tests/open_files.rs b/tests/open_files.rs index dcdaadf..c4e14da 100644 --- a/tests/open_files.rs +++ b/tests/open_files.rs @@ -1,6 +1,6 @@ //! File opening related tests -use embedded_sdmmc::{Error, Mode, VolumeIdx, VolumeManager}; +use embedded_sdmmc::{Error, Mode, VolumeIdx, VolumeManager, VolumeOpenMode}; mod utils; @@ -11,7 +11,7 @@ fn open_files() { let mut volume_mgr: VolumeManager>, utils::TestTimeSource, 4, 2, 1> = VolumeManager::new_with_limits(disk, time_source, 0xAA00_0000); let volume = volume_mgr - .open_raw_volume(VolumeIdx(0)) + .open_raw_volume(VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume"); let root_dir = volume_mgr.open_root_dir(volume).expect("open root dir"); diff --git a/tests/read_file.rs b/tests/read_file.rs index b0e7628..3d2f92e 100644 --- a/tests/read_file.rs +++ b/tests/read_file.rs @@ -14,7 +14,7 @@ fn read_file_512_blocks() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat16_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); let root_dir = volume_mgr .open_root_dir(fat16_volume) @@ -56,7 +56,7 @@ fn read_file_all() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat16_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); let root_dir = volume_mgr .open_root_dir(fat16_volume) @@ -90,7 +90,7 @@ fn read_file_prime_blocks() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat16_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); let root_dir = volume_mgr .open_root_dir(fat16_volume) @@ -133,7 +133,7 @@ fn read_file_backwards() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let fat16_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); let root_dir = volume_mgr .open_root_dir(fat16_volume) diff --git a/tests/volume.rs b/tests/volume.rs index 633a8d2..b3ec0cc 100644 --- a/tests/volume.rs +++ b/tests/volume.rs @@ -1,5 +1,7 @@ //! Volume related tests +use embedded_sdmmc::VolumeOpenMode; + mod utils; #[test] @@ -16,12 +18,12 @@ fn open_all_volumes() { // Open Volume 0 let fat16_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); // Fail to Open Volume 0 again assert!(matches!( - volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(0)), + volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite), Err(embedded_sdmmc::Error::VolumeAlreadyOpen) )); @@ -29,23 +31,23 @@ fn open_all_volumes() { // Open Volume 1 let fat32_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(1)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(1), VolumeOpenMode::ReadWrite) .expect("open volume 1"); // Fail to Volume 1 again assert!(matches!( - volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(1)), + volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(1), VolumeOpenMode::ReadWrite), Err(embedded_sdmmc::Error::VolumeAlreadyOpen) )); // Open Volume 0 again let fat16_volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); // Open any volume - too many volumes (0 and 1 are open) assert!(matches!( - volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(0)), + volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite), Err(embedded_sdmmc::Error::TooManyOpenVolumes) )); @@ -54,13 +56,13 @@ fn open_all_volumes() { // This isn't a valid volume assert!(matches!( - volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(2)), + volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(2), VolumeOpenMode::ReadWrite), Err(embedded_sdmmc::Error::FormatError(_e)) )); // This isn't a valid volume assert!(matches!( - volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(9)), + volume_mgr.open_raw_volume(embedded_sdmmc::VolumeIdx(9), VolumeOpenMode::ReadWrite), Err(embedded_sdmmc::Error::NoSuchVolume) )); @@ -79,7 +81,7 @@ fn close_volume_too_early() { let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); let volume = volume_mgr - .open_raw_volume(embedded_sdmmc::VolumeIdx(0)) + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadWrite) .expect("open volume 0"); let root_dir = volume_mgr.open_root_dir(volume).expect("open root dir"); @@ -102,6 +104,40 @@ fn close_volume_too_early() { )); } +#[test] +fn volume_read_only_open_file_read_write() { + let time_source = utils::make_time_source(); + let disk = utils::make_block_device(utils::DISK_SOURCE).unwrap(); + let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); + + let volume = volume_mgr + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadOnly) + .expect("open volume 0"); + let root_dir = volume_mgr.open_root_dir(volume).expect("open root dir"); + + // Dir open + assert!(matches!( + volume_mgr.open_file_in_dir(root_dir, "64MB.DAT", embedded_sdmmc::Mode::ReadWriteAppend), + Err(embedded_sdmmc::Error::VolumeReadOnly) + )); +} + +#[test] +fn volume_read_only_open_file_read_only() { + let time_source = utils::make_time_source(); + let disk = utils::make_block_device(utils::DISK_SOURCE).unwrap(); + let mut volume_mgr = embedded_sdmmc::VolumeManager::new(disk, time_source); + + let volume = volume_mgr + .open_raw_volume(embedded_sdmmc::VolumeIdx(0), VolumeOpenMode::ReadOnly) + .expect("open volume 0"); + let root_dir = volume_mgr.open_root_dir(volume).expect("open root dir"); + + volume_mgr + .open_file_in_dir(root_dir, "64MB.DAT", embedded_sdmmc::Mode::ReadOnly) + .unwrap(); +} + // **************************************************************************** // // End Of File