dir_diff/
lib.rs

1//! Determine if two directories have different contents.
2//!
3//! For now, only one function exists: are they different, or not? In the future,
4//! more functionality to actually determine the difference may be added.
5//!
6//! # Examples
7//!
8//! ```no_run
9//! extern crate dir_diff;
10//!
11//! assert!(dir_diff::is_different("dir/a", "dir/b").unwrap());
12//! ```
13
14#![cfg_attr(docsrs, feature(doc_auto_cfg))]
15
16extern crate walkdir;
17
18use std::cmp::Ordering;
19use std::fs::File;
20use std::io::prelude::*;
21use std::path::Path;
22
23use walkdir::{DirEntry, WalkDir};
24
25/// The various errors that can happen when diffing two directories
26#[derive(Debug)]
27pub enum Error {
28    Io(std::io::Error),
29    StripPrefix(std::path::StripPrefixError),
30    WalkDir(walkdir::Error),
31}
32
33impl std::fmt::Display for Error {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        match self {
36            Error::Io(inner) => write!(f, "I/O error: {}", inner),
37            Error::StripPrefix(inner) => write!(f, "Strip prefix error: {}", inner),
38            Error::WalkDir(inner) => write!(f, "Walk dir error: {}", inner),
39        }
40    }
41}
42
43impl std::error::Error for Error {}
44
45/// Are the contents of two directories different?
46///
47/// # Examples
48///
49/// ```no_run
50/// extern crate dir_diff;
51///
52/// assert!(dir_diff::is_different("dir/a", "dir/b").unwrap());
53/// ```
54pub fn is_different<A: AsRef<Path>, B: AsRef<Path>>(a_base: A, b_base: B) -> Result<bool, Error> {
55    let mut a_walker = walk_dir(a_base)?;
56    let mut b_walker = walk_dir(b_base)?;
57
58    for (a, b) in (&mut a_walker).zip(&mut b_walker) {
59        let a = a?;
60        let b = b?;
61
62        if a.depth() != b.depth()
63            || a.file_type() != b.file_type()
64            || a.file_name() != b.file_name()
65            || (a.file_type().is_file() && read_to_vec(a.path())? != read_to_vec(b.path())?)
66        {
67            return Ok(true);
68        }
69    }
70
71    Ok(a_walker.next().is_some() || b_walker.next().is_some())
72}
73
74fn walk_dir<P: AsRef<Path>>(path: P) -> Result<walkdir::IntoIter, std::io::Error> {
75    let mut walkdir = WalkDir::new(path).sort_by(compare_by_file_name).into_iter();
76    if let Some(Err(e)) = walkdir.next() {
77        Err(e.into())
78    } else {
79        Ok(walkdir)
80    }
81}
82
83fn compare_by_file_name(a: &DirEntry, b: &DirEntry) -> Ordering {
84    a.file_name().cmp(b.file_name())
85}
86
87fn read_to_vec<P: AsRef<Path>>(file: P) -> Result<Vec<u8>, std::io::Error> {
88    let mut data = Vec::new();
89    let mut file = File::open(file.as_ref())?;
90
91    file.read_to_end(&mut data)?;
92
93    Ok(data)
94}
95
96impl From<std::io::Error> for Error {
97    fn from(e: std::io::Error) -> Error {
98        Error::Io(e)
99    }
100}
101
102impl From<std::path::StripPrefixError> for Error {
103    fn from(e: std::path::StripPrefixError) -> Error {
104        Error::StripPrefix(e)
105    }
106}
107
108impl From<walkdir::Error> for Error {
109    fn from(e: walkdir::Error) -> Error {
110        Error::WalkDir(e)
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn test_display() {
120        use std::io::ErrorKind;
121
122        assert_eq!(
123            format!(
124                "{}",
125                Error::Io(std::io::Error::new(ErrorKind::Other, "oh no!"))
126            ),
127            "I/O error: oh no!"
128        );
129    }
130}