cargo-mkdist 0.1.1

A Cargo subcommand to build native distribution packages (apt, pacman, etc) directly from Rust, without external tools.
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
//! Debian (.deb) packager.

use super::Packager;
use crate::cfg::ResolvedTarget;
use crate::meta::{find_package, get as get_metadata};
use crate::stage::Staging;
use anyhow::Result;
use ar::Builder as ArBuilder;
use flate2::Compression;
use flate2::write::GzEncoder;
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use tar::Builder;
use walkdir::WalkDir;

pub struct ControlArgs<'a>
{
    f:              Box<File>,
    package:        &'a str,
    version:        &'a str,
    arch:           &'a str,
    section:        &'a str,
    priority:       &'a str,
    maintainer:     &'a str,
    description:    &'a str,
    installed_size: u64,
    depends:        &'a [String],
    recommends:     &'a [String],
    suggests:       &'a [String],
    conflicts:      &'a [String],
    breaks:         &'a [String],
    replaces:       &'a [String],
    provides:       &'a [String],
    enhances:       &'a [String],
    pre_depends:    &'a [String],
}

/// Debian packager – produces `.deb` files.
pub struct DebPackager;

impl Packager for DebPackager
{
    fn package(
        &self,
        staging: &Staging,
        target: &ResolvedTarget,
        out_dir: &Path,
        name: &str,
        version: &str,
    ) -> Result<PathBuf>
    {
        // We don't support Linux distribution on non-UNIX so far.
        #[cfg(not(unix))]
        panic!("Linux distributions are not supported on non-UNIX so far.");

        // Get metadata from Cargo
        let metadata = get_metadata()?;
        let pkg_meta = find_package(&metadata, target.package.clone())?;

        // Architecture from target triple
        let arch = deb_arch_from_triple(&target.target)?;

        // Extract control fields from extra
        let extra = &target.extra;
        let section = extra
            .get("section")
            .and_then(|v| v.as_str())
            .unwrap_or("utils")
            .to_string();
        let priority = extra
            .get("priority")
            .and_then(|v| v.as_str())
            .unwrap_or("optional")
            .to_string();
        let maintainer = extra
            .get("maintainer")
            .map(|v| v.to_string())
            .unwrap_or_else(|| pkg_meta.authors.join(", "))
            .to_string();
        let description = extra
            .get("description")
            .and_then(|v| v.as_str())
            .unwrap_or_else(|| {
                pkg_meta.description.as_deref().unwrap_or("Rust package")
            })
            .to_string();

        // Dependencies (comma-separated)
        let depends = split_deb_field(extra, "depends");
        let recommends = split_deb_field(extra, "recommends");
        let suggests = split_deb_field(extra, "suggests");
        let conflicts = split_deb_field(extra, "conflicts");
        let breaks = split_deb_field(extra, "breaks");
        let replaces = split_deb_field(extra, "replaces");
        let provides = split_deb_field(extra, "provides");
        let enhances = split_deb_field(extra, "enhances");
        let pre_depends = split_deb_field(extra, "pre-depends");

        // Conffiles – list of files that should be treated as configuration
        // files
        let conffiles = split_deb_field(extra, "conffiles");

        // Maintainer scripts
        let scripts = get_scripts(extra);

        // Compute installed size (in KiB, rounded up)
        let installed_size = compute_installed_size(staging)?;

        // Prepare staging directories
        let staging_root = staging.root.path();

        // 1. Build control.tar.gz
        let control_tar_gz = build_control_tarball(
            ControlArgs {
                f: Box::new(tempfile::tempfile()?),
                package: name,
                version,
                arch: &arch,
                section: &section,
                priority: &priority,
                maintainer: &maintainer,
                description: &description,
                installed_size,
                depends: &depends,
                recommends: &recommends,
                suggests: &suggests,
                conflicts: &conflicts,
                breaks: &breaks,
                replaces: &replaces,
                provides: &provides,
                enhances: &enhances,
                pre_depends: &pre_depends,
            },
            &conffiles,
            &scripts,
        )?;

        let mut metadata = File::open(control_tar_gz)?;

        // 2. Build data.tar.gz from staging
        let data_tar_gz = build_data_tarball(staging_root)?;

        // 3. Create .deb (ar archive)
        let output_path =
            out_dir.join(format!("{}_{}_{}.deb", name, version, arch));
        fs::create_dir_all(out_dir)?;

        let mut ar_builder = ArBuilder::new(File::create(&output_path)?);

        let mut data = File::open(data_tar_gz)?;

        let mut binary = tempfile::tempfile()?;

        binary.write_all(&b"2.0\n"[..])?;

        // add debian-binary
        ar_builder.append_file(b"debian-binary", &mut binary)?;

        // Add control.tar.gz
        ar_builder.append_file(b"control.tar.gz", &mut metadata)?;
        // Add data.tar.gz
        ar_builder.append_file(b"data.tar.gz", &mut data)?;

        Ok(output_path)
    }
}

