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
use std::process::Command;
use std::path::Path;

use super::{CliError, LalResult};

/// Helper for stash and build
pub fn tar(tarball: &Path) -> LalResult<()> {
    info!("Taring OUTPUT");
    let mut args: Vec<String> = vec![
        "czf".into(),
        tarball.to_str().unwrap().into(), // path created internally - always valid unicode
        "--transform=s,^OUTPUT/,,".into(), // remove leading OUTPUT
    ];

    // Avoid depending on wildcards (which would also hide hidden files)
    // All links, hidden files, and regular files should go into the tarball.
    let findargs = vec!["OUTPUT/", "-type", "f", "-o", "-type", "l"];
    debug!("find {}", findargs.join(" "));
    let find_output = Command::new("find").args(&findargs).output()?;
    let find_str = String::from_utf8_lossy(&find_output.stdout);

    // append each file as an arg to the main tar process
    for f in find_str.trim().split('\n') {
        args.push(f.into())
    }

    // basically `tar czf component.tar --transform.. $(find OUTPUT -type f -o -type l)`:
    debug!("tar {}", args.join(" "));
    let s = Command::new("tar").args(&args).status()?;

    if !s.success() {
        return Err(CliError::SubprocessFailure(s.code().unwrap_or(1001)));
    }
    Ok(())
}