src/backup/fixed_index.rs - FixedIndexReader: remove reference to chunk store

This commit is contained in:
Dietmar Maurer 2019-07-04 08:09:48 +02:00
parent a660978c9a
commit a7c72ad9eb
2 changed files with 17 additions and 18 deletions

View File

@ -95,7 +95,9 @@ impl DataStore {
pub fn open_fixed_reader<P: AsRef<Path>>(&self, filename: P) -> Result<FixedIndexReader, Error> { pub fn open_fixed_reader<P: AsRef<Path>>(&self, filename: P) -> Result<FixedIndexReader, Error> {
let index = FixedIndexReader::open(self.chunk_store.clone(), filename.as_ref())?; let full_path = self.chunk_store.relative_path(filename.as_ref());
let index = FixedIndexReader::open(&full_path)?;
Ok(index) Ok(index)
} }

View File

@ -30,9 +30,7 @@ pub struct FixedIndexHeader {
// split image into fixed size chunks // split image into fixed size chunks
pub struct FixedIndexReader { pub struct FixedIndexReader {
store: Arc<ChunkStore>,
_file: File, _file: File,
filename: PathBuf,
pub chunk_size: usize, pub chunk_size: usize,
pub size: usize, pub size: usize,
index_length: usize, index_length: usize,
@ -49,28 +47,31 @@ impl Drop for FixedIndexReader {
fn drop(&mut self) { fn drop(&mut self) {
if let Err(err) = self.unmap() { if let Err(err) = self.unmap() {
eprintln!("Unable to unmap file {:?} - {}", self.filename, err); eprintln!("Unable to unmap file - {}", err);
} }
} }
} }
impl FixedIndexReader { impl FixedIndexReader {
pub fn open(store: Arc<ChunkStore>, path: &Path) -> Result<Self, Error> { pub fn open(path: &Path) -> Result<Self, Error> {
let full_path = store.relative_path(path); File::open(path)
.map_err(Error::from)
.and_then(|file| Self::new(file))
.map_err(|err| format_err!("Unable to open fixed index {:?} - {}", path, err))
}
let mut file = File::open(&full_path) pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
.map_err(|err| format_err!("Unable to open fixed index {:?} - {}", full_path, err))?;
if let Err(err) = nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock) { if let Err(err) = nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock) {
bail!("unable to get shared lock on {:?} - {}", full_path, err); bail!("unable to get shared lock - {}", err);
} }
let header_size = std::mem::size_of::<FixedIndexHeader>(); let header_size = std::mem::size_of::<FixedIndexHeader>();
// todo: use static assertion when available in rust // todo: use static assertion when available in rust
if header_size != 4096 { bail!("got unexpected header size for {:?}", path); } if header_size != 4096 { bail!("got unexpected header size"); }
let mut buffer = vec![0u8; header_size]; let mut buffer = vec![0u8; header_size];
file.read_exact(&mut buffer)?; file.read_exact(&mut buffer)?;
@ -78,7 +79,7 @@ impl FixedIndexReader {
let header = unsafe { &mut * (buffer.as_ptr() as *mut FixedIndexHeader) }; let header = unsafe { &mut * (buffer.as_ptr() as *mut FixedIndexHeader) };
if header.magic != super::FIXED_SIZED_CHUNK_INDEX_1_0 { if header.magic != super::FIXED_SIZED_CHUNK_INDEX_1_0 {
bail!("got unknown magic number for {:?}", path); bail!("got unknown magic number");
} }
let size = u64::from_le(header.size) as usize; let size = u64::from_le(header.size) as usize;
@ -92,13 +93,12 @@ impl FixedIndexReader {
let stat = match nix::sys::stat::fstat(rawfd) { let stat = match nix::sys::stat::fstat(rawfd) {
Ok(stat) => stat, Ok(stat) => stat,
Err(err) => bail!("fstat {:?} failed - {}", path, err), Err(err) => bail!("fstat failed - {}", err),
}; };
let expected_index_size = (stat.st_size as usize) - header_size; let expected_index_size = (stat.st_size as usize) - header_size;
if index_size != expected_index_size { if index_size != expected_index_size {
bail!("got unexpected file size for {:?} ({} != {})", bail!("got unexpected file size ({} != {})", index_size, expected_index_size);
path, index_size, expected_index_size);
} }
let data = unsafe { nix::sys::mman::mmap( let data = unsafe { nix::sys::mman::mmap(
@ -110,8 +110,6 @@ impl FixedIndexReader {
header_size as i64) }? as *mut u8; header_size as i64) }? as *mut u8;
Ok(Self { Ok(Self {
store,
filename: full_path,
_file: file, _file: file,
chunk_size, chunk_size,
size, size,
@ -130,7 +128,7 @@ impl FixedIndexReader {
let index_size = self.index_length*32; let index_size = self.index_length*32;
if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) } { if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) } {
bail!("unmap file {:?} failed - {}", self.filename, err); bail!("unmap file failed - {}", err);
} }
self.index = std::ptr::null_mut(); self.index = std::ptr::null_mut();
@ -139,7 +137,6 @@ impl FixedIndexReader {
} }
pub fn print_info(&self) { pub fn print_info(&self) {
println!("Filename: {:?}", self.filename);
println!("Size: {}", self.size); println!("Size: {}", self.size);
println!("ChunkSize: {}", self.chunk_size); println!("ChunkSize: {}", self.chunk_size);
println!("CTime: {}", Local.timestamp(self.ctime as i64, 0).format("%c")); println!("CTime: {}", Local.timestamp(self.ctime as i64, 0).format("%c"));