diff --git a/src/cache/restore.rs b/src/cache/restore.rs index 7997e94..45000a1 100644 --- a/src/cache/restore.rs +++ b/src/cache/restore.rs @@ -2,7 +2,6 @@ use anyhow::Result; use std::convert::TryInto; use std::fs::OpenOptions; -use std::io::Cursor; use std::path::Path; use std::sync::Arc; @@ -14,8 +13,8 @@ use crate::cache::xml; use crate::io_engine::*; use crate::math::*; use crate::pdata::array_builder::*; +use crate::pdata::space_map_common::pack_root; use crate::pdata::space_map_metadata::*; -use crate::pdata::unpack::Pack; use crate::report::*; use crate::write_batcher::*; @@ -240,10 +239,8 @@ impl<'a> MetadataVisitor for Restorer<'a> { //------------------------------------------ fn build_metadata_sm(w: &mut WriteBatcher) -> Result> { - let mut sm_root = vec![0u8; SPACE_MAP_ROOT_SIZE]; - let mut cur = Cursor::new(&mut sm_root); let r = write_metadata_sm(w)?; - r.pack(&mut cur)?; + let sm_root = pack_root(&r, SPACE_MAP_ROOT_SIZE)?; Ok(sm_root) } diff --git a/src/pdata/space_map_common.rs b/src/pdata/space_map_common.rs index 72c0297..6d86090 100644 --- a/src/pdata/space_map_common.rs +++ b/src/pdata/space_map_common.rs @@ -195,6 +195,13 @@ impl Pack for SMRoot { } } +pub fn pack_root(root: &SMRoot, size: usize) -> Result> { + let mut sm_root = vec![0u8; size]; + let mut cur = Cursor::new(&mut sm_root); + root.pack(&mut cur)?; + Ok(sm_root) +} + //------------------------------------------ pub fn write_common(w: &mut WriteBatcher, sm: &dyn SpaceMap) -> Result<(Vec, u64)> { diff --git a/src/thin/restore.rs b/src/thin/restore.rs index a1fbb6a..078a3e3 100644 --- a/src/thin/restore.rs +++ b/src/thin/restore.rs @@ -2,7 +2,6 @@ use anyhow::{anyhow, Result}; use std::collections::BTreeMap; use std::fs::OpenOptions; -use std::io::Cursor; use std::ops::Deref; use std::path::Path; use std::sync::{Arc, Mutex}; @@ -10,10 +9,9 @@ use std::sync::{Arc, Mutex}; use crate::io_engine::*; use crate::pdata::btree_builder::*; use crate::pdata::space_map::*; -use crate::pdata::space_map_common::SMRoot; +use crate::pdata::space_map_common::pack_root; use crate::pdata::space_map_disk::*; use crate::pdata::space_map_metadata::*; -use crate::pdata::unpack::Pack; use crate::report::*; use crate::thin::block_time::*; use crate::thin::device_detail::*; @@ -160,7 +158,8 @@ impl<'a> Restorer<'a> { let data_sm_root = build_data_sm(self.w, data_sm.lock().unwrap().deref())?; // Build metadata space map - let (metadata_sm, metadata_sm_root) = build_metadata_sm(self.w)?; + let metadata_sm = write_metadata_sm(self.w)?; + let metadata_sm_root = pack_root(&metadata_sm, SPACE_MAP_ROOT_SIZE)?; // Write the superblock let sb = self.sb.as_ref().unwrap(); @@ -289,25 +288,12 @@ impl<'a> MetadataVisitor for Restorer<'a> { /// Writes a data space map to disk. Returns the space map root that needs /// to be written to the superblock. fn build_data_sm(w: &mut WriteBatcher, sm: &dyn SpaceMap) -> Result> { - let mut sm_root = vec![0u8; SPACE_MAP_ROOT_SIZE]; - let mut cur = Cursor::new(&mut sm_root); let r = write_disk_sm(w, sm)?; - r.pack(&mut cur)?; + let sm_root = pack_root(&r, SPACE_MAP_ROOT_SIZE)?; Ok(sm_root) } -/// Writes the metadata space map to disk. Returns the space map root that needs -/// to be written to the superblock. -fn build_metadata_sm(w: &mut WriteBatcher) -> Result<(SMRoot, Vec)> { - let mut sm_root = vec![0u8; SPACE_MAP_ROOT_SIZE]; - let mut cur = Cursor::new(&mut sm_root); - let r = write_metadata_sm(w)?; - r.pack(&mut cur)?; - - Ok((r, sm_root)) -} - //------------------------------------------ pub struct ThinRestoreOptions<'a> {