From 0b48f4a46d24248e4c012ef37d25b5963c27a78c Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 23 Aug 2026 12:43:10 +0900 Subject: fix(fs): carry file_get_contents results as bytes file_get_contents() and file_get_contents_with_max_length() return Vec instead of a from_utf8_lossy'd String. Call sites whose consumer takes a &str still convert lossily and are marked TODO(bytes). file_get_contents_with_max_length() now reads at most the requested number of bytes instead of the whole file. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe-php-shim/src/fs.rs | 46 +++++++++++++---------------------- crates/shirabe-php-shim/src/string.rs | 15 +++++++----- 2 files changed, 26 insertions(+), 35 deletions(-) (limited to 'crates/shirabe-php-shim') diff --git a/crates/shirabe-php-shim/src/fs.rs b/crates/shirabe-php-shim/src/fs.rs index 26ea1fdf..41383490 100644 --- a/crates/shirabe-php-shim/src/fs.rs +++ b/crates/shirabe-php-shim/src/fs.rs @@ -1,4 +1,3 @@ -use crate::PhpMixed; use crate::PhpResource; use crate::StreamBacking; use crate::StreamState; @@ -871,7 +870,7 @@ pub fn file_put_contents3(filename: &str, data: &str, flags: i64) -> Option Some(data.len() as i64) } -pub fn file_get_contents(path: impl AsRef) -> Option { +pub fn file_get_contents(path: impl AsRef) -> std::io::Result> { let path = path.as_ref(); // PHP supports the file:// stream wrapper; strip it to read the local file. let path = path @@ -879,34 +878,23 @@ pub fn file_get_contents(path: impl AsRef) -> Option { .and_then(|s| s.strip_prefix("file://")) .map_or(path, std::path::Path::new); std::fs::read(path) - .ok() - .map(|bytes| String::from_utf8_lossy(&bytes).into_owned()) -} - -/// `$use_include_path` and the stream `$context` have no effect; the read always goes to the -/// local filesystem. -pub fn file_get_contents5( - path: &str, - _use_include_path: bool, - _context: PhpMixed, - offset: i64, - length: Option, -) -> Option { +} + +pub fn file_get_contents_with_max_length( + path: impl AsRef, + length: usize, +) -> std::io::Result> { + let path = path.as_ref(); // PHP supports the file:// stream wrapper; strip it to read the local file. - let path = path.strip_prefix("file://").unwrap_or(path); - let bytes = std::fs::read(path).ok()?; - let len = bytes.len() as i64; - let start = if offset < 0 { - (len + offset).max(0) - } else { - offset.min(len) - } as usize; - let slice = &bytes[start..]; - let slice = match length { - Some(l) if l >= 0 => &slice[..(l as usize).min(slice.len())], - _ => slice, - }; - Some(String::from_utf8_lossy(slice).into_owned()) + let path = path + .to_str() + .and_then(|s| s.strip_prefix("file://")) + .map_or(path, std::path::Path::new); + let mut buf = Vec::new(); + std::fs::File::open(path)? + .take(length as u64) + .read_to_end(&mut buf)?; + Ok(buf) } pub fn getcwd() -> Option { diff --git a/crates/shirabe-php-shim/src/string.rs b/crates/shirabe-php-shim/src/string.rs index 024a3943..3a61a59d 100644 --- a/crates/shirabe-php-shim/src/string.rs +++ b/crates/shirabe-php-shim/src/string.rs @@ -299,11 +299,8 @@ pub fn trim(s: &str, chars: Option<&str>) -> String { } // Byte-based, matching PHP's substr. A negative start/length counts from the end. -// The result is reinterpreted as UTF-8 (lossily), which only matters when a slice -// boundary falls inside a multibyte sequence. -pub fn substr(s: &str, start: i64, length: Option) -> String { - let bytes = s.as_bytes(); - let len = bytes.len() as i64; +pub fn substr_bytes(s: &[u8], start: i64, length: Option) -> Vec { + let len = s.len() as i64; let start = if start < 0 { (len + start).max(0) } else { @@ -314,7 +311,13 @@ pub fn substr(s: &str, start: i64, length: Option) -> String { Some(l) if l < 0 => (len + l).max(start), Some(l) => (start + l).min(len), }; - String::from_utf8_lossy(&bytes[start as usize..end as usize]).into_owned() + s[start as usize..end as usize].to_vec() +} + +// The result is reinterpreted as UTF-8 (lossily), which only matters when a slice +// boundary falls inside a multibyte sequence. +pub fn substr(s: &str, start: i64, length: Option) -> String { + String::from_utf8_lossy(&substr_bytes(s.as_bytes(), start, length)).into_owned() } pub fn implode(glue: &str, pieces: &[String]) -> String { -- cgit v1.3.1-4-g156e