Skip to main content

compare_dir/
dir_comparer.rs

1use crate::file_comparer::{Classification, FileComparer, FileComparisonResult};
2use indicatif::{ProgressBar, ProgressStyle};
3
4use std::cmp::Ordering;
5use std::collections::HashMap;
6use std::path::{Path, PathBuf};
7use std::sync::mpsc;
8use walkdir::WalkDir;
9
10#[derive(Debug, Clone)]
11enum CompareProgress {
12    StartOfComparison,
13    TotalFiles(usize),
14    Result(usize, FileComparisonResult),
15}
16
17#[derive(Default)]
18struct ComparisonSummary {
19    pub in_both: usize,
20    pub only_in_dir1: usize,
21    pub only_in_dir2: usize,
22    pub dir1_newer: usize,
23    pub dir2_newer: usize,
24    pub same_time_diff_size: usize,
25    pub same_time_size_diff_content: usize,
26}
27
28impl ComparisonSummary {
29    pub fn update(&mut self, result: &FileComparisonResult) {
30        match result.classification {
31            Classification::OnlyInDir1 => self.only_in_dir1 += 1,
32            Classification::OnlyInDir2 => self.only_in_dir2 += 1,
33            Classification::InBoth => {
34                self.in_both += 1;
35                match result.modified_time_comparison {
36                    Some(Ordering::Greater) => self.dir1_newer += 1,
37                    Some(Ordering::Less) => self.dir2_newer += 1,
38                    _ => {
39                        if result.size_comparison != Some(Ordering::Equal) {
40                            self.same_time_diff_size += 1;
41                        } else if result.is_content_same == Some(false) {
42                            self.same_time_size_diff_content += 1;
43                        }
44                    }
45                }
46            }
47        }
48    }
49
50    pub fn print(&self, dir1_name: &str, dir2_name: &str) {
51        println!("Files in both: {}", self.in_both);
52        println!("Files only in {}: {}", dir1_name, self.only_in_dir1);
53        println!("Files only in {}: {}", dir2_name, self.only_in_dir2);
54        println!(
55            "Files in both ({} is newer): {}",
56            dir1_name, self.dir1_newer
57        );
58        println!(
59            "Files in both ({} is newer): {}",
60            dir2_name, self.dir2_newer
61        );
62        println!(
63            "Files in both (same time, different size): {}",
64            self.same_time_diff_size
65        );
66        println!(
67            "Files in both (same time and size, different content): {}",
68            self.same_time_size_diff_content
69        );
70    }
71}
72
73/// A tool for comparing the contents of two directories.
74pub struct DirectoryComparer {
75    dir1: PathBuf,
76    dir2: PathBuf,
77    pub buffer_size: usize,
78}
79
80impl DirectoryComparer {
81    /// Creates a new `DirectoryComparer` for the two given directories.
82    pub fn new(dir1: PathBuf, dir2: PathBuf) -> Self {
83        Self {
84            dir1,
85            dir2,
86            buffer_size: FileComparer::DEFAULT_BUFFER_SIZE,
87        }
88    }
89
90    /// Sets the maximum number of threads for parallel processing.
91    /// This initializes the global Rayon thread pool.
92    pub fn set_max_threads(parallel: usize) -> anyhow::Result<()> {
93        rayon::ThreadPoolBuilder::new()
94            .num_threads(parallel)
95            .build_global()
96            .map_err(|e| anyhow::anyhow!("Failed to initialize thread pool: {}", e))?;
97        Ok(())
98    }
99
100    /// Executes the directory comparison and prints results to stdout.
101    /// This is a convenience method for CLI usage.
102    pub fn run(&self) -> anyhow::Result<()> {
103        let progress = ProgressBar::new_spinner();
104        progress.enable_steady_tick(std::time::Duration::from_millis(120));
105        progress.set_style(
106            ProgressStyle::with_template("[{elapsed_precise}] {spinner:.green} {msg}").unwrap(),
107        );
108        progress.set_message("Scanning directories...");
109
110        let start_time = std::time::Instant::now();
111        let mut summary = ComparisonSummary::default();
112        let dir1_str = self.dir1.to_str().unwrap_or("dir1");
113        let dir2_str = self.dir2.to_str().unwrap_or("dir2");
114
115        let (tx, rx) = mpsc::channel();
116
117        std::thread::scope(|scope| {
118            scope.spawn(move || {
119                if let Err(e) = self.compare_streaming(tx) {
120                    log::error!("Error during comparison: {}", e);
121                }
122            });
123
124            // Receive results and update summary/UI
125            while let Ok(event) = rx.recv() {
126                match event {
127                    CompareProgress::StartOfComparison => {
128                        progress.set_message("Comparing files...");
129                    }
130                    CompareProgress::TotalFiles(total_files) => {
131                        progress.set_length(total_files as u64);
132                        progress.set_style(
133                            ProgressStyle::with_template(
134                                "[{elapsed_precise}] {bar:40.cyan/blue} {percent}% {pos:>7}/{len:7} {msg}",
135                            )
136                            .unwrap(),
137                        );
138                        progress.set_message("");
139                    }
140                    CompareProgress::Result(_, result) => {
141                        summary.update(&result);
142                        if !result.is_identical() {
143                            progress.suspend(|| {
144                                println!("{}", result.to_string(dir1_str, dir2_str));
145                            });
146                        }
147                        progress.inc(1);
148                    }
149                }
150            }
151        });
152
153        progress.finish();
154
155        eprintln!("\n--- Comparison Summary ---");
156        summary.print(dir1_str, dir2_str);
157        eprintln!("Comparison finished in {:?}.", start_time.elapsed());
158        Ok(())
159    }
160
161    fn get_next_file(it: &mut walkdir::IntoIter, dir: &Path) -> Option<(PathBuf, PathBuf)> {
162        for entry in it.filter_map(|e| e.ok()) {
163            if entry.file_type().is_file()
164                && let Ok(rel_path) = entry.path().strip_prefix(dir)
165            {
166                return Some((rel_path.to_path_buf(), entry.path().to_path_buf()));
167            }
168        }
169        None
170    }
171
172    /// Performs the directory comparison and streams results via a channel.
173    ///
174    /// # Arguments
175    /// * `tx` - A sender to transmit `FileComparisonResult` as they are computed.
176    fn compare_streaming(&self, tx: mpsc::Sender<CompareProgress>) -> anyhow::Result<()> {
177        let (tx_unordered, rx_unordered) = mpsc::channel();
178        std::thread::scope(|scope| {
179            scope.spawn(move || {
180                if let Err(e) = self.compare_unordered_streaming(tx_unordered) {
181                    log::error!("Error during unordered comparison: {}", e);
182                }
183            });
184
185            let mut buffer = HashMap::new();
186            let mut next_index = 0;
187
188            for event in rx_unordered {
189                if let CompareProgress::Result(i, _) = &event {
190                    let index = *i;
191                    if index == next_index {
192                        tx.send(event)?;
193                        next_index += 1;
194                        while let Some(buffered) = buffer.remove(&next_index) {
195                            tx.send(buffered)?;
196                            next_index += 1;
197                        }
198                    } else {
199                        buffer.insert(index, event);
200                    }
201                } else {
202                    tx.send(event)?;
203                }
204            }
205            Ok::<(), anyhow::Error>(())
206        })?;
207
208        Ok(())
209    }
210
211    fn compare_unordered_streaming(&self, tx: mpsc::Sender<CompareProgress>) -> anyhow::Result<()> {
212        log::info!("Scanning directory: {:?}", self.dir1);
213        let mut it1 = WalkDir::new(&self.dir1).sort_by_file_name().into_iter();
214        log::info!("Scanning directory: {:?}", self.dir2);
215        let mut it2 = WalkDir::new(&self.dir2).sort_by_file_name().into_iter();
216        let mut next1 = Self::get_next_file(&mut it1, &self.dir1);
217        let mut next2 = Self::get_next_file(&mut it2, &self.dir2);
218        let mut index = 0;
219        tx.send(CompareProgress::StartOfComparison)?;
220        rayon::scope(|scope| {
221            loop {
222                let cmp = match (&next1, &next2) {
223                    (Some((rel1, _)), Some((rel2, _))) => rel1.cmp(rel2),
224                    (Some(_), None) => Ordering::Less,
225                    (None, Some(_)) => Ordering::Greater,
226                    (None, None) => break,
227                };
228
229                match cmp {
230                    Ordering::Less => {
231                        let (rel1, _) = next1.take().unwrap();
232                        let result = FileComparisonResult::new(rel1, Classification::OnlyInDir1);
233                        tx.send(CompareProgress::Result(index, result))?;
234                        index += 1;
235                        next1 = Self::get_next_file(&mut it1, &self.dir1);
236                    }
237                    Ordering::Greater => {
238                        let (rel2, _) = next2.take().unwrap();
239                        let result = FileComparisonResult::new(rel2, Classification::OnlyInDir2);
240                        tx.send(CompareProgress::Result(index, result))?;
241                        index += 1;
242                        next2 = Self::get_next_file(&mut it2, &self.dir2);
243                    }
244                    Ordering::Equal => {
245                        let (rel_path, path1) = next1.take().unwrap();
246                        let (_, path2) = next2.take().unwrap();
247
248                        let mut result =
249                            FileComparisonResult::new(rel_path.clone(), Classification::InBoth);
250                        let buffer_size = self.buffer_size;
251                        let tx_clone = tx.clone();
252                        let i = index;
253                        scope.spawn(move |_| {
254                            let mut comparer = FileComparer::new(&path1, &path2);
255                            comparer.buffer_size = buffer_size;
256                            if let Err(error) = result.update(&comparer) {
257                                log::error!("Error during comparison of {:?}: {}", rel_path, error);
258                            }
259                            if tx_clone.send(CompareProgress::Result(i, result)).is_err() {
260                                log::error!(
261                                    "Receiver dropped, stopping comparison of {:?}",
262                                    rel_path
263                                );
264                            }
265                        });
266                        index += 1;
267                        next1 = Self::get_next_file(&mut it1, &self.dir1);
268                        next2 = Self::get_next_file(&mut it2, &self.dir2);
269                    }
270                }
271            }
272
273            tx.send(CompareProgress::TotalFiles(index))
274        })?;
275
276        Ok(())
277    }
278}
279
280#[cfg(test)]
281mod tests {
282    use super::*;
283    use std::fs;
284    use std::io::Write;
285
286    #[test]
287    fn test_comparison_summary() {
288        let mut summary = ComparisonSummary::default();
289        let res1 = FileComparisonResult::new(PathBuf::from("a"), Classification::OnlyInDir1);
290        let res2 = FileComparisonResult::new(PathBuf::from("b"), Classification::OnlyInDir2);
291        let mut res3 = FileComparisonResult::new(PathBuf::from("c"), Classification::InBoth);
292        res3.modified_time_comparison = Some(Ordering::Greater);
293
294        summary.update(&res1);
295        summary.update(&res2);
296        summary.update(&res3);
297
298        assert_eq!(summary.only_in_dir1, 1);
299        assert_eq!(summary.only_in_dir2, 1);
300        assert_eq!(summary.in_both, 1);
301        assert_eq!(summary.dir1_newer, 1);
302    }
303
304    #[test]
305    fn test_directory_comparer_integration() -> anyhow::Result<()> {
306        let dir1 = tempfile::tempdir()?;
307        let dir2 = tempfile::tempdir()?;
308
309        // Create files in dir1
310        let file1_path = dir1.path().join("same.txt");
311        let mut file1 = fs::File::create(&file1_path)?;
312        file1.write_all(b"same content")?;
313
314        let only1_path = dir1.path().join("only1.txt");
315        let mut only1 = fs::File::create(&only1_path)?;
316        only1.write_all(b"only in dir1")?;
317
318        // Create files in dir2
319        let file2_path = dir2.path().join("same.txt");
320        let mut file2 = fs::File::create(&file2_path)?;
321        file2.write_all(b"same content")?;
322
323        let only2_path = dir2.path().join("only2.txt");
324        let mut only2 = fs::File::create(&only2_path)?;
325        only2.write_all(b"only in dir2")?;
326
327        // Create a different file
328        let diff1_path = dir1.path().join("diff.txt");
329        let mut diff1 = fs::File::create(&diff1_path)?;
330        diff1.write_all(b"content 1")?;
331
332        let diff2_path = dir2.path().join("diff.txt");
333        let mut diff2 = fs::File::create(&diff2_path)?;
334        diff2.write_all(b"content 222")?; // different length and content
335
336        let comparer = DirectoryComparer::new(dir1.path().to_path_buf(), dir2.path().to_path_buf());
337        let (tx, rx) = mpsc::channel();
338
339        comparer.compare_streaming(tx)?;
340
341        let mut results = Vec::new();
342        while let Ok(res) = rx.recv() {
343            if let CompareProgress::Result(_, r) = res {
344                results.push(r);
345            }
346        }
347
348        results.sort_by(|a, b| a.relative_path.cmp(&b.relative_path));
349
350        assert_eq!(results.len(), 4);
351
352        // diff.txt
353        assert_eq!(results[0].relative_path.to_str().unwrap(), "diff.txt");
354        assert_eq!(results[0].classification, Classification::InBoth);
355        assert!(
356            results[0].is_content_same == Some(false)
357                || results[0].size_comparison != Some(Ordering::Equal)
358        );
359
360        // only1.txt
361        assert_eq!(results[1].relative_path.to_str().unwrap(), "only1.txt");
362        assert_eq!(results[1].classification, Classification::OnlyInDir1);
363
364        // only2.txt
365        assert_eq!(results[2].relative_path.to_str().unwrap(), "only2.txt");
366        assert_eq!(results[2].classification, Classification::OnlyInDir2);
367
368        // same.txt
369        assert_eq!(results[3].relative_path.to_str().unwrap(), "same.txt");
370        assert_eq!(results[3].classification, Classification::InBoth);
371        assert_eq!(results[3].size_comparison, Some(Ordering::Equal));
372
373        Ok(())
374    }
375}