Patchwork D11654: status: Extract TruncatedTimestamp from fs::Metadata without SystemTime

login
register
mail settings
Submitter phabricator
Date Oct. 13, 2021, 3:39 p.m.
Message ID <differential-rev-PHID-DREV-ermrwxavyiwjhwi3lkqt-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49978/
State Superseded
Headers show

Comments

phabricator - Oct. 13, 2021, 3:39 p.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  On Unix, the Rust standard library exposes `mtime` and `mtime_nsec` methods
  for `std::fs::Metada` whih is exactly what we need to construct a
  `TruncatedTimestamp`. This skips the computation in the conversion through
  `SystemTime` and `Result<Duration, Duration>`.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D11654

AFFECTED FILES
  rust/hg-core/src/dirstate/entry.rs
  rust/hg-core/src/dirstate_tree/status.rs

CHANGE DETAILS




To: SimonSapin, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/hg-core/src/dirstate_tree/status.rs b/rust/hg-core/src/dirstate_tree/status.rs
--- a/rust/hg-core/src/dirstate_tree/status.rs
+++ b/rust/hg-core/src/dirstate_tree/status.rs
@@ -199,15 +199,14 @@ 
                 // by a previous run of the `status` algorithm which found this
                 // directory eligible for `read_dir` caching.
                 if let Some(meta) = directory_metadata {
-                    if let Ok(current_mtime) = meta.modified() {
-                        let truncated =
-                            TruncatedTimestamp::from(current_mtime);
-                        if truncated.very_likely_equal(&cached_mtime) {
-                            // The mtime of that directory has not changed
-                            // since then, which means that the results of
-                            // `read_dir` should also be unchanged.
-                            return true;
-                        }
+                    if cached_mtime
+                        .very_likely_equal_to_mtime_of(meta)
+                        .unwrap_or(false)
+                    {
+                        // The mtime of that directory has not changed
+                        // since then, which means that the results of
+                        // `read_dir` should also be unchanged.
+                        return true;
                     }
                 }
             }
@@ -472,7 +471,7 @@ 
                     let is_up_to_date = if let Some(cached) =
                         dirstate_node.cached_directory_mtime()?
                     {
-                        cached.very_likely_equal(&truncated)
+                        cached.very_likely_equal(truncated)
                     } else {
                         false
                     };
diff --git a/rust/hg-core/src/dirstate/entry.rs b/rust/hg-core/src/dirstate/entry.rs
--- a/rust/hg-core/src/dirstate/entry.rs
+++ b/rust/hg-core/src/dirstate/entry.rs
@@ -1,7 +1,9 @@ 
 use crate::dirstate_tree::on_disk::DirstateV2ParseError;
 use crate::errors::HgError;
 use bitflags::bitflags;
-use std::convert::TryFrom;
+use std::convert::{TryFrom, TryInto};
+use std::fs;
+use std::io;
 use std::time::{SystemTime, UNIX_EPOCH};
 
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
@@ -69,6 +71,21 @@ 
         }
     }
 
+    pub fn for_mtime_of(metadata: &fs::Metadata) -> io::Result<Self> {
+        #[cfg(unix)]
+        {
+            use std::os::unix::fs::MetadataExt;
+            let seconds = metadata.mtime();
+            // i64 -> u32 with value always in the `0 .. NSEC_PER_SEC` range
+            let nanoseconds = metadata.mtime_nsec().try_into().unwrap();
+            Ok(Self::new_truncate(seconds, nanoseconds))
+        }
+        #[cfg(not(unix))]
+        {
+            metadata.modified().map(Self::from)
+        }
+    }
+
     /// The lower 31 bits of the number of seconds since the epoch.
     pub fn truncated_seconds(&self) -> u32 {
         self.truncated_seconds
@@ -93,10 +110,17 @@ 
     /// If someone is manipulating the modification times of some files to
     /// intentionally make `hg status` return incorrect results, not truncating
     /// wouldn’t help much since they can set exactly the expected timestamp.
-    pub fn very_likely_equal(&self, other: &Self) -> bool {
+    pub fn very_likely_equal(self, other: Self) -> bool {
         self.truncated_seconds == other.truncated_seconds
             && self.nanoseconds == other.nanoseconds
     }
+
+    pub fn very_likely_equal_to_mtime_of(
+        self,
+        metadata: &fs::Metadata,
+    ) -> io::Result<bool> {
+        Ok(self.very_likely_equal(Self::for_mtime_of(metadata)?))
+    }
 }
 
 impl From<SystemTime> for TruncatedTimestamp {