cache: add get_mtime utility

Unify how we get back mtimes from files. Ideally, all the callers to
get_mtime would be part of the cache and not split out like they
currently are.
This commit is contained in:
zimbatm 2021-02-22 15:49:46 +01:00
parent bde7493591
commit ec77b3f710
No known key found for this signature in database
GPG Key ID: 71BAF6D40C1D63D7
3 changed files with 12 additions and 13 deletions

View File

@ -1,12 +1,10 @@
//! The main formatting engine logic should be in this module.
use crate::eval_cache::{check_treefmt, create_manifest, read_manifest, RootManifest};
use crate::eval_cache::{check_treefmt, create_manifest, get_mtime, read_manifest, RootManifest};
use crate::{config, CmdContext, CmdMeta, FileMeta, CLOG};
use anyhow::{Error, Result};
use filetime::FileTime;
use rayon::prelude::*;
use std::collections::BTreeSet;
use std::fs::metadata;
use std::iter::Iterator;
use std::path::PathBuf;
use std::process::Command;
@ -128,8 +126,7 @@ pub fn glob_to_path(
pub fn path_to_filemeta(paths: Vec<PathBuf>) -> Result<BTreeSet<FileMeta>> {
let mut filemeta = BTreeSet::new();
for p in paths {
let metadata = metadata(&p)?;
let mtime = FileTime::from_last_modification_time(&metadata).unix_seconds();
let mtime = get_mtime(&p)?;
if !filemeta.insert(FileMeta {
mtime,
path: p.clone(),

View File

@ -2,6 +2,7 @@
use crate::{customlog, CmdContext, CLOG};
use anyhow::{anyhow, Error, Result};
use filetime::FileTime;
use serde::{Deserialize, Serialize};
use sha1::{Digest, Sha1};
use std::collections::BTreeMap;
@ -200,3 +201,9 @@ mod tests {
Ok(())
}
}
/// Small utility that stat() and retrieve the mtime of a file
pub fn get_mtime(path: &PathBuf) -> Result<i64> {
let metadata = std::fs::metadata(path)?;
Ok(FileTime::from_last_modification_time(&metadata).unix_seconds())
}

View File

@ -7,14 +7,13 @@ pub mod customlog;
pub mod engine;
pub mod eval_cache;
use crate::eval_cache::get_mtime;
use anyhow::Result;
use customlog::CustomLogOutput;
use filetime::FileTime;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::fmt;
use std::fs::metadata;
use std::path::PathBuf;
/// The global custom log and user-facing message output.
@ -83,12 +82,9 @@ impl CmdMeta {
/// We assume that cmd_path is absolute.
pub fn new(cmd_path: &PathBuf) -> Result<Self> {
assert!(cmd_path.is_absolute());
let metadata = metadata(&cmd_path)?;
let cmd_mtime = FileTime::from_last_modification_time(&metadata).unix_seconds();
Ok(CmdMeta {
path: cmd_path.clone(),
mtime: cmd_mtime,
mtime: get_mtime(&cmd_path)?,
})
}
}
@ -118,8 +114,7 @@ pub struct FileMeta {
impl FileMeta {
fn update_mtime(self) -> Result<Self> {
let metadata = metadata(&self.path)?;
let mtime = FileTime::from_last_modification_time(&metadata).unix_seconds();
let mtime = get_mtime(&self.path)?;
Ok(FileMeta {
path: self.path,
mtime,