Skip to content

Release v0.9.0 #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 89 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
6bd9b92
Merge pull request #139 from rust-embedded-community/main
eldruin Jul 13, 2024
7951b76
Implement embedded-io traits for File.
qwandor Aug 7, 2024
011726b
Merge pull request #140 from qwandor/embedded-io
thejpster Aug 7, 2024
3e672aa
Prefer chunks_exact.
thejpster Oct 11, 2024
5e718c2
Remove the reason from the Block Device API.
thejpster Oct 11, 2024
fe05b9f
Rename SearchId to Handle.
thejpster Oct 11, 2024
cc62a8f
Improve debug print for Handle.
thejpster Oct 11, 2024
0b860bc
Merge pull request #149 from rust-embedded-community/rename-searchid
thejpster Oct 11, 2024
db06494
Minor tidy up
thejpster Oct 6, 2024
a340b4d
Better support for volume labels.
thejpster Oct 6, 2024
77b45be
Simplify some casts
thejpster Oct 6, 2024
3548e14
Add extra test opening volumes.
thejpster Oct 11, 2024
504b440
Merge pull request #144 from rust-embedded-community/more-volume-labels
thejpster Oct 11, 2024
62546fc
Merge branch 'develop' into chunks-exact
thejpster Oct 12, 2024
aec477c
Merge pull request #147 from rust-embedded-community/chunks-exact
thejpster Oct 12, 2024
be37965
Use a RefCell to provide interior mutability for VolumeManager.
thejpster Oct 6, 2024
e9ec423
Return an error on lock failure.
thejpster Oct 11, 2024
870ed18
Simplify the shell example.
thejpster Oct 11, 2024
b6b5bd5
Fixed resource leak reading volume labels.
thejpster Oct 12, 2024
41cbb62
Make ClusterId print nicer.
thejpster Oct 12, 2024
f5bafa5
Ensure DirectoryInfo values are called dir_info
thejpster Oct 12, 2024
d2039d4
Clean up shell example
thejpster Oct 12, 2024
413d814
Better notes about double locking.
thejpster Oct 12, 2024
d33233d
Merge pull request #143 from rust-embedded-community/interior-mutability
thejpster Oct 12, 2024
c12c7ee
Better trace logs.
thejpster Oct 12, 2024
ae72878
Merge pull request #151 from rust-embedded-community/better-trace-logs
eldruin Oct 13, 2024
92b0c6a
Re-organise the blockdevice.rs file.
thejpster Oct 16, 2024
2a60468
Add a general BlockCache.
thejpster Oct 16, 2024
2fbd6cb
Pack the block cache into the existing RefCell.
thejpster Oct 16, 2024
242e05f
Invalidate cache if read fails.
thejpster Oct 16, 2024
c89b01b
Create a VolumeManager type alias
thejpster Oct 5, 2024
c67d6ca
Merge pull request #148 from rust-embedded-community/block-cache
thejpster Oct 19, 2024
1b9fa5c
Merge pull request #152 from rust-embedded-community/simplify-examples
thejpster Oct 19, 2024
7478430
Add a csum() method to ShortFileName.
jonathanpallant Oct 25, 2024
f2daa2e
Ensure cluster IDs print with a fixed width.
jonathanpallant Oct 25, 2024
20e5702
Ensure attributes print with a fixed width.
jonathanpallant Oct 25, 2024
fe2bdff
Add an LfnBuffer type.
jonathanpallant Oct 25, 2024
d89944c
Add iterate_dir_lfn.
jonathanpallant Oct 25, 2024
1ed2c1a
Fix defmt logs for LFNs.
jonathanpallant Oct 25, 2024
c8cc20f
LfnBuffer now borrows its contents.
thejpster Oct 27, 2024
b77b9ca
Handle surrogate-pairs in LFNs.
thejpster Oct 27, 2024
837ae27
Add missing items to CHANGELOG.
thejpster Oct 29, 2024
2c836db
Remove the last stack allocated block.
thejpster Oct 29, 2024
83831c0
Use libcore to memset a slice.
thejpster Oct 29, 2024
dfa1c3e
Create a VolumeManager type alias
thejpster Oct 5, 2024
7ed6189
Write to the second FAT, if it exists.
thejpster Oct 29, 2024
60f5021
Update the free cluster count when unmounting.
thejpster Oct 29, 2024
5c8539e
Merge branch 'develop' into write-second-fat
thejpster Oct 29, 2024
52e0e4f
Merge pull request #157 from rust-embedded-community/add-lfn-support
eldruin Oct 29, 2024
17b733d
Merge branch 'develop' into write-second-fat
thejpster Oct 29, 2024
8c944ce
Merge pull request #160 from rust-embedded-community/write-second-fat
thejpster Oct 29, 2024
b86f672
Merge branch 'develop' into remove-stacked-block
thejpster Oct 29, 2024
2c08eac
Merge pull request #159 from rust-embedded-community/remove-stacked-b…
thejpster Nov 3, 2024
a6cc6cd
Move FAT-specific code in make_dir_in_dir.
thejpster Oct 29, 2024
567aa09
Use cluster 0 when the parent dir is the root, for both FAT16 and FAT32.
thejpster Oct 29, 2024
2eed03b
Update disk image.
thejpster Oct 29, 2024
f497cc2
Remove resolved TODO
thejpster Nov 3, 2024
8a8d52c
Merge pull request #158 from rust-embedded-community/move-make-dir-code
thejpster Nov 3, 2024
ca9c9fa
Add support for partition id 04h
Graicc Nov 14, 2024
cad9505
Merge pull request #165 from Graicc/develop
thejpster Nov 15, 2024
dc223a1
chore Cargo - Set rust-version & add matrix for building and testing …
elpiel Nov 22, 2024
c941b5a
fix: MSRV - inclusive pattern matching
elpiel Nov 24, 2024
fb8e5d2
fix: volume - name - replace trim_ascii_end fn call
elpiel Nov 26, 2024
376035a
Merge pull request #167 from LechevSpace/chore/set-msrv
thejpster Nov 26, 2024
f7cdece
docs: README write to file example; reminder to run `file.flush()`
zpg6 Jan 18, 2025
bc9d7fd
fix: shorten file name
zpg6 Jan 18, 2025
9fdc5f6
fix: shorten filename to fit in "8.3"
zpg6 Jan 18, 2025
758c6bd
Add blank line
thejpster Jan 18, 2025
6ebe113
Merge pull request #174 from zpg6/docs/flush-after-write
thejpster Jan 18, 2025
714d3eb
docs: adds same snippet of writing to files to `lib.rs` for crate doc
zpg6 Jan 18, 2025
8a407ea
fix: `ignore` will pass ci/cd for known-to-be pseudocode
zpg6 Jan 18, 2025
b3014cf
fix: extraneous comma
zpg6 Jan 18, 2025
a9fd5a4
Update src/lib.rs with suggestion
zpg6 Jan 19, 2025
e7eef2a
Merge pull request #175 from zpg6/docs/writing-to-files-snippet-for-c…
thejpster Jan 20, 2025
8aceb57
Clean up imports and help text for examples.
thejpster Feb 8, 2025
5a5b4a9
Merge pull request #177 from rust-embedded-community/change-example-i…
thejpster Feb 9, 2025
e1f379f
fix bad typing in defmt debug
ValouBambou Feb 28, 2025
54bebcd
ci: set DEFMT_LOG env var in build
ValouBambou Mar 6, 2025
a17e0bc
Merge pull request #183 from ValouBambou/develop
thejpster Mar 7, 2025
82b0b02
Merge branch 'main' into update-develop
thejpster Jun 7, 2025
7e98d11
Merge pull request #190 from rust-embedded-community/update-develop
thejpster Jun 7, 2025
ef96021
fix seek and write to block start zeros rest of block after written d…
Jun 7, 2025
d94fe86
cargo fmt
Jun 7, 2025
8ad3628
improve to avoid unneeded read before write when writing 512 bytes to…
Jun 7, 2025
97b2471
update changelog
Jun 7, 2025
5a67cfb
Merge pull request #189 from yanshay/yanshay_fix_seek_write_block_start
thejpster Jun 7, 2025
a6b818e
Merge branch 'main' into develop
thejpster Jun 7, 2025
89ea599
Prepare a release of 0.9
thejpster Jun 7, 2025
fe6d98c
Fix typo in CHANGELOG
thejpster Jun 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@ jobs:
formatting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Check formatting
run: cargo fmt -- --check

