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.

54 lines
1.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::symbols::Symbol;
  3. use async_trait::async_trait;
  4. use std::borrow::Borrow;
  5. use std::error::Error;
  6. use std::fs;
  7. use std::marker::PhantomData;
  8. use std::os::unix::fs::MetadataExt;
  9. use std::path::Path;
  10. use users::get_user_by_name;
  11. #[derive(Debug)]
  12. pub struct Owner<_C, C, P, U> {
  13. path: P,
  14. user_name: U,
  15. command_runner: C,
  16. phantom: PhantomData<_C>,
  17. }
  18. impl<_C, C, P, U> Owner<_C, C, P, U> {
  19. pub fn new(path: P, user_name: U, command_runner: C) -> Self {
  20. Self {
  21. path,
  22. user_name,
  23. command_runner,
  24. phantom: PhantomData::default(),
  25. }
  26. }
  27. }
  28. #[async_trait(?Send)]
  29. impl<_C: CommandRunner, C: Borrow<_C>, P: AsRef<Path>, U: AsRef<str>> Symbol
  30. for Owner<_C, C, P, U>
  31. {
  32. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  33. if !self.path.as_ref().exists() {
  34. return Ok(false);
  35. }
  36. let actual_uid = fs::metadata(self.path.as_ref()).unwrap().uid();
  37. let target_uid = get_user_by_name(self.user_name.as_ref()).unwrap().uid();
  38. Ok(actual_uid == target_uid)
  39. }
  40. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  41. self
  42. .command_runner
  43. .borrow()
  44. .run_successfully(
  45. "chown",
  46. args!["-R", self.user_name.as_ref(), self.path.as_ref()],
  47. )
  48. .await
  49. }
  50. }