Add file abstraction function to check whether path exists

This commit is contained in:
Matthias Beyer 2017-09-17 15:43:57 +02:00
parent d97b77c30c
commit 29e1107d3e
5 changed files with 21 additions and 0 deletions

View file

@ -133,6 +133,10 @@ impl FileAbstraction for FSFileAbstraction {
create_dir_all(path).chain_err(|| SEK::DirNotCreated) create_dir_all(path).chain_err(|| SEK::DirNotCreated)
} }
fn exists(&self, path: &PathBuf) -> Result<bool, SE> {
Ok(path.exists())
}
fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance> { fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance> {
Box::new(FSFileAbstractionInstance::Absent(p)) Box::new(FSFileAbstractionInstance::Absent(p))
} }

View file

@ -152,6 +152,13 @@ impl FileAbstraction for InMemoryFileAbstraction {
Ok(()) Ok(())
} }
fn exists(&self, pb: &PathBuf) -> Result<bool, SE> {
let mut mtx = self.backend().lock().expect("Locking Mutex failed");
let backend = mtx.get_mut();
Ok(backend.contains_key(pb))
}
fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance> { fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance> {
Box::new(InMemoryFileAbstractionInstance::new(self.backend().clone(), p)) Box::new(InMemoryFileAbstractionInstance::new(self.backend().clone(), p))
} }

View file

@ -43,6 +43,8 @@ pub trait FileAbstraction : Debug {
fn rename(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE>; fn rename(&self, from: &PathBuf, to: &PathBuf) -> Result<(), SE>;
fn create_dir_all(&self, _: &PathBuf) -> Result<(), SE>; fn create_dir_all(&self, _: &PathBuf) -> Result<(), SE>;
fn exists(&self, &PathBuf) -> Result<bool, SE>;
fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance>; fn new_instance(&self, p: PathBuf) -> Box<FileAbstractionInstance>;
fn drain(&self) -> Result<Drain, SE>; fn drain(&self) -> Result<Drain, SE>;

View file

@ -115,6 +115,10 @@ impl<W: Write, M: Mapper> FileAbstraction for StdIoFileAbstraction<W, M> {
self.0.new_instance(p) self.0.new_instance(p)
} }
fn exists(&self, p: &PathBuf) -> Result<bool, SE> {
self.0.exists(p)
}
fn drain(&self) -> Result<Drain, SE> { fn drain(&self) -> Result<Drain, SE> {
self.0.drain() self.0.drain()
} }

View file

@ -136,6 +136,10 @@ impl<W: Write, M: Mapper> FileAbstraction for StdoutFileAbstraction<W, M> {
self.mem.new_instance(p) self.mem.new_instance(p)
} }
fn exists(&self, p: &PathBuf) -> Result<bool, SE> {
self.mem.exists(p)
}
fn drain(&self) -> Result<Drain, SE> { fn drain(&self) -> Result<Drain, SE> {
self.backend_cloned().map(Drain::new) self.backend_cloned().map(Drain::new)
} }