1use std::path::Path;
11use std::path::PathBuf;
12
13use home::home_dir;
14
15#[cfg(target_os = "macos")]
16pub fn config_path(name: &str) -> PathBuf {
19 let mut home = home_dir().expect("Failed to get home dir");
20 home.push("Library");
21 home.push(name);
22 home
23}
24
25#[cfg(windows)]
26pub fn config_path(name: &str) -> PathBuf {
29 let mut home = home_dir().expect("Failed to get home dir");
30 home.push("AppData");
31 home.push("Roaming");
32 home.push(name);
33 home
34}
35
36#[cfg(not(any(target_os = "macos", windows)))]
37pub fn config_path(name: &str) -> PathBuf {
40 let mut home = home_dir().expect("Failed to get home dir");
41 home.push(format!(".{}", name.to_lowercase()));
42 home
43}
44
45pub fn config_path_with(name: &str, then: &str) -> PathBuf {
47 let mut path = config_path(name);
48 path.push(then);
49 path
50}
51
52pub mod ethereum {
54 use std::path::PathBuf;
55
56 pub fn default() -> PathBuf {
58 super::config_path("Ethereum")
59 }
60
61 pub fn test() -> PathBuf {
63 let mut path = default();
64 path.push("testnet");
65 path
66 }
67
68 pub fn with_default(s: &str) -> PathBuf {
70 let mut path = default();
71 path.push(s);
72 path
73 }
74
75 pub fn with_testnet(s: &str) -> PathBuf {
77 let mut path = default();
78 path.push("testnet");
79 path.push(s);
80 path
81 }
82}
83
84#[cfg(unix)]
86pub fn restrict_permissions_owner(file_path: &Path, write: bool, executable: bool) -> Result<(), String> {
87 let perms =
88 ::std::os::unix::fs::PermissionsExt::from_mode(0o400 + write as u32 * 0o200 + executable as u32 * 0o100);
89 ::std::fs::set_permissions(file_path, perms).map_err(|e| format!("{:?}", e))
90}
91
92#[cfg(not(unix))]
94pub fn restrict_permissions_owner(_file_path: &Path, _write: bool, _executable: bool) -> Result<(), String> {
95 Ok(())
97}