Skip to content

Commit

Permalink
Have MDARegions::load() return an error if error in reading region
Browse files Browse the repository at this point in the history
Remove the TODO about verifying that the previous behavior was correct.
It still returns None if the region is determined not to be there.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jul 20, 2017
1 parent 6c8bae5 commit 9e5d902
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/engine/strat_engine/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ mod mda {
}

/// Construct MDARegions from data on the disk.
/// Return an error if there is an error reading either of the regions.
pub fn load<F>(header_size: Bytes, size: Sectors, f: &mut F) -> EngineResult<MDARegions>
where F: Read + Seek
{
Expand All @@ -401,18 +402,14 @@ mod mda {

/// Get an MDAHeader for the given index.
/// If there is a failure reading the first, fall back on the
/// second. If there is a failure reading both, return None.
/// TODO: Consider whether this is the correct behavior.
let mut get_mda = |index: usize| -> Option<MDAHeader> {
load_a_region(index)
.or_else(|_| load_a_region(index + 2))
.ok()
.unwrap_or(None)
/// second. If there is a failure reading both, return an error.
let mut get_mda = |index: usize| -> EngineResult<Option<MDAHeader>> {
load_a_region(index).or_else(|_| load_a_region(index + 2))
};

Ok(MDARegions {
region_size: region_size,
mdas: [get_mda(0), get_mda(1)],
mdas: [try!(get_mda(0)), try!(get_mda(1))],
})
}

Expand Down Expand Up @@ -850,17 +847,16 @@ mod tests {

#[test]
/// Make a buffer just large enough to contain a single static header.
/// Verify that the StaticHeader is read and that the BDA's last update
/// time is None.
/// Verify that the StaticHeader is read and that there is a failure reading
/// the BDA.
fn just_one_static_header() {
let mut vec = vec![0u8; 2 * SECTOR_SIZE];
vec[SECTOR_SIZE..2 * SECTOR_SIZE].clone_from_slice(&random_static_header(0, 0)
.sigblock_to_buf());
let mut buf = Cursor::new(vec);
assert!(StaticHeader::setup(&mut buf).unwrap().is_some());

let bda = BDA::load(&mut buf).unwrap().unwrap();
assert_eq!(bda.last_update_time(), None);
assert!(BDA::load(&mut buf).is_err());
}

#[test]
Expand Down

0 comments on commit 9e5d902

Please sign in to comment.