diff options
| author | David Lönnhager <david.l@mullvad.net> | 2021-10-25 10:28:22 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2021-10-26 16:00:16 +0200 |
| commit | 7d32dc09d9dd853e54e3ab19e0ca3773f71b9b2a (patch) | |
| tree | 4baeeb360fadf7565af8122d964bb689dc38d81a | |
| parent | 5164be71ca90f3a684ee81a026a6243097c512d7 (diff) | |
| download | mullvadvpn-7d32dc09d9dd853e54e3ab19e0ca3773f71b9b2a.tar.xz mullvadvpn-7d32dc09d9dd853e54e3ab19e0ca3773f71b9b2a.zip | |
Migrate account history location in migration module
| -rw-r--r-- | mullvad-daemon/src/account_history.rs | 22 | ||||
| -rw-r--r-- | mullvad-daemon/src/lib.rs | 11 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/mod.rs | 23 |
3 files changed, 27 insertions, 29 deletions
diff --git a/mullvad-daemon/src/account_history.rs b/mullvad-daemon/src/account_history.rs index 4b83915ff7..397351a31d 100644 --- a/mullvad-daemon/src/account_history.rs +++ b/mullvad-daemon/src/account_history.rs @@ -41,12 +41,9 @@ lazy_static::lazy_static! { impl AccountHistory { pub async fn new( - cache_dir: &Path, settings_dir: &Path, settings: &mut SettingsPersister, ) -> Result<AccountHistory> { - Self::migrate_from_old_file_location(cache_dir, settings_dir).await; - let mut options = fs::OpenOptions::new(); #[cfg(unix)] { @@ -115,25 +112,6 @@ impl AccountHistory { Ok(history) } - async fn migrate_from_old_file_location(old_dir: &Path, new_dir: &Path) { - use tokio::fs; - - let old_path = old_dir.join(ACCOUNT_HISTORY_FILE); - let new_path = new_dir.join(ACCOUNT_HISTORY_FILE); - if !old_path.exists() || new_path.exists() || new_path == old_path { - return; - } - - if let Err(error) = fs::copy(&old_path, &new_path).await { - log::error!( - "{}", - error.display_chain_with_msg("Failed to migrate account history file location") - ); - } else { - let _ = fs::remove_file(old_path).await; - } - } - fn try_format_v1(reader: &mut io::BufReader<fs::File>) -> Result<Option<AccountToken>> { #[derive(Deserialize)] struct OldFormat { diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index 556483b50e..c4785c1de8 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -603,10 +603,10 @@ where ); - if let Err(error) = migrations::migrate_all(&settings_dir).await { + if let Err(error) = migrations::migrate_all(&cache_dir, &settings_dir).await { log::error!( "{}", - error.display_chain_with_msg("Failed to migrate settings") + error.display_chain_with_msg("Failed to migrate settings or cache") ); } let mut settings = SettingsPersister::load(&settings_dir).await; @@ -625,10 +625,9 @@ where settings.show_beta_releases, ); tokio::spawn(version_updater.run()); - let account_history = - account_history::AccountHistory::new(&cache_dir, &settings_dir, &mut settings) - .await - .map_err(Error::LoadAccountHistory)?; + let account_history = account_history::AccountHistory::new(&settings_dir, &mut settings) + .await + .map_err(Error::LoadAccountHistory)?; // Restore the tunnel to a previous state let target_cache = cache_dir.join(TARGET_START_STATE_FILE); diff --git a/mullvad-daemon/src/migrations/mod.rs b/mullvad-daemon/src/migrations/mod.rs index 594ed21df8..e8f432a63f 100644 --- a/mullvad-daemon/src/migrations/mod.rs +++ b/mullvad-daemon/src/migrations/mod.rs @@ -1,5 +1,6 @@ use mullvad_types::settings::{Settings, CURRENT_SETTINGS_VERSION}; use std::path::Path; +use talpid_types::ErrorExt; use tokio::{ fs, io::{self, AsyncWriteExt}, @@ -11,6 +12,7 @@ mod v3; mod v4; const SETTINGS_FILE: &str = "settings.json"; +const ACCOUNT_HISTORY_FILE: &str = "account-history.json"; #[derive(err_derive::Error, Debug)] #[error(no_from)] @@ -43,12 +45,14 @@ trait SettingsMigration { fn migrate(&self, settings: &mut serde_json::Value) -> Result<()>; } -pub async fn migrate_all(settings_dir: &Path) -> Result<()> { +pub async fn migrate_all(cache_dir: &Path, settings_dir: &Path) -> Result<()> { #[cfg(windows)] windows::migrate_after_windows_update(settings_dir) .await .map_err(Error::WinMigrationError)?; + migrate_account_history_location(cache_dir, settings_dir).await; + let path = settings_dir.join(SETTINGS_FILE); if !path.is_file() { @@ -107,6 +111,23 @@ pub async fn migrate_all(settings_dir: &Path) -> Result<()> { Ok(()) } +async fn migrate_account_history_location(old_dir: &Path, new_dir: &Path) { + let old_path = old_dir.join(ACCOUNT_HISTORY_FILE); + let new_path = new_dir.join(ACCOUNT_HISTORY_FILE); + if !old_path.exists() || new_path.exists() || new_path == old_path { + return; + } + + if let Err(error) = fs::copy(&old_path, &new_path).await { + log::error!( + "{}", + error.display_chain_with_msg("Failed to migrate account history file location") + ); + } else { + let _ = fs::remove_file(old_path).await; + } +} + #[cfg(windows)] mod windows { use std::{ffi::OsStr, io, os::windows::ffi::OsStrExt, path::Path, ptr}; |
