From 79e692bff70bfbf29e7af55df9e0a8529b723ce5 Mon Sep 17 00:00:00 2001 From: Eva Pace Date: Tue, 30 May 2023 08:44:21 -0300 Subject: [PATCH] hostsfile: return bool to inform if file has been written This commit also makes the logs print accordingly to the new behavior. --- client/src/main.rs | 2 -- hostsfile/src/lib.rs | 21 ++++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index f9acca4..97ce3a8 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -280,8 +280,6 @@ fn update_hosts_file( hosts_path: PathBuf, peers: &[Peer], ) -> Result<(), WrappedIoError> { - log::info!("updating {} with the latest peers.", "/etc/hosts".yellow()); - let mut hosts_builder = HostsBuilder::new(format!("innernet {interface}")); for peer in peers { hosts_builder.add_hostname( diff --git a/hostsfile/src/lib.rs b/hostsfile/src/lib.rs index 6dbb434..ed396c1 100644 --- a/hostsfile/src/lib.rs +++ b/hostsfile/src/lib.rs @@ -116,7 +116,8 @@ impl HostsBuilder { /// Inserts a new section to the system's default hosts file. If there is a section with the /// same tag name already, it will be replaced with the new list instead. - pub fn write(&self) -> io::Result<()> { + /// Returns true if the hosts file has changed. + pub fn write(&self) -> io::Result { self.write_to(Self::default_path()?) } @@ -196,7 +197,9 @@ impl HostsBuilder { /// /// On Windows, the format of one hostname per line will be used, all other systems will use /// the same format as Unix and Unix-like systems (i.e. allow multiple hostnames per line). - pub fn write_to>(&self, hosts_path: P) -> io::Result<()> { + /// + /// Returns true if the hosts file has changed. + pub fn write_to>(&self, hosts_path: P) -> io::Result { let hosts_path = hosts_path.as_ref(); if hosts_path.is_dir() { // TODO(jake): use io::ErrorKind::IsADirectory when it's stable. @@ -274,22 +277,26 @@ impl HostsBuilder { Err(_) => { Self::write_clobber(hosts_path, &s)?; log::debug!("wrote hosts file with the clobber fallback strategy"); + Ok(true) }, - _ => { - log::debug!("wrote hosts file with the write-and-swap strategy"); + Ok(has_written) => { + if has_written { + log::debug!("wrote hosts file with the write-and-swap strategy"); + } + Ok(has_written) }, } - Ok(()) } - fn write_and_swap(temp_path: &Path, hosts_path: &Path, contents: &[u8]) -> io::Result<()> { + fn write_and_swap(temp_path: &Path, hosts_path: &Path, contents: &[u8]) -> io::Result { if Self::has_content_changed(hosts_path, contents)? { // Copy the file we plan on modifying so its permissions and metadata are preserved. std::fs::copy(hosts_path, temp_path)?; Self::write_clobber(temp_path, contents)?; std::fs::rename(temp_path, hosts_path)?; + return Ok(true); } - Ok(()) + Ok(false) } fn write_clobber(hosts_path: &Path, contents: &[u8]) -> io::Result<()> {