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)]
39pub fn check_parent_permissions<T: AsRef<Path>>(path: T) -> Result<()> {
40 use anyhow::{bail, ensure};
41 use std::os::unix::fs::PermissionsExt;
42
43 if let Some(parent) = path.as_ref().parent() {
44 let permissions = parent.metadata()?.permissions().mode();
45 ensure!(
46 permissions & 0o777 == 0o700,
47 "The folder {} must be readable and writeable only by the owner (0700)",
48 parent.display()
49 );
50 } else {
51 let path = path.as_ref();
52 bail!("Parent does not exist for path={}", path.display());
53 }
54
55 Ok(())
56}
57
58#[cfg(windows)]
59pub fn check_parent_permissions<T: AsRef<Path>>(_path: T) -> Result<()> {
60 Ok(())
61}
62
63#[cfg(unix)]
64fn set_user_read_only(file: &File) -> Result<()> {
65 use std::os::unix::fs::PermissionsExt;
66
67 let permissions = Permissions::from_mode(0o400);
68 file.set_permissions(permissions)?;
69 Ok(())
70}
71
72#[cfg(windows)]
73fn set_user_read_only(file: &File) -> Result<()> {
74 let mut permissions = file.metadata()?.permissions();
75 permissions.set_readonly(true);
76 file.set_permissions(permissions)?;
77 Ok(())
78}