onelf-format 0.6.0

Binary format definitions for onelf
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Runtime knowledge derived from the built-in framework catalog.
//!
//! The missing-resource lint and the environment-name classification the
//! runtime needs both come from the catalog's runtime path definitions,
//! so a mapping added to a descriptor extends both without a further
//! list anywhere else. [`subjects`] names, for every mapped resource,
//! the compiled-in path endings that reference it and the places whose
//! carried copy answers them; [`classify`] and [`names`] describe how a
//! library reads the value of each mapped environment name.
//!
//! The registered runtime hooks and the launch's own procedures assign
//! further environment names outside the path groups. Their names are
//! fixed closed lists in [`crate::rules`], where descriptor validation
//! also refuses to map them, so every consumer classifies one set of names.

use std::collections::BTreeMap;
use std::path::Path;
use std::sync::OnceLock;

use crate::catalog;
pub use crate::rules::ValueShape;
use crate::rules::{
    Descriptor, HOOK_NAMES, PROCEDURE_NAMES, PathSelection, ResourceKind, RuntimeBase,
    RuntimeHookId, RuntimePathGroup, SourceBase,
};

/// Where the bundle carries a resource so the runtime's search finds it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Place<'a> {
    /// Below one of the bundle's library directories.
    Library(&'a str),
    /// Below the bundle's root.
    Root(&'a str),
}

/// One mapped resource the lint watches: the endings of compiled-in
/// paths that reference it, the places whose carried copy answers them,
/// and the environment name the runtime maps it through.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Subject<'a> {
    /// Compiled-in path endings, each beginning with `/`.
    pub endings: Vec<String>,
    /// The places carrying a copy answers every ending.
    pub places: Vec<Place<'a>>,
    /// The kind the resource must have where it is carried.
    pub kind: ResourceKind,
    /// The environment name the runtime maps the resource through.
    pub variable: &'a str,
}

/// The runtime classification of one environment name.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NameClass {
    /// How the reading library splits the value.
    pub shape: ValueShape,
    /// The kind of path the name points at.
    pub kind: ResourceKind,
}

/// The names whose host copies stay reachable, which the lint leaves to
/// the runtime instead of asking the bundle to answer: the toolkit
/// module trees and the host's message catalogs.
const LINT_EXEMPT: &[&str] = &[
    "GI_TYPELIB_PATH",
    "GTK_PATH",
    "QT_PLUGIN_PATH",
    "TEXTDOMAINDIR",
];

/// One binding of a registered runtime hook to the descriptor group it
/// publishes, with the environment name that names the resource.
type HookBinding = (RuntimeHookId, &'static str, &'static str, &'static str);

/// The descriptor group each registered runtime hook publishes, with
/// the environment name that names it. The registry is closed, so this
/// is a fixed list.
const HOOK_BINDINGS: &[HookBinding] = &[
    (
        RuntimeHookId::GioModulePaths,
        "gio",
        "modules",
        "GIO_MODULE_DIR",
    ),
    (
        RuntimeHookId::TclTkLibraries,
        "tcl-8.6",
        "scripts",
        "TCL_LIBRARY",
    ),
    (
        RuntimeHookId::TclTkLibraries,
        "tcl-9.0",
        "scripts",
        "TCL_LIBRARY",
    ),
    (
        RuntimeHookId::TclTkLibraries,
        "tk-8.6",
        "scripts",
        "TK_LIBRARY",
    ),
    (
        RuntimeHookId::TclTkLibraries,
        "tk-9.0",
        "scripts",
        "TK_LIBRARY",
    ),
];

/// The missing-resource subjects the built-in catalog derives, in
/// catalog order.
///
/// A subject exists for every mapped resource the bundle can carry so
/// the runtime finds it. The names in [`LINT_EXEMPT`] have none, and
/// neither does the GIO module directory, whose path GLib builds at run
/// time; the hooks' carried resources join through their bindings.
pub fn subjects() -> Vec<Subject<'static>> {
    let mut out = subjects_in(catalog::builtins());
    out.extend(hook_subjects(RuntimeHookId::TclTkLibraries));
    out
}

