From 697f8de69d07416b23a7efa68e1ebc9f41851260 Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Sat, 28 Nov 2026 12:35:37 +0100 Subject: [PATCH] fix: serve static previews past the exec output cap The exec tail keeps only its last 50 KiB, so a larger file arrived without the probe's status line. Probe for existence and size, then read the file through the sandbox's read_file route, which captures up to 32 MiB. --- src/preview.rs | 59 ++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/preview.rs b/src/preview.rs index 210b094..97cf2ce 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -559,7 +559,7 @@ async fn try_static( let outcome = sandbox .run( rhema_sandbox::RunRequest { - command: static_read_command(&abs), + command: static_probe_command(&abs), cwd: workspace.into(), timeout: Duration::from_secs(20), env: Default::default(), @@ -569,24 +569,30 @@ async fn try_static( ) .await .map_err(|err| (StatusCode::BAD_GATEWAY, err.to_string()))?; - let read = parse_static_output(outcome.stdout).ok_or_else(|| { + let file_state = parse_static_status(&outcome.stdout).ok_or_else(|| { ( StatusCode::BAD_GATEWAY, "the static preview returned no status".to_owned(), ) })?; - let body = match read { - StaticRead::Body(body) => body, - StaticRead::Missing => { + match file_state { + StaticState::Missing => { return Err((StatusCode::NOT_FOUND, "file not found".to_owned())); } - StaticRead::Large => { + StaticState::Large => { return Err(( StatusCode::PAYLOAD_TOO_LARGE, "this file is too large for the static preview".to_owned(), )); } - }; + StaticState::Ok => {} + } + // The exec tail keeps only its last 50 KiB, so a large file has to + // cross through read_file, which captures up to 32 MiB. + let body = sandbox + .read_file(&abs) + .await + .map_err(|err| (StatusCode::BAD_GATEWAY, err.to_string()))?; let length = body.len(); let body = if method == Method::HEAD { Body::empty() @@ -649,33 +655,30 @@ fn static_request_path(uri: &Uri, root: &str, entry: &str) -> Result String { +fn static_probe_command(path: &str) -> String { format!( "p={q}; if [ ! -f \"$p\" ]; then printf 'missing'; exit 0; fi; \ s=$(stat -c %s -- \"$p\" 2>/dev/null) || {{ printf 'missing'; exit 0; }}; \ if [ \"$s\" -gt {max} ]; then printf 'large'; exit 0; fi; \ - printf 'ok\\n'; cat -- \"$p\"", + printf 'ok\\n'", q = shquote(path), max = STATIC_MAX_BYTES ) } -enum StaticRead { - Body(Vec), +enum StaticState { + Ok, Missing, Large, } -fn parse_static_output(stdout: Vec) -> Option { - let boundary = stdout.iter().position(|byte| *byte == b'\n'); - let (head, body) = match boundary { - Some(index) => (&stdout[..index], stdout[index + 1..].to_vec()), - None => (&stdout[..], Vec::new()), - }; +fn parse_static_status(stdout: &[u8]) -> Option { + let head = stdout.strip_suffix(b"\n").unwrap_or(stdout); + let head = head.strip_suffix(b"\r").unwrap_or(head); match head { - b"ok" => Some(StaticRead::Body(body)), - b"missing" => Some(StaticRead::Missing), - b"large" => Some(StaticRead::Large), + b"ok" => Some(StaticState::Ok), + b"missing" => Some(StaticState::Missing), + b"large" => Some(StaticState::Large), _ => None, } } @@ -845,20 +848,20 @@ mod tests { } #[test] - fn static_reads_split_the_status_from_the_body() { + fn static_probes_report_the_file_state() { assert!(matches!( - parse_static_output(b"ok\n".to_vec()), - Some(StaticRead::Body(body)) if body == b"" + parse_static_status(b"ok\n"), + Some(StaticState::Ok) )); assert!(matches!( - parse_static_output(b"missing".to_vec()), - Some(StaticRead::Missing) + parse_static_status(b"missing"), + Some(StaticState::Missing) )); assert!(matches!( - parse_static_output(b"large".to_vec()), - Some(StaticRead::Large) + parse_static_status(b"large"), + Some(StaticState::Large) )); - assert!(parse_static_output(Vec::new()).is_none()); + assert!(parse_static_status(b"").is_none()); } #[test]