vfstool_lib 0.9.0

A library for constructing and manipulating virtual file systems in Rust, based on OpenMW's VFS implementation.
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
// SPDX-License-Identifier: GPL-3.0-only
use crate::{
    VFS,
    analysis::{LayerIndex, SourceKind},
    matchers::CompiledGlob,
    paths::key_to_path_buf_lossy,
};
use std::{io, path::PathBuf};

/// Policy document: ordered rules evaluated against a VFS + layer index.
#[derive(Debug, Clone)]
pub struct Policy {
    /// Rules to evaluate.
    pub rules: Vec<Rule>,
}

/// Severity for policy violations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub enum Severity {
    /// Blocking failure.
    Error,
}

/// Supported policy rules.
#[derive(Debug, Clone)]
pub enum Rule {
    /// Winner source path must match `source_glob` for keys matching `path_glob`.
    WinnerMustMatch {
        /// Glob over normalized VFS keys.
        path_glob: String,
        /// Glob over winning source path strings.
        source_glob: String,
    },
    /// Winner source path must NOT match `source_glob` for keys matching `path_glob`.
    WinnerMustNotMatch {
        /// Glob over normalized VFS keys.
        path_glob: String,
        /// Glob over winning source path strings.
        source_glob: String,
    },
    /// At least one key matching `path_glob` must exist.
    MustExist {
        /// Glob over normalized VFS keys.
        path_glob: String,
    },
    /// Matching keys must have exactly one provider.
    MustBeUnique {
        /// Glob over normalized VFS keys.
        path_glob: String,
    },
    /// Matching keys must be served by the requested winner kind.
    WinnerKindMustBe {
        /// Glob over normalized VFS keys.
        path_glob: String,
        /// Required winner source kind.
        kind: SourceKind,
    },
    /// Matching keys must have <= `max` providers.
    MaxOverrideDepth {
        /// Glob over normalized VFS keys.
        path_glob: String,
        /// Maximum allowed provider count.
        max: usize,
    },
}

/// One policy violation.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub struct Violation {
    /// Rule identifier string.
    pub rule: String,
    /// Optional key associated with the violation.
    pub key: Option<PathBuf>,
    /// Human-readable message.
    pub message: String,
    /// Violation severity.
    pub severity: Severity,
}

/// Full policy evaluation result.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub struct PolicyResult {
    /// Violations found.
    pub violations: Vec<Violation>,
}

impl Policy {
    /// Evaluate this policy against layer + vfs state.
    ///
    /// # Errors
    ///
    /// Returns an error when provider/provenance resolution fails.
    pub fn evaluate(&self, index: &LayerIndex, vfs: &VFS) -> io::Result<PolicyResult> {
        let mut keys: Vec<PathBuf> = vfs
            .iter()
            .map(|(key, _)| key_to_path_buf_lossy(key))
            .collect();
        keys.sort();
        let mut violations = Vec::new();

        for rule in &self.rules {
            evaluate_rule(rule, index, vfs, &keys, &mut violations)?;
        }

        violations.sort_by(|a, b| {
            let ak = a
                .key
                .as_ref()
                .map_or_else(String::new, |k| k.display().to_string());
            let bk = b
                .key
                .as_ref()
                .map_or_else(String::new, |k| k.display().to_string());
            a.rule
                .cmp(&b.rule)
                .then(ak.cmp(&bk))
                .then(a.message.cmp(&b.message))
        });

        Ok(PolicyResult { violations })
    }
}

