From 4dd28631a3a68ecf240bbc53c90f0e9e7a5f39d0 Mon Sep 17 00:00:00 2001 From: mulhern Date: Thu, 20 Jul 2017 12:34:11 -0400 Subject: [PATCH] Manage the possibility of incomplete data read 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 --- src/engine/strat_engine/metadata.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/engine/strat_engine/metadata.rs b/src/engine/strat_engine/metadata.rs index 0ef999ae72..57a3606069 100644 --- a/src/engine/strat_engine/metadata.rs +++ b/src/engine/strat_engine/metadata.rs @@ -199,8 +199,8 @@ impl StaticHeader { fn setup(f: &mut F) -> EngineResult> 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. @@ -208,15 +208,13 @@ impl StaticHeader { /// 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> { - 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> { // 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); } } @@ -228,13 +226,13 @@ impl StaticHeader { pub fn determine_ownership(f: &mut F) -> EngineResult 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) {