onelf 0.6.0

Packer CLI for creating onelf single-binary packages
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
//! The framework rule catalog one assembly uses: the built-in descriptors,
//! the project-local descriptors a recipe selects with `[bundle] rules`,
//! and the identities it disables with `[bundle] disable-rules`.
//!
//! Local descriptor files are parsed with the shared TOML adapter and
//! validated together with the built-ins before anything in the AppDir
//! changes. A local rule extends the catalog and never replaces a
//! built-in identity, even a disabled one. The same catalog drives
//! discovery and lowers the runtime records a package carries, so a
//! packed launch needs none of the descriptor files.

use std::io::{self, Read};
use std::path::{Path, PathBuf};

use onelf_format::catalog;
use onelf_format::rules::{self, Descriptor, MAX_DESCRIPTOR_BYTES, RuleId};
use onelf_format::runtime_metadata::{RuntimeRuleRecord, SetPin};

use crate::bundle::PreparedPin;

/// A validated rule catalog: the local descriptors in recipe order and the
/// disabled identities. The default is the built-in catalog alone.
#[derive(Debug, Clone, Default)]
pub struct RuleCatalog {
    local: Vec<Descriptor>,
    disabled: Vec<RuleId>,
}

impl RuleCatalog {
    /// Load and validate the descriptor `files` and `disabled` identities a
    /// recipe at `recipe_path` names. Relative files resolve against
    /// `dir`, the recipe directory.
    ///
    /// Every failure names the recipe and, for a descriptor, its file and
    /// identity: a parse or schema error, a local identity that shadows a
    /// built-in or repeats another local file's, and an unknown or repeated
    /// disabled identity. Nothing is written.
    pub fn load(
        recipe_path: &Path,
        dir: &Path,
        files: &[PathBuf],
        disabled: &[String],
    ) -> io::Result<Self> {
        let invalid = |what: String| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!("{}: {what}", recipe_path.display()),
            )
        };
        let disabled = parse_disabled(disabled).map_err(invalid)?;
        let mut local: Vec<Descriptor> = Vec::with_capacity(files.len());
        let mut sources: Vec<PathBuf> = Vec::with_capacity(files.len());
        for file in files {
            let path = dir.join(file);
            let descriptor = read_descriptor(&path)
                .map_err(|e| invalid(format!("[bundle] rules {}: {e}", path.display())))?;
            let id = descriptor.id.as_str().to_string();
            if catalog::builtins().iter().any(|b| b.id == descriptor.id) {
                return Err(invalid(format!(
                    "[bundle] rules {}: rule {id} shadows the built-in rule of that \
                     identity, which a local rule may never replace, even when it is \
                     disabled. Give the local rule an identity of its own and disable \
                     the built-in with disable-rules",
                    path.display()
                )));
            }
            if let Some(at) = local.iter().position(|d| d.id == descriptor.id) {
                return Err(invalid(format!(
                    "[bundle] rules {}: rule {id} repeats the identity {} already \
                     declares",
                    path.display(),
                    sources[at].display()
                )));
            }
            local.push(descriptor);
            sources.push(path);
            let known: Vec<RuleId> = disabled
                .iter()
                .filter(|id| {
                    catalog::builtins().iter().any(|b| b.id == **id)
                        || local.iter().any(|d| d.id == **id)
                })
                .cloned()
                .collect::<std::collections::BTreeSet<_>>()
                .into_iter()
                .collect();
            rules::validate_catalog(catalog::builtins(), &local, &known).map_err(|e| {
                invalid(format!(
                    "[bundle] rules {}: rule {id}: {e}",
                    sources.last().unwrap().display()
                ))
            })?;
        }
        rules::validate_catalog(catalog::builtins(), &local, &disabled)
            .map_err(|e| invalid(format!("[bundle] disable-rules: {e}")))?;
        Ok(Self { local, disabled })
    }

    /// The project-local descriptors, in recipe order.
    pub fn local(&self) -> &[Descriptor] {
        &self.local
    }

    /// The disabled identities, in recipe order.
    pub fn disabled(&self) -> &[RuleId] {
        &self.disabled
    }

    /// The descriptor of `id`, built-in or local, whether or not it is
    /// enabled.
    pub fn find(&self, id: &str) -> Option<&Descriptor> {
        catalog::builtins()
            .iter()
            .chain(&self.local)
            .find(|d| d.id.as_str() == id)
    }

    /// The enabled descriptors, built-in and local, sorted by identity.
    pub fn enabled(&self) -> Vec<&Descriptor> {
        let mut enabled: Vec<&Descriptor> = catalog::builtins()
            .iter()
            .chain(&self.local)
            .filter(|d| !self.disabled.contains(&d.id))
            .collect();
        enabled.sort_by(|a, b| a.id.cmp(&b.id));
        enabled
    }

    /// The runtime records a package carries, or `None` when the runtime's
    /// built-in fallback already reproduces them exactly.
    ///
    /// Every enabled rule with runtime paths or hooks contributes one
    /// unbound record, so a carried record replaces the fallback whole. Each
    /// pin a selected set's preparation wrote contributes one bound record
    /// per rule that prepared into it, carrying that rule's runtime paths,
    /// which a launch evaluates against the pin's subtree only while that
    /// pin is selected. With no binding, the record is needed only when a
    /// local rule or a disabled built-in makes the enabled runtime behavior
    /// differ from the built-ins, so a recipe without either carries no
    /// record and packs exactly as before.
    pub fn runtime_records(
        &self,
        prepared: &[PreparedPin],
    ) -> io::Result<Option<Vec<RuntimeRuleRecord>>> {
        let unbound = |d: &Descriptor| RuntimeRuleRecord {
            id: d.id.clone(),
            binding: None,
            runtime: d.runtime.clone(),
        };
        let has_runtime =
            |d: &&Descriptor| !d.runtime.paths.is_empty() || !d.runtime.hooks.is_empty();
        let mut records: Vec<RuntimeRuleRecord> = self
            .enabled()
            .into_iter()
            .filter(has_runtime)
            .map(unbound)
            .collect();
        let fallback: Vec<RuntimeRuleRecord> = catalog::builtins()
            .iter()
            .filter(has_runtime)
            .map(unbound)
            .collect();
        let mut bound = false;
        for pin in prepared {
            let binding = SetPin {
                name: RuleId::set_name(&pin.set).map_err(|e| {
                    io::Error::new(io::ErrorKind::InvalidData, format!("set {}: {e}", pin.set))
                })?,
                hash: *blake3::Hash::from_hex(&pin.pin)
                    .map_err(|_| {
                        io::Error::new(
                            io::ErrorKind::InvalidData,
                            format!("set {}: pin is not a blake3 digest", pin.set),
                        )
                    })?
                    .as_bytes(),
            };
            for rule in &pin.rules {
                let Some(descriptor) = self.find(rule) else {
                    continue;
                };
                if descriptor.runtime.paths.is_empty() || self.disabled.contains(&descriptor.id) {
                    continue;
                }
                records.push(RuntimeRuleRecord {
                    id: descriptor.id.clone(),
                    binding: Some(binding.clone()),
                    runtime: rules::RuntimeRules {
                        paths: descriptor.runtime.paths.clone(),
                        hooks: Vec::new(),
                    },
                });
                bound = true;
            }
        }
        Ok((bound || records != fallback).then_some(records))
    }
}

