byssus 0.1.0

Live, read-only filesystem attachments between isolated workspaces
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! Relative path templates such as `{name}/workspace`.
//!
//! Rules (see `docs/DESIGN.md`, "Templates"):
//!
//! - a relative path of non-empty `/`-separated components, none `.` or `..`;
//! - the only placeholder is `{name}`, which must appear at least once and may
//!   appear anywhere within components;
//! - any other `{` or `}` is an error;
//! - components and the interpolated path must respect `NAME_MAX` and
//!   `PATH_MAX`.

use std::fmt;

use crate::name::{MAX_NAME_LEN, Name};

/// Maximum length in bytes of an interpolated relative path, excluding the
/// terminating NUL (`PATH_MAX` is 4096 including it).
pub const MAX_PATH_LEN: usize = 4095;

const PLACEHOLDER: &str = "{name}";
/// Placeholders for group set directory levels, in level order.
const LEVEL_PLACEHOLDERS: [&str; crate::name::MAX_SET_DEPTH] = ["{group}", "{subgroup}"];

#[derive(Debug, Clone, PartialEq, Eq)]
enum Piece {
    Literal(String),
    Name,
    /// A group set directory level: 0 is `{group}`, 1 is `{subgroup}`.
    Level(usize),
}

/// A validated path template.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Template {
    raw: String,
    components: Vec<Vec<Piece>>,
}

/// Why a template was rejected.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum TemplateError {
    /// The template is empty.
    #[error("template is empty")]
    Empty,
    /// The template begins with `/`.
    #[error("template must be relative (must not begin with '/')")]
    Absolute,
    /// The template ends with `/`.
    #[error("template must not end with '/'")]
    TrailingSlash,
    /// The template contains `//`.
    #[error("template contains an empty path component")]
    EmptyComponent,
    /// A component is `.` or `..`.
    #[error("template contains a '{0}' path component")]
    DotComponent(String),
    /// A `{` or `}` that is not part of `{name}`.
    #[error("template contains '{found}' at byte {offset}; the only placeholder is {{name}}")]
    InvalidBrace {
        /// The unexpected text, e.g. `{foo}` or a lone brace.
        found: String,
        /// Byte offset within the template.
        offset: usize,
    },
    /// The template contains a NUL byte.
    #[error("template contains a NUL byte")]
    Nul,
    /// The template does not contain `{name}`.
    #[error("template must contain {{name}}")]
    MissingName,
    /// `{group}` or `{subgroup}` is used outside a group set.
    #[error("{{group}} and {{subgroup}} are only allowed in group set templates")]
    GroupNotAllowed,
    /// A group set's target template does not contain every level
    /// placeholder the set uses.
    #[error("template must contain {0}")]
    MissingLevel(&'static str),
    /// A literal component is longer than `NAME_MAX`.
    #[error("template component is {0} bytes long; the maximum is {MAX_NAME_LEN}")]
    ComponentTooLong(usize),
}

/// Why interpolating a valid template with a valid name failed.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum InterpolateError {
    /// An interpolated component exceeds `NAME_MAX`.
    #[error("interpolated path component is {0} bytes long; the maximum is {MAX_NAME_LEN}")]
    ComponentTooLong(usize),
    /// The interpolated path exceeds `PATH_MAX`.
    #[error("interpolated path is {0} bytes long; the maximum is {MAX_PATH_LEN}")]
    PathTooLong(usize),
    /// An interpolated component is empty, `.` or `..`, or contains `/`.
    #[error("interpolated path component '{0}' is not a plain name")]
    InvalidComponent(String),
}

impl Template {
    /// Parses and validates a template for a statically configured group:
    /// `{name}` is the only placeholder.
    pub fn parse(raw: &str) -> Result<Self, TemplateError> {
        Self::parse_inner(raw, false)
    }

    /// Parses and validates a template for a group set: `{name}` and
    /// `{group}` are allowed.
    pub fn parse_for_set(raw: &str) -> Result<Self, TemplateError> {
        Self::parse_inner(raw, true)
    }

