chandler 0.0.3

a masking archiver
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
436
437
438
439
440
441
442
443
//! chandler assembles tape archives.

extern crate fancy_regex;
extern crate flate2;
extern crate lazy_static;
extern crate normalize_path;
extern crate serde;
extern crate tar;
extern crate toml;
extern crate walkdir;

use self::serde::{Deserialize, Serialize};
use normalize_path::NormalizePath;
use std::env;
use std::fs;
use std::io;
use std::path;
use std::time;

lazy_static::lazy_static! {
    /// EXTENSIONED_FILE_PATH_PATTERN matches file paths with extensions,
    /// including file extensions (.BAT, .EXE, and so on),
    /// as well as file paths missing traditional basenames (.gitignore, .git, and so on).
    pub static ref EXTENSIONED_FILE_PATH_PATTERN: fancy_regex::Regex = fancy_regex::Regex::new(r"^(.*/)*[^/]*\.[^/]*$").unwrap();

    /// FILE_MANAGER_CACHE_PATTERN matches file paths with file manager metadata files.
    pub static ref FILE_MANAGER_CACHE_PATTERN: fancy_regex::Regex = fancy_regex::Regex::new(r"^(.*/)?(\.DS_Store|Thumbs\.db)$").unwrap();

    /// SYSTEM_V_INIT_LINEAGE_PATTERN matches file paths within SysVinit (etc/init.d) directory trees.
    pub static ref SYSTEM_V_INIT_LINEAGE_PATTERN: fancy_regex::Regex = fancy_regex::Regex::new(r"^(.*/)?etc/init\.d(/.*)?$").unwrap();

    pub static ref COMMON_NONEXECUTABLE_FILE_PATH_PATTERN: fancy_regex::Regex = fancy_regex::Regex::new(r"(?i)^aliases|(ba|(m)?k|z)shrc|(bsd|gnu)?makefile|changelog|exports|fstab|license|readme|group|hosts|issue|mime|modules|profile|protocols|resolv|services|t(e)?mp|zshenv|((.*/)?etc/.+)$").unwrap();
}

#[test]
fn test_extensioned_file_path_pattern() -> Result<(), fancy_regex::Error> {
    let pattern = EXTENSIONED_FILE_PATH_PATTERN.clone();
    assert!(!pattern.is_match("hello")?);
    assert!(!pattern.is_match("HELLO")?);
    assert!(!pattern.is_match("hello-1.0/docs")?);
    assert!(pattern.is_match("HELLO.BAT")?);
    assert!(pattern.is_match("hello.bat")?);
    assert!(pattern.is_match("applications/hello.bat")?);
    assert!(pattern.is_match("HELLO.EXE")?);
    assert!(pattern.is_match("hello.exe")?);
    assert!(pattern.is_match("applications/hello.exe")?);
    assert!(pattern.is_match(".gitignore")?);
    assert!(pattern.is_match("DEGENERATE.")?);
    assert!(pattern.is_match("degenerate.")?);
    Ok(())
}

#[test]
fn test_file_manager_cache_pattern() -> Result<(), fancy_regex::Error> {
    let pattern = FILE_MANAGER_CACHE_PATTERN.clone();
    assert!(pattern.is_match(".DS_Store")?);
    assert!(pattern.is_match("docs/.DS_Store")?);
    assert!(pattern.is_match("/docs/.DS_Store")?);
    assert!(!pattern.is_match("docs")?);
    assert!(!pattern.is_match("/docs")?);
    assert!(pattern.is_match("Thumbs.db")?);
    assert!(pattern.is_match("docs/Thumbs.db")?);
    Ok(())
}

#[test]
fn test_system_v_init_lineage_pattern() -> Result<(), fancy_regex::Error> {
    let pattern = SYSTEM_V_INIT_LINEAGE_PATTERN.clone();
    assert!(pattern.is_match("/etc/init.d")?);
    assert!(pattern.is_match("etc/init.d")?);
    assert!(pattern.is_match("/etc/init.d/ssh")?);
    assert!(pattern.is_match("etc/init.d/ssh")?);
    assert!(!pattern.is_match("/root/.ssh")?);
    assert!(!pattern.is_match("root/.ssh")?);
    Ok(())
}

