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.

42 lines
1.3 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
  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 symbols::Symbol;
  8. use command_runner::CommandRunner;
  9. pub struct Owner<'a, D> where D: AsRef<str> + fmt::Display {
  10. path: D,
  11. user_name: Cow<'a, str>,
  12. command_runner: &'a CommandRunner
  13. }
  14. impl<'a, D> Owner<'a, D> where D: AsRef<str> + fmt::Display {
  15. pub fn new(path: D, user_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> Self {
  16. Owner { path: path, user_name: user_name, command_runner: command_runner }
  17. }
  18. }
  19. impl<'a, D> Symbol for Owner<'a, D> where D: AsRef<str> + fmt::Display {
  20. fn target_reached(&self) -> Result<bool, Box<Error>> {
  21. let actual_uid = fs::metadata(self.path.as_ref()).unwrap().uid();
  22. let target_uid = get_user_by_name(self.user_name.borrow()).unwrap().uid();
  23. Ok(actual_uid == target_uid)
  24. }
  25. fn execute(&self) -> Result<(), Box<Error>> {
  26. try!(self.command_runner.run_with_args("chown", &[self.user_name.borrow(), self.path.as_ref()]));
  27. Ok(())
  28. }
  29. }
  30. impl<'a, D> fmt::Display for Owner<'a, D> where D: AsRef<str> + fmt::Display {
  31. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  32. write!(f, "Owner {} for {}", self.user_name, self.path)
  33. }
  34. }