dinghy_lib/platform/
mod.rs

1use crate::utils::{file_name_as_str, LogCommandExt};
2use crate::Result;
3use crate::Runnable;
4use std::fs;
5use std::process::Command;
6
7use anyhow::{bail, Context};
8use log::debug;
9
10pub mod regular_platform;
11
12pub fn strip_runnable(runnable: &Runnable, mut command: Command) -> Result<Runnable> {
13    let exe_stripped_name = file_name_as_str(&runnable.exe)?;
14
15    let mut stripped_runnable = runnable.clone();
16    stripped_runnable.exe = runnable
17        .exe
18        .parent()
19        .map(|it| it.join(format!("{}-stripped", exe_stripped_name)))
20        .with_context(|| format!("{} is not a valid executable name", &runnable.exe.display()))?;
21
22    // Backup old runnable
23    fs::copy(&runnable.exe, &stripped_runnable.exe)?;
24
25    let command = command.arg(&stripped_runnable.exe);
26    debug!("Running command {:?}", command);
27
28    let output = command.log_invocation(2).output()?;
29    if !output.status.success() {
30        bail!(
31            "Error while stripping {}\nError: {}",
32            &stripped_runnable.exe.display(),
33            String::from_utf8(output.stdout)?
34        )
35    }
36
37    debug!(
38        "{} unstripped size = {} and stripped size = {}",
39        runnable.exe.display(),
40        fs::metadata(&runnable.exe)?.len(),
41        fs::metadata(&stripped_runnable.exe)?.len()
42    );
43    Ok(stripped_runnable)
44}