Skip to main content

urma_node/platform/
paths.rs

1//! Where the installed system service keeps its files, per operating system
2//! (URMA_NODE_INSTALL.md ยง2).
3use crate::config;
4use std::path::{Path, PathBuf};
5
6/// The installed service's files. `settings_dir` holds service.json and
7/// config.json; `state_dir` holds the token, the peer key and the data dir.
8#[derive(Clone, Debug, PartialEq, Eq)]
9pub struct Paths {
10    pub settings_dir: PathBuf,
11    pub state_dir: PathBuf,
12    pub settings: PathBuf,
13    pub config: PathBuf,
14    pub data: PathBuf,
15    pub token: PathBuf,
16    pub peer_key: PathBuf,
17}
18
19impl Paths {
20    /// The layout of the running operating system.
21    pub fn system() -> Self {
22        #[cfg(windows)]
23        {
24            Self::windows_under(&config::program_data())
25        }
26        #[cfg(not(windows))]
27        {
28            Self::linux_under(Path::new("/"))
29        }
30    }
31
32    /// The Linux layout below `root`: `/` on a real system, a temporary
33    /// directory in tests.
34    pub fn linux_under(root: &Path) -> Self {
35        let settings_dir = root.join("etc/urma-node");
36        let state_dir = root.join("var/lib/urma-node");
37        Self::from_dirs(settings_dir, state_dir)
38    }
39
40    /// The Windows layout below `program_data`: `%ProgramData%` on a real
41    /// system, a temporary directory in tests.
42    pub fn windows_under(program_data: &Path) -> Self {
43        let base = program_data.join(config::WINDOWS_PRODUCT_DIR);
44        Self::from_dirs(base.clone(), base)
45    }
46
47    fn from_dirs(settings_dir: PathBuf, state_dir: PathBuf) -> Self {
48        Self {
49            settings: settings_dir.join("service.json"),
50            config: settings_dir.join("config.json"),
51            data: state_dir.join("data"),
52            token: state_dir.join("service.token"),
53            peer_key: state_dir.join("peer.key"),
54            settings_dir,
55            state_dir,
56        }
57    }
58}