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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::path::{Path, PathBuf};
use generic_array::{GenericArray};
use error::SyncError;
use NumRoots;
use config::SyncInfo;
use archive::{Archive,ArchiveEntries};
use util::FnvHashMap;
use state::ArchiveEntryPerReplica;
use detect::util::*;
use detect::ext::is_item_in_sync;
mod ext;
mod util;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Difference<N: NumRoots> {
pub path: PathBuf,
pub roots: GenericArray<PathBuf, N>,
pub previous_state: Option<GenericArray<ArchiveEntryPerReplica, N>>,
pub current_state: GenericArray<ArchiveEntryPerReplica, N>
}
impl<N: NumRoots> Difference<N> {
pub fn absolute_path_for_root(&self, index: usize) -> PathBuf {
self.roots[index].join(&self.path)
}
}
pub struct DetectionResult<N: NumRoots> {
pub differences: Vec<Difference<N>>,
pub statistics: DetectionStatistics
}
impl<N: NumRoots> DetectionResult<N> {
fn new() -> Self {
DetectionResult {
differences: Vec::new(),
statistics: DetectionStatistics::new()
}
}
fn add_difference(&mut self, conflict: Difference<N>) {
let mut add = true;
self.differences.retain(|other| {
if other.path.starts_with(&conflict.path) {
debug!("Removing nested conflict at {:?}", other.path);
false
} else if conflict.path.starts_with(&other.path) {
debug!("Not adding nested conflict at {:?}", conflict.path);
add = false;
true
} else {
true
}
});
if add {
self.differences.push(conflict);
}
}
}
#[derive(Debug, Clone)]
pub struct SearchDirectories {
pub directories: Vec<PathBuf>,
pub recurse: bool
}
impl SearchDirectories {
pub fn from_root() -> SearchDirectories {
SearchDirectories {
directories: vec![Path::new("").to_path_buf()],
recurse: true
}
}
pub fn new(directories: Vec<PathBuf>, recurse: bool) -> Self {
SearchDirectories {
directories: directories,
recurse: recurse
}
}
}
#[derive(Debug)]
pub struct DetectionStatistics {
pub archive_hits: usize,
pub archive_additions: usize
}
impl DetectionStatistics {
pub fn new() -> Self {
DetectionStatistics {
archive_hits: 0,
archive_additions: 0_
}
}
}
pub trait ProgressCallback {
fn reading_directory(&self, path: &Path, checked: usize, remaining: usize);
}
pub struct EmptyProgressCallback;
impl ProgressCallback for EmptyProgressCallback {
fn reading_directory(&self, _: &Path, _: usize, _: usize) {}
}
pub fn find_updates<N, P>(archive: &Archive, search: &mut SearchDirectories, config: &SyncInfo<N>, progress_callback: &P) -> Result<DetectionResult<N>, SyncError> where N: NumRoots, P: ProgressCallback {
let mut current_entries: FnvHashMap<PathBuf, GenericArray<ArchiveEntryPerReplica, N>> = Default::default();
let mut result = DetectionResult::new();
let mut read_directories = 0;
check_all_roots_exist(config.roots.iter())?;
search.directories.retain(|dir| !is_ignored(&config.ignore, &dir));
loop {
current_entries.clear();
let sd = match search.directories.pop() {
Some(d) => d,
None => break
};
if sd.is_absolute() {
return Err(SyncError::AbsolutePathProvided(sd));
}
debug!("Reading dir {:?}", sd);
progress_callback.reading_directory(&sd, read_directories, search.directories.len());
read_directories += 1;
let mut sd_archive_file = archive.for_directory(&sd);
let mut sd_archive_entries: ArchiveEntries<N> = sd_archive_file.read()?.into();
scan_directory_contents(&sd, &mut current_entries, config)?;
debug!("Analysing items in {:?}", sd);
for (path, current_entry) in current_entries.iter_mut() {
let mut keep_checking = true;
if let Some(archive_entry) = sd_archive_entries.get(path) {
trace!("Checking archive files");
if are_archive_files_identical(archive_entry, current_entry) {
result.statistics.archive_hits += 1;
keep_checking = false;
}
}
if keep_checking {
if is_item_in_sync(path, current_entry, config.compare_file_contents, &config.roots)? {
sd_archive_entries.insert(path, current_entry.clone());
result.statistics.archive_additions += 1;
} else {
let difference = Difference {
path: path.to_path_buf(),
roots: config.roots.clone(),
previous_state: sd_archive_entries.get(path).cloned(),
current_state: current_entry.clone()
};
result.add_difference(difference);
continue;
}
}
if let Some(root) = config.roots.last() {
if search.recurse && root.join(path).is_dir() {
search.directories.push(path.clone());
}
}
}
if sd_archive_entries.is_dirty() {
sd_archive_file.write(&mut sd_archive_entries)?;
}
}
Ok(result)
}