melib: hash path instead of file contents in maildir backend

embed
Manos Pitsidianakis 2018-09-22 16:54:10 +03:00
parent 1321c056a6
commit afe34d5916
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 25 additions and 20 deletions

View File

@ -97,12 +97,17 @@ macro_rules! get_path_hash {
} }
fn get_file_hash(file: &Path) -> EnvelopeHash { fn get_file_hash(file: &Path) -> EnvelopeHash {
/*
let mut buf = Vec::with_capacity(2048); let mut buf = Vec::with_capacity(2048);
let mut f = fs::File::open(&file).unwrap_or_else(|_| panic!("Can't open {}", file.display())); let mut f = fs::File::open(&file).unwrap_or_else(|_| panic!("Can't open {}", file.display()));
f.read_to_end(&mut buf) f.read_to_end(&mut buf)
.unwrap_or_else(|_| panic!("Can't read {}", file.display())); .unwrap_or_else(|_| panic!("Can't read {}", file.display()));
let mut hasher = FnvHasher::default(); let mut hasher = FnvHasher::default();
hasher.write(&buf); hasher.write(&buf);
hasher.finish()
*/
let mut hasher = FnvHasher::default();
file.hash(&mut hasher);
hasher.finish() hasher.finish()
} }
@ -143,7 +148,7 @@ impl MailBackend for MaildirType {
p.push("new"); p.push("new");
watcher.watch(&p, RecursiveMode::NonRecursive).unwrap(); watcher.watch(&p, RecursiveMode::NonRecursive).unwrap();
} }
let map = self.hash_index.clone(); let hash_index = self.hash_index.clone();
thread::Builder::new() thread::Builder::new()
.name("folder watch".to_string()) .name("folder watch".to_string())
.spawn(move || { .spawn(move || {
@ -174,7 +179,7 @@ impl MailBackend for MaildirType {
.unwrap() .unwrap()
.to_path_buf(); .to_path_buf();
if let Some(env) = add_path_to_index( if let Some(env) = add_path_to_index(
&map, &hash_index,
pathbuf.as_path(), pathbuf.as_path(),
&cache_dir, &cache_dir,
file_name, file_name,
@ -197,9 +202,9 @@ impl MailBackend for MaildirType {
.to_path_buf(); .to_path_buf();
/* Linear search in hash_index to find old hash */ /* Linear search in hash_index to find old hash */
let old_hash: EnvelopeHash = { let old_hash: EnvelopeHash = {
let mut map_lock = map.lock().unwrap(); let mut index_lock = hash_index.lock().unwrap();
if let Some((k, v)) = if let Some((k, v)) =
map_lock.iter_mut().find(|(_, v)| v.1 == pathbuf) index_lock.iter_mut().find(|(_, v)| v.1 == pathbuf)
{ {
v.1 = pathbuf.clone(); v.1 = pathbuf.clone();
*k *k
@ -207,7 +212,7 @@ impl MailBackend for MaildirType {
/* Did we just miss a Create event? In any case, create /* Did we just miss a Create event? In any case, create
* envelope. */ * envelope. */
if let Some(env) = add_path_to_index( if let Some(env) = add_path_to_index(
&map, &hash_index,
pathbuf.as_path(), pathbuf.as_path(),
&cache_dir, &cache_dir,
file_name, file_name,
@ -221,11 +226,11 @@ impl MailBackend for MaildirType {
} }
}; };
let new_hash: EnvelopeHash = get_file_hash(pathbuf.as_path()); let new_hash: EnvelopeHash = get_file_hash(pathbuf.as_path());
let mut map_lock = map.lock().unwrap(); let mut index_lock = hash_index.lock().unwrap();
if map_lock.get_mut(&new_hash).is_none() { if index_lock.get_mut(&new_hash).is_none() {
let op = Box::new(MaildirOp::new(new_hash, map.clone())); let op = Box::new(MaildirOp::new(new_hash, hash_index.clone()));
if let Some(env) = Envelope::from_token(op, new_hash) { if let Some(env) = Envelope::from_token(op, new_hash) {
map_lock.insert(new_hash, (0, pathbuf.clone())); index_lock.insert(new_hash, (0, pathbuf.clone()));
/* Send Write notice */ /* Send Write notice */
@ -239,9 +244,9 @@ impl MailBackend for MaildirType {
/* Remove */ /* Remove */
DebouncedEvent::NoticeRemove(mut pathbuf) DebouncedEvent::NoticeRemove(mut pathbuf)
| DebouncedEvent::Remove(mut pathbuf) => { | DebouncedEvent::Remove(mut pathbuf) => {
let map = map.lock().unwrap(); let index_lock = hash_index.lock().unwrap();
let hash: EnvelopeHash = if let Some((k, _)) = let hash: EnvelopeHash = if let Some((k, _)) =
map.iter().find(|(_, v)| v.1 == pathbuf) index_lock.iter().find(|(_, v)| v.1 == pathbuf)
{ {
*k *k
} else { } else {
@ -254,10 +259,10 @@ impl MailBackend for MaildirType {
}); });
} }
/* Envelope hasn't changed, so handle this here */ /* Envelope hasn't changed, so handle this here */
DebouncedEvent::Rename(_, mut dest) => { DebouncedEvent::Rename(src, mut dest) => {
let new_hash: EnvelopeHash = get_file_hash(dest.as_path()); let old_hash: EnvelopeHash = get_file_hash(src.as_path());
let mut map = map.lock().unwrap(); let mut index_lock = hash_index.lock().unwrap();
if let Some(v) = map.get_mut(&new_hash) { if let Some(v) = index_lock.get_mut(&old_hash) {
v.1 = dest; v.1 = dest;
} else { } else {
/* Maybe a re-read should be triggered here just to be safe. */ /* Maybe a re-read should be triggered here just to be safe. */
@ -512,7 +517,7 @@ impl MaildirType {
} }
fn add_path_to_index( fn add_path_to_index(
map: &HashIndex, hash_index: &HashIndex,
path: &Path, path: &Path,
cache_dir: &xdg::BaseDirectories, cache_dir: &xdg::BaseDirectories,
file_name: PathBuf, file_name: PathBuf,
@ -520,13 +525,13 @@ fn add_path_to_index(
let env: Envelope; let env: Envelope;
let hash = get_file_hash(path); let hash = get_file_hash(path);
{ {
let mut map = map.lock().unwrap(); let mut index_lock = hash_index.lock().unwrap();
if (*map).contains_key(&hash) { if (*index_lock).contains_key(&hash) {
return None; return None;
} }
(*map).insert(hash, (0, path.to_path_buf())); (*index_lock).insert(hash, (0, path.to_path_buf()));
} }
let op = Box::new(MaildirOp::new(hash, map.clone())); let op = Box::new(MaildirOp::new(hash, hash_index.clone()));
if let Some(e) = Envelope::from_token(op, hash) { if let Some(e) = Envelope::from_token(op, hash) {
if let Ok(cached) = cache_dir.place_cache_file(file_name) { if let Ok(cached) = cache_dir.place_cache_file(file_name) {
/* place result in cache directory */ /* place result in cache directory */