diff options
| -rw-r--r-- | mullvad-daemon/src/main.rs | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 821206636d..78daca509b 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -643,10 +643,7 @@ impl Daemon { self.set_security_policy()?; - if let Some(ref file) = self.tunnel_log { - let _ = fs::remove_file(file); - fs::File::create(file).chain_err(|| "Unable to create the tunnel log file")?; - } + self.prepare_tunnel_log_file()?; let tunnel_monitor = self.spawn_tunnel_monitor(self.tunnel_endpoint.unwrap(), &account_token)?; @@ -657,6 +654,38 @@ impl Daemon { Ok(()) } + fn prepare_tunnel_log_file(&self) -> Result<()> { + if let Some(ref file) = self.tunnel_log { + if file.exists() { + Self::backup_existing_tunnel_log_file(file); + } + + let _ = fs::remove_file(file); + fs::File::create(file).chain_err(|| "Unable to create the tunnel log file")?; + } + + Ok(()) + } + + fn backup_existing_tunnel_log_file(log_file: &PathBuf) { + let mut backup = log_file.clone(); + backup.set_extension("old.log"); + + if backup.exists() { + // Try to remove file first, in case rename fails to overwrite it + fs::remove_file(&backup).unwrap_or_else(|error| { + warn!("Failed to remove old backup of tunnel log file ({})", error); + }); + } + + fs::rename(log_file, backup).unwrap_or_else(|error| { + warn!( + "Failed to create backup of previous tunnel log file ({})", + error + ); + }); + } + fn spawn_tunnel_monitor( &self, tunnel_endpoint: TunnelEndpoint, |
