move lock_file to tools.rs

This commit is contained in:
Dietmar Maurer 2018-12-19 10:02:24 +01:00
parent 08481a0b60
commit 365bb90f17
3 changed files with 54 additions and 46 deletions

View File

@ -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<P: AsRef<Path>>(filename: P, timeout: usize) -> Result<File, Error> {
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,

View File

@ -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(())

View File

@ -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<P: AsRef<Path>>(
path: P,
data: &[u8],
@ -53,6 +56,51 @@ pub fn file_set_contents<P: AsRef<Path>>(
Ok(())
}
pub fn lock_file<P: AsRef<Path>>(
filename: P,
timeout: usize
) -> Result<File, Error> {
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<C, R>(