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
#![recursion_limit="128"]

/*!

## Making deb packages

If you only want to make some `*.deb` files, and you're not a developer of tools
for Debian packaging, **[see `cargo deb` command usage described in the
README instead](https://github.com/mmstick/cargo-deb#readme)**.

```sh
cargo install cargo-deb
cargo deb # run this in your Cargo project directory
```

## Making tools for making deb packages

The library interface is experimental. See `main.rs` for usage.

```rust,ignore
let listener = &mut listener::StdErrListener {verbose}; // prints warnings
let options = Config::from_manifest(Path::new("Cargo.toml"), target, listener)?;

reset_deb_directory(&options)?;
cargo_build(&options, target, &[], verbose)?;
strip_binaries(&options, target, listener)?;

let bin_path = generate_debian_binary_file(&options)?;
let mut deb_contents = vec![];
deb_contents.push(bin_path);

let system_time = time::SystemTime::now().duration_since(time::UNIX_EPOCH)?.as_secs();
// Initailize the contents of the data archive (files that go into the filesystem).
let (data_archive, asset_hashes) = data::generate_archive(&options, system_time, listener)?;
let data_base_path = options.path_in_deb("data.tar");

// Initialize the contents of the control archive (metadata for the package manager).
let control_archive = control::generate_archive(&options, system_time, asset_hashes, listener)?;
let control_base_path = options.path_in_deb("control.tar");

// Order is important for Debian
deb_contents.push(compress::gz(&control_archive, &control_base_path)?);
deb_contents.push(compress::xz_or_gz(&data_archive, &data_base_path)?);

let generated = generate_deb(&options, &deb_contents)?;
```
*/

extern crate toml;
extern crate tar;
#[cfg(feature = "lzma")]
extern crate xz2;
extern crate zopfli;
extern crate md5;
extern crate file;
#[macro_use]
extern crate quick_error;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate getopts;
extern crate glob;

pub mod compress;
pub mod control;
pub mod data;
mod manifest;
mod dependencies;
mod try;
mod wordsplit;
mod error;
mod archive;
mod config;
pub mod listener;
use listener::Listener;

use std::fs;
use std::path::{Path, PathBuf};
use std::io::{self, Write};
use std::process::Command;
use std::os::unix::fs::OpenOptionsExt;
pub use error::*;

pub use manifest::Config;

const TAR_REJECTS_CUR_DIR: bool = true;

/// created by `build.rs`
const DEFAULT_TARGET: &str = include_str!(concat!(env!("OUT_DIR"), "/default_target.rs"));

/// Run `dpkg` to install `deb` archive at the given path
pub fn install_deb(path: &Path) -> CDResult<()> {
    let status = Command::new("sudo").arg("dpkg").arg("-i").arg(path)
        .status()?;
    if !status.success() {
        Err(CargoDebError::InstallFailed)?;
    }
    Ok(())
}

/// Uses the ar program to create the final Debian package, at least until a native ar implementation is implemented.
pub fn generate_deb(config: &Config, contents: &[PathBuf]) -> CDResult<PathBuf> {
    let out_relpath = format!("{}_{}_{}.deb", config.name, config.version, config.architecture);
    let out_abspath = config.path_in_deb(&out_relpath);
    {
        let deb_dir = out_abspath.parent().ok_or("invalid dir")?;

        let _ = fs::remove_file(&out_abspath); // Remove it if it exists
        let mut cmd = Command::new("ar");
        cmd.current_dir(&deb_dir).arg("r").arg(out_relpath);
        for path in contents {
            cmd.arg(&path.strip_prefix(&deb_dir).map_err(|_|"invalid path")?);
        }

        let output = cmd.output()
            .map_err(|e| CargoDebError::CommandFailed(e, "ar"))?;
        if !output.status.success() {
            return Err(CargoDebError::CommandError("ar", out_abspath.display().to_string(), output.stderr));
        }
    }
    Ok(out_abspath)
}

/// Creates the debian-binary file that will be added to the final ar archive.
pub fn generate_debian_binary_file(options: &Config) -> io::Result<PathBuf> {
    let bin_path = options.path_in_deb("debian-binary");
    let mut file = fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .mode(0o644)
        .open(&bin_path)?;
    file.write_all(b"2.0\n")?;
    Ok(bin_path)
}

/// Removes the target/debian directory so that we can start fresh.
pub fn reset_deb_directory(options: &Config) -> io::Result<()> {
    let deb_dir = options.deb_dir();
    let _ = fs::remove_dir_all(&deb_dir);
    fs::create_dir_all(deb_dir)
}

/// Builds a release binary with `cargo build --release`
pub fn cargo_build(options: &Config, target: Option<&str>, other_flags: &[String], verbose: bool) -> CDResult<()> {
    let mut cmd = Command::new("cargo");
    cmd.current_dir(&options.manifest_dir);
    cmd.arg("build").args(&["--release", "--all"]);

    for flag in other_flags {
        cmd.arg(flag);
    }

    if verbose {
        cmd.arg("--verbose");
    }
    if let Some(target) = target {
        cmd.arg(format!("--target={}", target));
    }
    if !options.default_features {
        cmd.arg("--no-default-features");
    }
    let features = &options.features;
    if !features.is_empty() {
        cmd.arg(format!("--features={}", features.join(",")));
    }

    let status = cmd.status().map_err(|e| CargoDebError::CommandFailed(e, "cargo"))?;
    if !status.success() {
        Err(CargoDebError::BuildFailed)?;
    }
    Ok(())
}

/// Strips the binary that was created with cargo
pub fn strip_binaries(options: &Config, target: Option<&str>, listener: &mut Listener) -> CDResult<()> {
    let mut cargo_config = None;
    let strip_tmp;
    let mut strip_cmd = "strip";

    if let Some(target) = target {
        cargo_config = options.cargo_config()?;
        if let Some(ref conf) = cargo_config {
            if let Some(cmd) = conf.strip_command(target) {
                listener.info(format!("Using '{}' for '{}'", cmd, target));
                strip_tmp = cmd;
                strip_cmd = &strip_tmp;
            }
        }
    }

    for path in options.built_binaries().into_iter().filter_map(|a| a.path()) {
        Command::new(strip_cmd)
            .arg("--strip-unneeded")
            .arg(path)
            .status()
            .and_then(|s| if s.success() {
                Ok(())
            } else {
                Err(io::Error::new(io::ErrorKind::Other, format!("{}",s)))
            })
            .map_err(|err| {
                if let Some(target) = target {
                    let conf_path = cargo_config.as_ref().map(|c|c.path()).unwrap_or(Path::new(".cargo/config"));
                    CargoDebError::StripFailed(path.to_owned(), format!("{}: {}.\nhint: Target-specific strip commands are configured in [target.{}] strip = \"{}\" in {}", strip_cmd, err, target, strip_cmd, conf_path.display()))
                } else {
                    CargoDebError::CommandFailed(err, "strip")
                }
            })?;
        listener.info(format!("Stripped '{}'", path.display()));
    }
    Ok(())
}