fn evaluate_rule(
    rule: &Rule,
    index: &LayerIndex,
    vfs: &VFS,
    keys: &[PathBuf],
    violations: &mut Vec<Violation>,
) -> io::Result<()> {
    match rule {
        Rule::WinnerMustMatch {
            path_glob,
            source_glob,
        } => evaluate_winner_source_rule(
            index,
            vfs,
            keys,
            WinnerSourceRule {
                rule_name: "winner_must_match",
                path_glob,
                source_glob,
                expect_match: true,
            },
            violations,
        ),
        Rule::WinnerMustNotMatch {
            path_glob,
            source_glob,
        } => evaluate_winner_source_rule(
            index,
            vfs,
            keys,
            WinnerSourceRule {
                rule_name: "winner_must_not_match",
                path_glob,
                source_glob,
                expect_match: false,
            },
            violations,
        ),
        Rule::MustExist { path_glob } => evaluate_must_exist(keys, path_glob, violations),
        Rule::MustBeUnique { path_glob } => {
            evaluate_provider_count(index, keys, path_glob, None, violations)
        }
        Rule::WinnerKindMustBe { path_glob, kind } => {
            evaluate_winner_kind(index, vfs, keys, path_glob, *kind, violations)
        }
        Rule::MaxOverrideDepth { path_glob, max } => {
            evaluate_provider_count(index, keys, path_glob, Some(*max), violations)
        }
    }
}

#[derive(Clone, Copy)]
struct WinnerSourceRule<'a> {
    rule_name: &'static str,
    path_glob: &'a str,
    source_glob: &'a str,
    expect_match: bool,
}

fn evaluate_winner_source_rule(
    index: &LayerIndex,
    vfs: &VFS,
    keys: &[PathBuf],
    rule: WinnerSourceRule<'_>,
    violations: &mut Vec<Violation>,
) -> io::Result<()> {
    let path_glob = compile_glob("path_glob", rule.path_glob)?;
    let source_glob = compile_glob("source_glob", rule.source_glob)?;
    for key in keys.iter().filter(|k| path_glob.is_match(k)) {
        let Some(prov) = index.provenance(vfs, key, false)? else {
            continue;
        };
        let matched = source_glob.is_match(&prov.winner.path);
        if matched != rule.expect_match {
            let message = if rule.expect_match {
                format!(
                    "winner '{}' does not match source glob '{}'",
                    prov.winner.path.display(),
                    rule.source_glob
                )
            } else {
                format!(
                    "winner '{}' matches forbidden source glob '{}'",
                    prov.winner.path.display(),
                    rule.source_glob
                )
            };
            violations.push(error_violation(rule.rule_name, Some(key.clone()), message));
        }
    }
    Ok(())
}

fn evaluate_must_exist(
    keys: &[PathBuf],
    path_glob_text: &str,
    violations: &mut Vec<Violation>,
) -> io::Result<()> {
    let path_glob = compile_glob("path_glob", path_glob_text)?;
    if !keys.iter().any(|k| path_glob.is_match(k)) {
        violations.push(error_violation(
            "must_exist",
            None,
            format!("no key matched '{path_glob_text}'"),
        ));
    }
    Ok(())
}

fn evaluate_provider_count(
    index: &LayerIndex,
    keys: &[PathBuf],
    path_glob_text: &str,
    max: Option<usize>,
    violations: &mut Vec<Violation>,
) -> io::Result<()> {
    let path_glob = compile_glob("path_glob", path_glob_text)?;
    for key in keys.iter().filter(|k| path_glob.is_match(k)) {
        let provider_count = index.sources_containing(key).len();
        match max {
            None if provider_count > 1 => violations.push(error_violation(
                "must_be_unique",
                Some(key.clone()),
                format!("key has {provider_count} providers"),
            )),
            Some(max) if provider_count > max => violations.push(error_violation(
                "max_override_depth",
                Some(key.clone()),
                format!("provider_count {provider_count} exceeds max {max}"),
            )),
            _ => {}
        }
    }
    Ok(())
}

fn evaluate_winner_kind(
    index: &LayerIndex,
    vfs: &VFS,
    keys: &[PathBuf],
    path_glob_text: &str,
    kind: SourceKind,
    violations: &mut Vec<Violation>,
) -> io::Result<()> {
    let path_glob = compile_glob("path_glob", path_glob_text)?;
    for key in keys.iter().filter(|k| path_glob.is_match(k)) {
        let Some(prov) = index.provenance(vfs, key, false)? else {
            continue;
        };
        if prov.winner.kind != kind {
            violations.push(error_violation(
                "winner_kind_must_be",
                Some(key.clone()),
                format!(
                    "winner kind mismatch: expected {:?}, got {:?}",
                    kind, prov.winner.kind
                ),
            ));
        }
    }
    Ok(())
}

