zzz-arc 0.4.0

A fast compression multitool supporting zst, tgz, txz, zip, and 7z formats with optional encryption
Documentation
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! file filtering system with comprehensive garbage file exclusion

use crate::Result;
use glob::Pattern;
use once_cell::sync::Lazy;
use std::path::Path;
use walkdir::WalkDir;

/// comprehensive list of garbage files to exclude by default
pub const GARBAGE_FILES: &[&str] = &[
    // macOS system files
    "__MACOSX",
    ".AppleDouble",
    ".DS_Store",
    "._*", // resource forks
    ".LSOverride",
    ".Spotlight-V100",         // spotlight index
    ".Trashes",                // trash
    ".fseventsd",              // filesystem events
    ".VolumeIcon.icns",        // volume icons
    ".DocumentRevisions-V100", // document revisions
    ".TemporaryItems",         // temporary items
    "Icon\r",                  // finder icon
    // Windows system files
    "thumbs.db",
    "Thumbs.db",
    "desktop.ini",
    "Desktop.ini",
    "ehthumbs.db",
    "ehthumbs_vista.db",
    "$RECYCLE.BIN",
    "System Volume Information",
    "hiberfil.sys",
    "pagefile.sys",
    "swapfile.sys",
    // Linux/Unix system files
    ".directory", // KDE folder metadata
    ".trash",
    ".Trash-*", // trash directories
    ".nfs*",    // NFS lock files
    "lost+found",
    // Development artifacts
    "__pycache__",
    "*.pyc",
    "*.pyo",
    "*.pyd",
    ".mypy_cache",
    ".pytest_cache",
    ".ruff_cache",
    ".hypothesis",
    ".coverage",
    ".nox",
    ".tox",
    "__pypackages__",
    ".venv",
    "venv",
    "node_modules",
    ".npm",
    ".pnpm-store",
    ".yarn",
    ".yarn-cache",
    "bower_components",
    ".git",
    ".svn",
    ".hg",
    ".bzr",
    "target",
    "target/debug",
    "target/release", // Rust build dirs
    ".gradle",
    ".maven",
    ".vscode",
    ".idea", // IDE files (configurable)
    ".direnv",
    ".cache",
    ".parcel-cache",
    ".turbo",
    ".next",
    ".nuxt",
    ".svelte-kit",
    "CMakeFiles",
    "CMakeCache.txt",
    "cmake-build-*",
    "npm-debug.log",
    "yarn-error.log",
    "pnpm-debug.log",
    // Temporary/backup files
    "*.tmp",
    "*.temp",
    "*.bak",
    "*.orig",
    "*~",
    ".#*",
    "#*#", // editor backup files
    "*.swp",
    "*.swo",  // vim swap files
    ".*.sw?", // vim swap file pattern
];

/// common sensitive files and directories for redact mode
pub const SENSITIVE_FILES: &[&str] = &[
    ".env",
    ".env.*",
    ".npmrc",
    ".pypirc",
    ".netrc",
    ".aws",
    ".ssh",
    ".gnupg",
    ".kube",
    "id_rsa",
    "id_dsa",
    "id_ecdsa",
    "id_ed25519",
    "*.pem",
    "*.key",
    "*.p12",
    "*.pfx",
    "*.kdbx",
    "*.gpg",
    "*.pgp",
    "*.age",
    ".sops.yaml",
    ".sops.yml",
    ".sops.json",
];

static COMPILED_GARBAGE_PATTERNS: Lazy<Vec<Pattern>> = Lazy::new(|| {
    GARBAGE_FILES
        .iter()
        .filter_map(|s| Pattern::new(s).ok())
        .collect()
});

pub struct FileFilter {
    use_defaults: bool,
    custom_excludes: Vec<Pattern>,
}

impl FileFilter {
    fn normalize_relative_path(path: &Path) -> String {
        let mut parts = Vec::new();
        for component in path.components() {
            if let std::path::Component::Normal(part) = component {
                parts.push(part.to_string_lossy());
            }
        }
        parts.join("/")
    }

    fn matches_patterns(patterns: &[Pattern], filename: &str, relative_path: &Path) -> bool {
        if !filename.is_empty() && patterns.iter().any(|pattern| pattern.matches(filename)) {
            return true;
        }

        let components: Vec<String> = relative_path
            .components()
            .filter_map(|component| match component {
                std::path::Component::Normal(part) => Some(part.to_string_lossy().to_string()),
                _ => None,
            })
            .collect();

        if components
            .iter()
            .any(|component| patterns.iter().any(|pattern| pattern.matches(component)))
        {
            return true;
        }

        for ancestor in relative_path.ancestors() {
            let ancestor_str = Self::normalize_relative_path(ancestor);
            if ancestor_str.is_empty() {
                continue;
            }
            if patterns
                .iter()
                .any(|pattern| pattern.matches(&ancestor_str))
            {
                return true;
            }
        }

        false
    }

