backup/datastore.rs: use Arc for chunk_store references

This commit is contained in:
Dietmar Maurer 2019-01-15 12:36:16 +01:00
parent 1629d2ad7b
commit 150f1bd8f6
3 changed files with 20 additions and 19 deletions

View File

@ -21,8 +21,8 @@ pub struct ArchiveIndexHeader {
}
pub struct ArchiveIndexReader<'a> {
store: &'a ChunkStore,
pub struct ArchiveIndexReader {
store: Arc<ChunkStore>,
file: File,
size: usize,
filename: PathBuf,
@ -32,7 +32,7 @@ pub struct ArchiveIndexReader<'a> {
ctime: u64,
}
impl <'a> Drop for ArchiveIndexReader<'a> {
impl Drop for ArchiveIndexReader {
fn drop(&mut self) {
if let Err(err) = self.unmap() {
@ -41,9 +41,9 @@ impl <'a> Drop for ArchiveIndexReader<'a> {
}
}
impl <'a> ArchiveIndexReader<'a> {
impl ArchiveIndexReader {
pub fn open(store: &'a ChunkStore, path: &Path) -> Result<Self, Error> {
pub fn open(store: Arc<ChunkStore>, path: &Path) -> Result<Self, Error> {
let full_path = store.relative_path(path);
@ -189,7 +189,7 @@ impl <'a> ArchiveIndexReader<'a> {
}
pub struct BufferedArchiveReader<'a> {
index: &'a ArchiveIndexReader<'a>,
index: &'a ArchiveIndexReader,
archive_size: u64,
read_buffer: Vec<u8>,
buffered_chunk_idx: usize,

View File

@ -65,14 +65,14 @@ impl DataStore {
pub fn create_image_writer<P: AsRef<Path>>(&self, filename: P, size: usize, chunk_size: usize) -> Result<ImageIndexWriter, Error> {
let index = ImageIndexWriter::create(&self.chunk_store, filename.as_ref(), size, chunk_size)?;
let index = ImageIndexWriter::create(self.chunk_store.clone(), filename.as_ref(), size, chunk_size)?;
Ok(index)
}
pub fn open_image_reader<P: AsRef<Path>>(&self, filename: P) -> Result<ImageIndexReader, Error> {
let index = ImageIndexReader::open(&self.chunk_store, filename.as_ref())?;
let index = ImageIndexReader::open(self.chunk_store.clone(), filename.as_ref())?;
Ok(index)
}
@ -90,7 +90,7 @@ impl DataStore {
pub fn open_archive_reader<P: AsRef<Path>>(&self, filename: P) -> Result<ArchiveIndexReader, Error> {
let index = ArchiveIndexReader::open(&self.chunk_store, filename.as_ref())?;
let index = ArchiveIndexReader::open(self.chunk_store.clone(), filename.as_ref())?;
Ok(index)
}

View File

@ -2,6 +2,7 @@ use failure::*;
use super::chunk_store::*;
use std::sync::Arc;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::os::unix::io::AsRawFd;
@ -21,8 +22,8 @@ pub struct ImageIndexHeader {
// split image into fixed size chunks
pub struct ImageIndexReader<'a> {
store: &'a ChunkStore,
pub struct ImageIndexReader {
store: Arc<ChunkStore>,
filename: PathBuf,
chunk_size: usize,
size: usize,
@ -31,7 +32,7 @@ pub struct ImageIndexReader<'a> {
ctime: u64,
}
impl <'a> Drop for ImageIndexReader<'a> {
impl Drop for ImageIndexReader {
fn drop(&mut self) {
if let Err(err) = self.unmap() {
@ -40,9 +41,9 @@ impl <'a> Drop for ImageIndexReader<'a> {
}
}
impl <'a> ImageIndexReader<'a> {
impl ImageIndexReader {
pub fn open(store: &'a ChunkStore, path: &Path) -> Result<Self, Error> {
pub fn open(store: Arc<ChunkStore>, path: &Path) -> Result<Self, Error> {
let full_path = store.relative_path(path);
@ -150,8 +151,8 @@ impl <'a> ImageIndexReader<'a> {
}
}
pub struct ImageIndexWriter<'a> {
store: &'a ChunkStore,
pub struct ImageIndexWriter {
store: Arc<ChunkStore>,
filename: PathBuf,
tmp_filename: PathBuf,
chunk_size: usize,
@ -162,7 +163,7 @@ pub struct ImageIndexWriter<'a> {
ctime: u64,
}
impl <'a> Drop for ImageIndexWriter<'a> {
impl Drop for ImageIndexWriter {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
@ -172,9 +173,9 @@ impl <'a> Drop for ImageIndexWriter<'a> {
}
}
impl <'a> ImageIndexWriter<'a> {
impl ImageIndexWriter {
pub fn create(store: &'a ChunkStore, path: &Path, size: usize, chunk_size: usize) -> Result<Self, Error> {
pub fn create(store: Arc<ChunkStore>, path: &Path, size: usize, chunk_size: usize) -> Result<Self, Error> {
let full_path = store.relative_path(path);
let mut tmp_path = full_path.clone();