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
use crate::utils::{file_name_as_str, LogCommandExt};
use crate::Result;
use crate::Runnable;
use std::fs;
use std::process::Command;

use anyhow::{bail, Context};
use log::debug;

pub mod regular_platform;

pub fn strip_runnable(runnable: &Runnable, mut command: Command) -> Result<Runnable> {
    let exe_stripped_name = file_name_as_str(&runnable.exe)?;

    let mut stripped_runnable = runnable.clone();
    stripped_runnable.exe = runnable
        .exe
        .parent()
        .map(|it| it.join(format!("{}-stripped", exe_stripped_name)))
        .with_context(|| format!("{} is not a valid executable name", &runnable.exe.display()))?;

    // Backup old runnable
    fs::copy(&runnable.exe, &stripped_runnable.exe)?;

    let command = command.arg(&stripped_runnable.exe);
    debug!("Running command {:?}", command);

    let output = command.log_invocation(2).output()?;
    if !output.status.success() {
        bail!(
            "Error while stripping {}\nError: {}",
            &stripped_runnable.exe.display(),
            String::from_utf8(output.stdout)?
        )
    }

    debug!(
        "{} unstripped size = {} and stripped size = {}",
        runnable.exe.display(),
        fs::metadata(&runnable.exe)?.len(),
        fs::metadata(&stripped_runnable.exe)?.len()
    );
    Ok(stripped_runnable)
}