1use std::{fs::File, path::Path};
2
3use crate::Timestamp;
4
5pub(crate) fn last_modified_from_path(path: &Path) -> Option<Timestamp> {
11 let file = match File::open(path) {
12 Ok(file) => file,
13 Err(_err) => {
14 warn!(
15 "failed to open file to get last modified time {}: {_err}",
16 path.display(),
17 );
18 return None;
19 }
20 };
21 last_modified_from_file(path, &file)
22}
23
24pub(crate) fn last_modified_from_file(
33 _path: &Path,
34 file: &File,
35) -> Option<Timestamp> {
36 let md = match file.metadata() {
37 Ok(md) => md,
38 Err(_err) => {
39 warn!(
40 "failed to get metadata (for last modified time) \
41 for {}: {_err}",
42 _path.display(),
43 );
44 return None;
45 }
46 };
47 let systime = match md.modified() {
48 Ok(systime) => systime,
49 Err(_err) => {
50 warn!(
51 "failed to get last modified time for {}: {_err}",
52 _path.display()
53 );
54 return None;
55 }
56 };
57 let timestamp = match Timestamp::try_from(systime) {
58 Ok(timestamp) => timestamp,
59 Err(_err) => {
60 warn!(
61 "system time {systime:?} out of bounds \
62 for Jiff timestamp for last modified time \
63 from {}: {_err}",
64 _path.display(),
65 );
66 return None;
67 }
68 };
69 Some(timestamp)
70}