1#![forbid(unsafe_code)]
17#![recursion_limit = "256"]
18
19#[macro_use]
20extern crate thiserror;
21
22pub mod commands;
23pub mod helpers;
24
25use anyhow::Result;
26use std::{
27 fs::{File, Permissions},
28 path::Path,
29};
30
31#[cfg(unix)]
32pub fn check_parent_permissions<T: AsRef<Path>>(path: T) -> Result<()> {
33 use anyhow::{bail, ensure};
34 use std::os::unix::fs::PermissionsExt;
35
36 if let Some(parent) = path.as_ref().parent() {
37 let permissions = parent.metadata()?.permissions().mode();
38 ensure!(permissions & 0o777 == 0o700, "The folder {:?} must be readable only by the owner (0700)", parent);
39 } else {
40 let path = path.as_ref();
41 bail!("Parent does not exist for path={}", path.display());
42 }
43
44 Ok(())
45}
46
47#[cfg(windows)]
48pub fn check_parent_permissions<T: AsRef<Path>>(_path: T) -> Result<()> {
49 Ok(())
50}
51
52#[cfg(unix)]
53fn set_user_read_only(file: &File) -> Result<()> {
54 use std::os::unix::fs::PermissionsExt;
55
56 let permissions = Permissions::from_mode(0o400);
57 file.set_permissions(permissions)?;
58 Ok(())
59}
60
61#[cfg(windows)]
62fn set_user_read_only(file: &File) -> Result<()> {
63 let mut permissions = file.metadata()?.permissions();
64 permissions.set_readonly(true);
65 file.set_permissions(permissions)?;
66 Ok(())
67}