summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2022-02-18 09:45:34 +0100
committerDavid Lönnhager <david.l@mullvad.net>2022-02-18 09:45:34 +0100
commitb23259c3b6000ff3b9ba0a3b359216098759fcfa (patch)
tree4e717f7148cec286ad65ade3cfc893d59ae23d89
parent3a28f409df24d59d77ef2f3f1f98bf98c2c2ca7e (diff)
parent7f54b1bc862f3e60fd0162cdb96aed874ab797b0 (diff)
downloadmullvadvpn-b23259c3b6000ff3b9ba0a3b359216098759fcfa.tar.xz
mullvadvpn-b23259c3b6000ff3b9ba0a3b359216098759fcfa.zip
Merge branch 'fix-settings-issues'
-rw-r--r--CHANGELOG.md1
-rw-r--r--mullvad-daemon/Cargo.toml2
-rw-r--r--mullvad-daemon/src/migrations/account_history.rs1
-rw-r--r--mullvad-daemon/src/migrations/mod.rs19
-rw-r--r--mullvad-daemon/src/settings.rs16
5 files changed, 33 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 924aafc79a..aea3803257 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -41,6 +41,7 @@ Line wrap the file at 100 chars. Th
### Fixed
- Fix the sometimes incorrect time added text after adding time to the account.
- Fix scrollbar no longer responsive and usable when covered by other elements.
+- Fix settings file being truncated before being read.
## [2022.1-beta1] - 2022-02-14
diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml
index e382c056c0..2d868e9076 100644
--- a/mullvad-daemon/Cargo.toml
+++ b/mullvad-daemon/Cargo.toml
@@ -25,7 +25,7 @@ rand = "0.7"
regex = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
-tokio = { version = "1.8", features = ["fs", "rt-multi-thread", "sync", "time"] }
+tokio = { version = "1.8", features = ["fs", "io-util", "rt-multi-thread", "sync", "time"] }
tokio-stream = "0.1"
uuid = { version = "0.8", features = ["v4"] }
diff --git a/mullvad-daemon/src/migrations/account_history.rs b/mullvad-daemon/src/migrations/account_history.rs
index e2c0f37c3f..f754f1ac68 100644
--- a/mullvad-daemon/src/migrations/account_history.rs
+++ b/mullvad-daemon/src/migrations/account_history.rs
@@ -67,7 +67,6 @@ pub async fn migrate_formats(settings_dir: &Path, settings: &mut serde_json::Val
file.write_all(token.as_bytes())
.await
.map_err(Error::WriteHistoryError)?;
- file.flush().await.map_err(Error::WriteHistoryError)?;
file.sync_all().await.map_err(Error::WriteHistoryError)?;
Ok(())
diff --git a/mullvad-daemon/src/migrations/mod.rs b/mullvad-daemon/src/migrations/mod.rs
index bb2d7fe3ea..98ad71c23c 100644
--- a/mullvad-daemon/src/migrations/mod.rs
+++ b/mullvad-daemon/src/migrations/mod.rs
@@ -62,9 +62,15 @@ pub enum Error {
#[error(display = "Unable to serialize settings to JSON")]
SerializeError(#[error(source)] serde_json::Error),
+ #[error(display = "Unable to open settings for writing")]
+ OpenError(#[error(source)] io::Error),
+
#[error(display = "Unable to write new settings")]
WriteError(#[error(source)] io::Error),
+ #[error(display = "Unable to sync settings to disk")]
+ SyncError(#[error(source)] io::Error),
+
#[error(display = "Failed to read the account history")]
ReadHistoryError(#[error(source)] io::Error),
@@ -102,6 +108,8 @@ pub async fn migrate_all(cache_dir: &Path, settings_dir: &Path) -> Result<()> {
return Err(Error::NoMatchingVersion);
}
+ let old_settings = settings.clone();
+
v1::migrate(&mut settings)?;
v2::migrate(&mut settings)?;
v3::migrate(&mut settings)?;
@@ -111,6 +119,11 @@ pub async fn migrate_all(cache_dir: &Path, settings_dir: &Path) -> Result<()> {
account_history::migrate_location(cache_dir, settings_dir).await;
account_history::migrate_formats(settings_dir, &mut settings).await?;
+ if settings == old_settings {
+ // Nothing changed
+ return Ok(());
+ }
+
let buffer = serde_json::to_string_pretty(&settings).map_err(Error::SerializeError)?;
let mut options = fs::OpenOptions::new();
@@ -124,10 +137,14 @@ pub async fn migrate_all(cache_dir: &Path, settings_dir: &Path) -> Result<()> {
.truncate(true)
.open(&path)
.await
- .map_err(Error::WriteError)?;
+ .map_err(Error::OpenError)?;
file.write_all(&buffer.into_bytes())
.await
.map_err(Error::WriteError)?;
+ file.sync_data().await.map_err(Error::SyncError)?;
+
+ log::debug!("Migrated settings. Wrote settings to {}", path.display());
+
Ok(())
}
diff --git a/mullvad-daemon/src/settings.rs b/mullvad-daemon/src/settings.rs
index 455b1775ed..32a06020e2 100644
--- a/mullvad-daemon/src/settings.rs
+++ b/mullvad-daemon/src/settings.rs
@@ -59,7 +59,7 @@ impl SettingsPersister {
"{}",
error.display_chain_with_msg("Failed to load settings. Using defaults.")
);
- (Settings::default(), true)
+ (Self::default_settings(), true)
}
};
@@ -91,7 +91,7 @@ impl SettingsPersister {
Err(error) => {
if error.kind() == io::ErrorKind::NotFound {
log::info!("No settings were found. Using defaults.");
- return Ok((Settings::default(), true));
+ return Ok((Self::default_settings(), true));
} else {
return Err(Error::ReadError(path.display().to_string(), error));
}
@@ -152,7 +152,7 @@ impl SettingsPersister {
/// Resets default settings
#[cfg(not(target_os = "android"))]
pub async fn reset(&mut self) -> Result<(), Error> {
- self.settings = Settings::default();
+ self.settings = Self::default_settings();
let path = self.path.clone();
self.save()
.or_else(|e| async move {
@@ -172,6 +172,16 @@ impl SettingsPersister {
self.settings.clone()
}
+ /// Modifies `Settings::default()` somewhat, e.g. depending on whether a beta version
+ /// is being run or not.
+ fn default_settings() -> Settings {
+ let mut settings = Settings::default();
+ if crate::version::is_beta_version() {
+ settings.show_beta_releases = true;
+ }
+ settings
+ }
+
/// Changes account number to the one given. Also saves the new settings to disk.
/// The boolean in the Result indicates if the account token changed or not
pub async fn set_account_token(