wolfpack 0.3.1

A package manager and a build tool that supports major package formats (deb, RPM, ipk, pkg, MSIX).
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
416
417
418
419
use fs_err::File;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt::Display;
use std::fmt::Formatter;
use std::io::Read;
use std::os::unix::fs::symlink;
use std::path::Path;
use std::path::PathBuf;

use normalize_path::NormalizePath;
use serde::Deserialize;
use serde::Serialize;
use walkdir::WalkDir;
use xz::write::XzEncoder;

use crate::archive::ArchiveWrite;
use crate::archive::TarBuilder;
use crate::hash::Sha256Reader;
use crate::pkg::Package;
use crate::pkg::PackageMeta;
use crate::pkg::SigningKey;

pub struct Repository {
    packages: Vec<PackageMeta>,
}

impl Repository {
    pub fn new<I, P>(paths: I) -> Result<Self, std::io::Error>
    where
        I: IntoIterator<Item = P>,
        P: AsRef<Path>,
    {
        let mut packages = Vec::new();
        let mut push_package = |directory: &Path, path: &Path| -> Result<(), std::io::Error> {
            let relative_path = Path::new(".").join(
                path.strip_prefix(directory)
                    .map_err(std::io::Error::other)?
                    .normalize(),
            );
            let mut reader = Sha256Reader::new(File::open(path)?);
            let compact = Package::read_compact_manifest(reader.by_ref())?;
            let (sha256, size) = reader.digest()?;
            let meta = PackageMeta {
                compact,
                pkgsize: size as u32,
                sum: sha256.to_string(),
                path: relative_path.clone(),
                repopath: relative_path,
            };
            packages.push(meta);
            Ok(())
        };
        for path in paths.into_iter() {
            let path = path.as_ref();
            if path.is_dir() {
                for entry in WalkDir::new(path).into_iter() {
                    let entry = entry?;
                    if entry.file_type().is_dir() || !is_package(entry.path()) {
                        continue;
                    }
                    push_package(path, entry.path())?
                }
            } else {
                // TODO
                push_package(Path::new("."), path)?
            }
        }
        Ok(Self { packages })
    }

    pub fn build<P: AsRef<Path>>(
        self,
        output_dir: P,
        signing_key: &SigningKey,
    ) -> Result<(), std::io::Error> {
        let output_dir = output_dir.as_ref();
        let meta = MetaConf::default().to_string();
        fs_err::write(output_dir.join("meta.conf"), &meta)?;
        symlink("meta.conf", output_dir.join("meta"))?;
        tar_xz_from_signed_file(
            Path::new("meta"),
            output_dir.join("meta.txz"),
            &meta,
            signing_key,
        )?;
        let mut packagesite = Vec::new();
        for manifest in self.packages.iter() {
            packagesite.extend(manifest.to_vec()?);
            packagesite.push(b'\n');
        }
        tar_xz_from_signed_file(
            Path::new("packagesite.yaml"),
            output_dir.join("packagesite.pkg"),
            packagesite,
            signing_key,
        )?;
        symlink("packagesite.pkg", output_dir.join("packagesite.txz"))?;
        let data_pkg = DataPkg {
            groups: Default::default(),
            packages: self.packages,
        };
        tar_xz_from_signed_file(
            Path::new("data"),
            output_dir.join("data.pkg"),
            data_pkg.to_vec()?,
            signing_key,
        )?;
        symlink("data.pkg", output_dir.join("data.txz"))?;
        Ok(())
    }

    pub fn iter(&self) -> impl Iterator<Item = &PackageMeta> {
        self.packages.iter()
    }
}

