Skip to content

Commit aec477c

Browse files
authored
Merge pull request #147 from rust-embedded-community/chunks-exact
Use chunks_exact and remove 'reason' from BlockDevice API
2 parents 504b440 + 62546fc commit aec477c

File tree

7 files changed

+83
-104
lines changed

7 files changed

+83
-104
lines changed

examples/linux/mod.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,14 @@ impl LinuxBlockDevice {
3434
impl BlockDevice for LinuxBlockDevice {
3535
type Error = std::io::Error;
3636

37-
fn read(
38-
&self,
39-
blocks: &mut [Block],
40-
start_block_idx: BlockIdx,
41-
reason: &str,
42-
) -> Result<(), Self::Error> {
37+
fn read(&self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Self::Error> {
4338
self.file
4439
.borrow_mut()
4540
.seek(SeekFrom::Start(start_block_idx.into_bytes()))?;
4641
for block in blocks.iter_mut() {
4742
self.file.borrow_mut().read_exact(&mut block.contents)?;
4843
if self.print_blocks {
49-
println!(
50-
"Read block ({}) {:?}: {:?}",
51-
reason, start_block_idx, &block
52-
);
44+
println!("Read block {:?}: {:?}", start_block_idx, &block);
5345
}
5446
}
5547
Ok(())

src/blockdevice.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ pub trait BlockDevice {
4343
/// The errors that the `BlockDevice` can return. Must be debug formattable.
4444
type Error: core::fmt::Debug;
4545
/// Read one or more blocks, starting at the given block index.
46-
fn read(
47-
&self,
48-
blocks: &mut [Block],
49-
start_block_idx: BlockIdx,
50-
reason: &str,
51-
) -> Result<(), Self::Error>;
46+
fn read(&self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
5247
/// Write one or more blocks, starting at the given block index.
5348
fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
5449
/// Determine how many blocks this device can hold.

src/fat/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ impl BlockCache {
2929
&mut self,
3030
block_device: &D,
3131
block_idx: BlockIdx,
32-
reason: &str,
3332
) -> Result<&Block, Error<D::Error>>
3433
where
3534
D: BlockDevice,
3635
{
3736
if Some(block_idx) != self.idx {
3837
self.idx = Some(block_idx);
3938
block_device
40-
.read(core::slice::from_mut(&mut self.block), block_idx, reason)
39+
.read(core::slice::from_mut(&mut self.block), block_idx)
4140
.map_err(Error::DeviceError)?;
4241
}
4342
Ok(&self.block)

src/fat/volume.rs

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ impl FatVolume {
176176
return Ok(());
177177
}
178178
let mut blocks = [Block::new()];
179+
trace!("Reading info sector");
179180
block_device
180-
.read(&mut blocks, fat32_info.info_location, "read_info_sector")
181+
.read(&mut blocks, fat32_info.info_location)
181182
.map_err(Error::DeviceError)?;
182183
let block = &mut blocks[0];
183184
if let Some(count) = self.free_clusters_count {
@@ -186,6 +187,7 @@ impl FatVolume {
186187
if let Some(next_free_cluster) = self.next_free_cluster {
187188
block[492..496].copy_from_slice(&next_free_cluster.0.to_le_bytes());
188189
}
190+
trace!("Writing info sector");
189191
block_device
190192
.write(&blocks, fat32_info.info_location)
191193
.map_err(Error::DeviceError)?;
@@ -219,8 +221,9 @@ impl FatVolume {
219221
let fat_offset = cluster.0 * 2;
220222
this_fat_block_num = self.lba_start + self.fat_start.offset_bytes(fat_offset);
221223
let this_fat_ent_offset = (fat_offset % Block::LEN_U32) as usize;
224+
trace!("Reading FAT");
222225
block_device
223-
.read(&mut blocks, this_fat_block_num, "read_fat")
226+
.read(&mut blocks, this_fat_block_num)
224227
.map_err(Error::DeviceError)?;
225228
// See <https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system>
226229
let entry = match new_value {
@@ -240,8 +243,9 @@ impl FatVolume {
240243
let fat_offset = cluster.0 * 4;
241244
this_fat_block_num = self.lba_start + self.fat_start.offset_bytes(fat_offset);
242245
let this_fat_ent_offset = (fat_offset % Block::LEN_U32) as usize;
246+
trace!("Reading FAT");
243247
block_device
244-
.read(&mut blocks, this_fat_block_num, "read_fat")
248+
.read(&mut blocks, this_fat_block_num)
245249
.map_err(Error::DeviceError)?;
246250
let entry = match new_value {
247251
ClusterId::INVALID => 0x0FFF_FFF6,
@@ -259,6 +263,7 @@ impl FatVolume {
259263
);
260264
}
261265
}
266+
trace!("Writing FAT");
262267
block_device
263268
.write(&blocks, this_fat_block_num)
264269
.map_err(Error::DeviceError)?;
@@ -283,8 +288,8 @@ impl FatVolume {
283288
let fat_offset = cluster.0 * 2;
284289
let this_fat_block_num = self.lba_start + self.fat_start.offset_bytes(fat_offset);
285290
let this_fat_ent_offset = (fat_offset % Block::LEN_U32) as usize;
286-
let block =
287-
fat_block_cache.read(block_device, this_fat_block_num, "next_cluster")?;
291+
trace!("Reading FAT");
292+
let block = fat_block_cache.read(block_device, this_fat_block_num)?;
288293
let fat_entry =
289294
LittleEndian::read_u16(&block[this_fat_ent_offset..=this_fat_ent_offset + 1]);
290295
match fat_entry {
@@ -306,8 +311,8 @@ impl FatVolume {
306311
let fat_offset = cluster.0 * 4;
307312
let this_fat_block_num = self.lba_start + self.fat_start.offset_bytes(fat_offset);
308313
let this_fat_ent_offset = (fat_offset % Block::LEN_U32) as usize;
309-
let block =
310-
fat_block_cache.read(block_device, this_fat_block_num, "next_cluster")?;
314+
trace!("Reading FAT");
315+
let block = fat_block_cache.read(block_device, this_fat_block_num)?;
311316
let fat_entry =
312317
LittleEndian::read_u32(&block[this_fat_ent_offset..=this_fat_ent_offset + 3])
313318
& 0x0FFF_FFFF;
@@ -406,14 +411,14 @@ impl FatVolume {
406411
let mut blocks = [Block::new()];
407412
while let Some(cluster) = current_cluster {
408413
for block in first_dir_block_num.range(dir_size) {
414+
trace!("Reading directory");
409415
block_device
410-
.read(&mut blocks, block, "read_dir")
416+
.read(&mut blocks, block)
411417
.map_err(Error::DeviceError)?;
412-
let entries_per_block = Block::LEN / OnDiskDirEntry::LEN;
413-
for entry in 0..entries_per_block {
414-
let start = entry * OnDiskDirEntry::LEN;
415-
let end = (entry + 1) * OnDiskDirEntry::LEN;
416-
let dir_entry = OnDiskDirEntry::new(&blocks[0][start..end]);
418+
for (i, dir_entry_bytes) in
419+
blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate()
420+
{
421+
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
417422
// 0x00 or 0xE5 represents a free entry
418423
if !dir_entry.is_valid() {
419424
let ctime = time_source.get_timestamp();
@@ -423,10 +428,11 @@ impl FatVolume {
423428
ClusterId::EMPTY,
424429
ctime,
425430
block,
426-
start as u32,
431+
(i * OnDiskDirEntry::LEN) as u32,
427432
);
428-
blocks[0][start..start + 32]
433+
dir_entry_bytes
429434
.copy_from_slice(&entry.serialize(FatType::Fat16)[..]);
435+
trace!("Updating directory");
430436
block_device
431437
.write(&blocks, block)
432438
.map_err(Error::DeviceError)?;
@@ -472,15 +478,16 @@ impl FatVolume {
472478
// Loop through the blocks in the cluster
473479
for block in first_dir_block_num.range(dir_size) {
474480
// Read a block of directory entries
481+
trace!("Reading directory");
475482
block_device
476-
.read(&mut blocks, block, "read_dir")
483+
.read(&mut blocks, block)
477484
.map_err(Error::DeviceError)?;
478485
// Are any entries in the block we just loaded blank? If so
479486
// we can use them.
480-
for entry in 0..Block::LEN / OnDiskDirEntry::LEN {
481-
let start = entry * OnDiskDirEntry::LEN;
482-
let end = (entry + 1) * OnDiskDirEntry::LEN;
483-
let dir_entry = OnDiskDirEntry::new(&blocks[0][start..end]);
487+
for (i, dir_entry_bytes) in
488+
blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate()
489+
{
490+
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
484491
// 0x00 or 0xE5 represents a free entry
485492
if !dir_entry.is_valid() {
486493
let ctime = time_source.get_timestamp();
@@ -490,10 +497,11 @@ impl FatVolume {
490497
ClusterId(0),
491498
ctime,
492499
block,
493-
start as u32,
500+
(i * OnDiskDirEntry::LEN) as u32,
494501
);
495-
blocks[0][start..start + 32]
502+
dir_entry_bytes
496503
.copy_from_slice(&entry.serialize(FatType::Fat32)[..]);
504+
trace!("Updating directory");
497505
block_device
498506
.write(&blocks, block)
499507
.map_err(Error::DeviceError)?;
@@ -578,17 +586,16 @@ impl FatVolume {
578586
let mut block_cache = BlockCache::empty();
579587
while let Some(cluster) = current_cluster {
580588
for block_idx in first_dir_block_num.range(dir_size) {
581-
let block = block_cache.read(block_device, block_idx, "read_dir")?;
582-
for entry in 0..Block::LEN / OnDiskDirEntry::LEN {
583-
let start = entry * OnDiskDirEntry::LEN;
584-
let end = (entry + 1) * OnDiskDirEntry::LEN;
585-
let dir_entry = OnDiskDirEntry::new(&block[start..end]);
589+
trace!("Reading FAT");
590+
let block = block_cache.read(block_device, block_idx)?;
591+
for (i, dir_entry_bytes) in block.chunks_exact(OnDiskDirEntry::LEN).enumerate() {
592+
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
586593
if dir_entry.is_end() {
587594
// Can quit early
588595
return Ok(());
589596
} else if dir_entry.is_valid() && !dir_entry.is_lfn() {
590597
// Block::LEN always fits on a u32
591-
let start = start as u32;
598+
let start = (i * OnDiskDirEntry::LEN) as u32;
592599
let entry = dir_entry.get_entry(FatType::Fat16, block_idx, start);
593600
func(&entry);
594601
}
@@ -631,19 +638,20 @@ impl FatVolume {
631638
while let Some(cluster) = current_cluster {
632639
let block_idx = self.cluster_to_block(cluster);
633640
for block in block_idx.range(BlockCount(u32::from(self.blocks_per_cluster))) {
641+
trace!("Reading FAT");
634642
block_device
635-
.read(&mut blocks, block, "read_dir")
643+
.read(&mut blocks, block)
636644
.map_err(Error::DeviceError)?;
637-
for entry in 0..Block::LEN / OnDiskDirEntry::LEN {
638-
let start = entry * OnDiskDirEntry::LEN;
639-
let end = (entry + 1) * OnDiskDirEntry::LEN;
640-
let dir_entry = OnDiskDirEntry::new(&blocks[0][start..end]);
645+
for (i, dir_entry_bytes) in
646+
blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate()
647+
{
648+
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
641649
if dir_entry.is_end() {
642650
// Can quit early
643651
return Ok(());
644652
} else if dir_entry.is_valid() && !dir_entry.is_lfn() {
645653
// Block::LEN always fits on a u32
646-
let start = start as u32;
654+
let start = (i * OnDiskDirEntry::LEN) as u32;
647655
let entry = dir_entry.get_entry(FatType::Fat32, block, start);
648656
func(&entry);
649657
}
@@ -757,20 +765,19 @@ impl FatVolume {
757765
D: BlockDevice,
758766
{
759767
let mut blocks = [Block::new()];
768+
trace!("Reading directory");
760769
block_device
761-
.read(&mut blocks, block, "read_dir")
770+
.read(&mut blocks, block)
762771
.map_err(Error::DeviceError)?;
763-
for entry in 0..Block::LEN / OnDiskDirEntry::LEN {
764-
let start = entry * OnDiskDirEntry::LEN;
765-
let end = (entry + 1) * OnDiskDirEntry::LEN;
766-
let dir_entry = OnDiskDirEntry::new(&blocks[0][start..end]);
772+
for (i, dir_entry_bytes) in blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate() {
773+
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
767774
if dir_entry.is_end() {
768775
// Can quit early
769776
break;
770777
} else if dir_entry.matches(match_name) {
771778
// Found it
772779
// Block::LEN always fits on a u32
773-
let start = start as u32;
780+
let start = (i * OnDiskDirEntry::LEN) as u32;
774781
return Ok(dir_entry.get_entry(fat_type, block, start));
775782
}
776783
}
@@ -893,19 +900,21 @@ impl FatVolume {
893900
D: BlockDevice,
894901
{
895902
let mut blocks = [Block::new()];
903+
trace!("Reading directory");
896904
block_device
897-
.read(&mut blocks, block, "read_dir")
905+
.read(&mut blocks, block)
898906
.map_err(Error::DeviceError)?;
899-
for entry in 0..Block::LEN / OnDiskDirEntry::LEN {
900-
let start = entry * OnDiskDirEntry::LEN;
901-
let end = (entry + 1) * OnDiskDirEntry::LEN;
902-
let dir_entry = OnDiskDirEntry::new(&blocks[0][start..end]);
907+
for (i, dir_entry_bytes) in blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate() {
908+
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
903909
if dir_entry.is_end() {
904910
// Can quit early
905911
break;
906912
} else if dir_entry.matches(match_name) {
907913
let mut blocks = blocks;
914+
let start = i * OnDiskDirEntry::LEN;
915+
// set first byte to the 'unused' marker
908916
blocks[0].contents[start] = 0xE5;
917+
trace!("Updating directory");
909918
return block_device
910919
.write(&blocks, block)
911920
.map_err(Error::DeviceError);
@@ -943,7 +952,7 @@ impl FatVolume {
943952
.map_err(|_| Error::ConversionError)?;
944953
trace!("Reading block {:?}", this_fat_block_num);
945954
block_device
946-
.read(&mut blocks, this_fat_block_num, "next_cluster")
955+
.read(&mut blocks, this_fat_block_num)
947956
.map_err(Error::DeviceError)?;
948957

949958
while this_fat_ent_offset <= Block::LEN - 2 {
@@ -974,7 +983,7 @@ impl FatVolume {
974983
.map_err(|_| Error::ConversionError)?;
975984
trace!("Reading block {:?}", this_fat_block_num);
976985
block_device
977-
.read(&mut blocks, this_fat_block_num, "next_cluster")
986+
.read(&mut blocks, this_fat_block_num)
978987
.map_err(Error::DeviceError)?;
979988

980989
while this_fat_ent_offset <= Block::LEN - 4 {
@@ -1070,6 +1079,7 @@ impl FatVolume {
10701079
let first_block = self.cluster_to_block(new_cluster);
10711080
let num_blocks = BlockCount(u32::from(self.blocks_per_cluster));
10721081
for block in first_block.range(num_blocks) {
1082+
trace!("Zeroing cluster");
10731083
block_device
10741084
.write(&blocks, block)
10751085
.map_err(Error::DeviceError)?;
@@ -1142,14 +1152,16 @@ impl FatVolume {
11421152
FatSpecificInfo::Fat32(_) => FatType::Fat32,
11431153
};
11441154
let mut blocks = [Block::new()];
1155+
trace!("Reading directory for update");
11451156
block_device
1146-
.read(&mut blocks, entry.entry_block, "read")
1157+
.read(&mut blocks, entry.entry_block)
11471158
.map_err(Error::DeviceError)?;
11481159
let block = &mut blocks[0];
11491160

11501161
let start = usize::try_from(entry.entry_offset).map_err(|_| Error::ConversionError)?;
11511162
block[start..start + 32].copy_from_slice(&entry.serialize(fat_type)[..]);
11521163

1164+
trace!("Updating directory");
11531165
block_device
11541166
.write(&blocks, entry.entry_block)
11551167
.map_err(Error::DeviceError)?;
@@ -1169,8 +1181,9 @@ where
11691181
D::Error: core::fmt::Debug,
11701182
{
11711183
let mut blocks = [Block::new()];
1184+
trace!("Reading BPB");
11721185
block_device
1173-
.read(&mut blocks, lba_start, "read_bpb")
1186+
.read(&mut blocks, lba_start)
11741187
.map_err(Error::DeviceError)?;
11751188
let block = &blocks[0];
11761189
let bpb = Bpb::create_from_bytes(block).map_err(Error::FormatError)?;
@@ -1214,12 +1227,9 @@ where
12141227
// Safe to unwrap since this is a Fat32 Type
12151228
let info_location = bpb.fs_info_block().unwrap();
12161229
let mut info_blocks = [Block::new()];
1230+
trace!("Reading info block");
12171231
block_device
1218-
.read(
1219-
&mut info_blocks,
1220-
lba_start + info_location,
1221-
"read_info_sector",
1222-
)
1232+
.read(&mut info_blocks, lba_start + info_location)
12231233
.map_err(Error::DeviceError)?;
12241234
let info_block = &info_blocks[0];
12251235
let info_sector =

src/sdcard/mod.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,9 @@ where
162162
/// Read one or more blocks, starting at the given block index.
163163
///
164164
/// This will trigger card (re-)initialisation.
165-
fn read(
166-
&self,
167-
blocks: &mut [Block],
168-
start_block_idx: BlockIdx,
169-
_reason: &str,
170-
) -> Result<(), Self::Error> {
165+
fn read(&self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Self::Error> {
171166
let mut inner = self.inner.borrow_mut();
172-
debug!(
173-
"Read {} blocks @ {} for {}",
174-
blocks.len(),
175-
start_block_idx.0,
176-
_reason
177-
);
167+
debug!("Read {} blocks @ {}", blocks.len(), start_block_idx.0,);
178168
inner.check_init()?;
179169
inner.read(blocks, start_block_idx)
180170
}

0 commit comments

Comments
 (0)