build-test:
runs-on: ubuntu-latest
strategy:
matrix:
# Always run MSRV too!
rust: ["stable", "1.76"]
features: ['log', 'defmt-log', '""']
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- name: Build
run: cargo build --no-default-features --features ${{matrix.features}} --verbose
env:
DEFMT_LOG: debug
- name: Run Tests
run: cargo test --no-default-features --features ${{matrix.features}} --verbose
18 changes: 14 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic

## [Unreleased]

## [Version 0.9.0] - 2025-06-08

### Changed

- None
- __Breaking Change__: `VolumeManager` now uses interior-mutability (with a `RefCell`) and so most methods are now `&self`. This also makes it easier to open multiple `File`, `Directory` or `Volume` objects at once.
- __Breaking Change__: The `VolumeManager`, `File`, `Directory` and `Volume` no longer implement `Send` or `Sync`.
- `VolumeManager` uses an interior block cache of 512 bytes, increasing its size by about 520 bytes but hugely reducing stack space required at run-time.
- __Breaking Change__: The `VolumeManager::device` method now takes a callback rather than giving you a reference to the underlying `BlockDevice`
- __Breaking Change__: `Error:LockError` variant added.
- __Breaking Change__: `SearchId` was renamed to `Handle`
- Fixed writing at block start mid-file (previously overwrote subsequent file data with zeros up to the end of the block)

