diff rust/hg-core/src/utils/files.rs @ 52058:b55f653a0b34

rust-utils: move the `filesystem_now` function to a util This is going to be useful for an upcoming `hg update` fastpath.
author Rapha?l Gom?s <rgomes@octobus.net>
date Mon, 30 Sep 2024 17:45:10 +0200
parents 529a655874fb
children 66e34bc44280
line wrap: on
line diff
--- a/rust/hg-core/src/utils/files.rs	Mon Sep 30 17:43:51 2024 +0200
+++ b/rust/hg-core/src/utils/files.rs	Mon Sep 30 17:45:10 2024 +0200
@@ -16,11 +16,15 @@
 };
 use lazy_static::lazy_static;
 use same_file::is_same_file;
-use std::borrow::{Cow, ToOwned};
 use std::ffi::{OsStr, OsString};
 use std::iter::FusedIterator;
 use std::ops::Deref;
 use std::path::{Path, PathBuf};
+use std::{
+    borrow::{Cow, ToOwned},
+    io,
+    time::SystemTime,
+};
 
 pub fn get_os_str_from_bytes(bytes: &[u8]) -> &OsStr {
     let os_str;
@@ -306,6 +310,25 @@
     }
 }
 
+/// Return the `mtime` of a temporary file newly-created in the `.hg` directory
+/// of the give repository.
+///
+/// This is similar to `SystemTime::now()`, with the result truncated to the
+/// same time resolution as other files’ modification times. Using `.hg`
+/// instead of the system’s default temporary directory (such as `/tmp`) makes
+/// it more likely the temporary file is in the same disk partition as contents
+/// of the working directory, which can matter since different filesystems may
+/// store timestamps with different resolutions.
+///
+/// This may fail, typically if we lack write permissions. In that case we
+/// should continue the `status()` algoritm anyway and consider the current
+/// date/time to be unknown.
+pub fn filesystem_now(repo_root: &Path) -> Result<SystemTime, io::Error> {
+    tempfile::tempfile_in(repo_root.join(".hg"))?
+        .metadata()?
+        .modified()
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;