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
use std::fs;
use std::io;
use std::io::prelude::*;
use std::path::Path;
const SEPARATOR: &str = "---------------------------------";
const BUF_SIZE: usize = 4096;
pub mod args;
pub mod comparison_report;
mod entries_cmp;
type ComparisonReport = comparison_report::ComparisonReport;
#[derive(Debug)]
pub enum Error {
ArgsError(args::Error),
IoError(io::Error),
}
impl From<args::Error> for Error {
fn from(e: args::Error) -> Self {
Error::ArgsError(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::IoError(e)
}
}
pub fn are_files_equal<P, Q>(ref_file: P, cmp_file: Q) -> Result<bool, io::Error>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
let mut ref_file = fs::File::open(ref_file)?;
let mut cmp_file = fs::File::open(cmp_file)?;
let mut ref_buf = [0; BUF_SIZE];
let mut cmp_buf = [0; BUF_SIZE];
Ok(loop {
let ref_read = ref_file.read(&mut ref_buf)?;
let cmp_read = cmp_file.read(&mut cmp_buf)?;
if ref_read == 0 || cmp_read == 0 {
break true;
} else if ref_read != cmp_read || ref_buf[..ref_read] != cmp_buf[..cmp_read] {
break false;
}
})
}
pub fn compare_folders<P, Q>(ref_folder: P, cmp_folder: Q) -> Result<ComparisonReport, io::Error>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
let ref_content_iter = fs::read_dir(&ref_folder)?;
let cmp_content_iter = fs::read_dir(&cmp_folder)?;
let mut ref_content = Vec::new();
let mut cmp_content = Vec::new();
for e in ref_content_iter {
ref_content.push(e?);
}
for e in cmp_content_iter {
cmp_content.push(e?);
}
let mut comparison = ComparisonReport {
absent_in_cmp: entries_cmp::get_absent_entries(&ref_content, &cmp_content)?,
absent_in_ref: entries_cmp::get_absent_entries(&cmp_content, &ref_content)?,
different_files: Vec::new(),
};
let present_entries = entries_cmp::get_present_entries(&ref_content, &cmp_content)?;
for (file_type, file_name) in present_entries {
let mut ref_path = ref_folder.as_ref().to_path_buf();
let mut cmp_path = cmp_folder.as_ref().to_path_buf();
ref_path.push(&file_name);
cmp_path.push(&file_name);
if let entries_cmp::SameEntries::SameDirs = file_type {
comparison.append(compare_folders(ref_path, cmp_path)?);
} else if let entries_cmp::SameEntries::SameFiles = file_type {
if !are_files_equal(&ref_path, cmp_path)? {
comparison.different_files.push(ref_path);
}
}
}
Ok(comparison)
}
pub fn run(args: &args::Args) -> Result<(), io::Error> {
println!("{}", SEPARATOR);
println!("Folder A : '{}'", args.ref_folder);
println!("Folder B : '{}'", args.cmp_folder);
let ref_folder = Path::new(args.ref_folder.as_str());
let cmp_folder = Path::new(args.cmp_folder.as_str());
let res = compare_folders(ref_folder, cmp_folder)?;
println!("{}", res);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {}
}