move lock_file to tools.rs
This commit is contained in:
parent
08481a0b60
commit
365bb90f17
@ -6,10 +6,11 @@ use crypto::digest::Digest;
|
|||||||
use crypto::sha2::Sha512Trunc256;
|
use crypto::sha2::Sha512Trunc256;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::File;
|
||||||
use nix::fcntl::{flock, FlockArg};
|
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
|
||||||
|
use crate::tools;
|
||||||
|
|
||||||
pub struct ChunkStore {
|
pub struct ChunkStore {
|
||||||
base: PathBuf,
|
base: PathBuf,
|
||||||
chunk_dir: PathBuf,
|
chunk_dir: PathBuf,
|
||||||
@ -50,47 +51,6 @@ fn digest_to_prefix(digest: &[u8]) -> PathBuf {
|
|||||||
path.into()
|
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 {
|
impl ChunkStore {
|
||||||
|
|
||||||
@ -140,7 +100,7 @@ impl ChunkStore {
|
|||||||
lockfile_path.push(".lock");
|
lockfile_path.push(".lock");
|
||||||
|
|
||||||
// make sure only one process/thread/task can use it
|
// 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 {
|
Ok(ChunkStore {
|
||||||
base,
|
base,
|
||||||
|
@ -67,7 +67,7 @@ impl DataStore {
|
|||||||
|
|
||||||
for path in image_list {
|
for path in image_list {
|
||||||
let mut index = self.open_image_reader(path)?;
|
let mut index = self.open_image_reader(path)?;
|
||||||
index.mark_used_chunks();
|
index.mark_used_chunks()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
50
src/tools.rs
50
src/tools.rs
@ -1,13 +1,16 @@
|
|||||||
use failure::*;
|
use failure::*;
|
||||||
use nix::unistd;
|
use nix::unistd;
|
||||||
use nix::sys::stat;
|
use nix::sys::stat;
|
||||||
|
use nix::fcntl::{flock, FlockArg};
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
|
|
||||||
|
use std::os::unix::io::AsRawFd;
|
||||||
|
|
||||||
pub fn file_set_contents<P: AsRef<Path>>(
|
pub fn file_set_contents<P: AsRef<Path>>(
|
||||||
path: P,
|
path: P,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
@ -53,6 +56,51 @@ pub fn file_set_contents<P: AsRef<Path>>(
|
|||||||
Ok(())
|
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
|
// Note: We cannot implement an Iterator, because Iterators cannot
|
||||||
// return a borrowed buffer ref (we want zero-copy)
|
// return a borrowed buffer ref (we want zero-copy)
|
||||||
pub fn file_chunker<C, R>(
|
pub fn file_chunker<C, R>(
|
||||||
|
Loading…
Reference in New Issue
Block a user