From 365bb90f17535994fa09aa363787e8855505bc75 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 19 Dec 2018 10:02:24 +0100 Subject: [PATCH] move lock_file to tools.rs --- src/backup/chunk_store.rs | 48 ++++--------------------------------- src/backup/datastore.rs | 2 +- src/tools.rs | 50 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/backup/chunk_store.rs b/src/backup/chunk_store.rs index f54956da..3398de99 100644 --- a/src/backup/chunk_store.rs +++ b/src/backup/chunk_store.rs @@ -6,10 +6,11 @@ use crypto::digest::Digest; use crypto::sha2::Sha512Trunc256; use std::sync::Mutex; -use std::fs::{File, OpenOptions}; -use nix::fcntl::{flock, FlockArg}; +use std::fs::File; use std::os::unix::io::AsRawFd; +use crate::tools; + pub struct ChunkStore { base: PathBuf, chunk_dir: PathBuf, @@ -50,47 +51,6 @@ fn digest_to_prefix(digest: &[u8]) -> PathBuf { path.into() } -fn lock_file>(filename: P, timeout: usize) -> Result { - - let path = filename.as_ref(); - let lockfile = match OpenOptions::new() - .create(true) - .append(true) - .open(path) { - Ok(file) => file, - Err(err) => bail!("Unable to open lock {:?} - {}", - path, err), - }; - - let fd = lockfile.as_raw_fd(); - - let now = std::time::SystemTime::now(); - let mut print_msg = true; - loop { - match flock(fd, FlockArg::LockExclusiveNonblock) { - Ok(_) => break, - Err(_) => { - if print_msg { - print_msg = false; - eprintln!("trying to aquire lock..."); - } - } - } - - match now.elapsed() { - Ok(elapsed) => { - if elapsed.as_secs() >= (timeout as u64) { - bail!("unable to aquire lock {:?} - got timeout", path); - } - } - Err(err) => { - bail!("unable to aquire lock {:?} - clock problems - {}", path, err); - } - } - std::thread::sleep(std::time::Duration::from_millis(100)); - } - Ok(lockfile) -} impl ChunkStore { @@ -140,7 +100,7 @@ impl ChunkStore { lockfile_path.push(".lock"); // make sure only one process/thread/task can use it - let lockfile = lock_file(lockfile_path, 10)?; + let lockfile = tools::lock_file(lockfile_path, 10)?; Ok(ChunkStore { base, diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index 86ab4434..6e60f4a8 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -67,7 +67,7 @@ impl DataStore { for path in image_list { let mut index = self.open_image_reader(path)?; - index.mark_used_chunks(); + index.mark_used_chunks()?; } Ok(()) diff --git a/src/tools.rs b/src/tools.rs index 9eb54ec7..d1ebe9d5 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,13 +1,16 @@ use failure::*; use nix::unistd; use nix::sys::stat; +use nix::fcntl::{flock, FlockArg}; -use std::fs::File; +use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::Path; use std::io::Read; use std::io::ErrorKind; +use std::os::unix::io::AsRawFd; + pub fn file_set_contents>( path: P, data: &[u8], @@ -53,6 +56,51 @@ pub fn file_set_contents>( Ok(()) } +pub fn lock_file>( + filename: P, + timeout: usize +) -> Result { + + let path = filename.as_ref(); + let lockfile = match OpenOptions::new() + .create(true) + .append(true) + .open(path) { + Ok(file) => file, + Err(err) => bail!("Unable to open lock {:?} - {}", + path, err), + }; + + let fd = lockfile.as_raw_fd(); + + let now = std::time::SystemTime::now(); + let mut print_msg = true; + loop { + match flock(fd, FlockArg::LockExclusiveNonblock) { + Ok(_) => break, + Err(_) => { + if print_msg { + print_msg = false; + eprintln!("trying to aquire lock..."); + } + } + } + + match now.elapsed() { + Ok(elapsed) => { + if elapsed.as_secs() >= (timeout as u64) { + bail!("unable to aquire lock {:?} - got timeout", path); + } + } + Err(err) => { + bail!("unable to aquire lock {:?} - clock problems - {}", path, err); + } + } + std::thread::sleep(std::time::Duration::from_millis(100)); + } + Ok(lockfile) +} + // Note: We cannot implement an Iterator, because Iterators cannot // return a borrowed buffer ref (we want zero-copy) pub fn file_chunker(