summaryrefslogtreecommitdiffhomepage
path: root/mullvad-paths/src/lib.rs
blob: cdbad6bc7b48f2fa99a7cf3f2bcca4da8dbe2d78 (plain)
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
91
92
93
94
#![deny(rust_2018_idioms)]

#[cfg(not(target_os = "windows"))]
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(err_derive::Error, Debug)]
pub enum Error {
    #[error(display = "Failed to create directory {}", _0)]
    CreateDirFailed(String, #[error(source)] io::Error),

    #[error(display = "Failed to set directory permissions on {}", _0)]
    SetDirPermissionFailed(String, #[error(source)] io::Error),

    #[cfg(any(windows, target_os = "macos"))]
    #[error(display = "Not able to find requested directory")]
    FindDirError,

    #[cfg(windows)]
    #[error(display = "Missing %ALLUSERSPROFILE% environment variable")]
    NoProgramDataDir,

    #[cfg(windows)]
    #[error(display = "Failed to create security attributes")]
    GetSecurityAttributes(#[error(source)] io::Error),

    #[cfg(all(windows, feature = "deduce-system-service"))]
    #[error(display = "Failed to deduce system service directory")]
    FailedToFindSystemServiceDir(#[error(source)] io::Error),
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
const PRODUCT_NAME: &str = "mullvad-vpn";

#[cfg(windows)]
pub const PRODUCT_NAME: &str = "Mullvad VPN";

#[cfg(target_os = "android")]
const APP_PATH: &str = "/data/data/net.mullvad.mullvadvpn";

#[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(not(target_os = "windows"))]
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;