Merge pull request #369 from matthiasbeyer/libimagstorestdhook/verify-link

Libimagstorestdhook/verify link
This commit is contained in:
Matthias Beyer 2016-04-20 17:33:25 +02:00
commit 227bdc69ab
3 changed files with 84 additions and 0 deletions

View File

@ -11,3 +11,9 @@ fs2 = "0.2.3"
[dependencies.libimagstore]
path = "../libimagstore"
[dependencies.libimagentrylink]
path = "../libimagentrylink"
[dependencies.libimagutil]
path = "../libimagutil"

View File

@ -19,7 +19,10 @@ extern crate toml;
extern crate fs2;
extern crate libimagstore;
extern crate libimagentrylink;
extern crate libimagutil;
pub mod debug;
pub mod flock;
pub mod linkverify;

View File

@ -0,0 +1,75 @@
use std::path::PathBuf;
use toml::Value;
use libimagstore::hook::Hook;
use libimagstore::hook::accessor::HookDataAccessor as HDA;
use libimagstore::hook::accessor::HookDataAccessorProvider;
use libimagstore::hook::accessor::NonMutableHookDataAccessor;
use libimagstore::hook::result::HookResult;
use libimagstore::store::FileLockEntry;
use libimagentrylink::internal::InternalLinker;
use libimagutil::trace::trace_error;
#[derive(Debug, Clone)]
pub struct LinkedEntriesExistHook {
store_location: PathBuf,
}
impl LinkedEntriesExistHook {
pub fn new(store_location: PathBuf) -> LinkedEntriesExistHook {
LinkedEntriesExistHook {
store_location: store_location,
}
}
}
impl Hook for LinkedEntriesExistHook {
fn name(&self) -> &'static str {
"stdhook_linked_entries_exist"
}
fn set_config(&mut self, _: &Value) {
() // We are not configurable here.
}
}
impl HookDataAccessorProvider for LinkedEntriesExistHook {
fn accessor(&self) -> HDA {
HDA::NonMutableAccess(self)
}
}
impl NonMutableHookDataAccessor for LinkedEntriesExistHook {
fn access(&self, fle: &FileLockEntry) -> HookResult<()> {
debug!("[LINKVERIFY HOOK] {:?}", fle.get_location());
let _ = fle.get_internal_links()
.map(|links| {
for link in links {
let mut path = self.store_location.clone();
path.push(link);
if !path.exists() {
warn!("File link does not exist: {:?} -> {:?}", fle.get_location(), path);
} else {
if !path.is_file() {
warn!("File link is not a file: {:?} -> {:?}", fle.get_location(), path);
}
}
}
})
.map_err(|e| {
warn!("Couldn't execute Link-Verify hook");
trace_error(&e);
});
Ok(())
}
}