Skip to content

Commit 6078e8c

Browse files
Reworked the examples.
Volume Manager API also now takes things that are "convertible to a short file name", so you can use a ShortFileName, or a &str.
1 parent fa56d67 commit 6078e8c

17 files changed

+537
-869
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2525

2626
### Added
2727

28-
- None
28+
- New examples, `append_file`, `create_file`, `delete_file`, `list_dir`
29+
- New test cases `tests/directories.rs`, `tests/read_file.rs`
2930

3031
### Removed
3132

3233
- __Breaking Change__: `Controller` alias for `VolumeManager` removed.
34+
- Old examples `create_test`, `test_mount`, `write_test`, `delete_test`
3335

3436
## [Version 0.5.0](https://github.com/rust-embedded-community/embedded-sdmmc-rs/releases/tag/v0.5.0) - 2023-05-20
3537

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ env_logger = "0.9"
2222
hex-literal = "0.3"
2323
flate2 = "1.0"
2424
sha256 = "1.4"
25+
chrono = "0.4"
2526

2627
[features]
2728
default = ["log"]

examples/append_file.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Append File Example.
2+
//!
3+
//! ```bash
4+
//! $ cargo run --example append_file -- ./disk.img
5+
//! $ cargo run --example append_file -- /dev/mmcblk0
6+
//! ```
7+
//!
8+
//! If you pass a block device it should be unmounted. No testing has been
9+
//! performed with Windows raw block devices - please report back if you try
10+
//! this! There is a gzipped example disk image which you can gunzip and test
11+
//! with if you don't have a suitable block device.
12+
//!
13+
//! ```bash
14+
//! zcat ./tests/disk.img.gz > ./disk.img
15+
//! $ cargo run --example append_file -- ./disk.img
16+
//! ```
17+
18+
extern crate embedded_sdmmc;
19+
20+
mod linux;
21+
use linux::*;
22+
23+
const FILE_TO_APPEND: &str = "README.TXT";
24+
25+
use embedded_sdmmc::{Error, Mode, VolumeIdx, VolumeManager};
26+
27+
fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
28+
env_logger::init();
29+
let mut args = std::env::args().skip(1);
30+
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
31+
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
32+
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
33+
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
34+
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
35+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
36+
let root_dir = volume_mgr.open_root_dir(volume)?;
37+
println!("\nCreating file {}...", FILE_TO_APPEND);
38+
let f = volume_mgr.open_file_in_dir(root_dir, FILE_TO_APPEND, Mode::ReadWriteAppend)?;
39+
volume_mgr.write(f, b"\r\n\r\nThis has been added to your file.\r\n")?;
40+
volume_mgr.close_file(f)?;
41+
volume_mgr.close_dir(root_dir)?;
42+
Ok(())
43+
}
44+
45+
// ****************************************************************************
46+
//
47+
// End Of File
48+
//
49+
// ****************************************************************************

examples/create_file.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! Create File Example.
2+
//!
3+
//! ```bash
4+
//! $ cargo run --example create_file -- ./disk.img
5+
//! $ cargo run --example create_file -- /dev/mmcblk0
6+
//! ```
7+
//!
8+
//! If you pass a block device it should be unmounted. No testing has been
9+
//! performed with Windows raw block devices - please report back if you try
10+
//! this! There is a gzipped example disk image which you can gunzip and test
11+
//! with if you don't have a suitable block device.
12+
//!
13+
//! ```bash
14+
//! zcat ./tests/disk.img.gz > ./disk.img
15+
//! $ cargo run --example create_file -- ./disk.img
16+
//! ```
17+
18+
extern crate embedded_sdmmc;
19+
20+
mod linux;
21+
use linux::*;
22+
23+
const FILE_TO_CREATE: &str = "CREATE.TXT";
24+
25+
use embedded_sdmmc::{Error, Mode, VolumeIdx, VolumeManager};
26+
27+
fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
28+
env_logger::init();
29+
let mut args = std::env::args().skip(1);
30+
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
31+
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
32+
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
33+
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
34+
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
35+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
36+
let root_dir = volume_mgr.open_root_dir(volume)?;
37+
println!("\nCreating file {}...", FILE_TO_CREATE);
38+
// This will panic if the file already exists: use ReadWriteCreateOrAppend
39+
// or ReadWriteCreateOrTruncate instead if you want to modify an existing
40+
// file.
41+
let f = volume_mgr.open_file_in_dir(root_dir, FILE_TO_CREATE, Mode::ReadWriteCreate)?;
42+
volume_mgr.write(f, b"Hello, this is a new file on disk\r\n")?;
43+
volume_mgr.close_file(f)?;
44+
volume_mgr.close_dir(root_dir)?;
45+
Ok(())
46+
}
47+
48+
// ****************************************************************************
49+
//
50+
// End Of File
51+
//
52+
// ****************************************************************************

examples/create_test.rs

Lines changed: 0 additions & 193 deletions
This file was deleted.

examples/delete_file.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! Delete File Example.
2+
//!
3+
//! ```bash
4+
//! $ cargo run --example delete_file -- ./disk.img
5+
//! $ cargo run --example delete_file -- /dev/mmcblk0
6+
//! ```
7+
//!
8+
//! NOTE: THIS EXAMPLE DELETES A FILE CALLED README.TXT. IF YOU DO NOT WANT THAT
9+
//! FILE DELETED FROM YOUR DISK IMAGE, DO NOT RUN THIS EXAMPLE.
10+
//!
11+
//! If you pass a block device it should be unmounted. No testing has been
12+
//! performed with Windows raw block devices - please report back if you try
13+
//! this! There is a gzipped example disk image which you can gunzip and test
14+
//! with if you don't have a suitable block device.
15+
//!
16+
//! ```bash
17+
//! zcat ./tests/disk.img.gz > ./disk.img
18+
//! $ cargo run --example delete_file -- ./disk.img
19+
//! ```
20+
21+
extern crate embedded_sdmmc;
22+
23+
mod linux;
24+
use linux::*;
25+
26+
const FILE_TO_DELETE: &str = "README.TXT";
27+
28+
use embedded_sdmmc::{Error, VolumeIdx, VolumeManager};
29+
30+
fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
31+
env_logger::init();
32+
let mut args = std::env::args().skip(1);
33+
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
34+
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
35+
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
36+
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
37+
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
38+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
39+
let root_dir = volume_mgr.open_root_dir(volume)?;
40+
println!("Deleting file {}...", FILE_TO_DELETE);
41+
volume_mgr.delete_file_in_dir(root_dir, FILE_TO_DELETE)?;
42+
println!("Deleted!");
43+
volume_mgr.close_dir(root_dir)?;
44+
Ok(())
45+
}
46+
47+
// ****************************************************************************
48+
//
49+
// End Of File
50+
//
51+
// ****************************************************************************

0 commit comments

Comments
 (0)