summaryrefslogtreecommitdiff
path: root/src/persist.rs
diff options
context:
space:
mode:
authortylermurphy534 <tylermurphy534@gmail.com>2022-11-11 01:25:10 -0500
committertylermurphy534 <tylermurphy534@gmail.com>2022-11-11 01:25:10 -0500
commit58208a126858d14e4d4bf4707e298919d234bc22 (patch)
treee3117bfb29af80a4065c1d5856fdd25ddf9b8177 /src/persist.rs
parentadd doc string for elevate_privlages (diff)
downloadcrab-58208a126858d14e4d4bf4707e298919d234bc22.tar.gz
crab-58208a126858d14e4d4bf4707e298919d234bc22.tar.bz2
crab-58208a126858d14e4d4bf4707e298919d234bc22.zip
update config format
Diffstat (limited to 'src/persist.rs')
-rw-r--r--src/persist.rs94
1 files changed, 47 insertions, 47 deletions
diff --git a/src/persist.rs b/src/persist.rs
index 1cddb7f..762d8b1 100644
--- a/src/persist.rs
+++ b/src/persist.rs
@@ -16,15 +16,15 @@ const PERSIST_PATH: &str = "/var/run/crab";
/// * `true` - If the user is persisted
/// * `false` - If the user is not persisted, or if the persist file is not trusted
pub fn get_persist(user: &str) -> bool {
- let json = match get_persist_config() {
- Some(data) => data,
- None => return false
- };
- let timestamp = match json[user].as_u64() {
- Some(data) => data,
- None => return false
- };
- return now() - timestamp < PERSIST_TIME && timestamp - 1 < now();
+ let json = match get_persist_config() {
+ Some(data) => data,
+ None => return false
+ };
+ let timestamp = match json[user].as_u64() {
+ Some(data) => data,
+ None => return false
+ };
+ return now() - timestamp < PERSIST_TIME && timestamp - 1 < now();
}
@@ -32,21 +32,21 @@ pub fn get_persist(user: &str) -> bool {
/// #### Arguments
/// * `user` - The user to set persisted
pub fn set_persist(user: &str) {
- let mut json = match get_persist_config() {
- Some(data) => data,
- None => return
- };
- json[user] = Value::from(now());
- let session = match get_current_session() {
- Some(data) => data,
- None => return
- };
- match secure::write_file(PERSIST_PATH, &format!("{}", session), &json.to_string()) {
- Ok(_) => {},
- Err(e) => {
- eprintln!("crab: An Internal Has Error: {}", e);
- }
- };
+ let mut json = match get_persist_config() {
+ Some(data) => data,
+ None => return
+ };
+ json[user] = Value::from(now());
+ let session = match get_current_session() {
+ Some(data) => data,
+ None => return
+ };
+ match secure::write_file(PERSIST_PATH, &format!("{}", session), &json.to_string()) {
+ Ok(_) => {},
+ Err(e) => {
+ eprintln!("crab: An Internal Has Error: {}", e);
+ }
+ };
}
@@ -55,15 +55,15 @@ pub fn set_persist(user: &str) {
/// * `None` - If crab failed to get the current session
/// * `Some(i32)` - If the session is retrieved, returns the i32/pid_t session id
fn get_current_session() -> Option<i32> {
- let pid: i32 = match std::process::id().try_into() {
- Ok(data) => data,
- Err(_) => return None
- };
- let pid_stat = match procinfo::pid::stat(pid) {
- Ok(data) => data,
- Err(_) => return None
- };
- Some(pid_stat.session)
+ let pid: i32 = match std::process::id().try_into() {
+ Ok(data) => data,
+ Err(_) => return None
+ };
+ let pid_stat = match procinfo::pid::stat(pid) {
+ Ok(data) => data,
+ Err(_) => return None
+ };
+ Some(pid_stat.session)
}
@@ -72,23 +72,23 @@ fn get_current_session() -> Option<i32> {
/// * `None` - If the persist file is untrusted or doesnt exist
/// * `Some(Value)` - If the persist file is retrieved, returns the serde_json Value of the file
fn get_persist_config() -> Option<Value> {
- let session = match get_current_session() {
- Some(data) => data,
- None => return None
- };
- let data = match secure::read_file(PERSIST_PATH, &format!("{}", session)) {
- Some(data) => data,
- None => "{}".to_string()
- };
- let json: Value = match serde_json::from_str(&data) {
- Ok(data) => data,
- Err(_) => return None
- };
- Some(json)
+ let session = match get_current_session() {
+ Some(data) => data,
+ None => return None
+ };
+ let data = match secure::read_file(PERSIST_PATH, &format!("{}", session)) {
+ Some(data) => data,
+ None => "{}".to_string()
+ };
+ let json: Value = match serde_json::from_str(&data) {
+ Ok(data) => data,
+ Err(_) => return None
+ };
+ Some(json)
}
// Gets the current time in seconds since the Unix Epoch
fn now() -> u64 {
- return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
+ return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
}