snarkos_cli/
lib.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16// See https://github.com/ProvableHQ/snarkVM/issues/2775
17#![forbid(unsafe_code)]
18#![recursion_limit = "256"]
19
20#[macro_use]
21extern crate thiserror;
22
23pub mod commands;
24pub use commands::CLI;
25
26pub mod helpers;
27
28use anyhow::Result;
29use std::{
30    fs::{File, Permissions},
31    path::Path,
32};
33
34#[cfg(unix)]
35pub fn check_parent_permissions<T: AsRef<Path>>(path: T) -> Result<()> {
36    use anyhow::{bail, ensure};
37    use std::os::unix::fs::PermissionsExt;
38
39    if let Some(parent) = path.as_ref().parent() {
40        let permissions = parent.metadata()?.permissions().mode();
41        ensure!(permissions & 0o777 == 0o700, "The folder {:?} must be readable only by the owner (0700)", parent);
42    } else {
43        let path = path.as_ref();
44        bail!("Parent does not exist for path={}", path.display());
45    }
46
47    Ok(())
48}
49
50#[cfg(windows)]
51pub fn check_parent_permissions<T: AsRef<Path>>(_path: T) -> Result<()> {
52    Ok(())
53}
54
55#[cfg(unix)]
56fn set_user_read_only(file: &File) -> Result<()> {
57    use std::os::unix::fs::PermissionsExt;
58
59    let permissions = Permissions::from_mode(0o400);
60    file.set_permissions(permissions)?;
61    Ok(())
62}
63
64#[cfg(windows)]
65fn set_user_read_only(file: &File) -> Result<()> {
66    let mut permissions = file.metadata()?.permissions();
67    permissions.set_readonly(true);
68    file.set_permissions(permissions)?;
69    Ok(())
70}