    /// create new file filter with optional custom patterns
    pub fn new(use_defaults: bool, custom_patterns: &[String]) -> Result<Self> {
        let mut custom_excludes = Vec::new();
        for pattern in custom_patterns {
            custom_excludes.push(Pattern::new(pattern)?);
        }
        Ok(Self {
            use_defaults,
            custom_excludes,
        })
    }

    /// check if a path should be excluded from archiving
    pub fn should_exclude(&self, path: &Path) -> bool {
        let filename = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

        // check custom patterns first (against filename only for consistency)
        for pattern in &self.custom_excludes {
            if pattern.matches(filename) {
                return true;
            }
        }

        // check default garbage files if enabled
        if self.use_defaults {
            for pattern in &*COMPILED_GARBAGE_PATTERNS {
                if pattern.matches(filename) {
                    return true;
                }
            }
        }

        false
    }

    /// check if a relative path should be excluded from archiving
    pub fn should_exclude_relative(&self, relative_path: &Path) -> bool {
        if relative_path.as_os_str().is_empty() {
            return false;
        }

        let filename = relative_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("");

        if Self::matches_patterns(&self.custom_excludes, filename, relative_path) {
            return true;
        }

        if self.use_defaults
            && Self::matches_patterns(&COMPILED_GARBAGE_PATTERNS, filename, relative_path)
        {
            return true;
        }

        false
    }

    /// check if a path should be excluded, based on its relative path to a root
    pub fn should_exclude_path(&self, root: &Path, path: &Path) -> bool {
        let relative_path = path.strip_prefix(root).unwrap_or(path);
        self.should_exclude_relative(relative_path)
    }

    /// check if a path should be included in archiving (inverse of should_exclude)
    pub fn should_include(&self, path: &Path) -> bool {
        !self.should_exclude(path)
    }

    /// check if a relative path should be included in archiving
    pub fn should_include_relative(&self, relative_path: &Path) -> bool {
        !self.should_exclude_relative(relative_path)
    }

    /// check if a path should be included, based on its relative path to a root
    pub fn should_include_path(&self, root: &Path, path: &Path) -> bool {
        !self.should_exclude_path(root, path)
    }

