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::{Hash32, NewHash, WriteThroughHasher};
fn pathbuf_from_hash(root: &PathBuf, hash: &Hash32) -> Result<PathBuf> {
let a = format!("{:02x}", hash.0[0]);
let b = format!("{:02x}", hash.0[1]);
let mut rest = String::new();
for i in 2..32 {
write!(&mut rest, "{:02x}", hash.0[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 Backend for PathBuf {
fn store(
&mut self,
source: &Fn(&mut Write, &mut Backend) -> Result<()>,
newhash: &NewHash,
) -> Result<Hash32> {
let tmp_path = temporary_path(self);
let mut file = try!(File::create(&tmp_path));
let hash;
{
let mut wth = WriteThroughHasher::new(&mut file, newhash);
try!(source(&mut wth, self));
hash = wth.finalize();
}
let pathbuf = try!(pathbuf_from_hash(self, &hash));
try!(fs::rename(tmp_path, pathbuf));
Ok(hash)
}
fn request(
&self,
hash: &Hash32,
read: &Fn(&mut Read) -> Result<()>,
) -> Result<()> {
let pathbuf = try!(pathbuf_from_hash(self, &hash));
let mut file = try!(File::open(pathbuf));
read(&mut file)
}
}
#[cfg(test)]
mod tests {
use test_common;
#[test]
fn disk_write_get() {
let mut store = test_common::diskstore();
let thing = Some(Box::new(8u8));
let hash = store.put(&thing).unwrap();
let retrieved = store.get(&hash).unwrap();
assert_eq!(thing, retrieved);
}
}