Extract mtime calculation
authorAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 13 Apr 2018 18:11:41 +0000 (21:11 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 14 Apr 2018 08:12:24 +0000 (11:12 +0300)
src/cargo/core/compiler/fingerprint.rs
src/cargo/sources/path.rs
src/cargo/util/paths.rs

index d4b5e4ee7df53d04746fddc6ca762530d6aa7711..5e734f1ae3691f944fec369262ed1981a5381ea2 100644 (file)
@@ -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<Path>,
 {
-    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
index cadb0cffa172149e7043f73729a5f39731e6e2e2..aa654abee60ab9aa6542b3acaf3d77005cb7836c 100644 (file)
@@ -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;
index 0cd231e709a242d58ba161cf7047c0d8b5823287..07296880719ee1c556e4d94d247f92a119d6faf4 100644 (file)
@@ -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<FileTime> {
+    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::*;