    /// walk a directory tree while applying filter pruning
    pub fn walk_entries<'a>(
        &'a self,
        root: &'a Path,
    ) -> impl Iterator<Item = walkdir::Result<walkdir::DirEntry>> + 'a {
        self.walk_entries_with_follow(root, false)
    }

    /// walk a directory tree while applying filter pruning, optionally following symlinks
    pub fn walk_entries_with_follow<'a>(
        &'a self,
        root: &'a Path,
        follow_links: bool,
    ) -> impl Iterator<Item = walkdir::Result<walkdir::DirEntry>> + 'a {
        WalkDir::new(root)
            .follow_links(follow_links)
            .into_iter()
            .filter_entry(move |entry| self.should_include_path(root, entry.path()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    #[test]
    fn test_file_filter_with_defaults() -> Result<()> {
        let filter = FileFilter::new(true, &[])?;

        // Test default garbage files
        assert!(filter.should_exclude(Path::new(".DS_Store")));
        assert!(filter.should_exclude(Path::new("thumbs.db")));
        assert!(filter.should_exclude(Path::new("__pycache__")));
        assert!(filter.should_exclude(Path::new("node_modules")));
        assert!(filter.should_exclude(Path::new(".git")));
        assert!(filter.should_exclude(Path::new("__MACOSX")));
        assert!(filter.should_exclude(Path::new(".AppleDouble")));
        assert!(filter.should_exclude(Path::new("lost+found")));
        assert!(filter.should_exclude(Path::new(".mypy_cache")));
        assert!(filter.should_exclude(Path::new(".ruff_cache")));
        assert!(filter.should_exclude(Path::new("npm-debug.log")));
        assert!(filter.should_exclude(Path::new("target")));
        assert!(filter.should_exclude(Path::new(".cache")));
        assert!(filter.should_exclude(Path::new(".venv")));
        assert!(filter.should_exclude(Path::new("__pypackages__")));
        assert!(filter.should_exclude(Path::new("CMakeCache.txt")));

        // Test pattern matching
        assert!(filter.should_exclude(Path::new("._resource_fork")));
        assert!(filter.should_exclude(Path::new("file.tmp")));
        assert!(filter.should_exclude(Path::new("backup~")));
        assert!(filter.should_exclude(Path::new(".#lockfile")));

        // Test normal files are not excluded
        assert!(!filter.should_exclude(Path::new("README.md")));
        assert!(!filter.should_exclude(Path::new("src/main.rs")));
        assert!(!filter.should_exclude(Path::new("Cargo.toml")));
        assert!(!filter.should_exclude(Path::new(".env")));

        Ok(())
    }

    #[test]
    fn test_file_filter_without_defaults() -> Result<()> {
        let filter = FileFilter::new(false, &[])?;

        // Default garbage files should NOT be excluded when defaults are disabled
        assert!(!filter.should_exclude(Path::new(".DS_Store")));
        assert!(!filter.should_exclude(Path::new("thumbs.db")));
        assert!(!filter.should_exclude(Path::new("__pycache__")));

        Ok(())
    }

    #[test]
    fn test_custom_excludes() -> Result<()> {
        let custom_patterns = vec![
            "*.log".to_string(),
            "test_*".to_string(),
            "secret.txt".to_string(),
        ];
        let filter = FileFilter::new(true, &custom_patterns)?;

        // Test custom patterns
        assert!(filter.should_exclude(Path::new("debug.log")));
        assert!(filter.should_exclude(Path::new("test_file.txt")));
        assert!(filter.should_exclude(Path::new("secret.txt")));

        // Test files that should not be excluded
        assert!(!filter.should_exclude(Path::new("important.txt")));
        assert!(!filter.should_exclude(Path::new("production_config.yaml")));

        Ok(())
    }

    #[test]
    fn test_custom_excludes_override_defaults() -> Result<()> {
        let custom_patterns = vec!["*.rs".to_string()];
        let filter = FileFilter::new(true, &custom_patterns)?;

        // Custom patterns should work
        assert!(filter.should_exclude(Path::new("main.rs")));
        assert!(filter.should_exclude(Path::new("lib.rs")));

        // Default patterns should still work
        assert!(filter.should_exclude(Path::new(".DS_Store")));

        // Non-matching files should not be excluded
        assert!(!filter.should_exclude(Path::new("Cargo.toml")));

        Ok(())
    }

    #[test]
    fn test_sensitive_patterns_match_paths() -> Result<()> {
        let custom_patterns = SENSITIVE_FILES
            .iter()
            .map(|pattern| (*pattern).to_string())
            .collect::<Vec<_>>();
        let filter = FileFilter::new(true, &custom_patterns)?;

        assert!(filter.should_exclude_relative(Path::new(".env")));
        assert!(filter.should_exclude_relative(Path::new("config/.env.local")));
        assert!(filter.should_exclude_relative(Path::new(".ssh/id_rsa")));
        assert!(filter.should_exclude_relative(Path::new("config/.aws/credentials")));
        assert!(filter.should_exclude_relative(Path::new("keys/server.pem")));
        assert!(filter.should_exclude_relative(Path::new("keys/key.p12")));
        assert!(filter.should_exclude_relative(Path::new("vault/passwords.kdbx")));

        assert!(!filter.should_exclude_relative(Path::new("docs/notes.txt")));

        Ok(())
    }

    #[test]
    fn test_path_based_matching() -> Result<()> {
        let filter = FileFilter::new(true, &[])?;
        let root = Path::new("project");

        assert!(filter.should_exclude_path(root, Path::new("project/.git/config")));
        assert!(filter.should_exclude_path(root, Path::new("project/vendor/.git/config")));
        assert!(filter.should_exclude_path(root, Path::new("project/target/debug/app")));
        assert!(!filter.should_exclude_path(root, Path::new("project/src/main.rs")));

        Ok(())
    }

    #[test]
    fn test_invalid_pattern() {
        // Test that invalid glob patterns return an error
        let result = FileFilter::new(true, &["[".to_string()]);
        assert!(result.is_err());
    }

    #[test]
    fn test_path_vs_filename_matching() -> Result<()> {
        let filter = FileFilter::new(true, &[])?;

        // Test that patterns match on filename, not full path
        assert!(filter.should_exclude(Path::new("some/path/.DS_Store")));
        assert!(filter.should_exclude(Path::new("nested/dir/thumbs.db")));

        // Test directory names in paths don't trigger false matches
        assert!(!filter.should_exclude(Path::new(".DS_Store_backup/file.txt")));

        Ok(())
    }
}