Skip to content

Commit 457de81

Browse files
Add more tests, and more changes.
- Cluster is now ClusterId - Helper method for turning bytes to a block count - Removed unsafe open_dir_entry API
1 parent c63688f commit 457de81

17 files changed

+351
-188
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1616
- Calling `SdCard::get_card_type` will now perform card initialisation ([#87] and [#90]).
1717
- Removed warning about unused arguments.
1818
- Types are now documented at the top level ([#86]).
19+
- Renamed `Cluster` to `ClusterId` and stopped you adding two together
1920

2021
[#72]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/issues/72
2122
[#86]: https://github.com/rust-embedded-community/embedded-sdmmc-rs/issues/86
@@ -31,6 +32,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3132
### Removed
3233

3334
- __Breaking Change__: `Controller` alias for `VolumeManager` removed.
35+
- __Breaking Change__: `VolumeManager::open_dir_entry` removed, as it was unsafe to the user to randomly pick a starting cluster.
3436
- Old examples `create_test`, `test_mount`, `write_test`, `delete_test`
3537

3638
## [Version 0.5.0](https://github.com/rust-embedded-community/embedded-sdmmc-rs/releases/tag/v0.5.0) - 2023-05-20
@@ -60,7 +62,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6062
- Optionally use [defmt](https://github.com/knurling-rs/defmt) for logging.
6163
Controlled by `defmt-log` feature flag.
6264
- __Breaking Change__: Use SPI blocking traits instead to ease SPI peripheral sharing.
63-
See: https://github.com/rust-embedded-community/embedded-sdmmc-rs/issues/28
65+
See: <https://github.com/rust-embedded-community/embedded-sdmmc-rs/issues/28>
6466
- Added `Controller::has_open_handles` and `Controller::free` methods.
6567
- __Breaking Change__: Changed interface to enforce correct SD state at compile time.
6668
- __Breaking Change__: Added custom error type for `File` operations.
@@ -81,7 +83,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
8183
- Added `Info_Sector` tracking for FAT32.
8284
- Change directory iteration to look in all the directory's clusters.
8385
- Added `write_test` and `create_test`.
84-
- De-duplicated FAT16 and FAT32 code (https://github.com/thejpster/embedded-sdmmc-rs/issues/10)
86+
- De-duplicated FAT16 and FAT32 code (<https://github.com/thejpster/embedded-sdmmc-rs/issues/10>)
8587

8688
## [Version 0.2.1](https://github.com/rust-embedded-community/embedded-sdmmc-rs/releases/tag/v0.2.1) - 2019-02-19
8789

src/blockdevice.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ impl BlockIdx {
177177
}
178178

179179
impl BlockCount {
180+
/// How many blocks are required to hold this many bytes.
181+
///
182+
/// ```
183+
/// # use embedded_sdmmc::BlockCount;
184+
/// assert_eq!(BlockCount::from_bytes(511), BlockCount(1));
185+
/// assert_eq!(BlockCount::from_bytes(512), BlockCount(1));
186+
/// assert_eq!(BlockCount::from_bytes(513), BlockCount(2));
187+
/// assert_eq!(BlockCount::from_bytes(1024), BlockCount(2));
188+
/// assert_eq!(BlockCount::from_bytes(1025), BlockCount(3));
189+
/// ```
190+
pub const fn from_bytes(byte_count: u32) -> BlockCount {
191+
let mut count = byte_count / Block::LEN_U32;
192+
if (count * Block::LEN_U32) != byte_count {
193+
count += 1;
194+
}
195+
BlockCount(count)
196+
}
197+
180198
/// Take a number of blocks and increment by the integer number of blocks
181199
/// required to get to the block that holds the byte at the given offset.
182200
pub fn offset_bytes(self, offset: u32) -> Self {
@@ -187,7 +205,7 @@ impl BlockCount {
187205
impl BlockIter {
188206
/// Create a new `BlockIter`, from the given start block, through (and
189207
/// including) the given end block.
190-
pub fn new(start: BlockIdx, inclusive_end: BlockIdx) -> BlockIter {
208+
pub const fn new(start: BlockIdx, inclusive_end: BlockIdx) -> BlockIter {
191209
BlockIter {
192210
inclusive_end,
193211
current: start,

src/fat/bpb.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Boot Parameter Block
22
33
use crate::{
4-
blockdevice::{Block, BlockCount},
4+
blockdevice::BlockCount,
55
fat::{FatType, OnDiskDirEntry},
66
};
77
use byteorder::{ByteOrder, LittleEndian};
@@ -29,13 +29,12 @@ impl<'a> Bpb<'a> {
2929
return Err("Bad BPB footer");
3030
}
3131

32-
let root_dir_blocks = ((u32::from(bpb.root_entries_count()) * OnDiskDirEntry::LEN_U32)
33-
+ (Block::LEN_U32 - 1))
34-
/ Block::LEN_U32;
35-
let data_blocks = bpb.total_blocks()
36-
- (u32::from(bpb.reserved_block_count())
37-
+ (u32::from(bpb.num_fats()) * bpb.fat_size())
38-
+ root_dir_blocks);
32+
let root_dir_blocks =
33+
BlockCount::from_bytes(u32::from(bpb.root_entries_count()) * OnDiskDirEntry::LEN_U32).0;
34+
let non_data_blocks = u32::from(bpb.reserved_block_count())
35+
+ (u32::from(bpb.num_fats()) * bpb.fat_size())
36+
+ root_dir_blocks;
37+
let data_blocks = bpb.total_blocks() - non_data_blocks;
3938
bpb.cluster_count = data_blocks / u32::from(bpb.blocks_per_cluster());
4039
if bpb.cluster_count < 4085 {
4140
return Err("FAT12 is unsupported");

src/fat/info.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{BlockCount, BlockIdx, Cluster};
1+
use crate::{BlockCount, BlockIdx, ClusterId};
22
use byteorder::{ByteOrder, LittleEndian};
33

44
/// Indentifies the supported types of FAT format
@@ -17,7 +17,7 @@ pub enum FatSpecificInfo {
1717
pub struct Fat32Info {
1818
/// The root directory does not have a reserved area in FAT32. This is the
1919
/// cluster it starts in (nominally 2).
20-
pub(crate) first_root_dir_cluster: Cluster,
20+
pub(crate) first_root_dir_cluster: ClusterId,
2121
/// Block idx of the info sector
2222
pub(crate) info_location: BlockIdx,
2323
}
@@ -78,11 +78,11 @@ impl<'a> InfoSector<'a> {
7878
}
7979

8080
/// Return the number of the next free cluster, if known.
81-
pub fn next_free_cluster(&self) -> Option<Cluster> {
81+
pub fn next_free_cluster(&self) -> Option<ClusterId> {
8282
match self.next_free() {
8383
// 0 and 1 are reserved clusters
8484
0xFFFF_FFFF | 0 | 1 => None,
85-
n => Some(Cluster(n)),
85+
n => Some(ClusterId(n)),
8686
}
8787
}
8888
}

src/fat/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::{Block, BlockDevice, BlockIdx, Error};
6666
mod test {
6767

6868
use super::*;
69-
use crate::{Attributes, BlockIdx, Cluster, DirEntry, ShortFileName, Timestamp};
69+
use crate::{Attributes, BlockIdx, ClusterId, DirEntry, ShortFileName, Timestamp};
7070

7171
fn parse(input: &str) -> Vec<u8> {
7272
let mut output = Vec::new();
@@ -143,7 +143,7 @@ mod test {
143143
mtime: Timestamp::from_calendar(2015, 11, 21, 19, 35, 18).unwrap(),
144144
ctime: Timestamp::from_calendar(2015, 11, 21, 19, 35, 18).unwrap(),
145145
attributes: Attributes::create_from_fat(Attributes::VOLUME),
146-
cluster: Cluster(0),
146+
cluster: ClusterId(0),
147147
size: 0,
148148
entry_block: BlockIdx(0),
149149
entry_offset: 0,
@@ -161,7 +161,7 @@ mod test {
161161
mtime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 54).unwrap(),
162162
ctime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 54).unwrap(),
163163
attributes: Attributes::create_from_fat(Attributes::DIRECTORY),
164-
cluster: Cluster(3),
164+
cluster: ClusterId(3),
165165
size: 0,
166166
entry_block: BlockIdx(0),
167167
entry_offset: 0,
@@ -186,7 +186,7 @@ mod test {
186186
mtime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 34).unwrap(),
187187
ctime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 34).unwrap(),
188188
attributes: Attributes::create_from_fat(Attributes::ARCHIVE),
189-
cluster: Cluster(9),
189+
cluster: ClusterId(9),
190190
size: 11120,
191191
entry_block: BlockIdx(0),
192192
entry_offset: 0,
@@ -203,7 +203,7 @@ mod test {
203203
mtime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 30).unwrap(),
204204
ctime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 30).unwrap(),
205205
attributes: Attributes::create_from_fat(Attributes::ARCHIVE),
206-
cluster: Cluster(5),
206+
cluster: ClusterId(5),
207207
size: 18693,
208208
entry_block: BlockIdx(0),
209209
entry_offset: 0,
@@ -228,7 +228,7 @@ mod test {
228228
mtime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 34).unwrap(),
229229
ctime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 34).unwrap(),
230230
attributes: Attributes::create_from_fat(Attributes::ARCHIVE),
231-
cluster: Cluster(8),
231+
cluster: ClusterId(8),
232232
size: 1494,
233233
entry_block: BlockIdx(0),
234234
entry_offset: 0,
@@ -253,7 +253,7 @@ mod test {
253253
mtime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 36).unwrap(),
254254
ctime: Timestamp::from_calendar(2016, 3, 1, 19, 56, 36).unwrap(),
255255
attributes: Attributes::create_from_fat(Attributes::ARCHIVE),
256-
cluster: Cluster(15),
256+
cluster: ClusterId(15),
257257
size: 12108,
258258
entry_block: BlockIdx(0),
259259
entry_offset: 0,

src/fat/ondiskdirentry.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Directory Entry as stored on-disk
22
3-
use crate::{fat::FatType, Attributes, BlockIdx, Cluster, DirEntry, ShortFileName, Timestamp};
3+
use crate::{fat::FatType, Attributes, BlockIdx, ClusterId, DirEntry, ShortFileName, Timestamp};
44
use byteorder::{ByteOrder, LittleEndian};
55

66
/// Represents a 32-byte directory entry as stored on-disk in a directory file.
@@ -129,16 +129,16 @@ impl<'a> OnDiskDirEntry<'a> {
129129
}
130130

131131
/// Which cluster, if any, does this file start at? Assumes this is from a FAT32 volume.
132-
pub fn first_cluster_fat32(&self) -> Cluster {
132+
pub fn first_cluster_fat32(&self) -> ClusterId {
133133
let cluster_no =
134134
(u32::from(self.first_cluster_hi()) << 16) | u32::from(self.first_cluster_lo());
135-
Cluster(cluster_no)
135+
ClusterId(cluster_no)
136136
}
137137

138138
/// Which cluster, if any, does this file start at? Assumes this is from a FAT16 volume.
139-
fn first_cluster_fat16(&self) -> Cluster {
139+
fn first_cluster_fat16(&self) -> ClusterId {
140140
let cluster_no = u32::from(self.first_cluster_lo());
141-
Cluster(cluster_no)
141+
ClusterId(cluster_no)
142142
}
143143

144144
/// Convert the on-disk format into a DirEntry

0 commit comments

Comments
 (0)