From 8c5c9e398d22cf4f7e859a3e7144f2aed8c57a7c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Apr 2018 21:11:41 +0300 Subject: [PATCH] Extract mtime calculation --- src/cargo/core/compiler/fingerprint.rs | 14 +++++--------- src/cargo/sources/path.rs | 5 ++--- src/cargo/util/paths.rs | 8 ++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/cargo/core/compiler/fingerprint.rs b/src/cargo/core/compiler/fingerprint.rs index d4b5e4ee7..5e734f1ae 100644 --- a/src/cargo/core/compiler/fingerprint.rs +++ b/src/cargo/core/compiler/fingerprint.rs @@ -220,9 +220,7 @@ impl Fingerprint { match *local { LocalFingerprint::MtimeBased(ref slot, ref path) => { let path = root.join(path); - let meta = fs::metadata(&path) - .chain_err(|| internal(format!("failed to stat `{}`", path.display())))?; - let mtime = FileTime::from_last_modification_time(&meta); + let mtime = paths::mtime(&path)?; *slot.0.lock().unwrap() = Some(mtime); } LocalFingerprint::EnvBased(..) | LocalFingerprint::Precalculated(..) => continue, @@ -718,22 +716,20 @@ where I: IntoIterator, I::Item: AsRef, { - let meta = match fs::metadata(output) { - Ok(meta) => meta, + let mtime = match paths::mtime(output) { + Ok(mtime) => mtime, Err(..) => return None, }; - let mtime = FileTime::from_last_modification_time(&meta); let any_stale = paths.into_iter().any(|path| { let path = path.as_ref(); - let meta = match fs::metadata(path) { - Ok(meta) => meta, + let mtime2 = match paths::mtime(path) { + Ok(mtime) => mtime, Err(..) => { info!("stale: {} -- missing", path.display()); return true; } }; - let mtime2 = FileTime::from_last_modification_time(&meta); if mtime2 > mtime { info!("stale: {} -- {} vs {}", path.display(), mtime2, mtime); true diff --git a/src/cargo/sources/path.rs b/src/cargo/sources/path.rs index cadb0cffa..aa654abee 100644 --- a/src/cargo/sources/path.rs +++ b/src/cargo/sources/path.rs @@ -11,6 +11,7 @@ use ignore::gitignore::GitignoreBuilder; use core::{Dependency, Package, PackageId, Registry, Source, SourceId, Summary}; use ops; use util::{self, internal, CargoResult}; +use util::paths; use util::Config; pub struct PathSource<'cfg> { @@ -529,9 +530,7 @@ impl<'cfg> Source for PathSource<'cfg> { // condition where this path was rm'ed - either way, // we can ignore the error and treat the path's mtime // as 0. - let mtime = fs::metadata(&file) - .map(|meta| FileTime::from_last_modification_time(&meta)) - .unwrap_or(FileTime::zero()); + let mtime = paths::mtime(&file).unwrap_or(FileTime::zero()); warn!("{} {}", mtime, file.display()); if mtime > max { max = mtime; diff --git a/src/cargo/util/paths.rs b/src/cargo/util/paths.rs index 0cd231e70..072968807 100644 --- a/src/cargo/util/paths.rs +++ b/src/cargo/util/paths.rs @@ -5,6 +5,8 @@ use std::io; use std::io::prelude::*; use std::path::{Component, Path, PathBuf}; +use filetime::FileTime; + use util::{internal, CargoResult}; use util::errors::{CargoError, CargoResultExt, Internal}; @@ -129,6 +131,12 @@ pub fn append(path: &Path, contents: &[u8]) -> CargoResult<()> { Ok(()) } +pub fn mtime(path: &Path) -> CargoResult { + let meta = fs::metadata(path) + .chain_err(|| internal(format!("failed to stat `{}`", path.display())))?; + Ok(FileTime::from_last_modification_time(&meta)) +} + #[cfg(unix)] pub fn path2bytes(path: &Path) -> CargoResult<&[u8]> { use std::os::unix::prelude::*; -- 2.30.2