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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
mod os;
mod stat;
use std::collections::HashMap;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use lazy_static::lazy_static;
const BUFSIZE: usize = 8 * 1024;
const FOLLOW_SYMLINKS_DEFAULT: bool = true;
const MAX_CACHE_SIZE: usize = 100;
lazy_static! {
static ref CACHE: Arc<Mutex<HashMap<(PathBuf, PathBuf, Signature, Signature), bool>>> = Arc::new(Mutex::new(HashMap::new()));
}
pub fn clear_cache() {
CACHE.lock().unwrap().clear();
}
pub fn cmp(f1: impl AsRef<Path>, f2: impl AsRef<Path>, shallow: bool) -> io::Result<bool> {
let s1 = sig(os::stat(f1.as_ref(), FOLLOW_SYMLINKS_DEFAULT)?);
let s2 = sig(os::stat(f2.as_ref(), FOLLOW_SYMLINKS_DEFAULT)?);
if s1.s_ifmt != stat::S_IFREG || s2.s_ifmt != stat::S_IFREG {
return Ok(false);
}
if shallow && s1 == s2 {
return Ok(true);
}
if s1.st_size != s2.st_size {
return Ok(false);
}
let key = (f1.as_ref().into(), f2.as_ref().into(), s1, s2);
let c_cache = Arc::clone(&CACHE);
let outcome = c_cache.lock().unwrap().get(&key).copied();
let outcome = if let Some(outcome) = outcome {
outcome
} else {
let outcome = do_cmp(f1, f2)?;
if c_cache.lock().unwrap().len() > MAX_CACHE_SIZE {
clear_cache();
}
c_cache.lock().unwrap().insert(key, outcome);
outcome
};
Ok(outcome)
}
pub fn cmpfiles<A, B, C, D>(
_dir1: A,
_dir2: B,
_common: D,
_shallow: bool,
) -> io::Result<(Vec<PathBuf>, Vec<PathBuf>, Vec<PathBuf>)>
where
A: AsRef<Path>,
B: AsRef<Path>,
C: AsRef<Path>,
D: AsRef<[C]>,
{
unimplemented!()
}
pub struct DirCmp;
impl DirCmp {
pub fn new(_a: impl AsRef<Path>, _b: impl AsRef<Path>) -> Self {
unimplemented!()
}
pub fn report_full_closure(&self) {
unimplemented!()
}
pub fn report(&self) {
unimplemented!()
}
}
fn sig(st: os::StatResult) -> Signature {
Signature {
s_ifmt: stat::S_IFMT(st.st_mode),
st_size: st.st_size,
st_mtime: st.st_mtime,
}
}
fn do_cmp(f1: impl AsRef<Path>, f2: impl AsRef<Path>) -> io::Result<bool> {
let mut f1 = File::open(f1.as_ref())?;
let mut f2 = File::open(f2.as_ref())?;
loop {
let mut buf1: [u8; BUFSIZE] = [0; BUFSIZE];
let mut buf2: [u8; BUFSIZE] = [0; BUFSIZE];
let len1 = f1.read(&mut buf1)?;
let len2 = f2.read(&mut buf2)?;
if len1 != len2 {
return Ok(false);
}
let read_size = len1;
if read_size == 0 {
return Ok(true);
}
if &buf1[..read_size] != &buf2[..read_size] {
return Ok(false);
}
}
}
#[derive(Debug)]
struct Signature {
s_ifmt: u32,
st_size: u64,
st_mtime: f64,
}
impl Signature {
fn canonicalize(&self) -> (u32, u64, [u8; 8]) {
(self.s_ifmt, self.st_size, self.st_mtime.to_ne_bytes())
}
}
impl PartialEq for Signature {
fn eq(&self, other: &Self) -> bool {
self.canonicalize() == other.canonicalize()
}
}
impl Eq for Signature {}
impl Hash for Signature {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.canonicalize().hash(state);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
use std::fs::{self, File};
use std::io::Write;
#[test]
fn test_stat() {
let temp_dir = env::temp_dir();
let test_dir = dbg!(temp_dir.join("test_filecmp").join("test_stat"));
if !test_dir.exists() {
fs::create_dir_all(&test_dir).unwrap();
}
assert!(
test_dir.is_dir(),
"Test directory {} must be an existed folder",
test_dir.display()
);
let foo_path = test_dir.join("foo.txt");
let bar_path = test_dir.join("bar.txt");
let baz_path = test_dir.join("baz.txt");
let mut foo = File::create(&foo_path).unwrap();
let mut bar = File::create(&bar_path).unwrap();
let mut baz = File::create(&baz_path).unwrap();
let buf_digit: &[u8; 10] = b"0123456789";
let buf_alphabet: &[u8; 6] = b"abcdeg";
let buf: &[u8] = &[&buf_digit[..], &buf_alphabet[..]].concat();
foo.write(buf).unwrap();
bar.write(buf).unwrap();
baz.write(buf_digit).unwrap();
let shallow = true;
assert!(cmp(&foo_path, &foo_path, shallow).unwrap());
assert!(cmp(&bar_path, &bar_path, shallow).unwrap());
assert!(cmp(&baz_path, &baz_path, shallow).unwrap());
assert!(cmp(&foo_path, &bar_path, shallow).unwrap());
assert!(!cmp(&foo_path, &baz_path, shallow).unwrap());
assert!(!cmp(&bar_path, &baz_path, shallow).unwrap());
let shallow = false;
assert!(cmp(&foo_path, &foo_path, shallow).unwrap());
assert!(cmp(&bar_path, &bar_path, shallow).unwrap());
assert!(cmp(&baz_path, &baz_path, shallow).unwrap());
assert!(cmp(&foo_path, &bar_path, shallow).unwrap());
assert!(!cmp(&foo_path, &baz_path, shallow).unwrap());
assert!(!cmp(&bar_path, &baz_path, shallow).unwrap());
}
}