use std::path::{Path, PathBuf};
use std::process::Command;
pub trait Fs: Send + Sync {
fn read(&self, path: &Path) -> Result<String, String>;
fn write(&self, path: &Path, content: &str) -> Result<(), String>;
fn canonicalize(&self, path: &Path) -> Result<PathBuf, String>;
fn is_local(&self) -> bool {
false
}
}
#[derive(Debug, Default, Clone)]
pub struct LocalFs;
impl Fs for LocalFs {
fn is_local(&self) -> bool {
true
}
fn read(&self, path: &Path) -> Result<String, String> {
std::fs::read_to_string(path).map_err(|e| format!("cannot read {}: {e}", path.display()))
}
fn write(&self, path: &Path, content: &str) -> Result<(), String> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir)
.map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
}
std::fs::write(path, content).map_err(|e| format!("cannot write {}: {e}", path.display()))
}
fn canonicalize(&self, path: &Path) -> Result<PathBuf, String> {
path.canonicalize()
.map_err(|e| format!("{}: {e}", path.display()))
}
}
#[derive(Debug, Clone)]
pub struct ContainerFs {
pub container_id: String,
}
impl ContainerFs {
fn exec(&self, args: &[&str], stdin: Option<&str>) -> Result<(String, i32), String> {
use std::io::Write;
use std::process::Stdio;
let mut cmd = Command::new("docker");
cmd.args(["exec", "-i", &self.container_id])
.args(args)
.stdin(if stdin.is_some() {
Stdio::piped()
} else {
Stdio::null()
})
.stdout(Stdio::piped())
.stderr(Stdio::piped());
let mut child = cmd
.spawn()
.map_err(|e| format!("docker exec failed: {e}"))?;
if let Some(body) = stdin {
child
.stdin
.take()
.ok_or("no stdin")?
.write_all(body.as_bytes())
.map_err(|e| format!("cannot send content: {e}"))?;
}
let out = child
.wait_with_output()
.map_err(|e| format!("docker exec failed: {e}"))?;
Ok((
String::from_utf8_lossy(&out.stdout).to_string(),
out.status.code().unwrap_or(-1),
))
}
}
impl Fs for ContainerFs {
fn read(&self, path: &Path) -> Result<String, String> {
let p = path.to_string_lossy().to_string();
match self.exec(&["cat", "--", &p], None)? {
(body, 0) => Ok(body),
_ => Err(format!("cannot read {p} in container")),
}
}
fn write(&self, path: &Path, content: &str) -> Result<(), String> {
let p = path.to_string_lossy().to_string();
let dir = path
.parent()
.map(|d| d.to_string_lossy().to_string())
.unwrap_or_default();
if !dir.is_empty() {
self.exec(&["mkdir", "-p", "--", &dir], None)?;
}
match self.exec(&["tee", "--", &p], Some(content))? {
(_, 0) => Ok(()),
_ => Err(format!("cannot write {p} in container")),
}
}
fn canonicalize(&self, path: &Path) -> Result<PathBuf, String> {
let p = path.to_string_lossy().to_string();
match self.exec(&["readlink", "-f", "--", &p], None)? {
(out, 0) if !out.trim().is_empty() => Ok(PathBuf::from(out.trim())),
_ => Err(format!("cannot resolve {p} in container")),
}
}
}
pub struct SessionFs {
shell: crate::shell::PersistentShell,
fallback: Box<dyn Fs>,
}
impl SessionFs {
pub fn container(container_id: String, workdir: PathBuf) -> Self {
Self {
shell: crate::shell::PersistentShell::container(container_id.clone(), workdir),
fallback: Box::new(ContainerFs { container_id }),
}
}
pub fn local(root: PathBuf) -> Self {
Self {
shell: crate::shell::PersistentShell::local(root),
fallback: Box::new(LocalFs),
}
}
fn run(&self, command: &str) -> Option<crate::shell::Output> {
use crate::shell::Shell;
if !self.shell.is_persistent() {
return None;
}
self.shell
.exec(command, Path::new("/"), std::time::Duration::from_secs(120))
.ok()
}
}
fn sh_quote(s: &str) -> String {
format!("'{}'", s.replace('\'', r"'\''"))
}
impl Fs for SessionFs {
fn read(&self, path: &Path) -> Result<String, String> {
let p = path.to_string_lossy().to_string();
match self.run(&format!("cat -- {}", sh_quote(&p))) {
Some(o) if o.exit_code == 0 => Ok(o.stdout),
Some(_) => Err(format!("cannot read {p} in container")),
None => self.fallback.read(path),
}
}
fn write(&self, path: &Path, content: &str) -> Result<(), String> {
let p = path.to_string_lossy().to_string();
let dir = path
.parent()
.map(|d| d.to_string_lossy().to_string())
.unwrap_or_default();
let mkdir = if dir.is_empty() {
String::new()
} else {
format!("mkdir -p -- {} && ", sh_quote(&dir))
};
let delim = format!(
"__BLYTE_CONTENT_{}_{}__",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
);
let cmd = format!(
"IFS= read -r -d '' __blyte_w <<'{delim}' || true\n{content}\n{delim}\n\
__blyte_w=${{__blyte_w%$'\\n'}}; {mkdir}printf %s \"$__blyte_w\" > {}",
sh_quote(&p)
);
match self.run(&cmd) {
Some(o) if o.exit_code == 0 => Ok(()),
Some(_) => Err(format!("cannot write {p} in container")),
None => self.fallback.write(path, content),
}
}
fn canonicalize(&self, path: &Path) -> Result<PathBuf, String> {
let p = path.to_string_lossy().to_string();
match self.run(&format!("readlink -f -- {}", sh_quote(&p))) {
Some(o) if o.exit_code == 0 && !o.stdout.trim().is_empty() => {
Ok(PathBuf::from(o.stdout.trim()))
}
Some(_) => Err(format!("cannot resolve {p} in container")),
None => self.fallback.canonicalize(path),
}
}
}