summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2022-05-04 13:21:31 +0200
committerDavid Lönnhager <david.l@mullvad.net>2022-05-04 13:59:35 +0200
commitb8ecd89b59a5277a60b2190a891601edead81054 (patch)
tree6aa93b42beff5ec64f7b5d45dd39d9cdb931a436
parent62e46dd7729648db5b243293218990073f938e1e (diff)
downloadmullvadvpn-b8ecd89b59a5277a60b2190a891601edead81054.tar.xz
mullvadvpn-b8ecd89b59a5277a60b2190a891601edead81054.zip
Wipe the device config if it cannot be parsed
-rw-r--r--mullvad-daemon/src/device/mod.rs39
1 files changed, 24 insertions, 15 deletions
diff --git a/mullvad-daemon/src/device/mod.rs b/mullvad-daemon/src/device/mod.rs
index ae27e10438..edce336c49 100644
--- a/mullvad-daemon/src/device/mod.rs
+++ b/mullvad-daemon/src/device/mod.rs
@@ -693,23 +693,10 @@ pub struct DeviceCacher {
impl DeviceCacher {
pub async fn new(settings_dir: &Path) -> Result<(DeviceCacher, Option<DeviceData>), Error> {
- let mut options = std::fs::OpenOptions::new();
- #[cfg(unix)]
- {
- use std::os::unix::fs::OpenOptionsExt;
- options.mode(0o600);
- }
- #[cfg(windows)]
- {
- use std::os::windows::fs::OpenOptionsExt;
- // exclusive access
- options.share_mode(0);
- }
-
let path = settings_dir.join(DEVICE_CACHE_FILENAME);
let cache_exists = path.is_file();
- let mut file = fs::OpenOptions::from(options)
+ let mut file = fs::OpenOptions::from(Self::file_options())
.write(true)
.read(true)
.create(true)
@@ -721,7 +708,13 @@ impl DeviceCacher {
let mut buffer = String::new();
reader.read_to_string(&mut buffer).await?;
if !buffer.is_empty() {
- serde_json::from_str(&buffer)?
+ serde_json::from_str(&buffer).unwrap_or_else(|error| {
+ log::error!(
+ "{}",
+ error.display_chain_with_msg("Wiping device config due to an error")
+ );
+ None
+ })
} else {
None
}
@@ -738,6 +731,22 @@ impl DeviceCacher {
))
}
+ fn file_options() -> std::fs::OpenOptions {
+ let mut options = std::fs::OpenOptions::new();
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::OpenOptionsExt;
+ options.mode(0o600);
+ }
+ #[cfg(windows)]
+ {
+ use std::os::windows::fs::OpenOptionsExt;
+ // exclusive access
+ options.share_mode(0);
+ }
+ options
+ }
+
pub async fn write(&mut self, device: Option<&DeviceData>) -> Result<(), Error> {
let data = serde_json::to_vec_pretty(&device).unwrap();