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;
|
|
|
|
|
|
|
|
use crypto::digest::Digest;
|
|
|
|
use crypto::sha2::Sha512Trunc256;
|
2018-12-07 15:12:45 +00:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
use std::fs::{File, OpenOptions};
|
|
|
|
use nix::fcntl::{flock, FlockArg};
|
|
|
|
use std::os::unix::io::AsRawFd;
|
2018-12-07 09:51:53 +00:00
|
|
|
|
|
|
|
pub struct ChunkStore {
|
|
|
|
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-07 17:14:07 +00:00
|
|
|
lockfile: File,
|
2018-12-07 13:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const HEX_CHARS: &'static [u8; 16] = b"0123456789abcdef";
|
|
|
|
|
|
|
|
fn u256_to_hex(digest: &[u8; 32]) -> String {
|
|
|
|
|
|
|
|
let mut buf = Vec::<u8>::with_capacity(64);
|
|
|
|
|
|
|
|
for i in 0..32 {
|
|
|
|
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-07 13:44:56 +00:00
|
|
|
fn u256_to_prefix(digest: &[u8; 32]) -> PathBuf {
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-07 09:51:53 +00:00
|
|
|
pub fn create<P: Into<PathBuf>>(path: P) -> Result<Self, Error> {
|
|
|
|
|
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) {
|
|
|
|
bail!("unable to create chunk store {:?} - {}", base, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = std::fs::create_dir(&chunk_dir) {
|
|
|
|
bail!("unable to create chunk store subdir {:?} - {}", chunk_dir, err);
|
|
|
|
}
|
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-07 17:14:07 +00:00
|
|
|
let mut l1path = base.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) {
|
|
|
|
bail!("unable to create chunk subdir {:?} - {}", l1path, err);
|
|
|
|
}
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
Self::open(base)
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open<P: Into<PathBuf>>(path: P) -> Result<Self, Error> {
|
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
let base: PathBuf = path.into();
|
|
|
|
let chunk_dir = Self::chunk_dir(&base);
|
|
|
|
|
2018-12-08 07:38:42 +00:00
|
|
|
let metadata = match std::fs::metadata(&chunk_dir) {
|
|
|
|
Ok(data) => data,
|
|
|
|
Err(err) => bail!("unable to open chunk store {:?} - {}", chunk_dir, err),
|
|
|
|
};
|
2018-12-07 17:14:07 +00:00
|
|
|
|
|
|
|
let mut lockfile_path = base.clone();
|
|
|
|
lockfile_path.push(".lock");
|
|
|
|
|
2018-12-08 07:38:42 +00:00
|
|
|
let lockfile = match OpenOptions::new()
|
2018-12-07 17:14:07 +00:00
|
|
|
.create(true)
|
|
|
|
.append(true)
|
2018-12-08 07:38:42 +00:00
|
|
|
.open(&lockfile_path) {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(err) => bail!("unable to open chunk store lock file {:?} - {}",
|
|
|
|
lockfile_path, err),
|
|
|
|
};
|
2018-12-07 17:14:07 +00:00
|
|
|
|
|
|
|
let fd = lockfile.as_raw_fd();
|
|
|
|
|
|
|
|
// fixme: lock with timeout
|
|
|
|
flock(fd, FlockArg::LockExclusive)?;
|
|
|
|
|
|
|
|
println!("Got LOCK {:?}", fd);
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
//std::thread::sleep_ms(30000);
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-08 07:21:20 +00:00
|
|
|
Ok(ChunkStore {
|
|
|
|
base,
|
|
|
|
chunk_dir,
|
|
|
|
hasher: Sha512Trunc256::new(),
|
|
|
|
lockfile,
|
|
|
|
mutex: Mutex::new(false)
|
|
|
|
})
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 13:44:56 +00:00
|
|
|
pub fn insert_chunk(&mut self, chunk: &[u8]) -> Result<([u8; 32]), Error> {
|
|
|
|
|
|
|
|
self.hasher.reset();
|
|
|
|
self.hasher.input(chunk);
|
|
|
|
|
|
|
|
let mut digest = [0u8; 32];
|
|
|
|
self.hasher.result(&mut digest);
|
|
|
|
println!("DIGEST {}", u256_to_hex(&digest));
|
|
|
|
|
|
|
|
let mut chunk_path = self.base.clone();
|
|
|
|
let prefix = u256_to_prefix(&digest);
|
2018-12-07 15:12:45 +00:00
|
|
|
chunk_path.push(&prefix);
|
|
|
|
let digest_str = u256_to_hex(&digest);
|
|
|
|
chunk_path.push(&digest_str);
|
|
|
|
|
|
|
|
let lock = self.mutex.lock();
|
|
|
|
|
|
|
|
if let Ok(metadata) = std::fs::metadata(&chunk_path) {
|
|
|
|
if metadata.is_file() {
|
|
|
|
return Ok(digest);
|
|
|
|
} else {
|
|
|
|
bail!("Got unexpected file type for chunk {}", digest_str);
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-07 15:12:45 +00:00
|
|
|
let mut chunk_dir = self.base.clone();
|
|
|
|
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 */ }
|
|
|
|
bail!("Atomic rename failed for chunk {} - {}", digest_str, err);
|
|
|
|
}
|
|
|
|
|
2018-12-07 13:44:56 +00:00
|
|
|
println!("PATH {:?}", chunk_path);
|
|
|
|
|
2018-12-07 15:12:45 +00:00
|
|
|
drop(lock);
|
|
|
|
|
2018-12-07 13:44:56 +00:00
|
|
|
Ok(digest)
|
|
|
|
}
|
|
|
|
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunk_store1() {
|
|
|
|
|
|
|
|
if let Err(_e) = std::fs::remove_dir_all(".testdir") { /* ignore */ }
|
|
|
|
|
|
|
|
let chunk_store = ChunkStore::open(".testdir");
|
|
|
|
assert!(chunk_store.is_err());
|
|
|
|
|
2018-12-07 13:44:56 +00:00
|
|
|
let mut chunk_store = ChunkStore::create(".testdir").unwrap();
|
2018-12-07 15:12:45 +00:00
|
|
|
chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
|
|
|
|
chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-07 09:51:53 +00:00
|
|
|
|
|
|
|
let chunk_store = ChunkStore::create(".testdir");
|
|
|
|
assert!(chunk_store.is_err());
|
|
|
|
|
|
|
|
|
|
|
|
}
|