1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#![cfg(not(target_os = "android"))]
#[cfg(any(target_os = "linux", target_os = "macos"))]
use std::fs;
use std::{io, path::PathBuf};
#[cfg(windows)]
use crate::windows::create_dir_recursive;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to create directory {0}")]
CreateDirFailed(String, #[source] io::Error),
#[error("Failed to set directory permissions on {0}")]
SetDirPermissionFailed(String, #[source] io::Error),
#[cfg(any(windows, target_os = "macos"))]
#[error("Not able to find requested directory")]
FindDirError,
#[cfg(windows)]
#[error("Missing %ALLUSERSPROFILE% environment variable")]
NoProgramDataDir,
#[cfg(windows)]
#[error("Failed to create security attributes")]
GetSecurityAttributes(#[source] io::Error),
#[error("Device data directory has not been set")]
NoDataDir,
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
const PRODUCT_NAME: &str = "mullvad-vpn";
#[cfg(windows)]
pub const PRODUCT_NAME: &str = "Mullvad VPN";
#[cfg(windows)]
fn get_allusersprofile_dir() -> Result<PathBuf> {
match std::env::var_os("ALLUSERSPROFILE") {
Some(dir) => Ok(PathBuf::from(&dir)),
None => Err(Error::NoProgramDataDir),
}
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn create_and_return(
dir_fn: fn() -> Result<PathBuf>,
permissions: Option<fs::Permissions>,
) -> Result<PathBuf> {
let dir = dir_fn()?;
fs::create_dir_all(&dir).map_err(|e| Error::CreateDirFailed(dir.display().to_string(), e))?;
if let Some(permissions) = permissions {
fs::set_permissions(&dir, permissions)
.map_err(|e| Error::SetDirPermissionFailed(dir.display().to_string(), e))?;
}
Ok(dir)
}
#[cfg(windows)]
fn create_and_return(
dir_fn: fn() -> Result<PathBuf>,
set_security_permissions: bool,
) -> Result<PathBuf> {
let dir = dir_fn()?;
create_dir_recursive(&dir, set_security_permissions)?;
Ok(dir)
}
mod cache;
pub use crate::cache::{cache_dir, get_cache_dir, get_default_cache_dir};
mod logs;
pub use crate::logs::{get_default_log_dir, get_log_dir, log_dir};
pub mod resources;
pub use crate::resources::{get_default_resource_dir, get_resource_dir};
mod rpc_socket;
pub use crate::rpc_socket::{get_default_rpc_socket_path, get_rpc_socket_path};
mod settings;
pub use crate::settings::{get_default_settings_dir, settings_dir};
#[cfg(windows)]
pub mod windows;
|