diff options
| author | Emīls <emils@mullvad.net> | 2018-06-23 00:41:25 +0100 |
|---|---|---|
| committer | Emīls Piņķis <emils@mullvad.net> | 2018-06-25 13:31:29 +0100 |
| commit | c8b23df372a2f7846b87d0733f8d047f8903bb4a (patch) | |
| tree | c5f1fa8705025e2e27030f29c1ed78f9cc2d3ff8 | |
| parent | 1d70f24f6a7fcb10d9ced9592a279ec8ee3c1592 (diff) | |
| download | mullvadvpn-c8b23df372a2f7846b87d0733f8d047f8903bb4a.tar.xz mullvadvpn-c8b23df372a2f7846b87d0733f8d047f8903bb4a.zip | |
Flush file writes when saving config and backup files
| -rw-r--r-- | mullvad-daemon/src/account_history.rs | 8 | ||||
| -rw-r--r-- | mullvad-daemon/src/settings.rs | 6 | ||||
| -rw-r--r-- | talpid-core/src/firewall/linux/dns.rs | 12 | ||||
| -rw-r--r-- | talpid-core/src/firewall/windows/system_state.rs | 4 |
4 files changed, 23 insertions, 7 deletions
diff --git a/mullvad-daemon/src/account_history.rs b/mullvad-daemon/src/account_history.rs index 016d2fd780..ab6797a6fa 100644 --- a/mullvad-daemon/src/account_history.rs +++ b/mullvad-daemon/src/account_history.rs @@ -94,11 +94,15 @@ impl AccountHistory { /// Serializes the account history and saves it to the file it was loaded from. fn save(&self) -> Result<()> { debug!("Writing account history to {}", self.cache_path.display()); - let file = File::create(&self.cache_path) + let mut file = File::create(&self.cache_path) .map(io::BufWriter::new) .chain_err(|| ErrorKind::WriteError(self.cache_path.clone()))?; - serde_json::to_writer_pretty(file, self) + serde_json::to_writer_pretty(&mut file, self) + .chain_err(|| ErrorKind::WriteError(self.cache_path.clone()))?; + + file.get_mut() + .sync_all() .chain_err(|| ErrorKind::WriteError(self.cache_path.clone())) } } diff --git a/mullvad-daemon/src/settings.rs b/mullvad-daemon/src/settings.rs index 0ff007cce8..4622db15ce 100644 --- a/mullvad-daemon/src/settings.rs +++ b/mullvad-daemon/src/settings.rs @@ -81,9 +81,11 @@ impl Settings { let path = Self::get_settings_path()?; debug!("Writing settings to {}", path.display()); - let file = File::create(&path).chain_err(|| ErrorKind::WriteError(path.clone()))?; + let mut file = File::create(&path).chain_err(|| ErrorKind::WriteError(path.clone()))?; - serde_json::to_writer_pretty(file, self).chain_err(|| ErrorKind::WriteError(path)) + serde_json::to_writer_pretty(&mut file, self) + .chain_err(|| ErrorKind::WriteError(path.clone()))?; + file.sync_all().chain_err(|| ErrorKind::WriteError(path)) } fn get_settings_path() -> Result<PathBuf> { diff --git a/talpid-core/src/firewall/linux/dns.rs b/talpid-core/src/firewall/linux/dns.rs index 9353476d56..840ffd609e 100644 --- a/talpid-core/src/firewall/linux/dns.rs +++ b/talpid-core/src/firewall/linux/dns.rs @@ -1,11 +1,12 @@ extern crate notify; extern crate resolv_conf; +use std::io::{self, Write}; use std::net::IpAddr; use std::ops::DerefMut; use std::path::Path; use std::sync::{mpsc, Arc, Mutex, MutexGuard}; -use std::{fs, io, thread}; +use std::{fs, thread}; use error_chain::ChainedError; @@ -106,7 +107,14 @@ impl DnsSettings { match fs::read(&backup_file) { Ok(backup) => { info!("Restoring DNS state from backup"); - fs::write(RESOLV_CONF_PATH, &backup).chain_err(|| ErrorKind::RestoreResolvConf)?; + let mut conf_file = + fs::File::create(RESOLV_CONF_PATH).chain_err(|| ErrorKind::RestoreResolvConf)?; + conf_file + .write_all(&backup) + .chain_err(|| ErrorKind::RestoreResolvConf)?; + conf_file + .sync_all() + .chain_err(|| ErrorKind::RestoreResolvConf)?; fs::remove_file(&backup_file).chain_err(|| ErrorKind::RemoveBackup)?; } Err(ref error) if error.kind() == io::ErrorKind::NotFound => { diff --git a/talpid-core/src/firewall/windows/system_state.rs b/talpid-core/src/firewall/windows/system_state.rs index 4695c13bc4..8c310667bb 100644 --- a/talpid-core/src/firewall/windows/system_state.rs +++ b/talpid-core/src/firewall/windows/system_state.rs @@ -24,7 +24,9 @@ impl SystemStateWriter { /// Writes a binary blob representing the system state to the backup location before any /// security policies are applied. pub fn write_backup(&self, data: &[u8]) -> io::Result<()> { - fs::write(&self.backup_path, &data) + let mut backup_file = File::create(&self.backup_path)?; + backup_file.write_all(data)?; + backup_file.sync_all() } pub fn read_backup(&self) -> io::Result<Option<Vec<u8>>> { |
