Skip to content

Commit

Permalink
Manage the possibility of incomplete data read
Browse files Browse the repository at this point in the history
When trying to read a StaticHeader it may be that the buffer doesn't
get filled with data from the file or device being read.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jul 20, 2017
1 parent 7cc6db9 commit 4dd2863
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/engine/strat_engine/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,24 +199,22 @@ impl StaticHeader {
fn setup<F>(f: &mut F) -> EngineResult<Option<StaticHeader>>
where F: Read + Seek
{
let (buf, _) = try!(StaticHeader::read_into_buf(f));
StaticHeader::setup_from_buf(&buf)
let (buf, bytes) = try!(StaticHeader::read_into_buf(f));
StaticHeader::setup_from_buf(&buf, bytes)
}

/// Try to find a valid StaticHeader in a buffer.
/// If there is an error in reading the first, try the next. If there is
/// no error in reading the first, assume it is correct, i.e., do not
/// verify that it matches the next.
/// Return None if the static header's magic number is wrong.
fn setup_from_buf(buf: &[u8; _BDA_STATIC_HDR_SIZE]) -> EngineResult<Option<StaticHeader>> {
let sigblock_spots = [&buf[SIGBLOCK_REGION_ONE.0..SIGBLOCK_REGION_ONE.1],
&buf[SIGBLOCK_REGION_TWO.0..SIGBLOCK_REGION_TWO.1]];

fn setup_from_buf(buf: &[u8; _BDA_STATIC_HDR_SIZE],
bytes: usize)
-> EngineResult<Option<StaticHeader>> {
// TODO: repair static header if one incorrect?
for buf in &sigblock_spots {
match StaticHeader::sigblock_from_buf(buf, buf.len()) {
Ok(val) => return Ok(val),
_ => continue,
for &(start, end) in &[SIGBLOCK_REGION_ONE, SIGBLOCK_REGION_TWO] {
if let Ok(val) = StaticHeader::sigblock_from_buf(&buf[start..end], bytes - start) {
return Ok(val);
}
}

Expand All @@ -228,13 +226,13 @@ impl StaticHeader {
pub fn determine_ownership<F>(f: &mut F) -> EngineResult<DevOwnership>
where F: Read + Seek
{
let (buf, _) = try!(StaticHeader::read_into_buf(f));
let (buf, bytes) = try!(StaticHeader::read_into_buf(f));

// Using setup() as a test of ownership sets a high bar. It is
// not sufficient to have STRAT_MAGIC to be considered "Ours",
// it must also have correct CRC, no weird stuff in fields,
// etc!
match StaticHeader::setup_from_buf(&buf) {
match StaticHeader::setup_from_buf(&buf, bytes) {
Ok(Some(sh)) => Ok(DevOwnership::Ours(sh.pool_uuid)),
Ok(None) => {
if buf.iter().any(|x| *x != 0) {
Expand Down

0 comments on commit 4dd2863

Please sign in to comment.