/// The missing-resource subjects a set of descriptors derives.
pub fn subjects_in(descriptors: &[Descriptor]) -> Vec<Subject<'_>> {
    descriptors
        .iter()
        .flat_map(|descriptor| &descriptor.runtime.paths)
        .filter_map(group_subject)
        .collect()
}

/// The missing-resource subject one runtime path group derives, or
/// `None` when the lint leaves the group to the runtime.
pub fn group_subject(group: &RuntimePathGroup) -> Option<Subject<'_>> {
    if group
        .variables
        .iter()
        .any(|name| LINT_EXEMPT.contains(&name.as_str()))
    {
        return None;
    }
    let mut endings = Vec::new();
    for candidate in &group.candidates {
        push_endings(&mut endings, group.kind, candidate.path.as_str());
    }
    Some(Subject {
        endings,
        places: group
            .candidates
            .iter()
            .map(|candidate| runtime_place(candidate.base, candidate.path.as_str()))
            .collect(),
        kind: group.kind,
        variable: group.variables.first()?.as_str(),
    })
}

/// The subjects the registered runtime hooks publish through the
/// descriptor groups their bindings name.
pub fn hook_subjects(hook: RuntimeHookId) -> Vec<Subject<'static>> {
    let mut out = Vec::new();
    for (bound, descriptor_id, group_id, variable) in HOOK_BINDINGS {
        if *bound != hook {
            continue;
        }
        let group = catalog::builtins()
            .iter()
            .find(|descriptor| descriptor.id.as_str() == *descriptor_id)
            .and_then(|descriptor| {
                descriptor
                    .resources
                    .plugins
                    .iter()
                    .chain(&descriptor.resources.data)
                    .find(|group| group.id.as_str() == *group_id)
            });
        let Some(group) = group else {
            continue;
        };
        let mut endings = Vec::new();
        for candidate in &group.candidates {
            push_endings(&mut endings, group.kind, candidate.path.as_str());
        }
        out.push(Subject {
            endings,
            places: group
                .candidates
                .iter()
                .map(|candidate| source_place(candidate.base, candidate.path.as_str()))
                .collect(),
            kind: group.kind,
            variable,
        });
    }
    out
}

/// Every environment name the built-in definitions map, in sorted order.
pub fn names() -> impl Iterator<Item = &'static str> {
    classes().keys().copied()
}

/// The runtime classification of one environment name.
pub fn classify(name: &str) -> Option<NameClass> {
    classes().get(name).copied()
}

/// The classification a set of descriptors gives one of its mapped
/// names, hooks and procedures apart.
pub fn classify_in(descriptors: &[Descriptor], name: &str) -> Option<NameClass> {
    descriptors
        .iter()
        .flat_map(|descriptor| &descriptor.runtime.paths)
        .find(|group| group.variables.iter().any(|v| v.as_str() == name))
        .map(group_class)
}

/// The classification one runtime path group gives all its aliases.
///
/// A first-match group assigns one path; the other selections join one
/// path per root and so read as a list.
pub fn group_class(group: &RuntimePathGroup) -> NameClass {
    NameClass {
        shape: match group.selection {
            PathSelection::First => ValueShape::Single,
            PathSelection::All | PathSelection::FirstPerLibraryRoot => ValueShape::List,
        },
        kind: group.kind,
    }
}

/// The classification of every mapped name, built once.
fn classes() -> &'static BTreeMap<&'static str, NameClass> {
    static CLASSES: OnceLock<BTreeMap<&'static str, NameClass>> = OnceLock::new();
    CLASSES.get_or_init(|| {
        let mut out = BTreeMap::new();
        for descriptor in catalog::builtins() {
            for group in &descriptor.runtime.paths {
                let class = group_class(group);
                for name in &group.variables {
                    out.insert(name.as_str(), class);
                }
            }
        }
        for (_, names) in HOOK_NAMES.iter() {
            for (name, shape, kind) in names.iter() {
                out.insert(
                    name,
                    NameClass {
                        shape: *shape,
                        kind: *kind,
                    },
                );
            }
        }
        for (name, shape, kind) in PROCEDURE_NAMES.iter() {
            out.insert(
                name,
                NameClass {
                    shape: *shape,
                    kind: *kind,
                },
            );
        }
        out
    })
}

