buildkit_llb/ops/exec/
mount.rs

1use std::path::{Path, PathBuf};
2
3use crate::utils::{OperationOutput, OutputIdx};
4
5/// Operand of *command execution operation* that specifies how are input sources mounted.
6#[derive(Debug, Clone)]
7pub enum Mount<'a, P: AsRef<Path>> {
8    /// Read-only output of another operation.
9    ReadOnlyLayer(OperationOutput<'a>, P),
10
11    /// Read-only output of another operation with a selector.
12    ReadOnlySelector(OperationOutput<'a>, P, P),
13
14    /// Empty layer that produces an output.
15    Scratch(OutputIdx, P),
16
17    /// Writable output of another operation.
18    Layer(OutputIdx, OperationOutput<'a>, P),
19
20    /// Writable persistent cache.
21    SharedCache(P),
22
23    /// Optional SSH agent socket at the specified path.
24    OptionalSshAgent(P),
25}
26
27impl<'a, P: AsRef<Path>> Mount<'a, P> {
28    /// Transform the mount into owned variant (basically, with `PathBuf` as the path).
29    pub fn into_owned(self) -> Mount<'a, PathBuf> {
30        use Mount::*;
31
32        match self {
33            ReadOnlySelector(op, path, selector) => {
34                ReadOnlySelector(op, path.as_ref().into(), selector.as_ref().into())
35            }
36
37            ReadOnlyLayer(op, path) => ReadOnlyLayer(op, path.as_ref().into()),
38            Scratch(output, path) => Scratch(output, path.as_ref().into()),
39            Layer(output, input, path) => Layer(output, input, path.as_ref().into()),
40            SharedCache(path) => SharedCache(path.as_ref().into()),
41            OptionalSshAgent(path) => OptionalSshAgent(path.as_ref().into()),
42        }
43    }
44
45    pub fn is_root(&self) -> bool {
46        use Mount::*;
47
48        let path = match self {
49            ReadOnlySelector(_, path, ..) => path,
50            ReadOnlyLayer(_, path) => path,
51            Scratch(_, path) => path,
52            Layer(_, _, path) => path,
53            SharedCache(path) => path,
54            OptionalSshAgent(_) => return false,
55        };
56
57        path.as_ref() == Path::new("/")
58    }
59}