mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
core: add a sandbox-backed fs helper
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "fs-ops",
|
||||
crate_name = "codex_fs_ops",
|
||||
)
|
||||
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "codex-fs-ops"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[lib]
|
||||
name = "codex_fs_ops"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
@@ -0,0 +1,38 @@
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub const READ_FILE_OPERATION_ARG: &str = "read";
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum FsCommand {
|
||||
ReadFile { path: PathBuf },
|
||||
}
|
||||
|
||||
pub fn parse_command_from_args(
|
||||
mut args: impl Iterator<Item = OsString>,
|
||||
) -> Result<FsCommand, String> {
|
||||
let Some(operation) = args.next() else {
|
||||
return Err("missing operation".to_string());
|
||||
};
|
||||
let Some(operation) = operation.to_str() else {
|
||||
return Err("operation must be valid UTF-8".to_string());
|
||||
};
|
||||
let Some(path) = args.next() else {
|
||||
return Err(format!("missing path for operation `{operation}`"));
|
||||
};
|
||||
if args.next().is_some() {
|
||||
return Err(format!(
|
||||
"unexpected extra arguments for operation `{operation}`"
|
||||
));
|
||||
}
|
||||
|
||||
let path = PathBuf::from(path);
|
||||
match operation {
|
||||
READ_FILE_OPERATION_ARG => Ok(FsCommand::ReadFile { path }),
|
||||
_ => Err(format!("unsupported filesystem operation `{operation}`")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "command_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,21 @@
|
||||
use super::FsCommand;
|
||||
use super::READ_FILE_OPERATION_ARG;
|
||||
use super::parse_command_from_args;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn parse_read_command() {
|
||||
let command = parse_command_from_args(
|
||||
[READ_FILE_OPERATION_ARG, "/tmp/example.png"]
|
||||
.into_iter()
|
||||
.map(Into::into),
|
||||
)
|
||||
.expect("command should parse");
|
||||
|
||||
assert_eq!(
|
||||
command,
|
||||
FsCommand::ReadFile {
|
||||
path: "/tmp/example.png".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
/// Special argv[1] flag used when the Codex executable self-invokes to run the
|
||||
/// internal sandbox-backed filesystem helper path.
|
||||
pub const CODEX_CORE_FS_OPS_ARG1: &str = "--codex-run-as-fs-ops";
|
||||
@@ -0,0 +1,7 @@
|
||||
mod command;
|
||||
mod constants;
|
||||
mod runner;
|
||||
|
||||
pub use command::READ_FILE_OPERATION_ARG;
|
||||
pub use constants::CODEX_CORE_FS_OPS_ARG1;
|
||||
pub use runner::run_from_args;
|
||||
@@ -0,0 +1,37 @@
|
||||
use crate::command::FsCommand;
|
||||
use crate::command::parse_command_from_args;
|
||||
use std::ffi::OsString;
|
||||
use std::io::Read;
|
||||
use std::io::Write;
|
||||
|
||||
pub fn run_from_args(
|
||||
args: impl Iterator<Item = OsString>,
|
||||
stdin: &mut impl Read,
|
||||
stdout: &mut impl Write,
|
||||
stderr: &mut impl Write,
|
||||
) -> std::io::Result<()> {
|
||||
let command = parse_command_from_args(args)
|
||||
.inspect_err(|error| {
|
||||
writeln!(stderr, "{error}").ok();
|
||||
})
|
||||
.map_err(std::io::Error::other)?;
|
||||
|
||||
execute(command, stdin, stdout)
|
||||
}
|
||||
|
||||
fn execute(
|
||||
command: FsCommand,
|
||||
_stdin: &mut impl Read,
|
||||
stdout: &mut impl Write,
|
||||
) -> std::io::Result<()> {
|
||||
match command {
|
||||
FsCommand::ReadFile { path } => {
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
std::io::copy(&mut file, stdout).map(|_| ())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "runner_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,78 @@
|
||||
use super::FsCommand;
|
||||
use super::execute;
|
||||
use crate::READ_FILE_OPERATION_ARG;
|
||||
use crate::run_from_args;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn run_from_args_streams_file_bytes_to_stdout() {
|
||||
let tempdir = tempdir().expect("tempdir");
|
||||
let path = tempdir.path().join("image.bin");
|
||||
let expected = b"hello\x00world".to_vec();
|
||||
std::fs::write(&path, &expected).expect("write test file");
|
||||
|
||||
let mut stdout = Vec::new();
|
||||
let mut stderr = Vec::new();
|
||||
let mut stdin = std::io::empty();
|
||||
run_from_args(
|
||||
[
|
||||
READ_FILE_OPERATION_ARG,
|
||||
path.to_str().expect("utf-8 test path"),
|
||||
]
|
||||
.into_iter()
|
||||
.map(Into::into),
|
||||
&mut stdin,
|
||||
&mut stdout,
|
||||
&mut stderr,
|
||||
)
|
||||
.expect("read should succeed");
|
||||
|
||||
assert_eq!(stdout, expected);
|
||||
assert_eq!(stderr, Vec::<u8>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_reports_directory_error() {
|
||||
let tempdir = tempdir().expect("tempdir");
|
||||
let mut stdout = Vec::new();
|
||||
let mut stdin = std::io::empty();
|
||||
|
||||
let error = execute(
|
||||
FsCommand::ReadFile {
|
||||
path: tempdir.path().to_path_buf(),
|
||||
},
|
||||
&mut stdin,
|
||||
&mut stdout,
|
||||
)
|
||||
.expect_err("reading a directory should fail");
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
assert_eq!(error.kind(), std::io::ErrorKind::IsADirectory);
|
||||
#[cfg(target_os = "windows")]
|
||||
assert_eq!(error.kind(), std::io::ErrorKind::PermissionDenied);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_from_args_serializes_errors_to_stderr() {
|
||||
let tempdir = tempdir().expect("tempdir");
|
||||
let missing = tempdir.path().join("missing.txt");
|
||||
let mut stdout = Vec::new();
|
||||
let mut stderr = Vec::new();
|
||||
let mut stdin = std::io::empty();
|
||||
|
||||
let result = run_from_args(
|
||||
[
|
||||
READ_FILE_OPERATION_ARG,
|
||||
missing.to_str().expect("utf-8 test path"),
|
||||
]
|
||||
.into_iter()
|
||||
.map(Into::into),
|
||||
&mut stdin,
|
||||
&mut stdout,
|
||||
&mut stderr,
|
||||
);
|
||||
|
||||
assert!(result.is_err(), "missing file should fail");
|
||||
assert_eq!(stdout, Vec::<u8>::new());
|
||||
}
|
||||
Reference in New Issue
Block a user