fn error_violation(rule: &str, key: Option<PathBuf>, message: String) -> Violation {
    Violation {
        rule: rule.into(),
        key,
        message,
        severity: Severity::Error,
    }
}

fn compile_glob(field: &str, glob: &str) -> io::Result<CompiledGlob> {
    CompiledGlob::new(glob).map_err(|err| {
        io::Error::new(
            io::ErrorKind::InvalidInput,
            format!("invalid {field} '{glob}': {err}"),
        )
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::{LayerIndex, SourceMeta};
    use crate::path_glob_matches;
    use std::{fs, path::Path};

    struct TempDir(PathBuf);

    impl TempDir {
        fn new(name: &str) -> Self {
            let dir = std::env::temp_dir().join(format!(
                "{name}_{}_{}",
                std::process::id(),
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .expect("system clock should be after unix epoch")
                    .as_nanos()
            ));
            fs::create_dir_all(&dir).expect("failed to create temp dir");
            Self(dir)
        }

        fn path(&self) -> &Path {
            &self.0
        }

        fn write(&self, rel: &str, data: &[u8]) {
            let target = self.0.join(rel);
            if let Some(parent) = target.parent() {
                fs::create_dir_all(parent).expect("failed to create parent dir");
            }
            fs::write(target, data).expect("failed to write file");
        }
    }

    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn glob_match_works() {
        assert!(path_glob_matches(
            "textures/**",
            Path::new("textures/foo/bar.dds")
        ));
        assert!(path_glob_matches(
            "textures/*.dds",
            Path::new("textures/a.dds")
        ));
        assert!(!path_glob_matches(
            "textures/*.dds",
            Path::new("textures/a/b.dds")
        ));
    }

    #[test]
    fn must_exist_reports_missing() {
        let temp = TempDir::new("policy_must_exist_reports_missing");
        let index = LayerIndex::from_file_lists(vec![(
            SourceMeta {
                path: PathBuf::from("/a"),
                kind: SourceKind::LooseDir,
            },
            vec![PathBuf::from("meshes/a.nif")],
        )]);
        let vfs = VFS::from_directories([temp.path()], None::<Vec<&str>>);

        let policy = Policy {
            rules: vec![Rule::MustExist {
                path_glob: "textures/**".into(),
            }],
        };
        let result = policy
            .evaluate(&index, &vfs)
            .expect("policy evaluate should not fail");
        assert_eq!(result.violations.len(), 1);
        assert_eq!(result.violations[0].rule, "must_exist");
    }

    #[test]
    fn must_exist_uses_actual_vfs_keys() {
        let index = LayerIndex::from_file_lists(vec![(
            SourceMeta {
                path: PathBuf::from("/a"),
                kind: SourceKind::LooseDir,
            },
            vec![PathBuf::from("missing.txt")],
        )]);
        let vfs = VFS::new();
        let policy = Policy {
            rules: vec![Rule::MustExist {
                path_glob: "missing.txt".into(),
            }],
        };

        let result = policy.evaluate(&index, &vfs).expect("policy should run");
        assert_eq!(result.violations.len(), 1);
    }

    #[test]
    fn winner_rules_use_actual_vfs_winner() {
        let low = TempDir::new("policy_actual_winner_low");
        let high = TempDir::new("policy_actual_winner_high");
        low.write("shared.txt", b"low");

        let index = LayerIndex::from_file_lists(vec![
            (
                SourceMeta {
                    path: low.path().to_path_buf(),
                    kind: SourceKind::LooseDir,
                },
                vec![PathBuf::from("shared.txt")],
            ),
            (
                SourceMeta {
                    path: high.path().to_path_buf(),
                    kind: SourceKind::LooseDir,
                },
                vec![PathBuf::from("shared.txt")],
            ),
        ]);
        let vfs = VFS::from_directories([low.path()], None::<Vec<&str>>);
        let policy = Policy {
            rules: vec![Rule::WinnerMustMatch {
                path_glob: "shared.txt".into(),
                source_glob: format!("{}", low.path().display()),
            }],
        };

        let result = policy.evaluate(&index, &vfs).expect("policy should run");
        assert!(result.violations.is_empty());
    }
}