fixed #966 - don't try to change config permissions unless necessary

This commit is contained in:
Eugene 2024-03-23 22:03:18 +01:00
parent 32078670a8
commit 81cefebe96
No known key found for this signature in database
GPG key ID: 5896FCBBDD1CF4F4

View file

@ -1,10 +1,21 @@
use std::os::unix::prelude::PermissionsExt; use std::os::unix::prelude::PermissionsExt;
use std::path::Path; use std::path::Path;
fn maybe_apply_permissions<P: AsRef<Path>>(
path: P,
permissions: std::fs::Permissions,
) -> std::io::Result<()> {
let current = std::fs::metadata(&path)?.permissions();
if current != permissions {
std::fs::set_permissions(path, permissions)?;
}
Ok(())
}
pub fn secure_directory<P: AsRef<Path>>(path: P) -> std::io::Result<()> { pub fn secure_directory<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
std::fs::set_permissions(path.as_ref(), std::fs::Permissions::from_mode(0o700)) maybe_apply_permissions(path.as_ref(), std::fs::Permissions::from_mode(0o700))
} }
pub fn secure_file<P: AsRef<Path>>(path: P) -> std::io::Result<()> { pub fn secure_file<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
std::fs::set_permissions(path.as_ref(), std::fs::Permissions::from_mode(0o600)) maybe_apply_permissions(path.as_ref(), std::fs::Permissions::from_mode(0o600))
} }