### Added

- None
- `File` now implements the `embedded-io` `Read`, `Write` and `Seek` traits.
- New `iterate_dir_lfn` method on `VolumeManager` and `Directory` - provides decoded Long File Names as `Option<&str>`

### Removed

- None
- __Breaking Change__: Removed the `reason: &str` argument from `BlockDevice`

## [Version 0.8.2] - 2025-06-07

Expand Down Expand Up @@ -180,7 +189,8 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: http://semver.org/spec/v2.0.0.html
[Unreleased]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/compare/v0.8.2...develop
[Unreleased]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/compare/v0.9.0...develop
[Version 0.9.0]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/compare/v0.8.2...v0.9.0
[Version 0.8.2]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/compare/v0.8.1...v0.8.2
[Version 0.8.1]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/compare/v0.8.0...v0.8.1
[Version 0.8.0]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/compare/v0.7.0...v0.8.0
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ license = "MIT OR Apache-2.0"
name = "embedded-sdmmc"
readme = "README.md"
repository = "https://github.com/rust-embedded-community/embedded-sdmmc-rs"
version = "0.8.2"
version = "0.9.0"

# Make sure to update the CI too!
rust-version = "1.76"

[dependencies]
byteorder = {version = "1", default-features = false}
defmt = {version = "0.3", optional = true}
embedded-hal = "1.0.0"
embedded-io = "0.6.1"
heapless = "^0.8"
log = {version = "0.4", default-features = false, optional = true}

Expand Down
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ designed for readability and simplicity over performance.
You will need something that implements the `BlockDevice` trait, which can read and write the 512-byte blocks (or sectors) from your card. If you were to implement this over USB Mass Storage, there's no reason this crate couldn't work with a USB Thumb Drive, but we only supply a `BlockDevice` suitable for reading SD and SDHC cards over SPI.

```rust
use embedded_sdmmc::{SdCard, VolumeManager, Mode, VolumeIdx};
// Build an SD Card interface out of an SPI device, a chip-select pin and the delay object
let sdcard = embedded_sdmmc::SdCard::new(sdmmc_spi, delay);
let sdcard = SdCard::new(sdmmc_spi, delay);
// Get the card size (this also triggers card initialisation because it's not been done yet)
println!("Card size is {} bytes", sdcard.num_bytes()?);
// Now let's look for volumes (also known as partitions) on our block device.
// To do this we need a Volume Manager. It will take ownership of the block device.
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
let volume_mgr = VolumeManager::new(sdcard, time_source);
// Try and access Volume 0 (i.e. the first partition).
// The volume object holds information about the filesystem on that volume.
let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
let volume0 = volume_mgr.open_volume(VolumeIdx(0))?;
println!("Volume 0: {:?}", volume0);
// Open the root directory (mutably borrows from the volume).
let mut root_dir = volume0.open_root_dir()?;
let root_dir = volume0.open_root_dir()?;
// Open a file called "MY_FILE.TXT" in the root directory
// This mutably borrows the directory.
let mut my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
let my_file = root_dir.open_file_in_dir("MY_FILE.TXT", Mode::ReadOnly)?;
// Print the contents of the file, assuming it's in ISO-8859-1 encoding
while !my_file.is_eof() {
let mut buffer = [0u8; 32];
Expand All @@ -37,13 +38,26 @@ while !my_file.is_eof() {
}
```

