snarkos_cli/
lib.rs

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