summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-03-02 01:37:16 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-03-06 14:05:43 -0300
commit6c77bc1978d78d60b0b8069166e3e7fdc4360f72 (patch)
tree55b57b0162b41ef7aaafd45f280c206d6f4cc933
parentcaf4303b84403b3b674593f742460fa690edd4a2 (diff)
downloadmullvadvpn-6c77bc1978d78d60b0b8069166e3e7fdc4360f72.tar.xz
mullvadvpn-6c77bc1978d78d60b0b8069166e3e7fdc4360f72.zip
Create backup of OpenVPN log file
Delete previous backup if it already exists.
-rw-r--r--mullvad-daemon/src/main.rs37
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,