summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2022-05-11 14:03:55 +0200
committerDavid Lönnhager <david.l@mullvad.net>2022-05-12 15:02:31 +0200
commit417a3c449b5117af2f7c03258e43df2e055d87ff (patch)
treebd2da447782f19834a0b98d625a417d941690887
parent27ee09b6f54076f8d0c25bb249609505bcebc0cc (diff)
downloadmullvadvpn-417a3c449b5117af2f7c03258e43df2e055d87ff.tar.xz
mullvadvpn-417a3c449b5117af2f7c03258e43df2e055d87ff.zip
Overwrite device.json if deserialization fails
-rw-r--r--mullvad-daemon/src/device/mod.rs21
1 files changed, 14 insertions, 7 deletions
diff --git a/mullvad-daemon/src/device/mod.rs b/mullvad-daemon/src/device/mod.rs
index 7d2c0d07cc..505923523e 100644
--- a/mullvad-daemon/src/device/mod.rs
+++ b/mullvad-daemon/src/device/mod.rs
@@ -842,6 +842,7 @@ impl DeviceCacher {
pub async fn new(settings_dir: &Path) -> Result<(DeviceCacher, PrivateDeviceState), Error> {
let path = settings_dir.join(DEVICE_CACHE_FILENAME);
let cache_exists = path.is_file();
+ let mut should_save = false;
let mut file = fs::OpenOptions::from(Self::file_options())
.write(true)
@@ -856,6 +857,7 @@ impl DeviceCacher {
reader.read_to_string(&mut buffer).await?;
if !buffer.is_empty() {
serde_json::from_str(&buffer).unwrap_or_else(|error| {
+ should_save = true;
log::error!(
"{}",
error.display_chain_with_msg("Wiping device config due to an error")
@@ -863,19 +865,24 @@ impl DeviceCacher {
PrivateDeviceState::LoggedOut
})
} else {
+ should_save = true;
PrivateDeviceState::LoggedOut
}
} else {
+ should_save = true;
PrivateDeviceState::LoggedOut
};
- Ok((
- DeviceCacher {
- file: io::BufWriter::new(file),
- path,
- },
- device,
- ))
+ let mut store = DeviceCacher {
+ file: io::BufWriter::new(file),
+ path,
+ };
+
+ if should_save {
+ store.write(&device).await?;
+ }
+
+ Ok((store, device))
}
fn file_options() -> std::fs::OpenOptions {