From 03e4753d8e43513fb2b312ef9ad72948a0eef852 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 21 Dec 2018 12:15:26 +0100 Subject: [PATCH] fix mutability for chunk store --- src/api3.rs | 11 +++++++++++ src/backup/chunk_store.rs | 19 ++++++++----------- src/backup/datastore.rs | 10 +++++----- src/backup/image_index.rs | 6 +++--- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/api3.rs b/src/api3.rs index 2fb3f200..cb525add 100644 --- a/src/api3.rs +++ b/src/api3.rs @@ -34,6 +34,17 @@ fn lookup_datastore(name: &str) -> Result, Error> { bail!("store not found"); } +// this is just a test for mutability/mutex handling - will remove later +fn test_sync_api_handler2(param: Value, _info: &ApiMethod) -> Result { + println!("This is a test {}", param); + + let datastore = lookup_datastore("store1")?; + + datastore.garbage_collection(); + + Ok(json!(null)) +} + fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result { println!("This is a test {}", param); diff --git a/src/backup/chunk_store.rs b/src/backup/chunk_store.rs index 16ae7902..5a3bf320 100644 --- a/src/backup/chunk_store.rs +++ b/src/backup/chunk_store.rs @@ -16,7 +16,6 @@ pub struct ChunkStore { name: String, // used for error reporting base: PathBuf, chunk_dir: PathBuf, - hasher: Sha512Trunc256, mutex: Mutex, _lockfile: File, } @@ -109,13 +108,12 @@ impl ChunkStore { name: name.to_owned(), base, chunk_dir, - hasher: Sha512Trunc256::new(), - _lockfile: lockfile, + _lockfile: lockfile, mutex: Mutex::new(false) }) } - pub fn touch_chunk(&mut self, digest:&[u8]) -> Result<(), Error> { + pub fn touch_chunk(&self, digest:&[u8]) -> Result<(), Error> { // fixme: nix::sys::stat::utimensat let mut chunk_path = self.chunk_dir.clone(); @@ -162,7 +160,7 @@ impl ChunkStore { Ok(()) } - pub fn sweep_used_chunks(&mut self) -> Result<(), Error> { + pub fn sweep_used_chunks(&self) -> Result<(), Error> { use nix::fcntl::OFlag; use nix::sys::stat::Mode; @@ -215,13 +213,12 @@ impl ChunkStore { Ok(()) } - pub fn insert_chunk(&mut self, chunk: &[u8]) -> Result<(bool, [u8; 32]), Error> { - - self.hasher.reset(); - self.hasher.input(chunk); + pub fn insert_chunk(&self, chunk: &[u8]) -> Result<(bool, [u8; 32]), Error> { + let mut hasher = Sha512Trunc256::new(); + hasher.input(chunk); let mut digest = [0u8; 32]; - self.hasher.result(&mut digest); + hasher.result(&mut digest); //println!("DIGEST {}", digest_to_hex(&digest)); let mut chunk_path = self.chunk_dir.clone(); @@ -284,7 +281,7 @@ fn test_chunk_store1() { let chunk_store = ChunkStore::open("test", ".testdir"); assert!(chunk_store.is_err()); - let mut chunk_store = ChunkStore::create("test", ".testdir").unwrap(); + let chunk_store = ChunkStore::create("test", ".testdir").unwrap(); let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap(); assert!(!exists); diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index de903562..cdf4a435 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -34,9 +34,9 @@ impl DataStore { Ok(index) } - pub fn open_image_reader>(&mut self, filename: P) -> Result { + pub fn open_image_reader>(&self, filename: P) -> Result { - let index = ImageIndexReader::open(&mut self.chunk_store, filename.as_ref())?; + let index = ImageIndexReader::open(&self.chunk_store, filename.as_ref())?; Ok(index) } @@ -61,19 +61,19 @@ impl DataStore { Ok(list) } - fn mark_used_chunks(&mut self) -> Result<(), Error> { + fn mark_used_chunks(&self) -> Result<(), Error> { let image_list = self.list_images()?; for path in image_list { - let mut index = self.open_image_reader(path)?; + let index = self.open_image_reader(path)?; index.mark_used_chunks()?; } Ok(()) } - pub fn garbage_collection(&mut self) -> Result<(), Error> { + pub fn garbage_collection(&self) -> Result<(), Error> { self.mark_used_chunks()?; diff --git a/src/backup/image_index.rs b/src/backup/image_index.rs index 25170dc5..fb521a61 100644 --- a/src/backup/image_index.rs +++ b/src/backup/image_index.rs @@ -22,7 +22,7 @@ pub struct ImageIndexHeader { // split image into fixed size chunks pub struct ImageIndexReader<'a> { - store: &'a mut ChunkStore, + store: &'a ChunkStore, filename: PathBuf, chunk_size: usize, size: usize, @@ -42,7 +42,7 @@ impl <'a> Drop for ImageIndexReader<'a> { impl <'a> ImageIndexReader<'a> { - pub fn open(store: &'a mut ChunkStore, path: &Path) -> Result { + pub fn open(store: &'a ChunkStore, path: &Path) -> Result { let full_path = store.relative_path(path); @@ -100,7 +100,7 @@ impl <'a> ImageIndexReader<'a> { Ok(()) } - pub fn mark_used_chunks(&mut self) -> Result<(), Error> { + pub fn mark_used_chunks(&self) -> Result<(), Error> { if self.index == std::ptr::null_mut() { bail!("detected closed index file."); } -- 2.39.5