diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-20 14:13:48 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-22 13:46:50 +0000 |
| commit | d0fe44cd2ab2c3ffa9357f0a2e36c8b238addd08 (patch) | |
| tree | 73471991b9b963d90e7aebaa27f0d82abc60e26f | |
| parent | f324e8b7074f99fe974771ffbec95dd3217b7133 (diff) | |
| download | mullvadvpn-d0fe44cd2ab2c3ffa9357f0a2e36c8b238addd08.tar.xz mullvadvpn-d0fe44cd2ab2c3ffa9357f0a2e36c8b238addd08.zip | |
Load settings file in `SettingsPersister`
| -rw-r--r-- | mullvad-daemon/src/settings.rs | 45 | ||||
| -rw-r--r-- | mullvad-types/src/settings/mod.rs | 2 |
2 files changed, 38 insertions, 9 deletions
diff --git a/mullvad-daemon/src/settings.rs b/mullvad-daemon/src/settings.rs index f0c9a82404..530bf90205 100644 --- a/mullvad-daemon/src/settings.rs +++ b/mullvad-daemon/src/settings.rs @@ -1,19 +1,28 @@ use log::info; use mullvad_types::settings::Settings; use std::{ - io, + fs::File, + io::{self, BufReader, Read}, ops::{Deref, DerefMut}, }; +use talpid_types::ErrorExt; #[cfg(windows)] use { log::{error, warn}, - std::io::ErrorKind, talpid_core::logging::windows::log_sink, }; pub use mullvad_types::settings::Error; + +#[derive(Debug)] +enum LoadSettingsError { + FileNotFound, + Other, +} + + #[derive(Debug)] pub struct SettingsPersister { settings: Settings, @@ -30,7 +39,7 @@ impl SettingsPersister { settings } #[cfg(windows)] - Err(error) if error.kind() == ErrorKind::NotFound => { + Err(LoadSettingsError::FileNotFound) => { if Self::migrate_after_windows_update() { match Settings::load() { Ok(settings) => { @@ -82,11 +91,31 @@ impl SettingsPersister { } } - fn load_settings_from_file() -> Result<Settings, io::Error> { - Settings::load().map_err(|error| match error { - Error::ReadError(_, io_error) => io_error, - _ => io::Error::new(io::ErrorKind::Other, "Failed to load settings"), - }) + fn load_settings_from_file() -> Result<Settings, LoadSettingsError> { + let path = Settings::get_settings_path().unwrap(); + let file = File::open(&path).map_err(|error| { + if error.kind() == io::ErrorKind::NotFound { + LoadSettingsError::FileNotFound + } else { + LoadSettingsError::Other + } + })?; + + info!("Loading settings from {}", path.display()); + let mut settings_bytes = vec![]; + BufReader::new(file) + .read_to_end(&mut settings_bytes) + .map_err(|_| LoadSettingsError::Other)?; + + Settings::load_from_bytes(&settings_bytes) + .or_else(|error| { + log::error!( + "{}", + error.display_chain_with_msg("Failed to parse settings file") + ); + Settings::migrate_from_bytes(&settings_bytes) + }) + .map_err(|_| LoadSettingsError::Other) } pub fn to_settings(&self) -> Settings { diff --git a/mullvad-types/src/settings/mod.rs b/mullvad-types/src/settings/mod.rs index a991587fb6..ca9d4adfe7 100644 --- a/mullvad-types/src/settings/mod.rs +++ b/mullvad-types/src/settings/mod.rs @@ -165,7 +165,7 @@ impl Settings { }) } - fn get_settings_path() -> Result<PathBuf> { + pub fn get_settings_path() -> Result<PathBuf> { let dir = ::mullvad_paths::settings_dir().map_err(Error::DirectoryError)?; Ok(dir.join(SETTINGS_FILE)) } |