/// Build control.tar.gz with metadata and scripts.
pub fn build_control_tarball(
    mut args: ControlArgs,
    conffiles: &[String],
    scripts: &HashMap<String, PathBuf>,
) -> Result<PathBuf>
{
    // Create temporary file for control.tar.gz
    let temp_dir = tempfile::tempdir()?;
    let control_path = temp_dir.path().join("control");

    args.f = Box::new(File::create(&control_path)?);

    // Write control file
    write_control(args)?;

    // Write conffiles if any
    if !conffiles.is_empty()
    {
        let conffile_path = temp_dir.path().join("conffiles");
        let mut conffile = File::create(&conffile_path)?;
        for conf in conffiles
        {
            writeln!(conffile, "{}", conf)?;
        }
        // conffiles will be added to tarball
    }

    // Copy scripts to temp dir
    for (name, path) in scripts
    {
        let dest = temp_dir.path().join(name);
        fs::copy(path, &dest)?;
        // Make executable
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&dest)?.permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&dest, perms)?;
        }
    }

    // Pack all files from temp_dir into control.tar.gz
    let output = temp_dir.path().join("control.tar.gz");
    let tar_gz = File::create(&output)?;
    let gz = GzEncoder::new(tar_gz, Compression::default());
    let mut tar = Builder::new(gz);

    for entry in WalkDir::new(temp_dir.path())
        .into_iter()
        .filter_entry(|e| e.path() != temp_dir.path())
    {
        let entry = entry?;
        let path = entry.path();
        let rel_path = path.strip_prefix(temp_dir.path())?;
        if path.is_file()
        {
            tar.append_file(rel_path, &mut File::open(path)?)?;
        }
    }

    tar.into_inner()?; // finish gz
    Ok(output)
}

/// Write the `control` file.
pub fn write_control(mut args: ControlArgs) -> Result<()>
{
    writeln!(args.f, "Package: {}", args.package)?;
    writeln!(args.f, "Version: {}", args.version)?;
    writeln!(args.f, "Architecture: {}", args.arch)?;
    writeln!(args.f, "Maintainer: {}", args.maintainer)?;
    writeln!(args.f, "Installed-Size: {}", args.installed_size)?;
    writeln!(args.f, "Section: {}", args.section)?;
    writeln!(args.f, "Priority: {}", args.priority)?;

    // Optional fields
    if !args.depends.is_empty()
    {
        writeln!(args.f, "Depends: {}", args.depends.join(", "))?;
    }
    if !args.pre_depends.is_empty()
    {
        writeln!(args.f, "Pre-Depends: {}", args.pre_depends.join(", "))?;
    }
    if !args.recommends.is_empty()
    {
        writeln!(args.f, "Recommends: {}", args.recommends.join(", "))?;
    }
    if !args.suggests.is_empty()
    {
        writeln!(args.f, "Suggests: {}", args.suggests.join(", "))?;
    }
    if !args.conflicts.is_empty()
    {
        writeln!(args.f, "Conflicts: {}", args.conflicts.join(", "))?;
    }
    if !args.breaks.is_empty()
    {
        writeln!(args.f, "Breaks: {}", args.breaks.join(", "))?;
    }
    if !args.replaces.is_empty()
    {
        writeln!(args.f, "Replaces: {}", args.replaces.join(", "))?;
    }
    if !args.provides.is_empty()
    {
        writeln!(args.f, "Provides: {}", args.provides.join(", "))?;
    }
    if !args.enhances.is_empty()
    {
        writeln!(args.f, "Enhances: {}", args.enhances.join(", "))?;
    }

    // Description: first line is summary, then extended description indented
    // with space
    let desc_lines: Vec<&str> = args.description.lines().collect();
    if let Some(first) = desc_lines.first()
    {
        writeln!(args.f, "Description: {}", first)?;
        for line in desc_lines.iter().skip(1)
        {
            writeln!(args.f, " {}", line)?;
        }
    }

    Ok(())
}

