parity_path/
lib.rs

1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Path utilities
10use std::path::Path;
11use std::path::PathBuf;
12
13use home::home_dir;
14
15#[cfg(target_os = "macos")]
16/// Get the config path for application `name`.
17/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
18pub 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)]
26/// Get the config path for application `name`.
27/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
28pub 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)))]
37/// Get the config path for application `name`.
38/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
39pub 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
45/// Get the specific folder inside a config path.
46pub fn config_path_with(name: &str, then: &str) -> PathBuf {
47	let mut path = config_path(name);
48	path.push(then);
49	path
50}
51
52/// Default ethereum paths
53pub mod ethereum {
54	use std::path::PathBuf;
55
56	/// Default path for ethereum installation on Mac Os
57	pub fn default() -> PathBuf {
58		super::config_path("Ethereum")
59	}
60
61	/// Default path for ethereum installation (testnet)
62	pub fn test() -> PathBuf {
63		let mut path = default();
64		path.push("testnet");
65		path
66	}
67
68	/// Get the specific folder inside default ethereum installation
69	pub fn with_default(s: &str) -> PathBuf {
70		let mut path = default();
71		path.push(s);
72		path
73	}
74
75	/// Get the specific folder inside default ethereum installation configured for testnet
76	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/// Restricts the permissions of given path only to the owner.
85#[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/// Restricts the permissions of given path only to the owner.
93#[cfg(not(unix))]
94pub fn restrict_permissions_owner(_file_path: &Path, _write: bool, _executable: bool) -> Result<(), String> {
95	//TODO: implement me
96	Ok(())
97}