summaryrefslogtreecommitdiffhomepage
path: root/mullvad-daemon/src
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-04-20 14:13:48 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-04-22 13:46:50 +0000
commitd0fe44cd2ab2c3ffa9357f0a2e36c8b238addd08 (patch)
tree73471991b9b963d90e7aebaa27f0d82abc60e26f /mullvad-daemon/src
parentf324e8b7074f99fe974771ffbec95dd3217b7133 (diff)
downloadmullvadvpn-d0fe44cd2ab2c3ffa9357f0a2e36c8b238addd08.tar.xz
mullvadvpn-d0fe44cd2ab2c3ffa9357f0a2e36c8b238addd08.zip
Load settings file in `SettingsPersister`
Diffstat (limited to 'mullvad-daemon/src')
-rw-r--r--mullvad-daemon/src/settings.rs45
1 files changed, 37 insertions, 8 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 {