use std::path::PathBuf;
use std::process::Child;
use kernel::{Address, AxCode, AxError, RunId};
use super::BacklogKind;
use super::tail::Tail;
pub(super) struct Member {
pub(super) scope: Address,
pub(super) what: String,
pub(super) body: Body,
}
pub(super) enum Body {
Command {
child: Child,
dir: PathBuf,
claim: Claim,
tail: Tail,
},
Run(RunState),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Claim {
Window(RunId),
Run(RunId),
Nobody,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum RunState {
Going,
Stopping,
}
impl Body {
pub(super) fn kind(&self) -> BacklogKind {
match self {
Body::Command { .. } => BacklogKind::Command,
Body::Run(_) => BacklogKind::Run,
}
}
}
pub(super) fn collect(dir: &std::path::Path) -> (String, String) {
let read = |name: &str| {
std::fs::read(dir.join(name))
.map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
.unwrap_or_default()
};
let out = read("out");
let err = read("err");
drop(std::fs::remove_dir_all(dir));
(out, err)
}
pub(super) fn storage(dir: &std::path::Path, err: &std::io::Error) -> AxError {
AxError::failure(
AxCode::StorageFatal,
"keep a command's output",
format!("{}: {err}", dir.display()),
)
.with_recovery("make the system temporary directory writable")
}