    fn parse_inner(raw: &str, allow_group: bool) -> Result<Self, TemplateError> {
        if raw.is_empty() {
            return Err(TemplateError::Empty);
        }
        if raw.contains('\0') {
            return Err(TemplateError::Nul);
        }
        if raw.starts_with('/') {
            return Err(TemplateError::Absolute);
        }
        if raw.ends_with('/') {
            return Err(TemplateError::TrailingSlash);
        }

        let mut components = Vec::new();
        let mut has_name = false;
        let mut offset = 0;
        for component in raw.split('/') {
            if component.is_empty() {
                return Err(TemplateError::EmptyComponent);
            }
            if component == "." || component == ".." {
                return Err(TemplateError::DotComponent(component.to_owned()));
            }
            let pieces = parse_component(component, offset, allow_group)?;
            has_name |= pieces.contains(&Piece::Name);
            let has_placeholder = pieces.iter().any(|p| !matches!(p, Piece::Literal(_)));
            if !has_placeholder && component.len() > MAX_NAME_LEN {
                return Err(TemplateError::ComponentTooLong(component.len()));
            }
            components.push(pieces);
            offset += component.len() + 1;
        }
        if !has_name {
            return Err(TemplateError::MissingName);
        }
        Ok(Self {
            raw: raw.to_owned(),
            components,
        })
    }

    /// Returns the template as written in configuration.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.raw
    }

    /// Interpolates `name`, returning the path components.
    pub fn components(&self, name: &Name) -> Result<Vec<String>, InterpolateError> {
        let mut out = Vec::with_capacity(self.components.len());
        let mut total = 0;
        for pieces in &self.components {
            let mut component = String::new();
            for piece in pieces {
                match piece {
                    Piece::Literal(s) => component.push_str(s),
                    Piece::Name => component.push_str(name.as_str()),
                    // Bound by `bind_levels` before interpolation.
                    Piece::Level(_) => unreachable!("unbound group set placeholder"),
                }
            }
            if component.len() > MAX_NAME_LEN {
                return Err(InterpolateError::ComponentTooLong(component.len()));
            }
            // Defense in depth: valid names and templates never produce these,
            // and RESOLVE_BENEATH would not stop one member reaching another's
            // directory beneath the same root.
            if component.is_empty()
                || component == "."
                || component == ".."
                || component.contains('/')
            {
                return Err(InterpolateError::InvalidComponent(component));
            }
            total += component.len() + usize::from(!out.is_empty());
            out.push(component);
        }
        if total > MAX_PATH_LEN {
            return Err(InterpolateError::PathTooLong(total));
        }
        Ok(out)
    }

    /// The number of group set levels this template refers to: 0 if it uses
    /// neither `{group}` nor `{subgroup}`, 1 for `{group}` only, 2 if it uses
    /// `{subgroup}`.
    #[must_use]
    pub fn depth(&self) -> usize {
        self.components
            .iter()
            .flatten()
            .filter_map(|p| match p {
                Piece::Level(level) => Some(level + 1),
                _ => None,
            })
            .max()
            .unwrap_or(0)
    }

    /// Whether the template contains the placeholder for `level`.
    #[must_use]
    pub fn has_level(&self, level: usize) -> bool {
        self.components
            .iter()
            .flatten()
            .any(|p| *p == Piece::Level(level))
    }

    /// The placeholder text for a level, for messages.
    #[must_use]
    pub fn level_placeholder(level: usize) -> &'static str {
        LEVEL_PLACEHOLDERS[level]
    }

    /// Substitutes directory level names (`levels[0]` for `{group}`,
    /// `levels[1]` for `{subgroup}`), yielding a template whose only
    /// placeholder is `{name}`.
    ///
    /// # Panics
    ///
    /// If the template refers to a level beyond `levels`.
    pub fn bind_levels(&self, levels: &[Name]) -> Result<Self, InterpolateError> {
        let mut components = Vec::with_capacity(self.components.len());
        let mut raw_parts = Vec::with_capacity(self.components.len());
        for pieces in &self.components {
            let mut bound: Vec<Piece> = Vec::new();
            let mut raw = String::new();
            for piece in pieces {
                let text = match piece {
                    Piece::Name => {
                        raw.push_str(PLACEHOLDER);
                        bound.push(Piece::Name);
                        continue;
                    }
                    Piece::Literal(text) => text.as_str(),
                    Piece::Level(level) => levels[*level].as_str(),
                };
                raw.push_str(text);
                if let Some(Piece::Literal(last)) = bound.last_mut() {
                    last.push_str(text);
                } else {
                    bound.push(Piece::Literal(text.to_owned()));
                }
            }
            if !bound.contains(&Piece::Name) && raw.len() > MAX_NAME_LEN {
                return Err(InterpolateError::ComponentTooLong(raw.len()));
            }
            components.push(bound);
            raw_parts.push(raw);
        }
        Ok(Self {
            raw: raw_parts.join("/"),
            components,
        })
    }

    /// Interpolates `name`, returning the relative path joined with `/`.
    pub fn interpolate(&self, name: &Name) -> Result<String, InterpolateError> {
        Ok(self.components(name)?.join("/"))
    }
}