For writing files:

```rust
let my_other_file = root_dir.open_file_in_dir("MY_DATA.CSV", embedded_sdmmc::Mode::ReadWriteCreateOrAppend)?;
my_other_file.write(b"Timestamp,Signal,Value\n")?;
my_other_file.write(b"2025-01-01T00:00:00Z,TEMP,25.0\n")?;
my_other_file.write(b"2025-01-01T00:00:01Z,TEMP,25.1\n")?;
my_other_file.write(b"2025-01-01T00:00:02Z,TEMP,25.2\n")?;

// Don't forget to flush the file so that the directory entry is updated
my_other_file.flush()?;
```

### Open directories and files

By default the `VolumeManager` will initialize with a maximum number of `4` open directories, files and volumes. This can be customized by specifying the `MAX_DIR`, `MAX_FILES` and `MAX_VOLUMES` generic consts of the `VolumeManager`:

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

## Supported features
Expand Down
24 changes: 11 additions & 13 deletions examples/append_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,35 @@
//! $ cargo run --example append_file -- /dev/mmcblk0
//! ```
//!
//! If you pass a block device it should be unmounted. No testing has been
//! performed with Windows raw block devices - please report back if you try
//! this! There is a gzipped example disk image which you can gunzip and test
//! with if you don't have a suitable block device.
//! If you pass a block device it should be unmounted. There is a gzipped
//! example disk image which you can gunzip and test with if you don't have a
//! suitable block device.
//!
//! ```bash
//! zcat ./tests/disk.img.gz > ./disk.img
//! $ cargo run --example append_file -- ./disk.img
//! ```

extern crate embedded_sdmmc;

mod linux;
use linux::*;

const FILE_TO_APPEND: &str = "README.TXT";

use embedded_sdmmc::{Error, Mode, VolumeIdx, VolumeManager};
use embedded_sdmmc::{Error, Mode, VolumeIdx};

type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;

fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
fn main() -> Result<(), Error<std::io::Error>> {
env_logger::init();
let mut args = std::env::args().skip(1);
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
let mut root_dir = volume.open_root_dir()?;
let volume_mgr: VolumeManager = VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
let root_dir = volume.open_root_dir()?;
println!("\nCreating file {}...", FILE_TO_APPEND);
let mut f = root_dir.open_file_in_dir(FILE_TO_APPEND, Mode::ReadWriteAppend)?;
let f = root_dir.open_file_in_dir(FILE_TO_APPEND, Mode::ReadWriteAppend)?;
f.write(b"\r\n\r\nThis has been added to your file.\r\n")?;
Ok(())
}
Expand Down
42 changes: 28 additions & 14 deletions examples/big_dir.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
extern crate embedded_sdmmc;
//! Big Directory Example.
//!
//! Attempts to create an infinite number of files in the root directory of the
//! first volume of the given block device. This is basically to see what
//! happens when the root directory runs out of space.
//!
//! ```bash
//! $ cargo run --example big_dir -- ./disk.img
//! $ cargo run --example big_dir -- /dev/mmcblk0
//! ```
//!
//! If you pass a block device it should be unmounted. There is a gzipped
//! example disk image which you can gunzip and test with if you don't have a
//! suitable block device.
//!
//! ```bash
//! zcat ./tests/disk.img.gz > ./disk.img
//! $ cargo run --example big_dir -- ./disk.img
//! ```

mod linux;
use linux::*;

use embedded_sdmmc::{Error, VolumeManager};
use embedded_sdmmc::{Error, Mode, VolumeIdx};

fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;

