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
use super::sync::SyncData;
use std::{collections::BTreeMap, fs::metadata, path::PathBuf, time::UNIX_EPOCH};
/// Implementation for finding the timestamp of a file.
impl SyncData {
/// Finds the last modified file and it's timestamp
///
/// Takes:
/// - List of files
/// - Directory name to trim from
///
/// Returns:
/// - Actual file which is modified at the end
/// - Float timestamp of that file
///
/// # Example
///
/// ```rust,no_run
/// use cover_files::sync::sync::SyncData;
/// use std::path::PathBuf;
///
/// let sync = SyncData {
/// source: PathBuf::from("source_directory"),
/// destination: PathBuf::from("destination_directory"),
/// changed_only: true,
/// delete: false,
/// verbose: false,
/// dry_run: false,
/// };
///
/// let src_files_list = sync.list_src_files();
/// let dest_files_list = sync.list_dest_files();
///
/// let src_timestamp = sync.file_timestamp(src_files_list.clone(), &sync.source);
/// let dest_timestamp = sync.file_timestamp(dest_files_list.clone(), &sync.destination);
///
/// for (path, dest_time) in &dest_timestamp {
/// let src = src_timestamp.get(path);
/// match src {
/// Some(src_time) => {
/// assert!(*src_time != 0.0 && *dest_time != 0.0);
/// }
/// None => {}
/// }
/// }
/// ```
pub fn file_timestamp(&self, files: Vec<PathBuf>, trim: &PathBuf) -> BTreeMap<PathBuf, f64> {
let mut map: BTreeMap<PathBuf, f64> = BTreeMap::new();
for entry in files {
let num = metadata(&entry)
.ok()
.and_then(|f| f.modified().ok())
.and_then(|f| f.duration_since(UNIX_EPOCH).ok())
.map(|f| f.as_secs() as f64)
.unwrap_or(0.0);
let file = entry
.clone()
.strip_prefix(trim)
.expect("[ERROR]: failed to get the prefix")
.to_path_buf();
map.insert(file, num);
}
map
}
}