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.

46 lines
1.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. use std::borrow::{ Borrow, Cow };
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::fs;
  5. use std::os::unix::fs::MetadataExt;
  6. use users::get_user_by_name;
  7. use command_runner::CommandRunner;
  8. use resources::Resource;
  9. use symbols::Symbol;
  10. pub struct Owner<'a, D> where D: AsRef<str> + fmt::Display {
  11. path: D,
  12. user_name: Cow<'a, str>,
  13. command_runner: &'a CommandRunner
  14. }
  15. impl<'a, D> Owner<'a, D> where D: AsRef<str> + fmt::Display {
  16. pub fn new(path: D, user_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> Self {
  17. Owner { path: path, user_name: user_name, command_runner: command_runner }
  18. }
  19. }
  20. impl<'a, D> Symbol for Owner<'a, D> where D: AsRef<str> + fmt::Display {
  21. fn target_reached(&self) -> Result<bool, Box<Error>> {
  22. let actual_uid = fs::metadata(self.path.as_ref()).unwrap().uid();
  23. let target_uid = get_user_by_name(self.user_name.borrow()).unwrap().uid();
  24. Ok(actual_uid == target_uid)
  25. }
  26. fn execute(&self) -> Result<(), Box<Error>> {
  27. try!(self.command_runner.run_with_args("chown", &[self.user_name.borrow(), self.path.as_ref()]));
  28. Ok(())
  29. }
  30. fn get_prerequisites(&self) -> Vec<Resource> {
  31. vec![ Resource::new("user", self.user_name.to_string()) ]
  32. }
  33. }
  34. impl<'a, D> fmt::Display for Owner<'a, D> where D: AsRef<str> + fmt::Display {
  35. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  36. write!(f, "Owner {} for {}", self.user_name, self.path)
  37. }
  38. }