#[test]
fn test_common_nonexecutable_file_path_pattern() -> Result<(), fancy_regex::Error> {
    let pattern = COMMON_NONEXECUTABLE_FILE_PATH_PATTERN.clone();
    assert!(pattern.is_match("bashrc")?);
    assert!(pattern.is_match("bsdmakefile")?);
    assert!(pattern.is_match("changelog")?);
    assert!(pattern.is_match("gnumakefile")?);
    assert!(pattern.is_match("license")?);
    assert!(pattern.is_match("makefile")?);
    assert!(pattern.is_match("README")?);
    assert!(pattern.is_match("readme")?);
    assert!(pattern.is_match("aliases")?);
    assert!(pattern.is_match("exports")?);
    assert!(pattern.is_match("fstab")?);
    assert!(pattern.is_match("group")?);
    assert!(pattern.is_match("hosts")?);
    assert!(pattern.is_match("issue")?);
    assert!(pattern.is_match("kshrc")?);
    assert!(pattern.is_match("mime")?);
    assert!(pattern.is_match("mkshrc")?);
    assert!(pattern.is_match("modules")?);
    assert!(pattern.is_match("profile")?);
    assert!(pattern.is_match("protocols")?);
    assert!(pattern.is_match("resolv")?);
    assert!(pattern.is_match("services")?);
    assert!(pattern.is_match("temp")?);
    assert!(pattern.is_match("tmp")?);
    assert!(pattern.is_match("zshenv")?);
    assert!(pattern.is_match("zshrc")?);
    assert!(pattern.is_match("/etc/sshd/sshd_config")?);
    assert!(pattern.is_match("etc/sshd/sshd_config")?);
    Ok(())
}

/// HeaderType models a tarball header type.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub enum HeaderType {
    /// Old models a vintage tar v7 header.
    Old,

    /// Gnu models a classical GNU tar header.
    Gnu,

    /// UStar models a POSIX UStar/PAX header.
    UStar,
}

/// HeaderType models a tarball header type.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub enum FileMode {
    /// Directory models a folder.
    Directory,

    /// File models an ordinary, non-directory file.
    File,
}

/// Condition models an archive entry state.
///
/// Fields with values present are intersected together (AND).
#[derive(Debug)]
pub struct Condition {
    /// mode denotes an FileMode.
    pub mode: Option<FileMode>,

    /// path denotes a file path.
    pub path: Option<fancy_regex::Regex>,
}

/// Rule applies given permissions for matching file patterns.
#[derive(Debug)]
pub struct Rule {
    /// when denotes a condition required to apply this rule's effects.
    pub when: Condition,

    /// skip excludes archive entries.
    pub skip: bool,

    /// mtime overrides entry modification timestamps (UNIX epoch).
    pub mtime: Option<u64>,

    /// uid denotes an effective user id.
    pub uid: Option<u64>,

    /// gid denotes an effective group id.
    pub gid: Option<u64>,

    /// username denotes an effective username.
    pub username: Option<String>,

    /// groupname denotes an effective group name.
    pub groupname: Option<String>,

    /// permissions denotes an effective chmod mask of file permissions.
    pub permissions: Option<u32>,
}

impl Rule {
    /// is_match determines whether a rule relates to an entry.
    pub fn is_match(&self, filemode: &FileMode, pth: &str) -> Result<bool, io::Error> {
        if let Some(when_mode) = &self.when.mode
            && when_mode != filemode
        {
            return Ok(false);
        }

        if let Some(when_path) = &self.when.path
            && !when_path.is_match(pth).map_err(io::Error::other)?
        {
            return Ok(false);
        }

        Ok(true)
    }

    /// is_skip determines whether a rule skips an entry.
    pub fn is_skip(&self, filemode: &FileMode, pth: &str) -> Result<bool, io::Error> {
        self.is_match(filemode, pth).map(|e| e && self.skip)
    }

    /// apply modifies headers.
    pub fn apply(&self, header: &mut tar::Header) -> Result<(), io::Error> {
        if let Some(mtime) = self.mtime {
            header.set_mtime(mtime);
        }

        if let Some(uid) = self.uid {
            header.set_uid(uid);
        }

        if let Some(gid) = self.gid {
            header.set_gid(gid);
        }

        if let Some(username) = &self.username {
            header.set_username(username)?;
        }

        if let Some(groupname) = &self.groupname {
            header.set_groupname(groupname)?;
        }

        if let Some(permissions) = &self.permissions {
            header.set_mode(*permissions);
        }

        Ok(())
    }
}

/// Chandler assembles gunzipped tarballs (TGZ, TAR.GZ).
#[derive(Debug)]
pub struct Chandler {
    /// verbose enables additional logging.
    pub verbose: bool,

    /// header_type denotes a tape archive format.
    pub header_type: HeaderType,

    /// cwd customizes the current working directory.
    pub cwd: Option<path::PathBuf>,

    /// rules collects a table of permissions to apply to inbound files.
    pub rules: Vec<Rule>,
}

/// permissions_to_u32 converts fs::Permissions objects to chmod integers.
pub fn permissions_to_u32(permissions: fs::Permissions) -> u32 {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        permissions.mode()
    }
    #[cfg(windows)]
    {
        if permissions.readonly() {
            0o444u32
        } else {
            0o666u32
        }
    }
}

