Skip to content

Commit 5e718c2

Browse files
committed
Remove the reason from the Block Device API.
The caller of the API should do their own logging. Also, we didn't have it on write and the asymmetry was displeasing.
1 parent 3e672aa commit 5e718c2

File tree

7 files changed

+56
-72
lines changed

7 files changed

+56
-72
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: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ impl FatVolume {
8080
return Ok(());
8181
}
8282
let mut blocks = [Block::new()];
83+
trace!("Reading info sector");
8384
block_device
84-
.read(&mut blocks, fat32_info.info_location, "read_info_sector")
85+
.read(&mut blocks, fat32_info.info_location)
8586
.map_err(Error::DeviceError)?;
8687
let block = &mut blocks[0];
8788
if let Some(count) = self.free_clusters_count {
@@ -90,6 +91,7 @@ impl FatVolume {
9091
if let Some(next_free_cluster) = self.next_free_cluster {
9192
block[492..496].copy_from_slice(&next_free_cluster.0.to_le_bytes());
9293
}
94+
trace!("Writing info sector");
9395
block_device
9496
.write(&blocks, fat32_info.info_location)
9597
.map_err(Error::DeviceError)?;
@@ -123,8 +125,9 @@ impl FatVolume {
123125
let fat_offset = cluster.0 * 2;
124126
this_fat_block_num = self.lba_start + self.fat_start.offset_bytes(fat_offset);
125127
let this_fat_ent_offset = (fat_offset % Block::LEN_U32) as usize;
128+
trace!("Reading FAT");
126129
block_device
127-
.read(&mut blocks, this_fat_block_num, "read_fat")
130+
.read(&mut blocks, this_fat_block_num)
128131
.map_err(Error::DeviceError)?;
129132
// See <https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system>
130133
let entry = match new_value {
@@ -144,8 +147,9 @@ impl FatVolume {
144147
let fat_offset = cluster.0 * 4;
145148
this_fat_block_num = self.lba_start + self.fat_start.offset_bytes(fat_offset);
146149
let this_fat_ent_offset = (fat_offset % Block::LEN_U32) as usize;
150+
trace!("Reading FAT");
147151
block_device
148-
.read(&mut blocks, this_fat_block_num, "read_fat")
152+
.read(&mut blocks, this_fat_block_num)
149153
.map_err(Error::DeviceError)?;
150154
let entry = match new_value {
151155
ClusterId::INVALID => 0x0FFF_FFF6,
@@ -163,6 +167,7 @@ impl FatVolume {
163167
);
164168
}
165169
}
170+
trace!("Writing FAT");
166171
block_device
167172
.write(&blocks, this_fat_block_num)
168173
.map_err(Error::DeviceError)?;
@@ -187,8 +192,8 @@ impl FatVolume {
187192
let fat_offset = cluster.0 * 2;
188193
let this_fat_block_num = self.lba_start + self.fat_start.offset_bytes(fat_offset);
189194
let this_fat_ent_offset = (fat_offset % Block::LEN_U32) as usize;
190-
let block =
191-
fat_block_cache.read(block_device, this_fat_block_num, "next_cluster")?;
195+
trace!("Reading FAT");
196+
let block = fat_block_cache.read(block_device, this_fat_block_num)?;
192197
let fat_entry =
193198
LittleEndian::read_u16(&block[this_fat_ent_offset..=this_fat_ent_offset + 1]);
194199
match fat_entry {
@@ -210,8 +215,8 @@ impl FatVolume {
210215
let fat_offset = cluster.0 * 4;
211216
let this_fat_block_num = self.lba_start + self.fat_start.offset_bytes(fat_offset);
212217
let this_fat_ent_offset = (fat_offset % Block::LEN_U32) as usize;
213-
let block =
214-
fat_block_cache.read(block_device, this_fat_block_num, "next_cluster")?;
218+
trace!("Reading FAT");
219+
let block = fat_block_cache.read(block_device, this_fat_block_num)?;
215220
let fat_entry =
216221
LittleEndian::read_u32(&block[this_fat_ent_offset..=this_fat_ent_offset + 3])
217222
& 0x0FFF_FFFF;
@@ -310,8 +315,9 @@ impl FatVolume {
310315
let mut blocks = [Block::new()];
311316
while let Some(cluster) = current_cluster {
312317
for block in first_dir_block_num.range(dir_size) {
318+
trace!("Reading directory");
313319
block_device
314-
.read(&mut blocks, block, "read_dir")
320+
.read(&mut blocks, block)
315321
.map_err(Error::DeviceError)?;
316322
for (i, dir_entry_bytes) in
317323
blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate()
@@ -330,6 +336,7 @@ impl FatVolume {
330336
);
331337
dir_entry_bytes
332338
.copy_from_slice(&entry.serialize(FatType::Fat16)[..]);
339+
trace!("Updating directory");
333340
block_device
334341
.write(&blocks, block)
335342
.map_err(Error::DeviceError)?;
@@ -375,8 +382,9 @@ impl FatVolume {
375382
// Loop through the blocks in the cluster
376383
for block in first_dir_block_num.range(dir_size) {
377384
// Read a block of directory entries
385+
trace!("Reading directory");
378386
block_device
379-
.read(&mut blocks, block, "read_dir")
387+
.read(&mut blocks, block)
380388
.map_err(Error::DeviceError)?;
381389
// Are any entries in the block we just loaded blank? If so
382390
// we can use them.
@@ -397,6 +405,7 @@ impl FatVolume {
397405
);
398406
dir_entry_bytes
399407
.copy_from_slice(&entry.serialize(FatType::Fat32)[..]);
408+
trace!("Updating directory");
400409
block_device
401410
.write(&blocks, block)
402411
.map_err(Error::DeviceError)?;
@@ -481,7 +490,8 @@ impl FatVolume {
481490
let mut block_cache = BlockCache::empty();
482491
while let Some(cluster) = current_cluster {
483492
for block_idx in first_dir_block_num.range(dir_size) {
484-
let block = block_cache.read(block_device, block_idx, "read_dir")?;
493+
trace!("Reading FAT");
494+
let block = block_cache.read(block_device, block_idx)?;
485495
for (i, dir_entry_bytes) in block.chunks_exact(OnDiskDirEntry::LEN).enumerate() {
486496
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
487497
if dir_entry.is_end() {
@@ -532,8 +542,9 @@ impl FatVolume {
532542
while let Some(cluster) = current_cluster {
533543
let block_idx = self.cluster_to_block(cluster);
534544
for block in block_idx.range(BlockCount(u32::from(self.blocks_per_cluster))) {
545+
trace!("Reading FAT");
535546
block_device
536-
.read(&mut blocks, block, "read_dir")
547+
.read(&mut blocks, block)
537548
.map_err(Error::DeviceError)?;
538549
for (i, dir_entry_bytes) in
539550
blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate()
@@ -658,8 +669,9 @@ impl FatVolume {
658669
D: BlockDevice,
659670
{
660671
let mut blocks = [Block::new()];
672+
trace!("Reading directory");
661673
block_device
662-
.read(&mut blocks, block, "read_dir")
674+
.read(&mut blocks, block)
663675
.map_err(Error::DeviceError)?;
664676
for (i, dir_entry_bytes) in blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate() {
665677
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
@@ -792,8 +804,9 @@ impl FatVolume {
792804
D: BlockDevice,
793805
{
794806
let mut blocks = [Block::new()];
807+
trace!("Reading directory");
795808
block_device
796-
.read(&mut blocks, block, "read_dir")
809+
.read(&mut blocks, block)
797810
.map_err(Error::DeviceError)?;
798811
for (i, dir_entry_bytes) in blocks[0].chunks_exact_mut(OnDiskDirEntry::LEN).enumerate() {
799812
let dir_entry = OnDiskDirEntry::new(dir_entry_bytes);
@@ -805,6 +818,7 @@ impl FatVolume {
805818
let start = i * OnDiskDirEntry::LEN;
806819
// set first byte to the 'unused' marker
807820
blocks[0].contents[start] = 0xE5;
821+
trace!("Updating directory");
808822
return block_device
809823
.write(&blocks, block)
810824
.map_err(Error::DeviceError);
@@ -842,7 +856,7 @@ impl FatVolume {
842856
.map_err(|_| Error::ConversionError)?;
843857
trace!("Reading block {:?}", this_fat_block_num);
844858
block_device
845-
.read(&mut blocks, this_fat_block_num, "next_cluster")
859+
.read(&mut blocks, this_fat_block_num)
846860
.map_err(Error::DeviceError)?;
847861

848862
while this_fat_ent_offset <= Block::LEN - 2 {
@@ -873,7 +887,7 @@ impl FatVolume {
873887
.map_err(|_| Error::ConversionError)?;
874888
trace!("Reading block {:?}", this_fat_block_num);
875889
block_device
876-
.read(&mut blocks, this_fat_block_num, "next_cluster")
890+
.read(&mut blocks, this_fat_block_num)
877891
.map_err(Error::DeviceError)?;
878892

879893
while this_fat_ent_offset <= Block::LEN - 4 {
@@ -969,6 +983,7 @@ impl FatVolume {
969983
let first_block = self.cluster_to_block(new_cluster);
970984
let num_blocks = BlockCount(u32::from(self.blocks_per_cluster));
971985
for block in first_block.range(num_blocks) {
986+
trace!("Zeroing cluster");
972987
block_device
973988
.write(&blocks, block)
974989
.map_err(Error::DeviceError)?;
@@ -1041,14 +1056,16 @@ impl FatVolume {
10411056
FatSpecificInfo::Fat32(_) => FatType::Fat32,
10421057
};
10431058
let mut blocks = [Block::new()];
1059+
trace!("Reading directory for update");
10441060
block_device
1045-
.read(&mut blocks, entry.entry_block, "read")
1061+
.read(&mut blocks, entry.entry_block)
10461062
.map_err(Error::DeviceError)?;
10471063
let block = &mut blocks[0];
10481064

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

1068+
trace!("Updating directory");
10521069
block_device
10531070
.write(&blocks, entry.entry_block)
10541071
.map_err(Error::DeviceError)?;
@@ -1068,8 +1085,9 @@ where
10681085
D::Error: core::fmt::Debug,
10691086
{
10701087
let mut blocks = [Block::new()];
1088+
trace!("Reading BPB");
10711089
block_device
1072-
.read(&mut blocks, lba_start, "read_bpb")
1090+
.read(&mut blocks, lba_start)
10731091
.map_err(Error::DeviceError)?;
10741092
let block = &blocks[0];
10751093
let bpb = Bpb::create_from_bytes(block).map_err(Error::FormatError)?;
@@ -1112,12 +1130,9 @@ where
11121130
// Safe to unwrap since this is a Fat32 Type
11131131
let info_location = bpb.fs_info_block().unwrap();
11141132
let mut info_blocks = [Block::new()];
1133+
trace!("Reading info block");
11151134
block_device
1116-
.read(
1117-
&mut info_blocks,
1118-
lba_start + info_location,
1119-
"read_info_sector",
1120-
)
1135+
.read(&mut info_blocks, lba_start + info_location)
11211136
.map_err(Error::DeviceError)?;
11221137
let info_block = &info_blocks[0];
11231138
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
}

src/volume_mgr.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
//!
33
//! The volume manager handles partitions and open files on a block device.
44
5-
use byteorder::{ByteOrder, LittleEndian};
65
use core::convert::TryFrom;
76

7+
use byteorder::{ByteOrder, LittleEndian};
8+
use heapless::Vec;
9+
810
use crate::fat::{self, BlockCache, FatType, OnDiskDirEntry, RESERVED_ENTRIES};
911

1012
use crate::filesystem::{
1113
Attributes, ClusterId, DirEntry, DirectoryInfo, FileInfo, Mode, RawDirectory, RawFile,
1214
SearchIdGenerator, TimeSource, ToShortFileName, MAX_FILE_SIZE,
1315
};
1416
use crate::{
15-
debug, Block, BlockCount, BlockDevice, BlockIdx, Error, RawVolume, ShortFileName, Volume,
16-
VolumeIdx, VolumeInfo, VolumeType, PARTITION_ID_FAT16, PARTITION_ID_FAT16_LBA,
17+
debug, trace, Block, BlockCount, BlockDevice, BlockIdx, Error, RawVolume, ShortFileName,
18+
Volume, VolumeIdx, VolumeInfo, VolumeType, PARTITION_ID_FAT16, PARTITION_ID_FAT16_LBA,
1719
PARTITION_ID_FAT32_CHS_LBA, PARTITION_ID_FAT32_LBA,
1820
};
19-
use heapless::Vec;
2021

2122
/// Wraps a block device and gives access to the FAT-formatted volumes within
2223
/// it.
@@ -142,8 +143,9 @@ where
142143

143144
let (part_type, lba_start, num_blocks) = {
144145
let mut blocks = [Block::new()];
146+
trace!("Reading partition table");
145147
self.block_device
146-
.read(&mut blocks, BlockIdx(0), "read_mbr")
148+
.read(&mut blocks, BlockIdx(0))
147149
.map_err(Error::DeviceError)?;
148150
let block = &blocks[0];
149151
// We only support Master Boot Record (MBR) partitioned cards, not
@@ -632,8 +634,9 @@ where
632634
)?;
633635
self.open_files[file_idx].current_cluster = current_cluster;
634636
let mut blocks = [Block::new()];
637+
trace!("Reading file ID {:?}", file);
635638
self.block_device
636-
.read(&mut blocks, block_idx, "read")
639+
.read(&mut blocks, block_idx)
637640
.map_err(Error::DeviceError)?;
638641
let block = &blocks[0];
639642
let to_copy = block_avail
@@ -747,9 +750,9 @@ where
747750
let mut blocks = [Block::new()];
748751
let to_copy = core::cmp::min(block_avail, bytes_to_write - written);
749752
if block_offset != 0 {
750-
debug!("Partial block write");
753+
debug!("Reading for partial block write");
751754
self.block_device
752-
.read(&mut blocks, block_idx, "read")
755+
.read(&mut blocks, block_idx)
753756
.map_err(Error::DeviceError)?;
754757
}
755758
let block = &mut blocks[0];
@@ -1157,12 +1160,7 @@ mod tests {
11571160
type Error = Error;
11581161

11591162
/// Read one or more blocks, starting at the given block index.
1160-
fn read(
1161-
&self,
1162-
blocks: &mut [Block],
1163-
start_block_idx: BlockIdx,
1164-
_reason: &str,
1165-
) -> Result<(), Self::Error> {
1163+
fn read(&self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Self::Error> {
11661164
// Actual blocks taken from an SD card, except I've changed the start and length of partition 0.
11671165
static BLOCKS: [Block; 3] = [
11681166
Block {

tests/utils/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ where
8585
{
8686
type Error = Error;
8787

88-
fn read(
89-
&self,
90-
blocks: &mut [Block],
91-
start_block_idx: BlockIdx,
92-
_reason: &str,
93-
) -> Result<(), Self::Error> {
88+
fn read(&self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Self::Error> {
9489
let borrow = self.contents.borrow();
9590
let contents: &[u8] = borrow.as_ref();
9691
let mut block_idx = start_block_idx;

0 commit comments

Comments
 (0)