comparison 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
comparison
equal deleted inserted replaced
52057:88aa21d654e5 52058:b55f653a0b34
14 path_auditor::PathAuditor, 14 path_auditor::PathAuditor,
15 replace_slice, 15 replace_slice,
16 }; 16 };
17 use lazy_static::lazy_static; 17 use lazy_static::lazy_static;
18 use same_file::is_same_file; 18 use same_file::is_same_file;
19 use std::borrow::{Cow, ToOwned};
20 use std::ffi::{OsStr, OsString}; 19 use std::ffi::{OsStr, OsString};
21 use std::iter::FusedIterator; 20 use std::iter::FusedIterator;
22 use std::ops::Deref; 21 use std::ops::Deref;
23 use std::path::{Path, PathBuf}; 22 use std::path::{Path, PathBuf};
23 use std::{
24 borrow::{Cow, ToOwned},
25 io,
26 time::SystemTime,
27 };
24 28
25 pub fn get_os_str_from_bytes(bytes: &[u8]) -> &OsStr { 29 pub fn get_os_str_from_bytes(bytes: &[u8]) -> &OsStr {
26 let os_str; 30 let os_str;
27 #[cfg(unix)] 31 #[cfg(unix)]
28 { 32 {
304 } 308 }
305 Cow::Owned(res) 309 Cow::Owned(res)
306 } 310 }
307 } 311 }
308 312
313 /// Return the `mtime` of a temporary file newly-created in the `.hg` directory
314 /// of the give repository.
315 ///
316 /// This is similar to `SystemTime::now()`, with the result truncated to the
317 /// same time resolution as other files’ modification times. Using `.hg`
318 /// instead of the system’s default temporary directory (such as `/tmp`) makes
319 /// it more likely the temporary file is in the same disk partition as contents
320 /// of the working directory, which can matter since different filesystems may
321 /// store timestamps with different resolutions.
322 ///
323 /// This may fail, typically if we lack write permissions. In that case we
324 /// should continue the `status()` algoritm anyway and consider the current
325 /// date/time to be unknown.
326 pub fn filesystem_now(repo_root: &Path) -> Result<SystemTime, io::Error> {
327 tempfile::tempfile_in(repo_root.join(".hg"))?
328 .metadata()?
329 .modified()
330 }
331
309 #[cfg(test)] 332 #[cfg(test)]
310 mod tests { 333 mod tests {
311 use super::*; 334 use super::*;
312 use pretty_assertions::assert_eq; 335 use pretty_assertions::assert_eq;
313 336