cargo-crapapp 0.4.2

Cargo subcommand for building CRAP app installers and self-contained app bundles
Documentation
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
use std::collections::BTreeSet;
use std::fs;
use std::io::Write;
use std::path::Path;

use anyhow::{Context, bail};
use chrono::{DateTime, TimeZone, Utc};
use flate2::Compression;
use flate2::write::GzEncoder;

use crate::linux_installer::{GeneratedFile, install_relative_path};
use crate::manifest_file::{AssociatedFile, AssociatedFileKind, EulaFile};
use crate::payload_file::PayloadFile;

pub struct DebSpec {
    pub package: String,
    pub version: String,
    pub bundled_at: String,
    pub maintainer: String,
    pub summary: String,
    pub description: String,
    pub homepage: Option<String>,
    pub architecture: String,
    pub files: Vec<PayloadFile>,
    pub generated_files: Vec<GeneratedFile>,
    pub associated_files: Vec<AssociatedFile>,
    pub eulas: Vec<EulaFile>,
}

pub fn build(spec: &DebSpec, output: &Path) -> anyhow::Result<()> {
    validate(spec)?;

    if let Some(parent) = output.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
    }

    let control = control_archive(spec)?;
    let data = data_archive(spec)?;
    let timestamp = package_timestamp(&spec.bundled_at);
    let mut package = Vec::new();
    package.extend_from_slice(b"!<arch>\n");
    write_ar_entry(&mut package, "debian-binary", b"2.0\n", timestamp)?;
    write_ar_entry(&mut package, "control.tar.gz", &control, timestamp)?;
    write_ar_entry(&mut package, "data.tar.gz", &data, timestamp)?;

    fs::write(output, package).with_context(|| format!("failed to write {}", output.display()))?;

    Ok(())
}

fn validate(spec: &DebSpec) -> anyhow::Result<()> {
    if spec.files.is_empty() {
        bail!("deb package has no files to package");
    }

    Ok(())
}

fn control_archive(spec: &DebSpec) -> anyhow::Result<Vec<u8>> {
    let timestamp = package_timestamp(&spec.bundled_at);
    let homepage = spec
        .homepage
        .as_deref()
        .filter(|homepage| !homepage.trim().is_empty())
        .map(|homepage| format!("Homepage: {homepage}\n"))
        .unwrap_or_default();
    let control = format!(
        "Package: {}\nVersion: {}\nArchitecture: {}\nMaintainer: {}\nInstalled-Size: {}\nSection: utils\nPriority: optional\n{}X-Cargo-Crapapp-Bundled-At: {}\nDescription: {}\n {}\n",
        spec.package,
        spec.version,
        spec.architecture,
        spec.maintainer,
        installed_size_kbytes(spec)?,
        homepage,
        spec.bundled_at,
        spec.summary,
        deb_long_description(&spec.description)
    );

    let mut tar = Vec::new();
    write_tar_file(&mut tar, "./control", control.as_bytes(), 0o644, timestamp)?;
    write_tar_file(
        &mut tar,
        "./postinst",
        metadata_cache_refresh_script().as_bytes(),
        0o755,
        timestamp,
    )?;
    finish_tar(&mut tar);
    gzip(&tar)
}

fn metadata_cache_refresh_script() -> &'static str {
    r#"#!/bin/sh
if command -v update-desktop-database >/dev/null 2>&1; then
  update-desktop-database /usr/share/applications >/dev/null 2>&1 || :
fi
if command -v gtk-update-icon-cache >/dev/null 2>&1; then
  gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor >/dev/null 2>&1 || :
fi
if command -v appstreamcli >/dev/null 2>&1; then
  appstreamcli refresh-cache --force >/dev/null 2>&1 || :
fi
exit 0
"#
}

