diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2018-05-16 17:07:32 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2018-05-18 15:28:07 +0200 |
| commit | 4f4755be007a461ba66b1b717a5db94c582dafb9 (patch) | |
| tree | bd4a024c61dcfd4e869d0e651c6b99c8461c8ad4 | |
| parent | 2c4e1d4b30216fd349930089df7d92a3d1268592 (diff) | |
| download | mullvadvpn-4f4755be007a461ba66b1b717a5db94c582dafb9.tar.xz mullvadvpn-4f4755be007a461ba66b1b717a5db94c582dafb9.zip | |
Move cache dir computation to own module
| -rw-r--r-- | mullvad-daemon/src/account_history.rs | 8 | ||||
| -rw-r--r-- | mullvad-daemon/src/cache.rs | 18 | ||||
| -rw-r--r-- | mullvad-daemon/src/main.rs | 9 | ||||
| -rw-r--r-- | mullvad-daemon/src/management_interface.rs | 2 | ||||
| -rw-r--r-- | mullvad-daemon/src/relays.rs | 4 | ||||
| -rw-r--r-- | mullvad-daemon/src/settings.rs | 3 |
6 files changed, 27 insertions, 17 deletions
diff --git a/mullvad-daemon/src/account_history.rs b/mullvad-daemon/src/account_history.rs index 201feba51b..b9ac4908e3 100644 --- a/mullvad-daemon/src/account_history.rs +++ b/mullvad-daemon/src/account_history.rs @@ -1,6 +1,5 @@ extern crate serde_json; -use app_dirs::{self, AppDataType}; use std::fs::File; use std::io; use std::path::PathBuf; @@ -55,8 +54,8 @@ impl AccountHistory { } } - pub fn get_accounts(&self) -> Vec<AccountToken> { - self.accounts.clone() + pub fn get_accounts(&self) -> &[AccountToken] { + &self.accounts } /// Add account token to the account history removing duplicate entries @@ -97,8 +96,7 @@ impl AccountHistory { } fn get_path() -> Result<PathBuf> { - let dir = app_dirs::app_root(AppDataType::UserCache, &::APP_INFO) - .chain_err(|| ErrorKind::DirectoryError)?; + let dir = ::cache::get_cache_dir().chain_err(|| ErrorKind::DirectoryError)?; Ok(dir.join(ACCOUNT_HISTORY_FILE)) } } diff --git a/mullvad-daemon/src/cache.rs b/mullvad-daemon/src/cache.rs new file mode 100644 index 0000000000..d58fd8f7c0 --- /dev/null +++ b/mullvad-daemon/src/cache.rs @@ -0,0 +1,18 @@ +use {ErrorKind, Result, ResultExt}; + +use std::fs; +use std::path::PathBuf; + +#[cfg(target_os = "linux")] +pub fn get_cache_dir() -> Result<PathBuf> { + let dir = PathBuf::from("/var/cache/mullvad-daemon"); + fs::create_dir_all(&dir).chain_err(|| ErrorKind::NoCacheDir)?; + Ok(dir) +} + +#[cfg(any(target_os = "macos", windows))] +pub fn get_cache_dir() -> Result<PathBuf> { + use mullvad_metadata::APP_INFO; + ::app_dirs::app_root(::app_dirs::AppDataType::UserCache, &APP_INFO) + .chain_err(|| ErrorKind::NoCacheDir) +} diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index e6150f1373..42e595b57a 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -48,6 +48,7 @@ extern crate talpid_types; extern crate windows_service; mod account_history; +mod cache; mod cli; mod geoip; mod logging; @@ -67,7 +68,6 @@ use jsonrpc_core::futures::sync::oneshot::Sender as OneshotSender; use management_interface::{BoxFuture, ManagementInterfaceServer, TunnelCommand}; use mullvad_rpc::{AccountsProxy, AppVersionProxy, HttpHandle}; -use mullvad_metadata::APP_INFO; use mullvad_types::account::{AccountData, AccountToken}; use mullvad_types::location::GeoIpLocation; use mullvad_types::relay_constraints::{RelaySettings, RelaySettingsUpdate}; @@ -223,7 +223,7 @@ impl Daemon { ErrorKind::DaemonIsAlreadyRunning ); - let cache_dir = get_cache_dir()?; + let cache_dir = cache::get_cache_dir()?; let mut rpc_manager = mullvad_rpc::MullvadRpcFactory::with_cache_dir(&cache_dir); let (rpc_handle, http_handle, tokio_remote) = @@ -923,11 +923,6 @@ fn get_resource_dir() -> PathBuf { } } -fn get_cache_dir() -> Result<PathBuf> { - app_dirs::app_root(app_dirs::AppDataType::UserCache, &APP_INFO) - .chain_err(|| ErrorKind::NoCacheDir) -} - #[cfg(unix)] fn running_as_admin() -> bool { let uid = unsafe { libc::getuid() }; diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index 53922f5838..df777d5ea8 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -557,7 +557,7 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for Managem try_future!(self.check_auth(&meta)); Box::new(future::result( AccountHistory::load() - .map(|account_history| account_history.get_accounts()) + .map(|account_history| account_history.get_accounts().to_vec()) .map_err(|error| { error!("Unable to get account history: {}", error.display_chain()); Error::internal_error() diff --git a/mullvad-daemon/src/relays.rs b/mullvad-daemon/src/relays.rs index 7a41316986..358fab2251 100644 --- a/mullvad-daemon/src/relays.rs +++ b/mullvad-daemon/src/relays.rs @@ -1,4 +1,3 @@ -use app_dirs; use chrono::{DateTime, Local}; use error_chain::ChainedError; use futures::Future; @@ -335,8 +334,7 @@ impl RelaySelector { } fn get_cache_path() -> Result<PathBuf> { - let dir = app_dirs::app_root(app_dirs::AppDataType::UserCache, &::APP_INFO) - .chain_err(|| ErrorKind::RelayCacheError)?; + let dir = ::cache::get_cache_dir().chain_err(|| ErrorKind::RelayCacheError)?; Ok(dir.join("relays.json")) } } diff --git a/mullvad-daemon/src/settings.rs b/mullvad-daemon/src/settings.rs index d0df5efb31..776606d1fc 100644 --- a/mullvad-daemon/src/settings.rs +++ b/mullvad-daemon/src/settings.rs @@ -95,7 +95,8 @@ impl Settings { #[cfg(windows)] fn get_settings_path() -> Result<PathBuf> { - let dir = ::app_dirs::app_root(::app_dirs::AppDataType::UserConfig, &::APP_INFO) + use mullvad_metadata::APP_INFO; + let dir = ::app_dirs::app_root(::app_dirs::AppDataType::UserConfig, &APP_INFO) .chain_err(|| ErrorKind::DirectoryError)?; Ok(dir.join(SETTINGS_FILE)) } |