fn main() -> Result<(), Error<std::io::Error>> {
env_logger::init();
let mut args = std::env::args().skip(1);
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
let mut volume = volume_mgr
.open_volume(embedded_sdmmc::VolumeIdx(1))
.unwrap();
let volume_mgr: VolumeManager = VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
let volume = volume_mgr.open_volume(VolumeIdx(0)).unwrap();
println!("Volume: {:?}", volume);
let mut root_dir = volume.open_root_dir().unwrap();
let root_dir = volume.open_root_dir().unwrap();

let mut file_num = 0;
loop {
file_num += 1;
let file_name = format!("{}.da", file_num);
println!("opening file {file_name} for writing");
let mut file = root_dir
.open_file_in_dir(
file_name.as_str(),
embedded_sdmmc::Mode::ReadWriteCreateOrTruncate,
)
let file = root_dir
.open_file_in_dir(file_name.as_str(), Mode::ReadWriteCreateOrTruncate)
.unwrap();
let buf = b"hello world, from rust";
println!("writing to file");
Expand Down
24 changes: 11 additions & 13 deletions examples/create_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,38 @@
//! $ cargo run --example create_file -- /dev/mmcblk0
//! ```
//!
//! If you pass a block device it should be unmounted. No testing has been
//! performed with Windows raw block devices - please report back if you try
//! this! There is a gzipped example disk image which you can gunzip and test
//! with if you don't have a suitable block device.
//! If you pass a block device it should be unmounted. There is a gzipped
//! example disk image which you can gunzip and test with if you don't have a
//! suitable block device.
//!
//! ```bash
//! zcat ./tests/disk.img.gz > ./disk.img
//! $ cargo run --example create_file -- ./disk.img
//! ```

extern crate embedded_sdmmc;

mod linux;
use linux::*;

const FILE_TO_CREATE: &str = "CREATE.TXT";

use embedded_sdmmc::{Error, Mode, VolumeIdx, VolumeManager};
use embedded_sdmmc::{Error, Mode, VolumeIdx};

type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;

fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
fn main() -> Result<(), Error<std::io::Error>> {
env_logger::init();
let mut args = std::env::args().skip(1);
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
let mut root_dir = volume.open_root_dir()?;
let volume_mgr: VolumeManager = VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
let root_dir = volume.open_root_dir()?;
println!("\nCreating file {}...", FILE_TO_CREATE);
// This will panic if the file already exists: use ReadWriteCreateOrAppend
// or ReadWriteCreateOrTruncate instead if you want to modify an existing
// file.
let mut f = root_dir.open_file_in_dir(FILE_TO_CREATE, Mode::ReadWriteCreate)?;
let f = root_dir.open_file_in_dir(FILE_TO_CREATE, Mode::ReadWriteCreate)?;
f.write(b"Hello, this is a new file on disk\r\n")?;
Ok(())
}
Expand Down
22 changes: 10 additions & 12 deletions examples/delete_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,33 @@
//! NOTE: THIS EXAMPLE DELETES A FILE CALLED README.TXT. IF YOU DO NOT WANT THAT
//! FILE DELETED FROM YOUR DISK IMAGE, DO NOT RUN THIS EXAMPLE.
//!
//! If you pass a block device it should be unmounted. No testing has been
//! performed with Windows raw block devices - please report back if you try
//! this! There is a gzipped example disk image which you can gunzip and test
//! with if you don't have a suitable block device.
//! If you pass a block device it should be unmounted. There is a gzipped
//! example disk image which you can gunzip and test with if you don't have a
//! suitable block device.
//!
//! ```bash
//! zcat ./tests/disk.img.gz > ./disk.img
//! $ cargo run --example delete_file -- ./disk.img
//! ```

extern crate embedded_sdmmc;

mod linux;
use linux::*;

const FILE_TO_DELETE: &str = "README.TXT";

use embedded_sdmmc::{Error, VolumeIdx, VolumeManager};
use embedded_sdmmc::{Error, VolumeIdx};

type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;

fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
fn main() -> Result<(), Error<std::io::Error>> {
env_logger::init();
let mut args = std::env::args().skip(1);
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
let mut root_dir = volume.open_root_dir()?;
let volume_mgr: VolumeManager = VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
let root_dir = volume.open_root_dir()?;
println!("Deleting file {}...", FILE_TO_DELETE);
root_dir.delete_file_in_dir(FILE_TO_DELETE)?;
println!("Deleted!");
Expand Down
12 changes: 2 additions & 10 deletions examples/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,14 @@ impl LinuxBlockDevice {
impl BlockDevice for LinuxBlockDevice {
type Error = std::io::Error;

fn read(
&self,
blocks: &mut [Block],
start_block_idx: BlockIdx,
reason: &str,
) -> Result<(), Self::Error> {
fn read(&self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Self::Error> {
self.file
.borrow_mut()
.seek(SeekFrom::Start(start_block_idx.into_bytes()))?;
for block in blocks.iter_mut() {
self.file.borrow_mut().read_exact(&mut block.contents)?;
if self.print_blocks {
println!(
"Read block ({}) {:?}: {:?}",
reason, start_block_idx, &block
);
println!("Read block {:?}: {:?}", start_block_idx, &block);
}
}
Ok(())
Expand Down
Loading