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#![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}