MCPcopy Create free account
hub / github.com/bootc-dev/bootc / last_utf8_content_from_file

Function last_utf8_content_from_file

crates/utils/src/command.rs:90–116  ·  view source on GitHub ↗

Parse the last chunk (e.g. 1024 bytes) from the provided file, ensure it's UTF-8, and return that value. This function is infallible; if the file cannot be read for some reason, a copy of a static string is returned.

(mut f: std::fs::File)

Source from the content-addressed store, hash-verified

88/// if the file cannot be read for some reason, a copy of a static string
89/// is returned.
90fn last_utf8_content_from_file(mut f: std::fs::File) -> String {
91 // u16 since we truncate to just the trailing bytes here
92 // to avoid pathological error messages
93 const MAX_STDERR_BYTES: u16 = 1024;
94 let size = f
95 .metadata()
96 .map_err(|e| {
97 tracing::warn!("failed to fstat: {e}");
98 })
99 .map(|m| m.len().try_into().unwrap_or(u16::MAX))
100 .unwrap_or(0);
101 let size = size.min(MAX_STDERR_BYTES);
102 let seek_offset = -(size as i32);
103 let mut stderr_buf = Vec::with_capacity(size.into());
104 // We should never fail to seek()+read() really, but let's be conservative
105 let r = match f
106 .seek(std::io::SeekFrom::End(seek_offset.into()))
107 .and_then(|_| f.read_to_end(&mut stderr_buf))
108 {
109 Ok(_) => String::from_utf8_lossy(&stderr_buf),
110 Err(e) => {
111 tracing::warn!("failed seek+read: {e}");
112 "<failed to read stderr>".into()
113 }
114 };
115 (&*r).to_owned()
116}
117
118impl ExitStatusExt for std::process::ExitStatus {
119 fn check_status(&mut self) -> Result<()> {

Callers 1

Calls 1

intoMethod · 0.45

Tested by

no test coverage detected