From 68a4c08fe91493fc9dee789f7ccc9e2f55631754 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 15 Jan 2023 01:13:18 -0500 Subject: [PATCH 1/2] Moved `chmod` module to `mode`; Added some types to `mode` --- src/chmod/mod.rs | 1 - src/lib.rs | 2 +- src/mode/mod.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) delete mode 100644 src/chmod/mod.rs create mode 100644 src/mode/mod.rs diff --git a/src/chmod/mod.rs b/src/chmod/mod.rs deleted file mode 100644 index 1af9b14..0000000 --- a/src/chmod/mod.rs +++ /dev/null @@ -1 +0,0 @@ -//! Functions for parsing and managing permissions diff --git a/src/lib.rs b/src/lib.rs index 3acdb72..a21defb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,10 @@ #![warn(clippy::all, clippy::pedantic)] use std::{env, path::PathBuf, process, string::ToString}; -pub mod chmod; mod cmd; pub use cmd::Cmd; pub mod math; +pub mod mode; pub mod pw; /// Defines the location relative to the binary where a command will be installed diff --git a/src/mode/mod.rs b/src/mode/mod.rs new file mode 100644 index 0000000..0215d18 --- /dev/null +++ b/src/mode/mod.rs @@ -0,0 +1,38 @@ +//! Functions for parsing and managing permissions + +#[derive(Debug, PartialEq)] +pub enum Bits { + Suid = 0o4000, + Sgid = 0o2000, + Sticky = 0o1000, + URead = 0o400, + UWrite = 0o200, + UExec = 0o100, + GRead = 0o40, + GWrite = 0o20, + GExec = 0o10, + ORead = 0o4, + OWrite = 0o2, + OExec = 0o1, +} + +#[derive(Debug, PartialEq)] +pub enum Op { + Add, + Remove, + Equals, +} + +#[derive(Debug, PartialEq)] +pub enum Who { + User, + Group, + Other, + All, +} + +pub struct Parser { + mode: u32, + op: Op, + who: Who, +} From c727a1b6f85b43086e1c8fd21ed28dd3f8831ded Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 15 Jan 2023 01:28:57 -0500 Subject: [PATCH 2/2] Add mode::Parser and constructors --- src/mode/mod.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/mode/mod.rs b/src/mode/mod.rs index 0215d18..f681ecb 100644 --- a/src/mode/mod.rs +++ b/src/mode/mod.rs @@ -1,5 +1,11 @@ //! Functions for parsing and managing permissions +pub fn get_umask() -> u32 { + let mask = unsafe { libc::umask(0) }; + let umask = unsafe {libc::umask(mask) }; + umask +} + #[derive(Debug, PartialEq)] pub enum Bits { Suid = 0o4000, @@ -33,6 +39,29 @@ pub enum Who { pub struct Parser { mode: u32, - op: Op, - who: Who, + op: Option, + who: Option, +} + +impl Default for Parser { + fn default() -> Self { + let umask = get_umask(); + let mut mode = 0o0777; + mode &= umask; + Self { + mode, + op: None, + who: None, + } + } +} + +impl Parser { + fn new(mode: u32) -> Self { + Self { + mode, + op: None, + who: None, + } + } }