use std::{fs::File, path::Path};
use crate::Timestamp;
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)
}
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)
}