fn data_archive(spec: &DebSpec) -> anyhow::Result<Vec<u8>> {
    let mut tar = Vec::new();
    let timestamp = package_timestamp(&spec.bundled_at);
    let mut directories = BTreeSet::new();

    for file in &spec.files {
        let bytes =
            fs::read(&file.source).with_context(|| format!("failed to read {}", file.source))?;
        let path = tar_path(&file.destination)?;
        collect_parent_directories(&path, &mut directories);
        let mode = if file.executable { 0o755 } else { 0o644 };
        write_tar_file(&mut tar, &path, &bytes, mode, timestamp)?;
    }

    for file in &spec.generated_files {
        let path = tar_path(&file.install_path)?;
        collect_parent_directories(&path, &mut directories);
        let mode = if file.executable { 0o755 } else { 0o644 };
        write_tar_file(&mut tar, &path, &file.bytes, mode, timestamp)?;
    }

    for file in &spec.associated_files {
        let path = tar_path(&file.path)?;
        match file.kind {
            AssociatedFileKind::Directory => {
                collect_parent_directories(&path, &mut directories);
                directories.remove(&path);
                write_tar_directory(&mut tar, &path, 0o755, timestamp)?
            }
            AssociatedFileKind::File => {
                collect_parent_directories(&path, &mut directories);
                write_tar_file(&mut tar, &path, b"", 0o644, timestamp)?
            }
        }
    }

    for eula in &spec.eulas {
        let source = Path::new(eula.path());
        let bytes = fs::read(source)
            .with_context(|| format!("failed to read Linux package EULA {}", source.display()))?;
        let file_name = source
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("LICENSE");
        let path = format!("./usr/share/doc/{}/licenses/{file_name}", spec.package);
        collect_parent_directories(&path, &mut directories);
        write_tar_file(&mut tar, &path, &bytes, 0o644, timestamp)?;
    }

    let existing_tar = std::mem::take(&mut tar);
    for directory in directories {
        write_tar_directory(&mut tar, &directory, 0o755, timestamp)?;
    }
    tar.extend_from_slice(&existing_tar);

    finish_tar(&mut tar);
    gzip(&tar)
}

fn collect_parent_directories(path: &str, directories: &mut BTreeSet<String>) {
    let path = path.strip_prefix("./").unwrap_or(path);
    let mut parts = path.split('/').collect::<Vec<_>>();
    parts.pop();

    let mut current = String::from("./");
    for part in parts {
        if part.is_empty() {
            continue;
        }
        if current != "./" {
            current.push('/');
        }
        current.push_str(part);
        directories.insert(current.clone());
    }
}

fn installed_size_kbytes(spec: &DebSpec) -> anyhow::Result<u64> {
    let mut bytes = 0;
    for file in &spec.files {
        bytes += fs::metadata(&file.source)
            .with_context(|| format!("failed to read metadata for {}", file.source))?
            .len();
    }
    for file in &spec.generated_files {
        bytes += file.bytes.len() as u64;
    }
    Ok(bytes.div_ceil(1024))
}

fn deb_long_description(description: &str) -> String {
    description
        .lines()
        .map(|line| if line.is_empty() { "." } else { line })
        .collect::<Vec<_>>()
        .join("\n ")
}

fn tar_path(path: &str) -> anyhow::Result<String> {
    Ok(format!("./{}", install_relative_path(path)?))
}

fn gzip(bytes: &[u8]) -> anyhow::Result<Vec<u8>> {
    let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
    encoder.write_all(bytes)?;
    Ok(encoder.finish()?)
}

fn package_timestamp(value: &str) -> u64 {
    DateTime::parse_from_rfc3339(value)
        .map(|value| value.with_timezone(&Utc).timestamp().max(0) as u64)
        .unwrap_or_else(|_| {
            Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0)
                .single()
                .expect("fixed source date must be valid")
                .timestamp() as u64
        })
}

fn write_ar_entry(
    output: &mut Vec<u8>,
    name: &str,
    bytes: &[u8],
    timestamp: u64,
) -> anyhow::Result<()> {
    if name.len() > 15 {
        bail!("ar entry name {name} is too long");
    }
    let mode = format!("{:o}", 0o644);
    let header = format!(
        "{:<16}{:<12}{:<6}{:<6}{:<8}{:<10}`\n",
        name,
        timestamp,
        0,
        0,
        mode,
        bytes.len()
    );
    output.extend_from_slice(header.as_bytes());
    output.extend_from_slice(bytes);
    if !bytes.len().is_multiple_of(2) {
        output.push(b'\n');
    }

    Ok(())
}

fn write_tar_file(
    output: &mut Vec<u8>,
    path: &str,
    bytes: &[u8],
    mode: u32,
    timestamp: u64,
) -> anyhow::Result<()> {
    write_tar_header(output, path, mode, bytes.len() as u64, b'0', timestamp)?;
    output.extend_from_slice(bytes);
    pad_tar(output);
    Ok(())
}

fn write_tar_directory(
    output: &mut Vec<u8>,
    path: &str,
    mode: u32,
    timestamp: u64,
) -> anyhow::Result<()> {
    let path = if path.ends_with('/') {
        path.to_owned()
    } else {
        format!("{path}/")
    };
    write_tar_header(output, &path, mode, 0, b'5', timestamp)
}

