diff options
author | tylermurphy534 <tylermurphy534@gmail.com> | 2022-11-10 11:28:10 -0500 |
---|---|---|
committer | tylermurphy534 <tylermurphy534@gmail.com> | 2022-11-10 11:28:10 -0500 |
commit | f7a13253e72f1dde7389fb98c090b753fdfc42b9 (patch) | |
tree | b2433e907a78636363dc042be77aa8492c45f656 /src/secure.rs | |
parent | update aur depends (diff) | |
download | crab-f7a13253e72f1dde7389fb98c090b753fdfc42b9.tar.gz crab-f7a13253e72f1dde7389fb98c090b753fdfc42b9.tar.bz2 crab-f7a13253e72f1dde7389fb98c090b753fdfc42b9.zip |
slight refactor
Diffstat (limited to 'src/secure.rs')
-rw-r--r-- | src/secure.rs | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/src/secure.rs b/src/secure.rs index 1fc3a11..5fb0cc8 100644 --- a/src/secure.rs +++ b/src/secure.rs @@ -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; |