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