feat: functionality to open a sandbox workspace in vs code
This commit is contained in:
+15
-4
@@ -92,11 +92,9 @@ fn send(w: &Mutex<UnixStream>, kind: u8, p: &[u8]) -> io::Result<()> {
|
||||
|
||||
/// Run a command as root in a running sandbox (no tty, empty stdin) and return its stdout, at most 64 KiB.
|
||||
pub fn capture(sock: &Path, argv: &[&str]) -> Result<String> {
|
||||
let mut s = UnixStream::connect(sock)?;
|
||||
s.set_read_timeout(Some(Duration::from_secs(2)))?;
|
||||
// uid 0, not "root": works even if the image has no /etc/passwd
|
||||
s.write_all(format!("{}\n", json!({"argv": argv, "user": "0", "tty": false})).as_bytes())?;
|
||||
s.write_all(&frame(b'e', &[]))?;
|
||||
let s = start(sock, argv, Some("0"))?;
|
||||
s.set_read_timeout(Some(Duration::from_secs(2)))?;
|
||||
const CAP: usize = 64 << 10;
|
||||
let mut buf = vec![0u8; CAP];
|
||||
let mut w = &mut buf[..]; // a slice writer errors once full: the guest can't make us buffer more
|
||||
@@ -109,6 +107,19 @@ pub fn capture(sock: &Path, argv: &[&str]) -> Result<String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a command without tty, with empty stdin; user None: the image user.
|
||||
fn start(sock: &Path, argv: &[&str], user: Option<&str>) -> Result<UnixStream> {
|
||||
let mut s = UnixStream::connect(sock)?;
|
||||
s.write_all(format!("{}\n", json!({"argv": argv, "user": user, "tty": false})).as_bytes())?;
|
||||
s.write_all(&frame(b'e', &[]))?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
/// Run a command as the image user, all its output to our stderr. None: the connection ended first.
|
||||
pub fn stream(sock: &Path, argv: &[&str]) -> Result<Option<i32>> {
|
||||
Ok(output(start(sock, argv, None)?, &mut io::stderr(), &mut io::stderr())?)
|
||||
}
|
||||
|
||||
/// Copy guest output frames to out/err until the exit status arrives (None: connection closed first).
|
||||
fn output(s: UnixStream, out: &mut impl Write, err: &mut impl Write) -> io::Result<Option<i32>> {
|
||||
let mut r = io::BufReader::new(s);
|
||||
|
||||
Reference in New Issue
Block a user