/// Where a runtime candidate is carried: a library-root candidate below
/// a library directory, a package-root candidate below the root.
fn runtime_place(base: RuntimeBase, path: &str) -> Place<'_> {
    match base {
        RuntimeBase::LibraryRoot => Place::Library(path),
        RuntimeBase::PackageRoot => Place::Root(path),
    }
}

/// Where a source candidate is carried: a library-directory candidate
/// below a library directory, an installation-prefix candidate below
/// the root.
fn source_place(base: SourceBase, path: &str) -> Place<'_> {
    match base {
        SourceBase::LibraryDir => Place::Library(path),
        SourceBase::InstallationPrefix => Place::Root(path),
    }
}

/// The compiled-in path endings that reference a candidate: the path
/// itself, and for a file also the path with its last extension
/// removed, which is how a library names a database its code reads with
/// the extension added (libmagic reads `magic.mgc` for a compiled-in
/// `magic`).
fn push_endings(out: &mut Vec<String>, kind: ResourceKind, path: &str) {
    let full = format!("/{path}");
    if !out.contains(&full) {
        out.push(full);
    }
    if kind != ResourceKind::File {
        return;
    }
    let source = Path::new(path);
    if let (Some(parent), Some(stem)) = (source.parent(), source.file_stem())
        && source.extension().is_some()
    {
        let stem = format!("/{}", parent.join(stem).to_string_lossy());
        if !out.contains(&stem) {
            out.push(stem);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rules::{
        BuildRules, EnvironmentName, Inheritance, RelativePath, ResourceKind, Resources, RuleId,
        RuntimeCandidate, RuntimeRules, SCHEMA_VERSION,
    };

    fn descriptor(paths: Vec<RuntimePathGroup>) -> Descriptor {
        Descriptor {
            schema: SCHEMA_VERSION,
            id: RuleId::new("proof-framework").unwrap(),
            triggers: Vec::new(),
            resources: Resources::default(),
            build: BuildRules::default(),
            runtime: RuntimeRules {
                paths,
                hooks: Vec::new(),
            },
        }
    }

    fn group(id: &str, variable: &str, selection: PathSelection) -> RuntimePathGroup {
        RuntimePathGroup {
            id: RuleId::new(id).unwrap(),
            kind: ResourceKind::Directory,
            candidates: vec![RuntimeCandidate {
                base: RuntimeBase::LibraryRoot,
                path: RelativePath::new("proof/plugins").unwrap(),
            }],
            variables: vec![EnvironmentName::new(variable).unwrap()],
            selection,
            inheritance: Inheritance::Replace,
        }
    }

    /// A mapping added to the catalog extends both consumers through the
    /// same derivation, with no per-consumer list to edit.
    #[test]
    fn a_new_mapping_extends_both_consumers_without_further_tables() {
        let synthetic = group("proof", "PROOF_PLUGIN_PATH", PathSelection::All);

        let subject = group_subject(&synthetic).expect("the lint watches the new mapping");
        assert_eq!(subject.endings, ["/proof/plugins"]);
        assert_eq!(subject.places, [Place::Library("proof/plugins")]);
        assert_eq!(subject.kind, ResourceKind::Directory);
        assert_eq!(subject.variable, "PROOF_PLUGIN_PATH");

        assert_eq!(
            group_class(&synthetic),
            NameClass {
                shape: ValueShape::List,
                kind: ResourceKind::Directory,
            }
        );

        let descriptor = descriptor(vec![synthetic]);
        assert_eq!(subjects_in(std::slice::from_ref(&descriptor)).len(), 1);
        assert_eq!(
            classify_in(std::slice::from_ref(&descriptor), "PROOF_PLUGIN_PATH"),
            Some(NameClass {
                shape: ValueShape::List,
                kind: ResourceKind::Directory,
            })
        );
        assert_eq!(
            classify_in(std::slice::from_ref(&descriptor), "OTHER"),
            None
        );
    }

    /// Every binding resolves against the built-in catalog, so a rename
    /// there cannot silently drop a hook's subject.
    #[test]
    fn every_hook_binding_names_a_catalog_group() {
        for (hook, descriptor_id, group_id, variable) in HOOK_BINDINGS {
            let descriptor = catalog::builtins()
                .iter()
                .find(|descriptor| descriptor.id.as_str() == *descriptor_id)
                .unwrap_or_else(|| panic!("{descriptor_id} is missing"));
            assert!(
                descriptor
                    .resources
                    .plugins
                    .iter()
                    .chain(&descriptor.resources.data)
                    .any(|group| group.id.as_str() == *group_id),
                "{descriptor_id}/{group_id} is missing"
            );
            let subject = hook_subjects(*hook)
                .into_iter()
                .find(|subject| subject.variable == *variable)
                .unwrap_or_else(|| panic!("{variable} has no subject"));
            assert!(!subject.places.is_empty(), "{variable}");
        }
    }

    /// The classification follows the group definitions: first-match
    /// names read as one path, the other selections as lists, and the
    /// kind is the group's.
    #[test]
    fn classification_follows_the_group_definitions() {
        for descriptor in catalog::builtins() {
            for group in &descriptor.runtime.paths {
                let class = group_class(group);
                assert_eq!(class.kind, group.kind, "{}", group.id.as_str());
                assert_eq!(
                    class.shape == ValueShape::Single,
                    group.selection == PathSelection::First,
                    "{}",
                    group.variables[0].as_str()
                );
            }
        }
    }

    /// The names the launch's hooks and procedures assign keep their
    /// classification reachable.
    #[test]
    fn hook_and_procedure_names_are_classified() {
        assert_eq!(
            classify("GIO_MODULE_DIR"),
            Some(NameClass {
                shape: ValueShape::Single,
                kind: ResourceKind::Directory,
            })
        );
        assert_eq!(
            classify("GIO_EXTRA_MODULES"),
            Some(NameClass {
                shape: ValueShape::List,
                kind: ResourceKind::Directory,
            })
        );
        assert_eq!(
            classify("SSL_CERT_FILE"),
            Some(NameClass {
                shape: ValueShape::Single,
                kind: ResourceKind::File,
            })
        );
        assert!(classify("ONELF_UNMAPPED_NAME").is_none());
        for name in names() {
            assert!(classify(name).is_some(), "{name}");
        }
    }

    /// The lint derives no subject for the names whose host copies stay
    /// reachable, and a file candidate contributes its extension-stripped
    /// ending next to the full path.
    #[test]
    fn subjects_skip_exempt_names_and_add_file_stems() {
        for exempt in LINT_EXEMPT {
            assert!(
                catalog::builtins()
                    .iter()
                    .all(|descriptor| subjects_in(std::slice::from_ref(descriptor))
                        .iter()
                        .all(|subject| subject.variable != *exempt)),
                "{exempt} has a subject"
            );
        }
        let magic = subjects()
            .into_iter()
            .find(|subject| subject.variable == "MAGIC")
            .expect("MAGIC has a subject");
        assert_eq!(
            magic.endings,
            [
                "/share/file/misc/magic.mgc",
                "/share/file/misc/magic",
                "/share/misc/magic.mgc",
                "/share/misc/magic",
            ]
        );
        assert_eq!(
            magic.places,
            [
                Place::Root("share/file/misc/magic.mgc"),
                Place::Root("share/misc/magic.mgc"),
            ]
        );
    }
}