rust/hg-core/src/dirstate/entry.rs
changeset 48218 15dedc0c5c35
parent 48194 1000db4a71f1
child 48252 602c8e8411f5
equal deleted inserted replaced
48217:e10f5dc7f5bf 48218:15dedc0c5c35
     1 use crate::dirstate_tree::on_disk::DirstateV2ParseError;
     1 use crate::dirstate_tree::on_disk::DirstateV2ParseError;
     2 use crate::errors::HgError;
     2 use crate::errors::HgError;
     3 use bitflags::bitflags;
     3 use bitflags::bitflags;
     4 use std::convert::TryFrom;
     4 use std::convert::{TryFrom, TryInto};
       
     5 use std::fs;
       
     6 use std::io;
     5 use std::time::{SystemTime, UNIX_EPOCH};
     7 use std::time::{SystemTime, UNIX_EPOCH};
     6 
     8 
     7 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
     9 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
     8 pub enum EntryState {
    10 pub enum EntryState {
     9     Normal,
    11     Normal,
    67         } else {
    69         } else {
    68             Err(DirstateV2ParseError)
    70             Err(DirstateV2ParseError)
    69         }
    71         }
    70     }
    72     }
    71 
    73 
       
    74     pub fn for_mtime_of(metadata: &fs::Metadata) -> io::Result<Self> {
       
    75         #[cfg(unix)]
       
    76         {
       
    77             use std::os::unix::fs::MetadataExt;
       
    78             let seconds = metadata.mtime();
       
    79             // i64 -> u32 with value always in the `0 .. NSEC_PER_SEC` range
       
    80             let nanoseconds = metadata.mtime_nsec().try_into().unwrap();
       
    81             Ok(Self::new_truncate(seconds, nanoseconds))
       
    82         }
       
    83         #[cfg(not(unix))]
       
    84         {
       
    85             metadata.modified().map(Self::from)
       
    86         }
       
    87     }
       
    88 
    72     /// The lower 31 bits of the number of seconds since the epoch.
    89     /// The lower 31 bits of the number of seconds since the epoch.
    73     pub fn truncated_seconds(&self) -> u32 {
    90     pub fn truncated_seconds(&self) -> u32 {
    74         self.truncated_seconds
    91         self.truncated_seconds
    75     }
    92     }
    76 
    93 
    91     /// on filesystems that support sub-second precision.
   108     /// on filesystems that support sub-second precision.
    92     ///
   109     ///
    93     /// If someone is manipulating the modification times of some files to
   110     /// If someone is manipulating the modification times of some files to
    94     /// intentionally make `hg status` return incorrect results, not truncating
   111     /// intentionally make `hg status` return incorrect results, not truncating
    95     /// wouldn’t help much since they can set exactly the expected timestamp.
   112     /// wouldn’t help much since they can set exactly the expected timestamp.
    96     pub fn very_likely_equal(&self, other: &Self) -> bool {
   113     pub fn very_likely_equal(self, other: Self) -> bool {
    97         self.truncated_seconds == other.truncated_seconds
   114         self.truncated_seconds == other.truncated_seconds
    98             && self.nanoseconds == other.nanoseconds
   115             && self.nanoseconds == other.nanoseconds
       
   116     }
       
   117 
       
   118     pub fn very_likely_equal_to_mtime_of(
       
   119         self,
       
   120         metadata: &fs::Metadata,
       
   121     ) -> io::Result<bool> {
       
   122         Ok(self.very_likely_equal(Self::for_mtime_of(metadata)?))
    99     }
   123     }
   100 }
   124 }
   101 
   125 
   102 impl From<SystemTime> for TruncatedTimestamp {
   126 impl From<SystemTime> for TruncatedTimestamp {
   103     fn from(system_time: SystemTime) -> Self {
   127     fn from(system_time: SystemTime) -> Self {