use crate::error::Error;
use crate::model;
use crate::stdio::Stdio;
use crate::store::{Store, StoreError};
use crate::{checked, ffi};
use std::os::fd::RawFd;
pub struct Session {
store: Store,
}
impl Session {
pub fn new(store: Store) -> Self {
Self { store }
}
pub fn store(&self) -> &Store {
&self.store
}
pub fn boot(&self, spec: &model::BootSpec) -> Result<(), Error> {
let _ = std::fs::remove_dir_all(self.store.container_dir(&spec.id));
let code = ffi::czbridge_boot(
spec.clone(),
&self.store.root().display().to_string(),
&self.store.kernel().display().to_string(),
self.store.initfs_reference(),
&self.store.initfs().display().to_string(),
);
checked(code)
.map(|_| ())
.map_err(|error| Error::failed(format!("boot {}", spec.id), error))
}
pub fn exec(
&self,
name: &str,
id: &str,
configuration: &model::LinuxProcessConfiguration,
stdio: Stdio,
) -> Result<i32, Error> {
let code = ffi::czbridge_exec(
name,
id,
configuration.clone(),
stdio.terminal,
stdio.stdin,
stdio.stdout,
stdio.stderr,
);
checked(code).map_err(|error| Error::failed("exec", error))
}
pub fn resize(&self, id: &str, terminal: RawFd) -> Result<(), Error> {
checked(ffi::czbridge_resize(id, terminal))
.map(|_| ())
.map_err(|error| Error::failed(format!("resize {id}"), error))
}
pub fn is_running(&self, name: &str) -> bool {
ffi::czbridge_is_running(name)
}
pub fn is_unpacked(&self, image: &str) -> Result<bool, Error> {
let code = ffi::czbridge_is_unpacked(&self.store.root().display().to_string(), image);
Ok(checked(code).map_err(|error| Error::failed("find the image", error))? == 1)
}
pub fn images(&self) -> Result<Vec<String>, StoreError> {
self.store.images()
}
pub fn version() -> String {
format!(
"Containerization {}, kernel {}",
crate::INITFS_VERSION,
crate::KERNEL_VERSION
)
}
}