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
use crate::{SandboxConfig, SandboxOutput};

use std::ffi::{OsStr, OsString};
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;

use anyhow::Result;

pub struct Command {
    pub config: SandboxConfig,
}

impl Command {
    pub fn new(bin: impl Into<PathBuf>) -> Self {
        Self {
            config: SandboxConfig {
                bin: bin.into(),
                ..Default::default()
            },
        }
    }

    pub fn run(&self) -> Result<SandboxOutput> {
        crate::run(&self.config)
    }

    pub fn arg(&mut self, a: impl Into<OsString>) -> &mut Self {
        self.config.args.push(a.into());
        self
    }

    pub fn arg_if(&mut self, cond: bool, a: impl Into<OsString>) -> &mut Self {
        if cond {
            self.arg(a)
        } else {
            self
        }
    }

    pub fn inherit_env(&mut self, k: impl Into<OsString>) -> &mut Self {
        self.config.env.push(k.into()); // TODO: check b'=' and b'\0' ?
        self
    }

    pub fn add_env(&mut self, k: impl Into<OsString>, v: impl AsRef<OsStr>) -> &mut Self {
        let mut e: OsString = k.into();
        e.push(OsStr::from_bytes(b"="));
        e.push(v.as_ref());
        self.config.env.push(e); // TODO: check b'=' and b'\0' ?
        self
    }

    pub fn bindmount_ro(&mut self, src: impl Into<PathBuf>, dst: impl Into<PathBuf>) -> &mut Self {
        self.config.bindmount_ro.push(crate::BindMount {
            src: src.into(),
            dst: dst.into(),
        });
        self
    }

    pub fn chroot(&mut self, chroot: impl Into<PathBuf>) -> &mut Self {
        self.config.chroot = Some(chroot.into());
        self
    }

    pub fn stdio(
        &mut self,
        stdin: impl Into<PathBuf>,
        stdout: impl Into<PathBuf>,
        stderr: impl Into<PathBuf>,
    ) -> &mut Self {
        self.config.stdin = Some(stdin.into());
        self.config.stdout = Some(stdout.into());
        self.config.stderr = Some(stderr.into());
        self
    }

    pub fn mount_proc(&mut self, path: impl Into<PathBuf>) -> &mut Self {
        self.config.mount_proc = Some(path.into());
        self
    }
}