/// Build data.tar.gz from staging directory.
pub fn build_data_tarball(staging_root: &Path) -> Result<PathBuf>
{
    let temp_dir = tempfile::tempdir()?;
    let output = temp_dir.path().join("data.tar.gz");
    let tar_gz = File::create(&output)?;
    let gz = GzEncoder::new(tar_gz, Compression::default());
    let mut tar = Builder::new(gz);

    // Walk staging root, skipping root itself
    for entry in WalkDir::new(staging_root)
        .into_iter()
        .filter_entry(|e| e.path() != staging_root)
    {
        let entry = entry?;
        let path = entry.path();
        let rel_path = path.strip_prefix(staging_root)?;
        let meta = entry.metadata()?;

        if meta.is_file()
        {
            tar.append_file(rel_path, &mut File::open(path)?)?;
        }
        else if meta.is_dir()
        {
            tar.append_dir(rel_path, path)?;
        }
        else if meta.is_symlink()
        {
            let target = fs::read_link(path)?;
            let mut h = tar::Header::new_gnu();
            tar.append_link(&mut h, rel_path, target)?;
        }
    }

    tar.into_inner()?;
    Ok(output)
}

/// Compute installed size in KiB (rounded up).
pub fn compute_installed_size(staging: &Staging) -> Result<u64>
{
    let mut total_bytes = 0u64;
    for entry in WalkDir::new(staging.root.path()).into_iter()
    {
        let entry = entry?;
        if entry.path().is_file()
        {
            total_bytes += entry.metadata()?.len();
        }
    }
    // Round up to KiB (1 KiB = 1024 bytes)
    let kib = total_bytes.div_ceil(1024);
    Ok(kib)
}

/// Split a comma-separated field from extra into a vector of trimmed strings.
pub fn split_deb_field(
    extra: &HashMap<String, toml::Value>,
    key: &str,
) -> Vec<String>
{
    extra
        .get(key)
        .and_then(|v| v.as_str())
        .map(|s| s.split(',').map(|s| s.trim().to_string()).collect())
        .unwrap_or_default()
}

/// Get maintainer scripts from extra.
pub fn get_scripts(
    extra: &HashMap<String, toml::Value>,
) -> HashMap<String, PathBuf>
{
    let mut map = HashMap::new();
    // Script names: preinst, postinst, prerm, postrm
    for script in &["preinst", "postinst", "prerm", "postrm"]
    {
        if let Some(path) = extra
            .get(*script)
            .and_then(|v| v.as_str())
            .map(PathBuf::from)
        {
            if path.exists()
            {
                map.insert(script.to_string(), path);
            }
            else
            {
                eprintln!("warning: script '{}' not found, skipping.", script);
            }
        }
    }
    map
}

/// Determine Debian architecture from Rust target triple.
pub fn deb_arch_from_triple(triple: &str) -> Result<String>
{
    let arch = match triple.split('-').next().unwrap_or("")
    {
        "x86_64" => "amd64",
        "aarch64" => "arm64",
        "armv7" => "armhf",
        "arm" => "armel",
        "i686" | "i586" | "i386" => "i386",
        "powerpc64le" => "ppc64el",
        "s390x" => "s390x",
        "mips64el" => "mips64el",
        "riscv64" => "riscv64",
        other =>
        {
            anyhow::bail!("Unsupported architecture for Debian: {}", other)
        },
    };
    Ok(arch.to_string())
}