You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.1 KiB
47 lines
1.1 KiB
use std::error::Error;
|
|
use std::fmt;
|
|
use std::fs;
|
|
use std::io;
|
|
use std::ops::Deref;
|
|
use std::path::Path;
|
|
|
|
use symbols::Symbol;
|
|
use resources::{DirResource, Resource};
|
|
|
|
pub struct NotASymlink<D> where D: AsRef<str> + fmt::Display {
|
|
path: D
|
|
}
|
|
|
|
impl<D> NotASymlink<D> where D: AsRef<str> + fmt::Display {
|
|
pub fn new(path: D) -> Self {
|
|
NotASymlink {
|
|
path: path
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<D> Symbol for NotASymlink<D> where D: AsRef<str> + fmt::Display {
|
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|
let metadata = fs::symlink_metadata(self.path.as_ref());
|
|
// Check if file exists
|
|
if let Err(e) = metadata {
|
|
return if e.kind() == io::ErrorKind::NotFound {
|
|
Ok(true)
|
|
} else {
|
|
Err(Box::new(e))
|
|
};
|
|
}
|
|
Ok(!metadata.unwrap().file_type().is_symlink())
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<Error>> {
|
|
try!(fs::remove_file(self.path.as_ref()));
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<D> fmt::Display for NotASymlink<D> where D: AsRef<str> + fmt::Display {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
|
|
write!(f, "NotASymlink {}", self.path)
|
|
}
|
|
}
|