slight refactor
This commit is contained in:
parent
3c1fd3acfe
commit
f7a13253e7
4 changed files with 34 additions and 40 deletions
10
src/help.rs
10
src/help.rs
|
@ -1,10 +0,0 @@
|
|||
pub fn help() {
|
||||
let help =
|
||||
"Usage:
|
||||
crab [-d] command [args]
|
||||
Options:
|
||||
-v --version Get the current version of the package
|
||||
-h --help Generates the crab help message
|
||||
-d If your user is set to persist, dont save persistance";
|
||||
println!("{}", help);
|
||||
}
|
16
src/main.rs
16
src/main.rs
|
@ -14,7 +14,6 @@ const ERROR_RUN_ROOT: u8 = 6;
|
|||
|
||||
mod persist;
|
||||
mod flags;
|
||||
mod help;
|
||||
mod secure;
|
||||
|
||||
fn main() -> ExitCode {
|
||||
|
@ -22,7 +21,7 @@ fn main() -> ExitCode {
|
|||
let flags = match flags::parse(&args[1..]) {
|
||||
Some(data) => data,
|
||||
None => {
|
||||
help::help();
|
||||
help();
|
||||
return ExitCode::from(ERROR_ARGS);
|
||||
}
|
||||
};
|
||||
|
@ -31,7 +30,7 @@ fn main() -> ExitCode {
|
|||
return ExitCode::SUCCESS;
|
||||
}
|
||||
if flags.help {
|
||||
help::help();
|
||||
help();
|
||||
return ExitCode::SUCCESS;
|
||||
}
|
||||
if args.len() - flags.arg_count < 2 {
|
||||
|
@ -74,6 +73,17 @@ fn main() -> ExitCode {
|
|||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
fn help() {
|
||||
let help =
|
||||
"Usage:
|
||||
crab [-d] command [args]
|
||||
Options:
|
||||
-v --version Get the current version of the package
|
||||
-h --help Generates the crab help message
|
||||
-d If your user is set to persist, dont save persistance";
|
||||
println!("{}", help);
|
||||
}
|
||||
|
||||
struct Config {
|
||||
users: Vec<(String, bool)>
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ pub fn get_persist(user: &str) -> bool {
|
|||
pub fn set_persist(user: &str) {
|
||||
let mut json = match get_terminal_config() {
|
||||
Some(data) => data,
|
||||
None => return
|
||||
None => serde_json::from_str("{}").unwrap()
|
||||
};
|
||||
json[user] = Value::from(now());
|
||||
let id = match get_terminal_process() {
|
||||
|
@ -30,22 +30,22 @@ pub fn set_persist(user: &str) {
|
|||
};
|
||||
match secure::write_file(PERSIST_PATH, &format!("{}", id), &json.to_string()) {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
eprintln!("Internal Error: {}", e)
|
||||
Err(_) => {
|
||||
eprintln!("crab: An Internal Has Error")
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn get_terminal_process() -> Option<i32> {
|
||||
let id: i32 = match std::process::id().try_into() {
|
||||
let pid: i32 = match std::process::id().try_into() {
|
||||
Ok(data) => data,
|
||||
Err(_) => return None
|
||||
};
|
||||
let stat = match procinfo::pid::stat(id) {
|
||||
let pid_stat = match procinfo::pid::stat(pid) {
|
||||
Ok(data) => data,
|
||||
Err(_) => return None
|
||||
};
|
||||
Some(stat.session)
|
||||
Some(pid_stat.session)
|
||||
}
|
||||
|
||||
fn get_terminal_config() -> Option<Value> {
|
||||
|
|
|
@ -1,46 +1,40 @@
|
|||
use std::{os::{unix::prelude::PermissionsExt, linux::fs::MetadataExt}, fs, io::ErrorKind};
|
||||
use std::{os::{unix::prelude::PermissionsExt, linux::fs::MetadataExt}, fs, io};
|
||||
use nix::unistd;
|
||||
|
||||
pub fn write_file(dir: &str, file: &str, data: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
make_file_root(dir)?;
|
||||
pub fn write_file(dir: &str, file: &str, data: &str) -> Result<(), io::Error> {
|
||||
fs::create_dir_all(dir)?;
|
||||
set_file_permissions(0, 0, 0o100600, dir)?;
|
||||
let path = path(dir, file);
|
||||
std::fs::write(&path, "")?;
|
||||
make_file_root(&path)?;
|
||||
std::fs::write(&path, data)?;
|
||||
fs::write(&path, "")?;
|
||||
set_file_permissions(0, 0, 0o100600, &path)?;
|
||||
fs::write(&path, data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read_file(dir: &str, file: &str) -> Option<String> {
|
||||
let path = path(dir,file);
|
||||
if !is_file_root(&path) {
|
||||
if !check_file_permissions(0, 0, 0o100600, &path) {
|
||||
return None;
|
||||
}
|
||||
match std::fs::read_to_string(&path) {
|
||||
match fs::read_to_string(&path) {
|
||||
Ok(data) => return Some(data),
|
||||
Err(_) => return None
|
||||
};
|
||||
}
|
||||
|
||||
fn make_file_root(path: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
unistd::chown(std::path::Path::new(path), Some(unistd::Uid::from(0)), Some(unistd::Gid::from(0)))?;
|
||||
let metadata = std::fs::metadata(path)?;
|
||||
fn set_file_permissions(uid: u32, gid: u32, mode: u32, path: &str) -> Result<(), io::Error> {
|
||||
unistd::chown(std::path::Path::new(path), Some(unistd::Uid::from(uid)), Some(unistd::Gid::from(gid)))?;
|
||||
let metadata = fs::metadata(path)?;
|
||||
let mut perms = metadata.permissions();
|
||||
perms.set_mode(0o100600);
|
||||
perms.set_mode(mode);
|
||||
fs::set_permissions(path, perms)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_file_root(path: &str) -> bool {
|
||||
return check_file_permissions(0, 0, 0o100600, path);
|
||||
}
|
||||
|
||||
fn check_file_permissions(uid: u32, gid: u32, mode: u32, path: &str) -> bool {
|
||||
let metadata = match std::fs::metadata(path) {
|
||||
let metadata = match fs::metadata(path) {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
return e.kind() == ErrorKind::NotFound
|
||||
}
|
||||
Err(_) => return false
|
||||
};
|
||||
let perms = metadata.permissions();
|
||||
return perms.mode() == mode && metadata.st_uid() == uid && metadata.st_gid() == gid;
|
||||
|
|
Loading…
Reference in a new issue