1use indicatif::{ProgressBar, ProgressStyle};
2use log::info;
3use rayon::prelude::*;
4use std::cmp::Ordering;
5use std::collections::HashMap;
6use std::fs;
7use std::io::{self, Read};
8use std::path::{Path, PathBuf};
9use std::sync::{Arc, Mutex, mpsc};
10use walkdir::WalkDir;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Classification {
15 OnlyInDir1,
17 OnlyInDir2,
19 InBoth,
21}
22
23#[derive(Debug, Clone)]
25pub struct FileComparisonResult {
26 pub relative_path: PathBuf,
28 pub classification: Classification,
30 pub modified_time_comparison: Option<Ordering>,
32 pub size_comparison: Option<Ordering>,
34 pub is_content_same: Option<bool>,
36}
37
38impl FileComparisonResult {
39 pub fn new(relative_path: PathBuf, classification: Classification) -> Self {
40 Self {
41 relative_path,
42 classification,
43 modified_time_comparison: None,
44 size_comparison: None,
45 is_content_same: None,
46 }
47 }
48
49 pub fn is_identical(&self) -> bool {
50 self.classification == Classification::InBoth
51 && self.modified_time_comparison == Some(Ordering::Equal)
52 && self.size_comparison == Some(Ordering::Equal)
53 && self.is_content_same == Some(true)
54 }
55
56 pub fn to_string(&self, dir1_name: &str, dir2_name: &str) -> String {
57 let mut parts = Vec::new();
58 match self.classification {
59 Classification::OnlyInDir1 => parts.push(format!("Only in {}", dir1_name)),
60 Classification::OnlyInDir2 => parts.push(format!("Only in {}", dir2_name)),
61 Classification::InBoth => {}
62 }
63
64 if let Some(comp) = &self.modified_time_comparison {
65 match comp {
66 Ordering::Greater => parts.push(format!("{} is newer", dir1_name)),
67 Ordering::Less => parts.push(format!("{} is newer", dir2_name)),
68 Ordering::Equal => {}
69 }
70 }
71
72 if let Some(comp) = &self.size_comparison {
73 match comp {
74 Ordering::Greater => parts.push(format!("Size of {} is larger", dir1_name)),
75 Ordering::Less => parts.push(format!("Size of {} is larger", dir2_name)),
76 Ordering::Equal => {}
77 }
78 }
79
80 if let Some(same) = self.is_content_same
81 && !same
82 {
83 parts.push("Content differ".to_string());
84 }
85
86 format!("{}: {}", self.relative_path.display(), parts.join(", "))
87 }
88}
89
90#[derive(Default)]
91pub struct ComparisonSummary {
92 pub in_both: usize,
93 pub only_in_dir1: usize,
94 pub only_in_dir2: usize,
95 pub dir1_newer: usize,
96 pub dir2_newer: usize,
97 pub same_time_diff_size: usize,
98 pub same_time_size_diff_content: usize,
99}
100
101impl ComparisonSummary {
102 pub fn update(&mut self, result: &FileComparisonResult) {
103 match result.classification {
104 Classification::OnlyInDir1 => self.only_in_dir1 += 1,
105 Classification::OnlyInDir2 => self.only_in_dir2 += 1,
106 Classification::InBoth => {
107 self.in_both += 1;
108 match result.modified_time_comparison {
109 Some(Ordering::Greater) => self.dir1_newer += 1,
110 Some(Ordering::Less) => self.dir2_newer += 1,
111 _ => {
112 if result.size_comparison != Some(Ordering::Equal) {
113 self.same_time_diff_size += 1;
114 } else if result.is_content_same == Some(false) {
115 self.same_time_size_diff_content += 1;
116 }
117 }
118 }
119 }
120 }
121 }
122
123 pub fn print(&self, dir1_name: &str, dir2_name: &str) {
124 println!("Files in both: {}", self.in_both);
125 println!("Files only in {}: {}", dir1_name, self.only_in_dir1);
126 println!("Files only in {}: {}", dir2_name, self.only_in_dir2);
127 println!(
128 "Files in both ({} is newer): {}",
129 dir1_name, self.dir1_newer
130 );
131 println!(
132 "Files in both ({} is newer): {}",
133 dir2_name, self.dir2_newer
134 );
135 println!(
136 "Files in both (same time, different size): {}",
137 self.same_time_diff_size
138 );
139 println!(
140 "Files in both (same time and size, different content): {}",
141 self.same_time_size_diff_content
142 );
143 }
144}
145
146#[derive(Clone)]
148pub struct DirectoryComparer {
149 dir1: PathBuf,
150 dir2: PathBuf,
151 total_files: Arc<Mutex<usize>>,
152}
153
154impl DirectoryComparer {
155 pub fn new(dir1: PathBuf, dir2: PathBuf) -> Self {
157 Self {
158 dir1,
159 dir2,
160 total_files: Arc::new(Mutex::new(0)),
161 }
162 }
163
164 pub fn set_max_threads(parallel: usize) -> anyhow::Result<()> {
167 rayon::ThreadPoolBuilder::new()
168 .num_threads(parallel)
169 .build_global()
170 .map_err(|e| anyhow::anyhow!("Failed to initialize thread pool: {}", e))?;
171 Ok(())
172 }
173
174 pub fn run(&self) -> anyhow::Result<()> {
177 let pb = ProgressBar::new_spinner();
178 pb.enable_steady_tick(std::time::Duration::from_millis(120));
179 pb.set_style(
180 ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] {msg}").unwrap(),
181 );
182 pb.set_message("Scanning directories...");
183
184 let start_time = std::time::Instant::now();
185 let mut summary = ComparisonSummary::default();
186 let dir1_str = self.dir1.to_str().unwrap_or("dir1");
187 let dir2_str = self.dir2.to_str().unwrap_or("dir2");
188
189 let (tx, rx) = mpsc::channel();
190 let comparer = self.clone();
191
192 std::thread::scope(|s| {
193 s.spawn(move || {
194 if let Err(e) = comparer.compare_streaming(tx) {
195 eprintln!("Error during comparison: {}", e);
196 }
197 });
198
199 let mut length_set = false;
201 while let Ok(result) = rx.recv() {
202 if !length_set {
203 let total_files = *self.total_files.lock().unwrap();
204 if total_files > 0 {
205 pb.set_length(total_files as u64);
206 pb.set_style(
207 ProgressStyle::with_template(
208 "[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} ({percent}%) {msg}",
209 )
210 .unwrap(),
211 );
212 pb.set_message("");
213 length_set = true;
214 }
215 }
216 summary.update(&result);
217 if !result.is_identical() {
218 pb.suspend(|| {
219 println!("{}", result.to_string(dir1_str, dir2_str));
220 });
221 }
222 pb.inc(1);
223 }
224 });
225
226 pb.finish_and_clear();
227
228 eprintln!("\n--- Comparison Summary ---");
229 summary.print(dir1_str, dir2_str);
230 eprintln!("Comparison finished in {:?}.", start_time.elapsed());
231 Ok(())
232 }
233
234 fn get_files(dir: &Path) -> anyhow::Result<HashMap<PathBuf, PathBuf>> {
235 let mut files = HashMap::new();
236 for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
237 if entry.file_type().is_file() {
238 let rel_path = entry.path().strip_prefix(dir)?.to_path_buf();
239 files.insert(rel_path, entry.path().to_path_buf());
240 }
241 }
242 Ok(files)
243 }
244
245 fn compare_streaming(&self, tx: mpsc::Sender<FileComparisonResult>) -> anyhow::Result<()> {
250 let (dir1_files, dir2_files) = rayon::join(
251 || {
252 info!("Scanning directory: {:?}", self.dir1);
253 Self::get_files(&self.dir1)
254 },
255 || {
256 info!("Scanning directory: {:?}", self.dir2);
257 Self::get_files(&self.dir2)
258 },
259 );
260 let dir1_files = dir1_files?;
261 let dir2_files = dir2_files?;
262
263 let mut all_rel_paths: Vec<_> = dir1_files.keys().chain(dir2_files.keys()).collect();
264 all_rel_paths.sort();
265 all_rel_paths.dedup();
266
267 *self.total_files.lock().unwrap() = all_rel_paths.len();
268
269 all_rel_paths.into_par_iter().for_each(|rel_path| {
270 let in_dir1 = dir1_files.get(rel_path);
271 let in_dir2 = dir2_files.get(rel_path);
272
273 let result = match (in_dir1, in_dir2) {
274 (Some(_), None) => {
275 FileComparisonResult::new(rel_path.clone(), Classification::OnlyInDir1)
276 }
277 (None, Some(_)) => {
278 FileComparisonResult::new(rel_path.clone(), Classification::OnlyInDir2)
279 }
280 (Some(p1), Some(p2)) => {
281 let mut result =
282 FileComparisonResult::new(rel_path.clone(), Classification::InBoth);
283 let m1 = fs::metadata(p1).ok();
284 let m2 = fs::metadata(p2).ok();
285
286 if let (Some(m1), Some(m2)) = (m1, m2) {
287 let t1 = m1.modified().ok();
288 let t2 = m2.modified().ok();
289 if let (Some(t1), Some(t2)) = (t1, t2) {
290 result.modified_time_comparison = Some(t1.cmp(&t2));
291 }
292
293 let s1 = m1.len();
294 let s2 = m2.len();
295 result.size_comparison = Some(s1.cmp(&s2));
296
297 if s1 == s2 {
298 info!("Comparing content: {:?}", rel_path);
299 result.is_content_same =
300 Some(compare_contents(p1, p2).unwrap_or(false));
301 }
302 }
303 result
304 }
305 (None, None) => unreachable!(),
306 };
307 let _ = tx.send(result);
308 });
309
310 Ok(())
311 }
312}
313
314fn compare_contents(p1: &Path, p2: &Path) -> io::Result<bool> {
315 let mut f1 = fs::File::open(p1)?;
316 let mut f2 = fs::File::open(p2)?;
317
318 let mut buf1 = [0u8; 8192];
319 let mut buf2 = [0u8; 8192];
320
321 loop {
322 let n1 = f1.read(&mut buf1)?;
323 let n2 = f2.read(&mut buf2)?;
324
325 if n1 != n2 || buf1[..n1] != buf2[..n2] {
326 return Ok(false);
327 }
328
329 if n1 == 0 {
330 return Ok(true);
331 }
332 }
333}
334
335#[cfg(test)]
336mod tests {
337 use super::*;
338 use std::io::Write;
339 use tempfile::NamedTempFile;
340
341 #[test]
342 fn test_compare_contents_identical() -> io::Result<()> {
343 let mut f1 = NamedTempFile::new()?;
344 let mut f2 = NamedTempFile::new()?;
345 f1.write_all(b"hello world")?;
346 f2.write_all(b"hello world")?;
347 assert!(compare_contents(f1.path(), f2.path())?);
348 Ok(())
349 }
350
351 #[test]
352 fn test_compare_contents_different() -> io::Result<()> {
353 let mut f1 = NamedTempFile::new()?;
354 let mut f2 = NamedTempFile::new()?;
355 f1.write_all(b"hello world")?;
356 f2.write_all(b"hello rust")?;
357 assert!(!compare_contents(f1.path(), f2.path())?);
358 Ok(())
359 }
360
361 #[test]
362 fn test_compare_contents_different_size() -> io::Result<()> {
363 let mut f1 = NamedTempFile::new()?;
364 let mut f2 = NamedTempFile::new()?;
365 f1.write_all(b"hello world")?;
366 f2.write_all(b"hello")?;
367 assert!(!compare_contents(f1.path(), f2.path())?);
369 Ok(())
370 }
371
372 #[test]
373 fn test_comparison_summary() {
374 let mut summary = ComparisonSummary::default();
375 let res1 = FileComparisonResult::new(PathBuf::from("a"), Classification::OnlyInDir1);
376 let res2 = FileComparisonResult::new(PathBuf::from("b"), Classification::OnlyInDir2);
377 let mut res3 = FileComparisonResult::new(PathBuf::from("c"), Classification::InBoth);
378 res3.modified_time_comparison = Some(Ordering::Greater);
379
380 summary.update(&res1);
381 summary.update(&res2);
382 summary.update(&res3);
383
384 assert_eq!(summary.only_in_dir1, 1);
385 assert_eq!(summary.only_in_dir2, 1);
386 assert_eq!(summary.in_both, 1);
387 assert_eq!(summary.dir1_newer, 1);
388 }
389
390 #[test]
391 fn test_directory_comparer_integration() -> anyhow::Result<()> {
392 let dir1 = tempfile::tempdir()?;
393 let dir2 = tempfile::tempdir()?;
394
395 let file1_path = dir1.path().join("same.txt");
397 let mut file1 = fs::File::create(&file1_path)?;
398 file1.write_all(b"same content")?;
399
400 let only1_path = dir1.path().join("only1.txt");
401 let mut only1 = fs::File::create(&only1_path)?;
402 only1.write_all(b"only in dir1")?;
403
404 let file2_path = dir2.path().join("same.txt");
406 let mut file2 = fs::File::create(&file2_path)?;
407 file2.write_all(b"same content")?;
408
409 let only2_path = dir2.path().join("only2.txt");
410 let mut only2 = fs::File::create(&only2_path)?;
411 only2.write_all(b"only in dir2")?;
412
413 let diff1_path = dir1.path().join("diff.txt");
415 let mut diff1 = fs::File::create(&diff1_path)?;
416 diff1.write_all(b"content 1")?;
417
418 let diff2_path = dir2.path().join("diff.txt");
419 let mut diff2 = fs::File::create(&diff2_path)?;
420 diff2.write_all(b"content 222")?; let comparer = DirectoryComparer::new(dir1.path().to_path_buf(), dir2.path().to_path_buf());
423 let (tx, rx) = mpsc::channel();
424
425 comparer.compare_streaming(tx)?;
426
427 let mut results = Vec::new();
428 while let Ok(res) = rx.recv() {
429 results.push(res);
430 }
431
432 results.sort_by(|a, b| a.relative_path.cmp(&b.relative_path));
433
434 assert_eq!(results.len(), 4);
435
436 assert_eq!(results[0].relative_path.to_str().unwrap(), "diff.txt");
438 assert_eq!(results[0].classification, Classification::InBoth);
439 assert!(
440 results[0].is_content_same == Some(false)
441 || results[0].size_comparison != Some(Ordering::Equal)
442 );
443
444 assert_eq!(results[1].relative_path.to_str().unwrap(), "only1.txt");
446 assert_eq!(results[1].classification, Classification::OnlyInDir1);
447
448 assert_eq!(results[2].relative_path.to_str().unwrap(), "only2.txt");
450 assert_eq!(results[2].classification, Classification::OnlyInDir2);
451
452 assert_eq!(results[3].relative_path.to_str().unwrap(), "same.txt");
454 assert_eq!(results[3].classification, Classification::InBoth);
455 assert_eq!(results[3].size_comparison, Some(Ordering::Equal));
456
457 Ok(())
458 }
459}