impl IntoIterator for Repository {
    type Item = <Vec<PackageMeta> as IntoIterator>::Item;
    type IntoIter = <Vec<PackageMeta> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.packages.into_iter()
    }
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum MirrorType {
    Srv,
    Http,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum SignatureType {
    Pubkey,
    Fingerprints,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RepoConf {
    #[serde(skip)]
    pub name: String,
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub env: HashMap<String, String>,
    pub url: String,
    #[serde(skip_serializing_if = "is_true")]
    pub enabled: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mirror_type: Option<MirrorType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signature_type: Option<SignatureType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pubkey: Option<PathBuf>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fingerprints: Option<PathBuf>,
    #[serde(skip_serializing_if = "is_zero")]
    pub ip_version: u32,
    #[serde(skip_serializing_if = "is_zero")]
    pub priority: u32,
}

impl RepoConf {
    pub fn new(name: String, url: String, pubkey: PathBuf) -> Self {
        Self {
            name,
            env: Default::default(),
            url,
            enabled: true,
            mirror_type: None,
            signature_type: Some(SignatureType::Pubkey),
            pubkey: Some(pubkey),
            fingerprints: None,
            ip_version: 0,
            priority: 0,
        }
    }
}

impl Display for RepoConf {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        let mut wrapper = HashMap::new();
        wrapper.insert(self.name.clone(), self);
        let s = serde_json::to_string_pretty(&wrapper).map_err(|_| std::fmt::Error)?;
        f.write_str(&s)
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MetaConf {
    version: u32,
    packing_format: PackingFormat,
    manifests: String,
    data: String,
    filesite: String,
    manifests_archive: String,
    filesite_archive: String,
}

impl Default for MetaConf {
    fn default() -> Self {
        Self {
            version: 2,
            packing_format: Default::default(),
            manifests: "packagesite.yaml".into(),
            data: "data".into(),
            filesite: "filesite.yaml".into(),
            manifests_archive: "packagesite".into(),
            filesite_archive: "filesite".into(),
        }
    }
}

impl Display for MetaConf {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        let s = serde_json::to_string_pretty(self).map_err(|_| std::fmt::Error)?;
        f.write_str(&s)
    }
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "snake_case")]
pub enum PackingFormat {
    Tzst,
    #[default]
    Txz,
    Tbz,
    Tgz,
    Tar,
}

impl PackingFormat {
    pub fn as_str(&self) -> &str {
        use PackingFormat::*;
        match self {
            Tzst => "tzst",
            Txz => "txz",
            Tbz => "tbz",
            Tgz => "tgz",
            Tar => "tar",
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct DataPkg {
    pub groups: Vec<String>,
    pub packages: Vec<PackageMeta>,
}

impl DataPkg {
    pub fn to_vec(&self) -> Result<Vec<u8>, serde_json::Error> {
        serde_json::to_vec(self)
    }
}

fn is_zero(value: &u32) -> bool {
    *value == 0
}

fn is_true(value: &bool) -> bool {
    *value
}

fn is_package(path: &Path) -> bool {
    let Some(extension) = path.extension() else {
        return false;
    };
    PACKAGE_EXTENSIONS
        .iter()
        .any(|e| OsStr::new(e) == extension)
}

fn tar_xz_from_signed_file<P1, P2, C>(
    inner_path: P1,
    outer_path: P2,
    contents: C,
    signing_key: &SigningKey,
) -> Result<(), std::io::Error>
where
    P1: AsRef<Path>,
    P2: AsRef<Path>,
    C: AsRef<[u8]>,
{
    let signature = sign(signing_key, contents.as_ref())?;
    TarXzFile::from_files(
        [
            // signature should be the first entry in the archive
            (Path::new("signature"), &signature[..]),
            (inner_path.as_ref(), contents.as_ref()),
        ],
        xz_file(outer_path)?,
    )?
    .finish()?;
    Ok(())
}

fn xz_file<P: AsRef<Path>>(path: P) -> Result<XzFile, std::io::Error> {
    Ok(XzEncoder::new(
        File::create(path.as_ref())?,
        COMPRESSION_LEVEL,
    ))
}

fn sign<C: AsRef<[u8]>>(signing_key: &SigningKey, contents: C) -> Result<Vec<u8>, std::io::Error> {
    let signature = signing_key
        .sign(contents.as_ref())
        .map_err(|_| std::io::Error::other("signing failed"))?;
    let mut s = Vec::new();
    s.extend(b"$PKGSIGN:ecdsa$");
    s.extend(signature.serialize_der());
    Ok(s)
}

const COMPRESSION_LEVEL: u32 = 9;
const PACKAGE_EXTENSIONS: [&str; 6] = ["pkg", "tzst", "txz", "tbz", "tgz", "tar"];

type XzFile = XzEncoder<File>;
type TarXzFile = TarBuilder<XzFile>;

#[cfg(test)]
mod tests {
    use fs_err::create_dir_all;
    use std::process::Command;

    use arbtest::arbtest;
    use command_error::CommandExt;
    use tempfile::TempDir;

    use super::*;
    use crate::pkg::CompactManifest;
    use crate::test::prevent_concurrency;
    use crate::test::DirectoryOfFiles;

    #[test]
    fn write_read() {
        arbtest(|u| {
            let package: CompactManifest = u.arbitrary()?;
            let directory: DirectoryOfFiles = u.arbitrary()?;
            let mut buf: Vec<u8> = Vec::new();
            Package::new(package.clone(), directory.path().into())
                .write(&mut buf)
                .unwrap();
            let actual = Package::read_compact_manifest(&buf[..]).unwrap();
            assert_eq!(package, actual);
            Ok(())
        });
    }

    #[ignore = "Needs FreeBSD's `pkg`"]
    #[test]
    fn freebsd_pkg_adds_repo() {
        let _guard = prevent_concurrency("freebsd-pkg");
        arbtest(|u| {
            let workdir = TempDir::new().unwrap();
            let package_file = workdir.path().join("test.pkg");
            let verifying_key_file = workdir.path().join("verifying-key");
            let signing_key_file = workdir.path().join("signing-key");
            let mut package: CompactManifest = u.arbitrary()?;
            package.flatsize = 100;
            package.deps.clear(); // missing dependencies
            package.arch = "Linux:3.2.0:amd64".into();
            package.abi = "Linux:3.2.0:amd64".into();
            let directory: DirectoryOfFiles = u.arbitrary()?;
            Package::new(package.clone(), directory.path().into())
                .write(File::create(package_file.as_path()).unwrap())
                .unwrap();
            let (signing_key, verifying_key) = SigningKey::generate();
            fs_err::write(&verifying_key_file, verifying_key.to_der().unwrap()).unwrap();
            fs_err::write(&signing_key_file, signing_key.to_der().unwrap()).unwrap();
            let repository = Repository::new([workdir.path()]).unwrap();
            repository.build(workdir.path(), &signing_key).unwrap();
            create_dir_all("/etc/pkg").unwrap();
            let repo_conf = RepoConf::new(
                "test".into(),
                format!("file://{}", workdir.path().display()),
                verifying_key_file.clone(),
            );
            fs_err::write("/etc/pkg/test.conf", format!("{}\n", repo_conf)).unwrap();
            assert!(
                Command::new("pkg")
                    .arg("--debug")
                    .arg("update")
                    .arg("--force")
                    .arg("--repository")
                    .arg("test")
                    .status_checked()
                    .unwrap()
                    .success(),
                "repo.conf = {:?}",
                repo_conf
            );
            assert!(
                Command::new("pkg")
                    .arg("install")
                    .arg("-y")
                    .arg(package.name.to_string())
                    .status_checked()
                    .unwrap()
                    .success(),
                "manifest:\n========{:?}========",
                package
            );
            assert!(
                Command::new("pkg")
                    .arg("remove")
                    .arg("-y")
                    .arg(package.name.to_string())
                    .status_checked()
                    .unwrap()
                    .success(),
                "manifest:\n========{:?}========",
                package
            );
            Ok(())
        });
    }
}