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.

57 lines
1.9 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
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 std::path::Path;
  7. use users::get_user_by_name;
  8. use command_runner::CommandRunner;
  9. use resources::Resource;
  10. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  11. pub struct Owner<'a, C: 'a + CommandRunner, D> where D: AsRef<str> + fmt::Display {
  12. path: D,
  13. user_name: Cow<'a, str>,
  14. command_runner: &'a C
  15. }
  16. impl<'a, C: CommandRunner, D> Owner<'a, C, D> where D: AsRef<str> + fmt::Display {
  17. pub fn new(path: D, user_name: Cow<'a, str>, command_runner: &'a C) -> Self {
  18. Owner { path: path, user_name: user_name, command_runner: command_runner }
  19. }
  20. }
  21. impl<'a, C: CommandRunner, D> Symbol for Owner<'a, C, D> where D: AsRef<str> + fmt::Display {
  22. fn target_reached(&self) -> Result<bool, Box<Error>> {
  23. if !Path::new(self.path.as_ref()).exists() {
  24. return Ok(false);
  25. }
  26. let actual_uid = fs::metadata(self.path.as_ref()).unwrap().uid();
  27. let target_uid = get_user_by_name(self.user_name.borrow()).unwrap().uid();
  28. Ok(actual_uid == target_uid)
  29. }
  30. fn execute(&self) -> Result<(), Box<Error>> {
  31. self.command_runner.run_successfully("chown", &[self.user_name.borrow(), self.path.as_ref()])
  32. }
  33. fn get_prerequisites(&self) -> Vec<Resource> {
  34. vec![ Resource::new("user", self.user_name.to_string()) ]
  35. }
  36. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  37. Box::new(SymbolAction::new(runner, self))
  38. }
  39. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  40. Box::new(OwnedSymbolAction::new(runner, *self))
  41. }
  42. }
  43. impl<'a, C: CommandRunner, D> fmt::Display for Owner<'a, C, D> where D: AsRef<str> + fmt::Display {
  44. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  45. write!(f, "Owner {} for {}", self.user_name, self.path)
  46. }
  47. }