fn parse_component(
    component: &str,
    base_offset: usize,
    allow_group: bool,
) -> Result<Vec<Piece>, TemplateError> {
    let mut pieces = Vec::new();
    let mut literal = String::new();
    let mut rest = component;
    let mut offset = base_offset;
    while let Some(pos) = rest.find(['{', '}']) {
        literal.push_str(&rest[..pos]);
        let tail = &rest[pos..];
        if let Some(after) = tail.strip_prefix(PLACEHOLDER) {
            if !literal.is_empty() {
                pieces.push(Piece::Literal(std::mem::take(&mut literal)));
            }
            pieces.push(Piece::Name);
            rest = after;
            offset += pos + PLACEHOLDER.len();
        } else if let Some((level, after)) = LEVEL_PLACEHOLDERS
            .iter()
            .enumerate()
            .find_map(|(level, p)| tail.strip_prefix(p).map(|after| (level, after)))
        {
            if !allow_group {
                return Err(TemplateError::GroupNotAllowed);
            }
            if !literal.is_empty() {
                pieces.push(Piece::Literal(std::mem::take(&mut literal)));
            }
            pieces.push(Piece::Level(level));
            rest = after;
            offset += pos + LEVEL_PLACEHOLDERS[level].len();
        } else {
            let found = if tail.starts_with('{') {
                tail.find('}').map_or("{", |end| &tail[..=end])
            } else {
                "}"
            };
            return Err(TemplateError::InvalidBrace {
                found: found.to_owned(),
                offset: offset + pos,
            });
        }
    }
    literal.push_str(rest);
    if !literal.is_empty() {
        pieces.push(Piece::Literal(literal));
    }
    Ok(pieces)
}

