From 9feb786a51e1aaabd7b2a31d4f89e1dc51d237a3 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Mon, 23 May 2022 23:23:52 -0700 Subject: [PATCH] working_copy: extract function for getting mtime We had a few lines of duplicated code for this, so I moved it into a function and added better error handling. --- lib/src/working_copy.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index eaa1690e9..31e6a6070 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -14,7 +14,6 @@ use std::cell::{RefCell, RefMut}; use std::collections::{BTreeMap, HashSet}; -use std::convert::TryInto; use std::ffi::OsString; use std::fs; use std::fs::{File, Metadata, OpenOptions}; @@ -154,10 +153,21 @@ fn create_parent_dirs(disk_path: &Path) { .unwrap_or_else(|_| panic!("failed to create parent directories for {:?}", &disk_path)); } +fn mtime_from_metadata(metadata: &Metadata) -> MillisSinceEpoch { + let time = metadata + .modified() + .expect("File mtime not supported on this platform?"); + let since_epoch = time + .duration_since(UNIX_EPOCH) + .expect("mtime before unix epoch"); + + MillisSinceEpoch( + u64::try_from(since_epoch.as_millis()).expect("mtime billions of years into the future"), + ) +} + fn file_state(metadata: &Metadata) -> FileState { - let time = metadata.modified().unwrap(); - let since_epoch = time.duration_since(UNIX_EPOCH).unwrap(); - let mtime = MillisSinceEpoch(since_epoch.as_millis().try_into().unwrap()); + let mtime = mtime_from_metadata(metadata); let size = metadata.len(); let metadata_file_type = metadata.file_type(); let file_type = if metadata_file_type.is_dir() { @@ -279,9 +289,7 @@ impl TreeState { fn update_own_mtime(&mut self) { if let Ok(metadata) = self.state_path.join("tree_state").symlink_metadata() { - let time = metadata.modified().unwrap(); - let since_epoch = time.duration_since(UNIX_EPOCH).unwrap(); - self.own_mtime = MillisSinceEpoch(since_epoch.as_millis().try_into().unwrap()); + self.own_mtime = mtime_from_metadata(&metadata); } else { self.own_mtime = MillisSinceEpoch(0); }