summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2021-06-02 11:53:45 +0200
committerDavid Lönnhager <david.l@mullvad.net>2021-06-02 11:53:45 +0200
commit8dfba41e2d7e29a192e22f9533dfcffbdb285172 (patch)
tree36eacc0d39be60c41bde4fa92567e6956695d2e2
parent2c31c16266a370054cf5467bd6840d581cb98d42 (diff)
parente08fe9ebca1a95be1fc6f50142433740d1f0e8ff (diff)
downloadmullvadvpn-8dfba41e2d7e29a192e22f9533dfcffbdb285172.tar.xz
mullvadvpn-8dfba41e2d7e29a192e22f9533dfcffbdb285172.zip
Merge branch 'fix-account-parse-error'
-rw-r--r--CHANGELOG.md1
-rw-r--r--mullvad-daemon/src/account_history.rs63
2 files changed, 39 insertions, 25 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 15d3b55d96..cc19d0cb0f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -53,6 +53,7 @@ Line wrap the file at 100 chars. Th
- Fix relay selection failing to pick a WireGuard relay when no tunnel protocol is specified.
- Fix time left not always being translated in desktop app settings.
- Fix API address cache to use the supplied ports instead of always using port 443.
+- Do not try to parse an empty account history.
#### Windows
- Prevent tray icons from being extraced to `%TEMP%` directory.
diff --git a/mullvad-daemon/src/account_history.rs b/mullvad-daemon/src/account_history.rs
index 31e7feac26..1c5095321f 100644
--- a/mullvad-daemon/src/account_history.rs
+++ b/mullvad-daemon/src/account_history.rs
@@ -15,7 +15,7 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(err_derive::Error, Debug)]
#[error(no_from)]
pub enum Error {
- #[error(display = "Unable to read account history file")]
+ #[error(display = "Unable to open or read account history file")]
Read(#[error(source)] io::Error),
#[error(display = "Failed to serialize account history")]
@@ -60,32 +60,45 @@ impl AccountHistory {
options.share_mode(0);
}
let path = settings_dir.join(ACCOUNT_HISTORY_FILE);
- log::info!("Opening account history file in {}", path.display());
- let mut reader = options
- .write(true)
- .read(true)
- .create(true)
- .open(path)
- .map(io::BufReader::new)
- .map_err(Error::Read)?;
+ let (file, accounts) = if path.is_file() {
+ log::info!("Opening account history file in {}", path.display());
+ let mut reader = options
+ .write(true)
+ .read(true)
+ .open(path)
+ .map(io::BufReader::new)
+ .map_err(Error::Read)?;
- let accounts: VecDeque<AccountEntry> = match serde_json::from_reader(&mut reader) {
- Err(e) => {
- log::warn!(
- "{}",
- e.display_chain_with_msg("Failed to read+deserialize account history")
- );
- Self::try_old_format(&mut reader)?
- .into_iter()
- .map(|account| AccountEntry {
- account,
- wireguard: None,
- })
- .collect()
- }
- Ok(accounts) => accounts,
+ let accounts: VecDeque<AccountEntry> = match serde_json::from_reader(&mut reader) {
+ Err(e) => {
+ log::warn!(
+ "{}",
+ e.display_chain_with_msg("Failed to read+deserialize account history")
+ );
+ Self::try_old_format(&mut reader)?
+ .into_iter()
+ .map(|account| AccountEntry {
+ account,
+ wireguard: None,
+ })
+ .collect()
+ }
+ Ok(accounts) => accounts,
+ };
+
+ (reader.into_inner(), accounts)
+ } else {
+ log::info!("Creating account history file in {}", path.display());
+ (
+ options
+ .write(true)
+ .create(true)
+ .open(path)
+ .map_err(Error::Read)?,
+ VecDeque::new(),
+ )
};
- let file = io::BufWriter::new(reader.into_inner());
+ let file = io::BufWriter::new(file);
let mut history = AccountHistory {
file: Arc::new(Mutex::new(file)),
accounts: Arc::new(Mutex::new(accounts)),