impl Default for Chandler {
    /// default constructs an executable-aggressive Chandler configuration.
    fn default() -> Self {
        Chandler {
            verbose: false,
            header_type: HeaderType::UStar,
            cwd: None,
            rules: vec![
                Rule {
                    when: Condition {
                        mode: None,
                        path: Some(FILE_MANAGER_CACHE_PATTERN.clone()),
                    },
                    skip: true,
                    mtime: None,
                    uid: None,
                    gid: None,
                    username: None,
                    groupname: None,
                    permissions: None,
                },
                Rule {
                    when: Condition {
                        mode: None,
                        path: None,
                    },
                    skip: false,
                    mtime: None,
                    uid: None,
                    gid: None,
                    username: None,
                    groupname: None,
                    permissions: Some(0o755u32),
                },
                Rule {
                    when: Condition {
                        mode: Some(FileMode::File),
                        path: Some(COMMON_NONEXECUTABLE_FILE_PATH_PATTERN.clone()),
                    },
                    skip: false,
                    mtime: None,
                    uid: None,
                    gid: None,
                    username: None,
                    groupname: None,
                    permissions: Some(0o644u32),
                },
                Rule {
                    when: Condition {
                        mode: Some(FileMode::File),
                        path: Some(EXTENSIONED_FILE_PATH_PATTERN.clone()),
                    },
                    skip: false,
                    mtime: None,
                    uid: None,
                    gid: None,
                    username: None,
                    groupname: None,
                    permissions: Some(0o644u32),
                },
                Rule {
                    when: Condition {
                        mode: None,
                        path: Some(SYSTEM_V_INIT_LINEAGE_PATTERN.clone()),
                    },
                    skip: false,
                    mtime: None,
                    uid: None,
                    gid: None,
                    username: None,
                    groupname: None,
                    permissions: Some(0o755u32),
                },
            ],
        }
    }
}

impl Chandler {
    /// archive generates a tarball.
    pub fn archive(&self, target: &path::Path, source: &path::Path) -> Result<(), io::Error> {
        if let Some(cwd_pathbuf) = &self.cwd {
            env::set_current_dir(cwd_pathbuf.as_path())?;
        }

        let file = fs::File::create(target)?;
        let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default());
        let mut builder = tar::Builder::new(gz_encoder);
        let walker = walkdir::WalkDir::new(source).sort_by(
            |a: &walkdir::DirEntry, b: &walkdir::DirEntry| a.file_name().cmp(b.file_name()),
        );

        for entry in walker {
            let entry = entry?;
            let pth = entry.path();
            let pth_clean = pth.normalize();
            let pth_str = pth_clean.to_str().ok_or_else(|| {
                io::Error::other(format!("unable to render path {:?}", pth_clean))
            })?;

            if pth_str.is_empty() || pth_str == "." {
                continue;
            }

            let metadata = entry.metadata()?;
            let mut header = match self.header_type {
                HeaderType::Old => tar::Header::new_old(),
                HeaderType::Gnu => tar::Header::new_gnu(),
                HeaderType::UStar => tar::Header::new_ustar(),
            };

            header.set_path(&pth_clean)?;

            let mtime = metadata
                .modified()?
                .duration_since(time::UNIX_EPOCH)
                .map(|e| e.as_secs())
                .map_err(io::Error::other)?;

            header.set_mtime(mtime);
            header.set_mode(permissions_to_u32(metadata.permissions()));

            #[cfg(unix)]
            {
                use std::os::unix::fs::MetadataExt;
                header.set_uid(metadata.uid() as u64);
                header.set_gid(metadata.gid() as u64);
            }
            #[cfg(not(unix))]
            {
                eprintln!("warning: nonunix environment. dropping uid, gid.");
            }

            let filemode = if metadata.is_dir() {
                FileMode::Directory
            } else if metadata.is_file() {
                FileMode::File
            } else {
                return Err(io::Error::other(format!(
                    "unsupported file type: {pth_str}"
                )));
            };

            if filemode == FileMode::Directory {
                header.set_entry_type(tar::EntryType::Directory);
                header.set_size(0);
            } else if filemode == FileMode::File {
                header.set_size(metadata.len());
            }

            if self.verbose {
                eprintln!("a {pth_str}");
            }

            if self
                .rules
                .iter()
                .any(|e| e.is_skip(&filemode, pth_str).unwrap_or(false))
            {
                continue;
            }

            for rule in &self.rules {
                if !rule.is_match(&filemode, pth_str)? {
                    continue;
                }

                rule.apply(&mut header)?;
            }

            header.set_cksum();

            if filemode == FileMode::Directory {
                builder.append(&header, &[] as &[u8])?;
            } else if filemode == FileMode::File {
                let mut source_file = fs::File::open(pth_clean)?;
                builder.append(&header, &mut source_file)?;
            }
        }

        builder.into_inner()?.finish().map(|_| ())
    }
}