diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-16 01:44:34 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-22 13:46:50 +0000 |
| commit | 3f221a9e644bc77fe64fa63e32c1c0d02e6397c8 (patch) | |
| tree | 519296e5783eabf0e1e33c7a99dac26b50c86663 | |
| parent | 5ce4927c57bf23ca2b37d792634c08bef1a52e53 (diff) | |
| download | mullvadvpn-3f221a9e644bc77fe64fa63e32c1c0d02e6397c8.tar.xz mullvadvpn-3f221a9e644bc77fe64fa63e32c1c0d02e6397c8.zip | |
Use resource dir to store settings file on Android
| -rw-r--r-- | mullvad-daemon/src/lib.rs | 3 | ||||
| -rw-r--r-- | mullvad-daemon/src/main.rs | 3 | ||||
| -rw-r--r-- | mullvad-daemon/src/settings.rs | 48 | ||||
| -rw-r--r-- | mullvad-jni/src/lib.rs | 1 |
4 files changed, 30 insertions, 25 deletions
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index c645eb1ed5..6dd6812dd2 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -461,6 +461,7 @@ where pub fn start( log_dir: Option<PathBuf>, resource_dir: PathBuf, + settings_dir: PathBuf, cache_dir: PathBuf, event_listener: L, command_channel: DaemonCommandChannel, @@ -507,7 +508,7 @@ where ); tokio_remote.spawn(|_| version_check_future); - let mut settings = SettingsPersister::load(); + let mut settings = SettingsPersister::load(&settings_dir); if version::is_beta_version() && settings.show_beta_releases.is_none() { let _ = settings.set_show_beta_releases(true); diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 8795861dbe..0b02dfbe5e 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -111,6 +111,8 @@ fn create_daemon( log_dir: Option<PathBuf>, ) -> Result<Daemon<ManagementInterfaceEventBroadcaster>, String> { let resource_dir = mullvad_paths::get_resource_dir(); + let settings_dir = mullvad_paths::settings_dir() + .map_err(|e| e.display_chain_with_msg("Unable to get settings dir"))?; let cache_dir = mullvad_paths::cache_dir() .map_err(|e| e.display_chain_with_msg("Unable to get cache dir"))?; @@ -120,6 +122,7 @@ fn create_daemon( Daemon::start( log_dir, resource_dir, + settings_dir, cache_dir, event_listener, command_channel, diff --git a/mullvad-daemon/src/settings.rs b/mullvad-daemon/src/settings.rs index ded46aea1d..618b40d2e9 100644 --- a/mullvad-daemon/src/settings.rs +++ b/mullvad-daemon/src/settings.rs @@ -7,6 +7,7 @@ use std::{ fs::File, io::{self, BufReader, Read}, ops::Deref, + path::{Path, PathBuf}, }; use talpid_types::ErrorExt; @@ -20,6 +21,9 @@ use { }; +static SETTINGS_FILE: &str = "settings.json"; + + #[derive(err_derive::Error, Debug)] pub enum Error { #[error(display = "Unable to remove settings file {}", _0)] @@ -31,9 +35,6 @@ pub enum Error { #[error(display = "Unable to write settings to {}", _0)] WriteError(String, #[error(source)] io::Error), - - #[error(display = "Settings operation failed")] - SettingsError(#[error(source)] mullvad_types::settings::Error), } #[derive(Debug)] @@ -46,13 +47,15 @@ enum LoadSettingsError { #[derive(Debug)] pub struct SettingsPersister { settings: Settings, + path: PathBuf, } impl SettingsPersister { /// Loads user settings from file. If no file is present it returns the defaults. - pub fn load() -> Self { - let settings = Self::load_settings(); - let mut persister = SettingsPersister { settings }; + pub fn load(settings_dir: &Path) -> Self { + let path = settings_dir.join(SETTINGS_FILE); + let settings = Self::load_settings(&path); + let mut persister = SettingsPersister { settings, path }; // Force IPv6 to be enabled on Android if cfg!(target_os = "android") { @@ -67,11 +70,13 @@ impl SettingsPersister { persister } - fn load_settings() -> Settings { - Self::load_settings_from_file() + fn load_settings(path: &Path) -> Settings { + Self::load_settings_from_file(path) .or_else(|error| match error { #[cfg(windows)] - LoadSettingsError::FileNotFound => Self::try_load_settings_after_windows_update(), + LoadSettingsError::FileNotFound => { + Self::try_load_settings_after_windows_update(path) + } _ => Err(error), }) .unwrap_or_else(|_| { @@ -80,9 +85,8 @@ impl SettingsPersister { }) } - fn load_settings_from_file() -> Result<Settings, LoadSettingsError> { - let path = Settings::get_settings_path().unwrap(); - let file = File::open(&path).map_err(|error| { + fn load_settings_from_file(path: &Path) -> Result<Settings, LoadSettingsError> { + let file = File::open(path).map_err(|error| { if error.kind() == io::ErrorKind::NotFound { LoadSettingsError::FileNotFound } else { @@ -108,11 +112,11 @@ impl SettingsPersister { } #[cfg(windows)] - fn try_load_settings_after_windows_update() -> Result<Settings, LoadSettingsError> { + fn try_load_settings_after_windows_update(path: &Path) -> Result<Settings, LoadSettingsError> { info!("No settings file found. Attempting migration from Windows Update backup location"); if Self::migrate_after_windows_update() { - let result = Self::load_settings_from_file(); + let result = Self::load_settings_from_file(path); match &result { Ok(_) => info!("Successfully loaded migrated settings"), @@ -151,15 +155,13 @@ impl SettingsPersister { /// Serializes the settings and saves them to the file it was loaded from. fn save(&mut self) -> Result<(), Error> { - let path = Settings::get_settings_path()?; - - debug!("Writing settings to {}", path.display()); - let mut file = - File::create(&path).map_err(|e| Error::WriteError(path.display().to_string(), e))?; + debug!("Writing settings to {}", self.path.display()); + let mut file = File::create(&self.path) + .map_err(|e| Error::WriteError(self.path.display().to_string(), e))?; serde_json::to_writer_pretty(&mut file, &self.settings).map_err(Error::SerializeError)?; file.sync_all() - .map_err(|e| Error::WriteError(path.display().to_string(), e)) + .map_err(|e| Error::WriteError(self.path.display().to_string(), e)) } /// Resets default settings @@ -172,10 +174,8 @@ impl SettingsPersister { e.display_chain_with_msg("Unable to save default settings") ); log::error!("Will attempt to remove settings file"); - Settings::get_settings_path().and_then(|path| { - fs::remove_file(&path) - .map_err(|e| Error::DeleteError(path.display().to_string(), e)) - }) + fs::remove_file(&self.path) + .map_err(|e| Error::DeleteError(self.path.display().to_string(), e)) }) } diff --git a/mullvad-jni/src/lib.rs b/mullvad-jni/src/lib.rs index 90fcc1a482..b3996ee507 100644 --- a/mullvad-jni/src/lib.rs +++ b/mullvad-jni/src/lib.rs @@ -179,6 +179,7 @@ fn spawn_daemon( let jvm = android_context.jvm.clone(); let daemon = Daemon::start( Some(resource_dir.clone()), + resource_dir.clone(), resource_dir, cache_dir, listener, |