fn write_tar_header(
    output: &mut Vec<u8>,
    path: &str,
    mode: u32,
    size: u64,
    typeflag: u8,
    timestamp: u64,
) -> anyhow::Result<()> {
    let name = path.strip_prefix("./").unwrap_or(path);
    if name.len() > 100 {
        bail!("tar path {path} is too long");
    }

    let mut header = [0u8; 512];
    write_tar_bytes(&mut header[0..100], name.as_bytes());
    write_tar_octal(&mut header[100..108], mode as u64)?;
    write_tar_octal(&mut header[108..116], 0)?;
    write_tar_octal(&mut header[116..124], 0)?;
    write_tar_octal(&mut header[124..136], size)?;
    write_tar_octal(&mut header[136..148], timestamp)?;
    for byte in &mut header[148..156] {
        *byte = b' ';
    }
    header[156] = typeflag;
    write_tar_bytes(&mut header[257..263], b"ustar\0");
    write_tar_bytes(&mut header[263..265], b"00");
    write_tar_bytes(&mut header[265..297], b"root");
    write_tar_bytes(&mut header[297..329], b"root");

    let checksum = header.iter().map(|byte| *byte as u64).sum::<u64>();
    write_tar_checksum(&mut header[148..156], checksum)?;
    output.extend_from_slice(&header);

    Ok(())
}

fn write_tar_bytes(field: &mut [u8], bytes: &[u8]) {
    let len = bytes.len().min(field.len());
    field[..len].copy_from_slice(&bytes[..len]);
}

fn write_tar_octal(field: &mut [u8], value: u64) -> anyhow::Result<()> {
    let width = field.len();
    let text = format!("{value:0width$o}", width = width - 1);
    if text.len() + 1 > width {
        bail!("tar octal value {value} does not fit in {width} bytes");
    }
    field[..text.len()].copy_from_slice(text.as_bytes());
    field[text.len()] = 0;
    Ok(())
}

fn write_tar_checksum(field: &mut [u8], value: u64) -> anyhow::Result<()> {
    let text = format!("{value:06o}\0 ");
    if text.len() != field.len() {
        bail!("tar checksum field has invalid width");
    }
    field.copy_from_slice(text.as_bytes());
    Ok(())
}

fn pad_tar(output: &mut Vec<u8>) {
    while !output.len().is_multiple_of(512) {
        output.push(0);
    }
}

fn finish_tar(output: &mut Vec<u8>) {
    output.extend_from_slice(&[0; 1024]);
}

#[cfg(test)]
mod tests {
    use super::{DebSpec, build};
    use crate::payload_file::PayloadFile;
    use std::fs;

    #[test]
    fn deb_package_contains_standard_members() {
        let temp_dir =
            std::env::temp_dir().join(format!("cargo-crapapp-deb-{}", std::process::id()));
        let source_dir = temp_dir.join("source");
        let executable = source_dir.join("../../example");
        let output = temp_dir.join("example.deb");

        let _ = fs::remove_dir_all(&temp_dir);
        fs::create_dir_all(&source_dir).expect("source dir should be created");
        fs::write(&executable, b"bin").expect("executable should be written");

        build(
            &DebSpec {
                package: "example".to_owned(),
                version: "1.0.0".to_owned(),
                bundled_at: "2000-01-01T00:00:00Z".to_owned(),
                maintainer: "ufnkam".to_owned(),
                summary: "Example App".to_owned(),
                description: "Example App".to_owned(),
                homepage: Some("https://example.com".to_owned()),
                architecture: "amd64".to_owned(),
                files: vec![PayloadFile::executable(
                    executable.display().to_string(),
                    "/usr/bin/example".to_owned(),
                )],
                generated_files: Vec::new(),
                associated_files: Vec::new(),
                eulas: Vec::new(),
            },
            &output,
        )
        .expect("deb should be written");

        let bytes = fs::read(&output).expect("deb should be readable");
        assert!(bytes.starts_with(b"!<arch>\n"));
        assert_eq!(&bytes[8..24], b"debian-binary   ");
        assert!(
            bytes
                .windows("debian-binary".len())
                .any(|window| { window == b"debian-binary" })
        );
        assert!(
            bytes
                .windows("control.tar.gz".len())
                .any(|window| { window == b"control.tar.gz" })
        );
        assert!(
            bytes
                .windows("data.tar.gz".len())
                .any(|window| { window == b"data.tar.gz" })
        );

        let _ = fs::remove_dir_all(&temp_dir);
    }
}