summaryrefslogtreecommitdiffhomepage
path: root/mullvad-paths/src/settings.rs
blob: 8eff863677bf1d3348476a4fe162ac0896c7b000 (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
use crate::Result;
use std::{env, path::PathBuf};

/// Creates and returns the settings directory pointed to by `MULLVAD_SETTINGS_DIR`, or the default
/// one if that variable is unset.
pub fn settings_dir() -> Result<PathBuf> {
    crate::create_and_return(get_settings_dir, None)
}

fn get_settings_dir() -> Result<PathBuf> {
    match env::var_os("MULLVAD_SETTINGS_DIR") {
        Some(path) => Ok(PathBuf::from(path)),
        None => get_default_settings_dir(),
    }
}

pub fn get_default_settings_dir() -> Result<PathBuf> {
    #[cfg(not(target_os = "android"))]
    {
        let dir;
        #[cfg(unix)]
        {
            dir = Ok(PathBuf::from("/etc"));
        }
        #[cfg(windows)]
        {
            dir = dirs::data_local_dir().ok_or_else(|| crate::Error::FindDirError);
        }
        dir.map(|dir| dir.join(crate::PRODUCT_NAME))
    }
    #[cfg(target_os = "android")]
    {
        Ok(PathBuf::from(crate::APP_PATH))
    }
}