A library for writing host-specific, single-binary configuration management and deployment tools
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

7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::fs;
  4. use std::io;
  5. use std::ops::Deref;
  6. use std::path::Path;
  7. use symbols::Symbol;
  8. use resources::{DirResource, Resource};
  9. pub struct NotASymlink<D> where D: AsRef<str> + fmt::Display {
  10. path: D
  11. }
  12. impl<D> NotASymlink<D> where D: AsRef<str> + fmt::Display {
  13. pub fn new(path: D) -> Self {
  14. NotASymlink {
  15. path: path
  16. }
  17. }
  18. }
  19. impl<D> Symbol for NotASymlink<D> where D: AsRef<str> + fmt::Display {
  20. fn target_reached(&self) -> Result<bool, Box<Error>> {
  21. let metadata = fs::symlink_metadata(self.path.as_ref());
  22. // Check if file exists
  23. if let Err(e) = metadata {
  24. return if e.kind() == io::ErrorKind::NotFound {
  25. Ok(true)
  26. } else {
  27. Err(Box::new(e))
  28. };
  29. }
  30. Ok(!metadata.unwrap().file_type().is_symlink())
  31. }
  32. fn execute(&self) -> Result<(), Box<Error>> {
  33. try!(fs::remove_file(self.path.as_ref()));
  34. Ok(())
  35. }
  36. }
  37. impl<D> fmt::Display for NotASymlink<D> where D: AsRef<str> + fmt::Display {
  38. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  39. write!(f, "NotASymlink {}", self.path)
  40. }
  41. }