aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim/src/zip.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-06 03:46:41 +0900
committernsfisis <nsfisis@gmail.com>2026-08-06 03:46:41 +0900
commit92d2199afcecd0056e82d2559700769715401ef0 (patch)
treeb9d65620db53bc19f800247660e560f3f8775217 /crates/shirabe-php-shim/src/zip.rs
parent27e81d5e5ca0a89eb176a65b1d9f186658b16f50 (diff)
downloadphp-shirabe-92d2199afcecd0056e82d2559700769715401ef0.tar.gz
php-shirabe-92d2199afcecd0056e82d2559700769715401ef0.tar.zst
php-shirabe-92d2199afcecd0056e82d2559700769715401ef0.zip
refactor(php-shim): take filesystem paths as impl AsRef<Path>
The shim's filesystem entry points took `&str` even though each one resolves to a local path through `std::fs` or a syscall, so callers holding a `PathBuf` had to stringify it at the call site. They now take `impl AsRef<Path>`, the form `file_exists`, `is_dir`, `unlink` and the rest of the already-converted set use. `Phar`, `PharData` and `ZipArchive` keep their archive path as a `PathBuf`. `PharData::compress` names the compressed sibling by appending the suffix to the file name rather than formatting the path into a `String`. Arguments PHP resolves through a stream wrapper (`fopen`, `file_put_contents`, `include`) still take `&str`, as do the byte-string operations (`dirname`, `basename`, `pathinfo`) and archive-internal entry names, which are `/`-joined logical names rather than OS paths. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-php-shim/src/zip.rs')
-rw-r--r--crates/shirabe-php-shim/src/zip.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/crates/shirabe-php-shim/src/zip.rs b/crates/shirabe-php-shim/src/zip.rs
index 83dd70ce..56f02db7 100644
--- a/crates/shirabe-php-shim/src/zip.rs
+++ b/crates/shirabe-php-shim/src/zip.rs
@@ -27,7 +27,7 @@ enum ZipState {
Writer {
writer: zip::ZipWriter<std::fs::File>,
/// The destination path, retained so `close` can confirm the file exists.
- path: String,
+ path: std::path::PathBuf,
status: String,
},
}
@@ -65,10 +65,11 @@ impl ZipArchive {
}
}
- pub fn open(&mut self, filename: &str, flags: i64) -> Result<(), i64> {
+ pub fn open(&mut self, filename: impl AsRef<std::path::Path>, flags: i64) -> Result<(), i64> {
if let Some(mock) = &self.mock {
return mock.open;
}
+ let filename = filename.as_ref();
if flags & Self::CREATE != 0 {
let file = match std::fs::File::create(filename) {
Ok(f) => f,
@@ -76,7 +77,7 @@ impl ZipArchive {
};
*self.state.borrow_mut() = ZipState::Writer {
writer: zip::ZipWriter::new(file),
- path: filename.to_string(),
+ path: filename.to_path_buf(),
status: String::new(),
};
self.num_files = 0;
@@ -143,7 +144,7 @@ impl ZipArchive {
Some(stat)
}
- pub fn extract_to(&self, path: &str) -> Result<bool, ErrorException> {
+ pub fn extract_to(&self, path: impl AsRef<std::path::Path>) -> Result<bool, ErrorException> {
if let Some(mock) = &self.mock {
return mock.extract_to.clone().map_err(|message| ErrorException {
message,
@@ -157,7 +158,7 @@ impl ZipArchive {
let ZipState::Reader(archive) = &mut *state else {
return Ok(false);
};
- Ok(archive.extract(path).is_ok())
+ Ok(archive.extract(path.as_ref()).is_ok())
}
pub fn locate_name(&self, name: &str) -> Option<i64> {
@@ -230,8 +231,8 @@ impl ZipArchive {
.is_ok()
}
- pub fn add_file(&self, filepath: &str, local_name: &str) -> bool {
- let contents = match std::fs::read(filepath) {
+ pub fn add_file(&self, filepath: impl AsRef<std::path::Path>, local_name: &str) -> bool {
+ let contents = match std::fs::read(filepath.as_ref()) {
Ok(c) => c,
Err(_) => return false,
};