#[cfg(all(feature = "vm", target_os = "linux"))]
use crate::config::Config;
use crate::error::SdkError;
#[cfg(all(feature = "vm", target_os = "linux"))]
use crate::vm_helpers::map_microvm_error;
use crate::vm_helpers::{map_pty_session_error, map_snapshot_bytes_error};
use mimobox_core::{PtyEvent, PtySize, SandboxResult};
use std::path::{Path, PathBuf};
#[cfg(all(feature = "vm", target_os = "linux"))]
use std::sync::Arc;
use std::sync::mpsc;
#[non_exhaustive]
pub struct ExecuteResult {
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
pub exit_code: Option<i32>,
pub timed_out: bool,
pub elapsed: std::time::Duration,
}
impl ExecuteResult {
pub fn new(
stdout: Vec<u8>,
stderr: Vec<u8>,
exit_code: Option<i32>,
timed_out: bool,
elapsed: std::time::Duration,
) -> Self {
Self {
stdout,
stderr,
exit_code,
timed_out,
elapsed,
}
}
}
#[non_exhaustive]
pub struct HttpResponse {
pub status: u16,
pub headers: std::collections::HashMap<String, String>,
pub body: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SandboxSnapshot {
pub(crate) inner: mimobox_core::SandboxSnapshot,
}
impl SandboxSnapshot {
pub fn from_bytes(data: &[u8]) -> Result<Self, SdkError> {
mimobox_core::SandboxSnapshot::from_bytes(data)
.map(Self::from_core)
.map_err(map_snapshot_bytes_error)
}
pub fn from_file(path: PathBuf) -> Result<Self, SdkError> {
mimobox_core::SandboxSnapshot::from_file(path)
.map(Self::from_core)
.map_err(map_snapshot_bytes_error)
}
pub fn memory_file_path(&self) -> Option<&Path> {
self.inner.memory_file_path()
}
pub fn as_bytes(&self) -> Result<&[u8], SdkError> {
self.inner.as_bytes().map_err(map_snapshot_bytes_error)
}
pub fn to_bytes(&self) -> Result<Vec<u8>, SdkError> {
#[cfg(all(feature = "vm", target_os = "linux"))]
if let Some(memory_path) = self.inner.memory_file_path() {
return mimobox_vm::MicrovmSnapshot::from_memory_file(memory_path)
.and_then(|snapshot| snapshot.snapshot())
.map_err(map_microvm_error);
}
self.inner.to_bytes().map_err(map_snapshot_bytes_error)
}
pub fn into_bytes(self) -> Result<Vec<u8>, SdkError> {
#[cfg(all(feature = "vm", target_os = "linux"))]
if let Some(memory_path) = self.inner.memory_file_path().map(Path::to_path_buf) {
return mimobox_vm::MicrovmSnapshot::from_memory_file(&memory_path)
.and_then(|snapshot| snapshot.snapshot())
.map_err(map_microvm_error);
}
self.inner.into_bytes().map_err(map_snapshot_bytes_error)
}
pub fn size(&self) -> usize {
self.inner.size()
}
pub(crate) fn from_core(inner: mimobox_core::SandboxSnapshot) -> Self {
Self { inner }
}
}
#[cfg(all(feature = "vm", target_os = "linux"))]
#[derive(Debug, Clone)]
pub struct RestorePoolConfig {
pub pool_size: usize,
pub base_config: Config,
}
#[cfg(all(feature = "vm", target_os = "linux"))]
#[derive(Clone)]
pub struct RestorePool {
pub(crate) inner: Arc<mimobox_vm::RestorePool>,
}
impl From<SandboxResult> for ExecuteResult {
fn from(r: SandboxResult) -> Self {
Self {
stdout: r.stdout,
stderr: r.stderr,
exit_code: r.exit_code,
timed_out: r.timed_out,
elapsed: r.elapsed,
}
}
}
#[cfg(feature = "vm")]
impl From<mimobox_vm::HttpResponse> for HttpResponse {
fn from(value: mimobox_vm::HttpResponse) -> Self {
Self {
status: value.status,
headers: value.headers,
body: value.body,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StreamEvent {
Stdout(Vec<u8>),
Stderr(Vec<u8>),
Exit(i32),
TimedOut,
}
#[non_exhaustive]
pub struct PtySession {
inner: Box<dyn mimobox_core::PtySession>,
}
impl std::fmt::Debug for PtySession {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PtySession").finish_non_exhaustive()
}
}
impl PtySession {
pub(crate) fn from_inner(inner: Box<dyn mimobox_core::PtySession>) -> Self {
Self { inner }
}
pub fn send_input(&mut self, data: &[u8]) -> Result<(), SdkError> {
self.inner.send_input(data).map_err(map_pty_session_error)
}
pub fn resize(&mut self, cols: u16, rows: u16) -> Result<(), SdkError> {
self.inner
.resize(PtySize { cols, rows })
.map_err(map_pty_session_error)
}
pub fn output(&self) -> &mpsc::Receiver<PtyEvent> {
self.inner.output_rx()
}
pub fn kill(&mut self) -> Result<(), SdkError> {
self.inner.kill().map_err(map_pty_session_error)
}
pub fn wait(&mut self) -> Result<i32, SdkError> {
self.inner.wait().map_err(map_pty_session_error)
}
}