1 | use std::{fs::File, path::Path}; |
2 | |
3 | use crate::Timestamp; |
4 | |
5 | /// Returns the last modified time for the given file path as a Jiff timestamp. |
6 | /// |
7 | /// If there was a problem accessing the last modified time or if it could not |
8 | /// fit in a Jiff timestamp, then a warning message is logged and `None` is |
9 | /// returned. |
10 | pub(crate) fn last_modified_from_path(path: &Path) -> Option<Timestamp> { |
11 | let file: File = match File::open(path) { |
12 | Ok(file: File) => file, |
13 | Err(_err: Error) => { |
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 | |
24 | /// Returns the last modified time for the given file as a Jiff timestamp. |
25 | /// |
26 | /// If there was a problem accessing the last modified time or if it could not |
27 | /// fit in a Jiff timestamp, then a warning message is logged and `None` is |
28 | /// returned. |
29 | /// |
30 | /// The path given should be the path to the given file. It is used for |
31 | /// diagnostic purposes. |
32 | pub(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 | } |
71 | |