2018-12-07 09:51:53 +00:00
|
|
|
use failure::*;
|
|
|
|
use std::path::{Path, PathBuf};
|
2018-12-07 13:44:56 +00:00
|
|
|
use std::io::Write;
|
2018-12-19 10:08:57 +00:00
|
|
|
use std::time::Duration;
|
2018-12-07 13:44:56 +00:00
|
|
|
|
|
|
|
use crypto::digest::Digest;
|
|
|
|
use crypto::sha2::Sha512Trunc256;
|
2018-12-07 15:12:45 +00:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2018-12-19 09:02:24 +00:00
|
|
|
use std::fs::File;
|
2018-12-07 17:14:07 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-19 09:02:24 +00:00
|
|
|
use crate::tools;
|
|
|
|
|
2018-12-07 09:51:53 +00:00
|
|
|
pub struct ChunkStore {
|
2018-12-19 12:40:26 +00:00
|
|
|
name: String, // used for error reporting
|
2018-12-07 09:51:53 +00:00
|
|
|
base: PathBuf,
|
|
|
|
chunk_dir: PathBuf,
|
2018-12-07 13:44:56 +00:00
|
|
|
hasher: Sha512Trunc256,
|
2018-12-07 15:12:45 +00:00
|
|
|
mutex: Mutex<bool>,
|
2018-12-19 11:49:23 +00:00
|
|
|
_lockfile: File,
|
2018-12-07 13:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const HEX_CHARS: &'static [u8; 16] = b"0123456789abcdef";
|
|
|
|
|
2018-12-19 08:51:33 +00:00
|
|
|
pub fn digest_to_hex(digest: &[u8]) -> String {
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
let mut buf = Vec::<u8>::with_capacity(digest.len()*2);
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
for i in 0..digest.len() {
|
2018-12-07 13:44:56 +00:00
|
|
|
buf.push(HEX_CHARS[(digest[i] >> 4) as usize]);
|
|
|
|
buf.push(HEX_CHARS[(digest[i] & 0xf) as usize]);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { String::from_utf8_unchecked(buf) }
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 08:51:33 +00:00
|
|
|
fn digest_to_prefix(digest: &[u8]) -> PathBuf {
|
2018-12-07 13:44:56 +00:00
|
|
|
|
|
|
|
let mut buf = Vec::<u8>::with_capacity(3+1+2+1);
|
|
|
|
|
|
|
|
buf.push(HEX_CHARS[(digest[0] as usize) >> 4]);
|
|
|
|
buf.push(HEX_CHARS[(digest[0] as usize) &0xf]);
|
|
|
|
buf.push(HEX_CHARS[(digest[1] as usize) >> 4]);
|
|
|
|
buf.push('/' as u8);
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-07 13:44:56 +00:00
|
|
|
buf.push(HEX_CHARS[(digest[1] as usize) & 0xf]);
|
|
|
|
buf.push(HEX_CHARS[(digest[2] as usize) >> 4]);
|
|
|
|
buf.push('/' as u8);
|
|
|
|
|
|
|
|
let path = unsafe { String::from_utf8_unchecked(buf)};
|
|
|
|
|
|
|
|
path.into()
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 10:20:02 +00:00
|
|
|
|
2018-12-07 09:51:53 +00:00
|
|
|
impl ChunkStore {
|
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
fn chunk_dir<P: AsRef<Path>>(path: P) -> PathBuf {
|
|
|
|
|
|
|
|
let mut chunk_dir: PathBuf = PathBuf::from(path.as_ref());
|
|
|
|
chunk_dir.push(".chunks");
|
|
|
|
|
|
|
|
chunk_dir
|
|
|
|
}
|
|
|
|
|
2018-12-19 12:40:26 +00:00
|
|
|
pub fn create<P: Into<PathBuf>>(name: &str, path: P) -> Result<Self, Error> {
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
let base: PathBuf = path.into();
|
|
|
|
let chunk_dir = Self::chunk_dir(&base);
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-08 07:38:42 +00:00
|
|
|
if let Err(err) = std::fs::create_dir(&base) {
|
2018-12-19 12:40:26 +00:00
|
|
|
bail!("unable to create chunk store '{}' at {:?} - {}", name, base, err);
|
2018-12-08 07:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = std::fs::create_dir(&chunk_dir) {
|
2018-12-19 12:40:26 +00:00
|
|
|
bail!("unable to create chunk store '{}' subdir {:?} - {}", name, chunk_dir, err);
|
2018-12-08 07:38:42 +00:00
|
|
|
}
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-07 13:44:56 +00:00
|
|
|
// create 4096 subdir
|
|
|
|
for i in 0..4096 {
|
2018-12-16 12:52:16 +00:00
|
|
|
let mut l1path = chunk_dir.clone();
|
2018-12-07 13:44:56 +00:00
|
|
|
l1path.push(format!("{:03x}",i));
|
2018-12-08 07:38:42 +00:00
|
|
|
if let Err(err) = std::fs::create_dir(&l1path) {
|
2018-12-19 12:40:26 +00:00
|
|
|
bail!("unable to create chunk store '{}' subdir {:?} - {}", name, l1path, err);
|
2018-12-08 07:38:42 +00:00
|
|
|
}
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 12:40:26 +00:00
|
|
|
Self::open(name, base)
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 12:40:26 +00:00
|
|
|
pub fn open<P: Into<PathBuf>>(name: &str, path: P) -> Result<Self, Error> {
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
let base: PathBuf = path.into();
|
|
|
|
let chunk_dir = Self::chunk_dir(&base);
|
|
|
|
|
2018-12-09 08:42:17 +00:00
|
|
|
if let Err(err) = std::fs::metadata(&chunk_dir) {
|
2018-12-19 12:40:26 +00:00
|
|
|
bail!("unable to open chunk store '{}' at {:?} - {}", name, chunk_dir, err);
|
2018-12-09 08:42:17 +00:00
|
|
|
}
|
2018-12-07 17:14:07 +00:00
|
|
|
|
|
|
|
let mut lockfile_path = base.clone();
|
|
|
|
lockfile_path.push(".lock");
|
|
|
|
|
2018-12-12 10:21:00 +00:00
|
|
|
// make sure only one process/thread/task can use it
|
2018-12-19 10:08:57 +00:00
|
|
|
let lockfile = tools::open_file_locked(
|
|
|
|
lockfile_path, Duration::from_secs(10))?;
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-08 07:21:20 +00:00
|
|
|
Ok(ChunkStore {
|
2018-12-19 12:40:26 +00:00
|
|
|
name: name.to_owned(),
|
2018-12-08 07:21:20 +00:00
|
|
|
base,
|
|
|
|
chunk_dir,
|
|
|
|
hasher: Sha512Trunc256::new(),
|
2018-12-19 11:49:23 +00:00
|
|
|
_lockfile: lockfile,
|
2018-12-08 07:21:20 +00:00
|
|
|
mutex: Mutex::new(false)
|
|
|
|
})
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
pub fn touch_chunk(&mut self, digest:&[u8]) -> Result<(), Error> {
|
|
|
|
|
2018-12-19 08:51:33 +00:00
|
|
|
// fixme: nix::sys::stat::utimensat
|
2018-12-18 10:06:03 +00:00
|
|
|
let mut chunk_path = self.chunk_dir.clone();
|
2018-12-19 08:51:33 +00:00
|
|
|
let prefix = digest_to_prefix(&digest);
|
2018-12-18 10:06:03 +00:00
|
|
|
chunk_path.push(&prefix);
|
2018-12-19 08:51:33 +00:00
|
|
|
let digest_str = digest_to_hex(&digest);
|
2018-12-18 10:06:03 +00:00
|
|
|
chunk_path.push(&digest_str);
|
|
|
|
|
|
|
|
std::fs::metadata(&chunk_path)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-19 12:40:26 +00:00
|
|
|
fn sweep_old_files(&self, handle: &mut nix::dir::Dir) -> Result<(), Error> {
|
2018-12-19 08:51:33 +00:00
|
|
|
|
|
|
|
let rawfd = handle.as_raw_fd();
|
|
|
|
|
|
|
|
let now = unsafe { libc::time(std::ptr::null_mut()) };
|
|
|
|
|
|
|
|
for entry in handle.iter() {
|
2018-12-19 11:49:23 +00:00
|
|
|
let entry = match entry {
|
|
|
|
Ok(entry) => entry,
|
|
|
|
Err(_) => continue /* ignore */,
|
|
|
|
};
|
|
|
|
let file_type = match entry.file_type() {
|
|
|
|
Some(file_type) => file_type,
|
2018-12-19 12:40:26 +00:00
|
|
|
None => bail!("unsupported file system type on chunk store '{}'", self.name),
|
2018-12-19 11:49:23 +00:00
|
|
|
};
|
|
|
|
if file_type != nix::dir::Type::File { continue; }
|
|
|
|
|
|
|
|
let filename = entry.file_name();
|
|
|
|
if let Ok(stat) = nix::sys::stat::fstatat(rawfd, filename, nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
|
|
|
|
let age = now - stat.st_atime;
|
|
|
|
println!("FOUND {} {:?}", age/(3600*24), filename);
|
|
|
|
if age/(3600*24) >= 2 {
|
|
|
|
println!("UNLINK {} {:?}", age/(3600*24), filename);
|
2018-12-19 12:40:26 +00:00
|
|
|
let res = unsafe { libc::unlinkat(rawfd, filename.as_ptr(), 0) };
|
|
|
|
if res != 0 {
|
|
|
|
let err = nix::Error::last();
|
|
|
|
bail!("unlink chunk {:?} failed on store '{}' - {}", filename, self.name, err);
|
|
|
|
}
|
2018-12-19 08:51:33 +00:00
|
|
|
}
|
2018-12-19 11:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 12:40:26 +00:00
|
|
|
Ok(())
|
2018-12-19 08:51:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 10:18:55 +00:00
|
|
|
pub fn sweep_used_chunks(&mut self) -> Result<(), Error> {
|
|
|
|
|
2018-12-19 11:30:37 +00:00
|
|
|
use nix::fcntl::OFlag;
|
|
|
|
use nix::sys::stat::Mode;
|
|
|
|
use nix::dir::Dir;
|
|
|
|
|
2018-12-19 11:49:23 +00:00
|
|
|
let base_handle = match Dir::open(
|
2018-12-19 11:30:37 +00:00
|
|
|
&self.chunk_dir, OFlag::O_RDONLY, Mode::empty()) {
|
2018-12-19 11:20:01 +00:00
|
|
|
Ok(h) => h,
|
2018-12-19 12:40:26 +00:00
|
|
|
Err(err) => bail!("unable to open store '{}' chunk dir {:?} - {}",
|
|
|
|
self.name, self.chunk_dir, err),
|
2018-12-19 11:20:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let base_fd = base_handle.as_raw_fd();
|
|
|
|
|
2018-12-19 08:51:33 +00:00
|
|
|
for i in 0..4096 {
|
2018-12-19 11:20:01 +00:00
|
|
|
let l1name = PathBuf::from(format!("{:03x}", i));
|
|
|
|
let mut l1_handle = match nix::dir::Dir::openat(
|
2018-12-19 11:30:37 +00:00
|
|
|
base_fd, &l1name, OFlag::O_RDONLY, Mode::empty()) {
|
2018-12-19 11:20:01 +00:00
|
|
|
Ok(h) => h,
|
2018-12-19 12:40:26 +00:00
|
|
|
Err(err) => bail!("unable to open store '{}' dir {:?}/{:?} - {}",
|
|
|
|
self.name, self.chunk_dir, l1name, err),
|
2018-12-19 11:20:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let l1_fd = l1_handle.as_raw_fd();
|
|
|
|
|
|
|
|
for l1_entry in l1_handle.iter() {
|
2018-12-19 11:40:26 +00:00
|
|
|
let l1_entry = match l1_entry {
|
|
|
|
Ok(l1_entry) => l1_entry,
|
|
|
|
Err(_) => continue /* ignore errors? */,
|
|
|
|
};
|
|
|
|
let file_type = match l1_entry.file_type() {
|
|
|
|
Some(file_type) => file_type,
|
2018-12-19 12:40:26 +00:00
|
|
|
None => bail!("unsupported file system type on chunk store '{}'", self.name),
|
2018-12-19 11:40:26 +00:00
|
|
|
};
|
|
|
|
if file_type != nix::dir::Type::Directory { continue; }
|
|
|
|
|
|
|
|
let l2name = l1_entry.file_name();
|
|
|
|
if l2name.to_bytes_with_nul()[0] == b'.' { continue; }
|
|
|
|
|
|
|
|
let mut l2_handle = match Dir::openat(
|
|
|
|
l1_fd, l2name, OFlag::O_RDONLY, Mode::empty()) {
|
|
|
|
Ok(h) => h,
|
|
|
|
Err(err) => bail!(
|
2018-12-19 12:40:26 +00:00
|
|
|
"unable to open store '{}' dir {:?}/{:?}/{:?} - {}",
|
|
|
|
self.name, self.chunk_dir, l1name, l2name, err),
|
2018-12-19 11:40:26 +00:00
|
|
|
};
|
2018-12-19 12:40:26 +00:00
|
|
|
self.sweep_old_files(&mut l2_handle)?;
|
2018-12-19 08:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 10:18:55 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-08 10:25:11 +00:00
|
|
|
pub fn insert_chunk(&mut self, chunk: &[u8]) -> Result<(bool, [u8; 32]), Error> {
|
2018-12-07 13:44:56 +00:00
|
|
|
|
|
|
|
self.hasher.reset();
|
|
|
|
self.hasher.input(chunk);
|
|
|
|
|
|
|
|
let mut digest = [0u8; 32];
|
|
|
|
self.hasher.result(&mut digest);
|
2018-12-19 08:51:33 +00:00
|
|
|
//println!("DIGEST {}", digest_to_hex(&digest));
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-16 12:52:16 +00:00
|
|
|
let mut chunk_path = self.chunk_dir.clone();
|
2018-12-19 08:51:33 +00:00
|
|
|
let prefix = digest_to_prefix(&digest);
|
2018-12-07 15:12:45 +00:00
|
|
|
chunk_path.push(&prefix);
|
2018-12-19 08:51:33 +00:00
|
|
|
let digest_str = digest_to_hex(&digest);
|
2018-12-07 15:12:45 +00:00
|
|
|
chunk_path.push(&digest_str);
|
|
|
|
|
|
|
|
let lock = self.mutex.lock();
|
|
|
|
|
|
|
|
if let Ok(metadata) = std::fs::metadata(&chunk_path) {
|
|
|
|
if metadata.is_file() {
|
2018-12-08 10:25:11 +00:00
|
|
|
return Ok((true, digest));
|
2018-12-07 15:12:45 +00:00
|
|
|
} else {
|
2018-12-19 12:54:22 +00:00
|
|
|
bail!("Got unexpected file type on store '{}' for chunk {}", self.name, digest_str);
|
2018-12-07 15:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-16 12:52:16 +00:00
|
|
|
let mut chunk_dir = self.chunk_dir.clone();
|
2018-12-07 15:12:45 +00:00
|
|
|
chunk_dir.push(&prefix);
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-07 15:12:45 +00:00
|
|
|
if let Err(_) = std::fs::create_dir(&chunk_dir) { /* ignore */ }
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-07 15:12:45 +00:00
|
|
|
let mut tmp_path = chunk_path.clone();
|
|
|
|
tmp_path.set_extension("tmp");
|
|
|
|
let mut f = std::fs::File::create(&tmp_path)?;
|
2018-12-07 13:44:56 +00:00
|
|
|
f.write_all(chunk)?;
|
|
|
|
|
2018-12-07 15:12:45 +00:00
|
|
|
if let Err(err) = std::fs::rename(&tmp_path, &chunk_path) {
|
|
|
|
if let Err(_) = std::fs::remove_file(&tmp_path) { /* ignore */ }
|
2018-12-19 12:54:22 +00:00
|
|
|
bail!("Atomic rename on store '{}' failed for chunk {} - {}", self.name, digest_str, err);
|
2018-12-07 15:12:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 13:44:56 +00:00
|
|
|
println!("PATH {:?}", chunk_path);
|
|
|
|
|
2018-12-07 15:12:45 +00:00
|
|
|
drop(lock);
|
|
|
|
|
2018-12-08 10:25:11 +00:00
|
|
|
Ok((false, digest))
|
2018-12-07 13:44:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
pub fn relative_path(&self, path: &Path) -> PathBuf {
|
|
|
|
|
|
|
|
let mut full_path = self.base.clone();
|
|
|
|
full_path.push(path);
|
|
|
|
full_path
|
|
|
|
}
|
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
pub fn base_path(&self) -> PathBuf {
|
|
|
|
self.base.clone()
|
|
|
|
}
|
|
|
|
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunk_store1() {
|
|
|
|
|
|
|
|
if let Err(_e) = std::fs::remove_dir_all(".testdir") { /* ignore */ }
|
|
|
|
|
2018-12-19 12:54:22 +00:00
|
|
|
let chunk_store = ChunkStore::open("test", ".testdir");
|
2018-12-07 09:51:53 +00:00
|
|
|
assert!(chunk_store.is_err());
|
|
|
|
|
2018-12-19 12:54:22 +00:00
|
|
|
let mut chunk_store = ChunkStore::create("test", ".testdir").unwrap();
|
2018-12-08 10:25:11 +00:00
|
|
|
let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
|
|
|
|
assert!(!exists);
|
|
|
|
|
|
|
|
let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
|
|
|
|
assert!(exists);
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-19 12:54:22 +00:00
|
|
|
let chunk_store = ChunkStore::create("test", ".testdir");
|
2018-12-07 09:51:53 +00:00
|
|
|
assert!(chunk_store.is_err());
|
|
|
|
|
|
|
|
|
|
|
|
}
|