/// Read one descriptor file, bounded before parsing, through the shared
/// TOML adapter, which validates it before returning.
fn read_descriptor(path: &Path) -> io::Result<Descriptor> {
    let mut text = String::new();
    std::fs::File::open(path)?
        .take(MAX_DESCRIPTOR_BYTES as u64 + 1)
        .read_to_string(&mut text)?;
    onelf_format::rules_toml::parse_descriptor(&text)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.to_string()))
}

/// The disabled identities as rule identities, refusing malformed ones.
fn parse_disabled(disabled: &[String]) -> Result<Vec<RuleId>, String> {
    disabled
        .iter()
        .map(|id| RuleId::new(id).map_err(|e| format!("[bundle] disable-rules {id:?}: {e}")))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    const VENDOR: &str = r#"schema = 1
id = "vendor-framework"

[[triggers]]
soname-prefix = "libvendor.so"

[[resources.plugins]]
id = "plugins"
kind = "directory"
destination = "lib/vendor-plugins"
requirement = "optional"
candidates = [{ base = "library-dir", path = "vendor-plugins" }]

[[runtime.paths]]
id = "plugins"
kind = "directory"
variables = ["VENDOR_PLUGIN_DIR"]
selection = "first"
inheritance = "replace"
candidates = [{ base = "library-root", path = "vendor-plugins" }]
"#;

    fn dir(tag: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("onelf-rules-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    fn load(dir: &Path, files: &[(&str, &str)], disabled: &[&str]) -> io::Result<RuleCatalog> {
        for (name, text) in files {
            std::fs::write(dir.join(name), text).unwrap();
        }
        let names: Vec<PathBuf> = files.iter().map(|(n, _)| PathBuf::from(n)).collect();
        let disabled: Vec<String> = disabled.iter().map(|s| s.to_string()).collect();
        RuleCatalog::load(&dir.join("onelf.toml"), dir, &names, &disabled)
    }

    fn failure(result: io::Result<RuleCatalog>) -> String {
        result.unwrap_err().to_string()
    }

    #[test]
    fn a_local_rule_extends_the_catalog_and_a_disable_removes_a_builtin() {
        let d = dir("extend");
        let catalog = load(&d, &[("vendor.toml", VENDOR)], &["alsa"]).unwrap();
        let ids: Vec<&str> = catalog.enabled().iter().map(|r| r.id.as_str()).collect();
        assert!(ids.contains(&"vendor-framework"));
        assert!(!ids.contains(&"alsa"));
        assert!(ids.windows(2).all(|p| p[0] < p[1]), "{ids:?}");
        assert!(
            catalog.find("alsa").is_some(),
            "a disabled rule stays known"
        );
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn shadowing_a_builtin_is_refused_even_when_it_is_disabled() {
        let d = dir("shadow");
        let shadow = VENDOR.replace("vendor-framework", "alsa");
        let err = failure(load(&d, &[("alsa.toml", &shadow)], &["alsa"]));
        assert!(
            err.contains("alsa.toml") && err.contains("rule alsa shadows"),
            "{err}"
        );
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn a_repeated_local_identity_names_both_files() {
        let d = dir("repeat");
        let err = failure(load(&d, &[("a.toml", VENDOR), ("b.toml", VENDOR)], &[]));
        assert!(
            err.contains("b.toml") && err.contains("a.toml") && err.contains("vendor-framework"),
            "{err}"
        );
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn unknown_and_repeated_disables_are_refused() {
        let d = dir("disable");
        let err = failure(load(&d, &[], &["no-such-rule"]));
        assert!(
            err.contains("disable-rules") && err.contains("no-such-rule"),
            "{err}"
        );
        let err = failure(load(&d, &[], &["alsa", "alsa"]));
        assert!(
            err.contains("disable-rules") && err.contains("alsa"),
            "{err}"
        );
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn bad_schema_unknown_fields_and_truncation_name_the_file() {
        let d = dir("schema");
        for (name, text) in [
            ("version.toml", VENDOR.replace("schema = 1", "schema = 2")),
            ("field.toml", VENDOR.replace("selection =", "selektion =")),
            ("cut.toml", VENDOR[..VENDOR.len() / 2].to_string()),
            (
                "ld.toml",
                VENDOR.replace("VENDOR_PLUGIN_DIR", "LD_LIBRARY_PATH"),
            ),
        ] {
            let err = failure(load(&d, &[(name, &text)], &[]));
            assert!(
                err.contains(name) && err.contains("[bundle] rules"),
                "{err}"
            );
        }
        let err = failure(load(&d, &[], &[]).and_then(|_| {
            RuleCatalog::load(
                &d.join("onelf.toml"),
                &d,
                &[PathBuf::from("absent.toml")],
                &[],
            )
        }));
        assert!(err.contains("absent.toml"), "{err}");
        let _ = std::fs::remove_dir_all(&d);
    }

    #[test]
    fn a_conflicting_mapping_names_the_file_that_introduced_it() {
        let d = dir("conflict");
        let alias = VENDOR.replace("VENDOR_PLUGIN_DIR", "ALSA_PLUGIN_DIR");
        let err = failure(load(&d, &[("alias.toml", &alias)], &[]));
        assert!(
            err.contains("alias.toml") && err.contains("ALSA_PLUGIN_DIR"),
            "{err}"
        );
        assert!(
            load(&d, &[("alias.toml", &alias)], &["alsa"]).is_ok(),
            "disabling the built-in frees its variable"
        );
        let _ = std::fs::remove_dir_all(&d);
    }

    /// The record is carried exactly when the fallback would not
    /// reproduce it: never for the built-ins alone or a disable of a rule
    /// without runtime behavior, always for a local runtime rule, a
    /// disabled built-in mapping, or a prepared pin.
    #[test]
    fn runtime_records_are_needed_only_where_the_fallback_differs() {
        let d = dir("records");
        let plain = RuleCatalog::default();
        assert_eq!(plain.runtime_records(&[]).unwrap(), None);
        let no_runtime = load(&d, &[], &["tcl-8.6"]).unwrap();
        assert_eq!(no_runtime.runtime_records(&[]).unwrap(), None);
        let silent = VENDOR[..VENDOR.find("[[runtime.paths]]").unwrap()].to_string();
        let discovery_only = load(&d, &[("silent.toml", &silent)], &[]).unwrap();
        assert_eq!(discovery_only.runtime_records(&[]).unwrap(), None);

        let local = load(&d, &[("vendor.toml", VENDOR)], &[]).unwrap();
        let records = local.runtime_records(&[]).unwrap().unwrap();
        assert!(records.iter().any(|r| r.id.as_str() == "vendor-framework"));
        assert!(records.iter().any(|r| r.id.as_str() == "alsa"));
        assert!(records.iter().all(|r| r.binding.is_none()));

        let disabled = load(&d, &[], &["alsa"]).unwrap();
        let records = disabled.runtime_records(&[]).unwrap().unwrap();
        assert!(!records.iter().any(|r| r.id.as_str() == "alsa"));
        assert!(records.iter().any(|r| r.id.as_str() == "gstreamer"));

        let pin = PreparedPin {
            set: "toolkit".to_string(),
            pin: "ab".repeat(32),
            root: PathBuf::from(".onelf-prepared/toolkit").join("ab".repeat(32)),
            files: Vec::new(),
            rules: vec!["gstreamer".to_string()],
        };
        let records = plain.runtime_records(&[pin]).unwrap().unwrap();
        let bound: Vec<_> = records.iter().filter(|r| r.binding.is_some()).collect();
        assert_eq!(bound.len(), 1);
        assert_eq!(bound[0].id.as_str(), "gstreamer");
        assert_eq!(bound[0].binding.as_ref().unwrap().name.as_str(), "toolkit");
        assert!(bound[0].runtime.hooks.is_empty());
        let _ = std::fs::remove_dir_all(&d);
    }
}