use std::path::PathBuf;
use std::fs::{self, File};
use std::io::{Result, Read, Write};
use std::fmt::Write as FmtWrite;
use rand::{thread_rng, Rng};
use backend::Backend;
use hash::ContentHasher;
fn pathbuf_from_hash<H: ContentHasher>(
root: &PathBuf,
hash: &H::Digest
) -> Result<PathBuf> {
let bytes: &[u8] = hash.as_ref();
let a = format!("{:02x}", bytes[0]);
let b = format!("{:02x}", bytes[1]);
let mut rest = String::new();
for i in 2..32 {
write!(&mut rest, "{:02x}", bytes[i])
.expect("in-memory write to succeed");
};
let mut pathbuf = root.clone();
pathbuf.push(a);
pathbuf.push(b);
try!(fs::create_dir_all(pathbuf.to_str().unwrap()));
pathbuf.push(rest);
Ok(pathbuf)
}
fn temporary_path(root: &PathBuf) -> PathBuf {
let mut rand = root.clone();
let s: String = thread_rng().gen_ascii_chars().take(10).collect();
rand.push(s);
rand
}
impl<H> Backend<H> for PathBuf where H: ContentHasher {
fn store(
&mut self,
source: &Fn(&mut Write, &mut Backend<H>) -> Result<H::Digest>
) -> Result<H::Digest> {
let tmp_path = temporary_path(self);
let mut file = try!(File::create(&tmp_path));
let hash = try!(source(&mut file, self));
let pathbuf = try!(pathbuf_from_hash::<H>(self, &hash));
try!(fs::rename(tmp_path, pathbuf));
Ok(hash)
}
fn request(
&self,
hash: &H::Digest,
read: &Fn(&mut Read) -> Result<()>,
) -> Result<()> {
let pathbuf = try!(pathbuf_from_hash::<H>(self, &hash));
let mut file = try!(File::open(pathbuf));
read(&mut file)
}
}
#[cfg(test)]
mod tests {
extern crate tempdir;
use store::Store;
use tempdir::TempDir;
use default::BlakeWrap;
#[test]
fn disk_write_get() {
let mut store: Store<_, BlakeWrap> = Store::new_with_backend(
Box::new(TempDir::new("content").unwrap())
);
let thing = Some(Box::new(8u8));
let hash = store.put(&thing).unwrap();
let retrieved = store.get(&hash).unwrap();
assert_eq!(thing, retrieved);
}
}