Skip to main content

compare_dir/
lib.rs

1mod dir_comparer;
2mod file_comparer;
3mod file_hash_cache;
4mod file_hasher;
5mod file_iterator;
6mod progress;
7
8pub use dir_comparer::{DirectoryComparer, FileComparisonMethod};
9pub use file_comparer::{Classification, FileComparer, FileComparisonResult};
10pub(crate) use file_hash_cache::FileHashCache;
11pub use file_hasher::{DuplicatedFiles, FileHasher};
12pub(crate) use file_iterator::FileIterator;
13pub(crate) use progress::Progress;
14pub use progress::ProgressBuilder;
15
16use std::path::{Path, StripPrefixError};
17
18pub(crate) fn human_readable_size(size: u64) -> String {
19    const KB: u64 = 1024;
20    if size < KB {
21        return format!("{} bytes", size);
22    }
23    const KB_AS_F: f64 = KB as f64;
24    let mut size = size as f64;
25    for unit in ["KB", "MB"] {
26        size /= KB_AS_F;
27        if size < KB_AS_F {
28            return format!("{:.1}{}", size, unit);
29        }
30    }
31    format!("{:.1}GB", size / KB_AS_F)
32}
33
34/// Workaround for https://github.com/kojiishi/compare-dir/issues/8
35pub(crate) fn strip_prefix<'a>(path: &'a Path, base: &Path) -> Result<&'a Path, StripPrefixError> {
36    let result = path.strip_prefix(base);
37    #[cfg(windows)]
38    if let Ok(result_path) = result {
39        let result_os_str = result_path.as_os_str();
40        let result_bytes = result_os_str.as_encoded_bytes();
41        if !result_bytes.is_empty() && result_bytes[0] as char == std::path::MAIN_SEPARATOR {
42            // TODO: Use `slice_encoded_bytes` once stabilized.
43            // https://github.com/rust-lang/rust/issues/118485
44            return Ok(Path::new(unsafe {
45                use std::ffi::OsStr;
46                OsStr::from_encoded_bytes_unchecked(&result_bytes[1..])
47            }));
48        }
49    }
50    result
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn human_readable_size_tests() {
59        assert_eq!(human_readable_size(0), "0 bytes");
60        assert_eq!(human_readable_size(1), "1 bytes");
61        assert_eq!(human_readable_size(1023), "1023 bytes");
62        assert_eq!(human_readable_size(1024), "1.0KB");
63        assert_eq!(human_readable_size(1024 * 1024), "1.0MB");
64        assert_eq!(human_readable_size(1024 * 1024 * 1024), "1.0GB");
65        assert_eq!(human_readable_size(1024 * 1024 * 1024 * 1024), "1024.0GB");
66    }
67
68    #[cfg(windows)]
69    #[test]
70    fn strip_prefix_share_root() -> anyhow::Result<()> {
71        let path = Path::new(r"\\server\share\dir1\dir2");
72        let base = Path::new(r"\\server\share");
73        assert_eq!(strip_prefix(path, base)?.to_str().unwrap(), r"dir1\dir2");
74        assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir1\dir2");
75        Ok(())
76    }
77
78    #[cfg(windows)]
79    #[test]
80    fn strip_prefix_unc_root() -> anyhow::Result<()> {
81        let path = Path::new(r"\\?\UNC\server\share\dir1\dir2");
82        let base = Path::new(r"\\?\UNC\server\share");
83        assert_eq!(strip_prefix(path, base)?.to_str().unwrap(), r"dir1\dir2");
84        // assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir1\dir2");
85        Ok(())
86    }
87}