impl fmt::Display for Template {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.raw)
    }
}

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

    fn name(s: &str) -> Name {
        Name::new(s).unwrap()
    }

    #[test]
    fn simple_templates() {
        let t = Template::parse("{name}/workspace").unwrap();
        assert_eq!(
            t.interpolate(&name("libcurl")).unwrap(),
            "libcurl/workspace"
        );
        assert_eq!(t.as_str(), "{name}/workspace");

        let t = Template::parse("{name}").unwrap();
        assert_eq!(t.interpolate(&name("x")).unwrap(), "x");
    }

    #[test]
    fn multiple_and_embedded_placeholders() {
        let t = Template::parse("pre-{name}-post/{name}/{name}{name}").unwrap();
        assert_eq!(
            t.components(&name("ab")).unwrap(),
            vec!["pre-ab-post", "ab", "abab"]
        );
    }

    #[test]
    fn literals_may_contain_dots_and_unicode() {
        let t = Template::parse("..hidden/{name}/.git/é").unwrap();
        assert_eq!(t.interpolate(&name("n")).unwrap(), "..hidden/n/.git/é");
    }

    #[test]
    fn rejects_structural_errors() {
        assert_eq!(Template::parse(""), Err(TemplateError::Empty));
        assert_eq!(Template::parse("/{name}"), Err(TemplateError::Absolute));
        assert_eq!(
            Template::parse("{name}/"),
            Err(TemplateError::TrailingSlash)
        );
        assert_eq!(
            Template::parse("a//{name}"),
            Err(TemplateError::EmptyComponent)
        );
        assert_eq!(
            Template::parse("../{name}"),
            Err(TemplateError::DotComponent("..".into()))
        );
        assert_eq!(
            Template::parse("{name}/."),
            Err(TemplateError::DotComponent(".".into()))
        );
        assert_eq!(Template::parse("a\0/{name}"), Err(TemplateError::Nul));
        assert_eq!(
            Template::parse("workspace"),
            Err(TemplateError::MissingName)
        );
    }

    #[test]
    fn rejects_invalid_braces() {
        let cases = [
            ("{foo}/{name}", "{foo}", 0),
            ("{name}/{NAME}", "{NAME}", 7),
            ("a{/{name}", "{", 1),
            ("{name}}", "}", 6),
            ("x/}{name}", "}", 2),
            ("{name/x", "{", 0),
            ("{{name}}", "{{name}", 0),
        ];
        for (input, found, offset) in cases {
            assert_eq!(
                Template::parse(input),
                Err(TemplateError::InvalidBrace {
                    found: found.into(),
                    offset
                }),
                "{input}"
            );
        }
    }

    #[test]
    fn literal_component_length() {
        let long = "a".repeat(MAX_NAME_LEN + 1);
        assert_eq!(
            Template::parse(&format!("{long}/{{name}}")),
            Err(TemplateError::ComponentTooLong(MAX_NAME_LEN + 1))
        );
        let ok = "a".repeat(MAX_NAME_LEN);
        assert!(Template::parse(&format!("{ok}/{{name}}")).is_ok());
    }

    #[test]
    fn interpolated_component_length() {
        let t = Template::parse("prefix-{name}").unwrap();
        let n = name(&"a".repeat(MAX_NAME_LEN));
        assert_eq!(
            t.components(&n),
            Err(InterpolateError::ComponentTooLong(MAX_NAME_LEN + 7))
        );
    }

    #[test]
    fn interpolated_path_length() {
        // 16 components of a 255-byte name plus 15 separators = 4095: allowed.
        let raw = vec!["{name}"; 16].join("/");
        let t = Template::parse(&raw).unwrap();
        let n = name(&"a".repeat(MAX_NAME_LEN));
        assert_eq!(t.interpolate(&n).unwrap().len(), MAX_PATH_LEN);

        let raw = format!("{raw}/x{{name}}");
        let t = Template::parse(&raw).unwrap();
        let n = name(&"a".repeat(MAX_NAME_LEN - 1));
        assert!(matches!(
            t.components(&n),
            Err(InterpolateError::PathTooLong(_))
        ));
    }

    #[test]
    fn level_placeholders_only_in_sets() {
        assert_eq!(
            Template::parse("{group}/{name}"),
            Err(TemplateError::GroupNotAllowed)
        );
        assert_eq!(
            Template::parse("{subgroup}/{name}"),
            Err(TemplateError::GroupNotAllowed)
        );
        let t = Template::parse_for_set("{group}/groups/{subgroup}/view/{name}").unwrap();
        assert_eq!(t.depth(), 2);
        assert!(t.has_level(0) && t.has_level(1));
        let one = Template::parse_for_set("{group}/projects/{name}").unwrap();
        assert_eq!(one.depth(), 1);
        assert!(!one.has_level(1));
        assert_eq!(Template::parse_for_set("{name}").unwrap().depth(), 0);
        assert_eq!(
            Template::parse_for_set("{group}/x"),
            Err(TemplateError::MissingName)
        );
        assert!(matches!(
            Template::parse_for_set("{groups}/{name}"),
            Err(TemplateError::InvalidBrace { .. })
        ));
    }

    #[test]
    fn bind_levels_substitutes_and_keeps_name() {
        let t = Template::parse_for_set("{group}/groups/{subgroup}/view-{group}/{name}").unwrap();
        let bound = t.bind_levels(&[name("acme"), name("research")]).unwrap();
        assert_eq!(bound.depth(), 0);
        assert_eq!(bound.as_str(), "acme/groups/research/view-acme/{name}");
        assert_eq!(
            bound.interpolate(&name("webapp")).unwrap(),
            "acme/groups/research/view-acme/webapp"
        );
        assert_eq!(
            bound,
            Template::parse("acme/groups/research/view-acme/{name}").unwrap()
        );
    }

    #[test]
    fn bind_levels_enforces_component_length() {
        let t = Template::parse_for_set("x{group}/{name}").unwrap();
        let long = name(&"a".repeat(MAX_NAME_LEN));
        assert_eq!(
            t.bind_levels(&[long]),
            Err(InterpolateError::ComponentTooLong(MAX_NAME_LEN + 1))
        );
    }

    #[test]
    fn valid_names_never_introduce_traversal() {
        let t = Template::parse("{name}/{name}.d/x{name}").unwrap();
        for s in ["a", "a.b", "a..b", "-", "_", "a-", "x.."] {
            for c in t.components(&name(s)).unwrap() {
                assert!(c != "." && c != ".." && !c.contains('/') && !c.is_empty());
            }
        }
    }
}