jiff/util/fs.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
use std::{fs::File, path::Path};
use crate::Timestamp;
/// Returns the last modified time for the given file path as a Jiff timestamp.
///
/// If there was a problem accessing the last modified time or if it could not
/// fit in a Jiff timestamp, then a warning message is logged and `None` is
/// returned.
pub(crate) fn last_modified_from_path(path: &Path) -> Option<Timestamp> {
let file = match File::open(path) {
Ok(file) => file,
Err(_err) => {
warn!(
"failed to open file to get last modified time {}: {_err}",
path.display(),
);
return None;
}
};
last_modified_from_file(path, &file)
}
/// Returns the last modified time for the given file as a Jiff timestamp.
///
/// If there was a problem accessing the last modified time or if it could not
/// fit in a Jiff timestamp, then a warning message is logged and `None` is
/// returned.
///
/// The path given should be the path to the given file. It is used for
/// diagnostic purposes.
pub(crate) fn last_modified_from_file(
_path: &Path,
file: &File,
) -> Option<Timestamp> {
let md = match file.metadata() {
Ok(md) => md,
Err(_err) => {
warn!(
"failed to get metadata (for last modified time) \
for {}: {_err}",
_path.display(),
);
return None;
}
};
let systime = match md.modified() {
Ok(systime) => systime,
Err(_err) => {
warn!(
"failed to get last modified time for {}: {_err}",
_path.display()
);
return None;
}
};
let timestamp = match Timestamp::try_from(systime) {
Ok(timestamp) => timestamp,
Err(_err) => {
warn!(
"system time {systime:?} out of bounds \
for Jiff timestamp for last modified time \
from {}: {_err}",
_path.display(),
);
return None;
}
};
Some(timestamp)
}