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