From 92d2199afcecd0056e82d2559700769715401ef0 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 6 Aug 2026 03:46:41 +0900 Subject: refactor(php-shim): take filesystem paths as impl AsRef 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`, 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) --- crates/shirabe/src/autoload/autoload_generator.rs | 8 ++++---- crates/shirabe/src/cache.rs | 2 +- crates/shirabe/src/console/application.rs | 2 +- crates/shirabe/src/downloader/phar_downloader.rs | 2 +- crates/shirabe/src/downloader/tar_downloader.rs | 2 +- crates/shirabe/src/factory.rs | 2 +- crates/shirabe/src/installer/library_installer.rs | 2 +- crates/shirabe/src/package/archiver/zip_archiver.rs | 7 ++----- crates/shirabe/src/package/locker.rs | 2 +- crates/shirabe/src/repository/filesystem_repository.rs | 2 +- crates/shirabe/src/repository/vcs/fossil_driver.rs | 2 +- crates/shirabe/src/repository/vcs/git_driver.rs | 2 +- crates/shirabe/src/repository/vcs/hg_driver.rs | 2 +- crates/shirabe/src/util/filesystem.rs | 4 ++-- crates/shirabe/src/util/http/curl_downloader.rs | 14 +++++++------- crates/shirabe/src/util/tar.rs | 2 +- 16 files changed, 27 insertions(+), 30 deletions(-) (limited to 'crates/shirabe/src') diff --git a/crates/shirabe/src/autoload/autoload_generator.rs b/crates/shirabe/src/autoload/autoload_generator.rs index 70740a59..ef026d72 100644 --- a/crates/shirabe/src/autoload/autoload_generator.rs +++ b/crates/shirabe/src/autoload/autoload_generator.rs @@ -179,12 +179,12 @@ impl AutoloadGenerator { // Fixes failing Windows realpath() implementation. // See https://bugs.php.net/bug.php?id=72738 let base_path = filesystem.normalize_path( - &realpath(&realpath(&Platform::get_cwd(false).unwrap_or_default()).unwrap_or_default()) + &realpath(realpath(Platform::get_cwd(false).unwrap_or_default()).unwrap_or_default()) .unwrap_or_default(), ); let vendor_path = filesystem.normalize_path( &realpath( - &realpath(config.get("vendor-dir").as_string().unwrap_or("")).unwrap_or_default(), + realpath(config.get("vendor-dir").as_string().unwrap_or("")).unwrap_or_default(), ) .unwrap_or_default(), ); @@ -692,7 +692,7 @@ impl AutoloadGenerator { } else { format!( "{}/{}", - realpath(&Platform::get_cwd(false).unwrap_or_default()).unwrap_or_default(), + realpath(Platform::get_cwd(false).unwrap_or_default()).unwrap_or_default(), dir ) }; @@ -1853,7 +1853,7 @@ class ComposerStaticInit{} install_path.clone() }; - let resolved_path = realpath(&format!( + let resolved_path = realpath(format!( "{}/{}", install_path_for_resolve, updir.clone().unwrap_or_default() diff --git a/crates/shirabe/src/cache.rs b/crates/shirabe/src/cache.rs index 21e8f252..2e7ff3cb 100644 --- a/crates/shirabe/src/cache.rs +++ b/crates/shirabe/src/cache.rs @@ -198,7 +198,7 @@ impl Cache { unlink(&temp_file_name); let free_space = if function_exists("disk_free_space") { - disk_free_space(&dirname(&temp_file_name)) + disk_free_space(dirname(&temp_file_name)) .map(|space| space.to_string()) .unwrap_or_default() } else { diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs index 62e73aa9..19317363 100644 --- a/crates/shirabe/src/console/application.rs +++ b/crates/shirabe/src/console/application.rs @@ -2248,7 +2248,7 @@ impl ApplicationHandle { } else if let Some(pe) = e.downcast_ref::() { let details = pe.get_details(); - let file = realpath(&Factory::get_composer_file().unwrap_or_default()); + let file = realpath(Factory::get_composer_file().unwrap_or_default()); let line = details.line; diff --git a/crates/shirabe/src/downloader/phar_downloader.rs b/crates/shirabe/src/downloader/phar_downloader.rs index 32ac0b3f..795d0645 100644 --- a/crates/shirabe/src/downloader/phar_downloader.rs +++ b/crates/shirabe/src/downloader/phar_downloader.rs @@ -62,7 +62,7 @@ impl ArchiveDownloader for PharDownloader { path: &str, ) -> anyhow::Result> { // Can throw an UnexpectedValueException - let archive = Phar::new(file.to_string())?; + let archive = Phar::new(file)?; archive.extract_to(path, None, true)?; // TODO: handle openssl signed phars // https://github.com/composer/composer/pull/33#issuecomment-2250768 diff --git a/crates/shirabe/src/downloader/tar_downloader.rs b/crates/shirabe/src/downloader/tar_downloader.rs index a4446287..3c73595b 100644 --- a/crates/shirabe/src/downloader/tar_downloader.rs +++ b/crates/shirabe/src/downloader/tar_downloader.rs @@ -61,7 +61,7 @@ impl ArchiveDownloader for TarDownloader { file: &str, path: &str, ) -> anyhow::Result> { - let archive = PharData::new(file.to_string())?; + let archive = PharData::new(file)?; archive.extract_to(path, None, true)?; Ok(None) diff --git a/crates/shirabe/src/factory.rs b/crates/shirabe/src/factory.rs index a1fda63b..de632143 100644 --- a/crates/shirabe/src/factory.rs +++ b/crates/shirabe/src/factory.rs @@ -512,7 +512,7 @@ impl Factory { Self::create_config(Some(io.clone()), Some(&cwd))? }; let is_global = local_config_source != Config::SOURCE_UNKNOWN - && realpath(&config.get_str("home")?) == realpath(&dirname(&local_config_source)); + && realpath(&config.get_str("home")?) == realpath(dirname(&local_config_source)); config.merge(&local_config_data, &local_config_source); if let Some(ref composer_file_path) = composer_file { diff --git a/crates/shirabe/src/installer/library_installer.rs b/crates/shirabe/src/installer/library_installer.rs index 479fc6d5..a037fac9 100644 --- a/crates/shirabe/src/installer/library_installer.rs +++ b/crates/shirabe/src/installer/library_installer.rs @@ -205,7 +205,7 @@ impl LibraryInstaller { self.filesystem .borrow_mut() .ensure_directory_exists(&self.vendor_dir.borrow()); - let realpath = realpath(&self.vendor_dir.borrow()).unwrap_or_default(); + let realpath = realpath(self.vendor_dir.borrow().as_str()).unwrap_or_default(); *self.vendor_dir.borrow_mut() = realpath; } diff --git a/crates/shirabe/src/package/archiver/zip_archiver.rs b/crates/shirabe/src/package/archiver/zip_archiver.rs index bf0144fb..186d76c7 100644 --- a/crates/shirabe/src/package/archiver/zip_archiver.rs +++ b/crates/shirabe/src/package/archiver/zip_archiver.rs @@ -74,15 +74,12 @@ impl ArchiverInterface for ZipArchiver { if filepath.is_dir() { zip.add_empty_dir(&relative_path.to_string_lossy()); } else { - zip.add_file( - &filepath.to_string_lossy(), - &relative_path.to_string_lossy(), - ); + zip.add_file(&filepath, &relative_path.to_string_lossy()); } // setExternalAttributesName() is only available with libzip 0.11.2 or above if method_exists(&PhpMixed::Null, "setExternalAttributesName") { - let perms = fileperms(&filepath.to_string_lossy()); + let perms = fileperms(&filepath); zip.set_external_attributes_name( &relative_path.to_string_lossy(), ZipArchive::OPSYS_UNIX, diff --git a/crates/shirabe/src/package/locker.rs b/crates/shirabe/src/package/locker.rs index b7d8ba7a..d2d1b109 100644 --- a/crates/shirabe/src/package/locker.rs +++ b/crates/shirabe/src/package/locker.rs @@ -807,7 +807,7 @@ impl Locker { if path.is_none() { return Ok(None); } - let path = realpath(&path.unwrap()); + let path = realpath(path.unwrap()); let source_type = package.get_source_type(); let mut datetime: Option> = None; diff --git a/crates/shirabe/src/repository/filesystem_repository.rs b/crates/shirabe/src/repository/filesystem_repository.rs index 1949803b..b8ee30c6 100644 --- a/crates/shirabe/src/repository/filesystem_repository.rs +++ b/crates/shirabe/src/repository/filesystem_repository.rs @@ -644,7 +644,7 @@ impl FilesystemRepository { let install_path = if package.as_root().is_some() { let to = self.filesystem.borrow_mut().normalize_path( - &realpath(&Platform::get_cwd(false).unwrap_or_default()).unwrap_or_default(), + &realpath(Platform::get_cwd(false).unwrap_or_default()).unwrap_or_default(), ); Some( self.filesystem diff --git a/crates/shirabe/src/repository/vcs/fossil_driver.rs b/crates/shirabe/src/repository/vcs/fossil_driver.rs index a75f6874..1b61ed4d 100644 --- a/crates/shirabe/src/repository/vcs/fossil_driver.rs +++ b/crates/shirabe/src/repository/vcs/fossil_driver.rs @@ -123,7 +123,7 @@ impl FossilDriver { let mut fs = Filesystem::new(None); fs.ensure_directory_exists(&self.checkout_dir)?; - if !is_writable(&dirname(&self.checkout_dir)) { + if !is_writable(dirname(&self.checkout_dir)) { return Err(RuntimeException { message: format!( "Can not clone {} to access package information. The \"{}\" directory is not writable by the current user.", diff --git a/crates/shirabe/src/repository/vcs/git_driver.rs b/crates/shirabe/src/repository/vcs/git_driver.rs index d3d489f2..1376ebbc 100644 --- a/crates/shirabe/src/repository/vcs/git_driver.rs +++ b/crates/shirabe/src/repository/vcs/git_driver.rs @@ -95,7 +95,7 @@ impl GitDriver { let mut fs = Filesystem::new(None); fs.ensure_directory_exists(&dirname(&self.repo_dir))?; - if !is_writable(&dirname(&self.repo_dir)) { + if !is_writable(dirname(&self.repo_dir)) { return Err(RuntimeException { message: format!( "Can not clone {} to access package information. The \"{}\" directory is not writable by the current user.", diff --git a/crates/shirabe/src/repository/vcs/hg_driver.rs b/crates/shirabe/src/repository/vcs/hg_driver.rs index a346094c..0283ed38 100644 --- a/crates/shirabe/src/repository/vcs/hg_driver.rs +++ b/crates/shirabe/src/repository/vcs/hg_driver.rs @@ -69,7 +69,7 @@ impl HgDriver { let mut fs = Filesystem::new(None); fs.ensure_directory_exists(&cache_vcs_dir)?; - if !is_writable(&dirname(&self.repo_dir)) { + if !is_writable(dirname(&self.repo_dir)) { return Err(RuntimeException { message: format!( "Can not clone {} to access package information. The \"{}\" directory is not writable by the current user.", diff --git a/crates/shirabe/src/util/filesystem.rs b/crates/shirabe/src/util/filesystem.rs index 51885cc3..daf26e22 100644 --- a/crates/shirabe/src/util/filesystem.rs +++ b/crates/shirabe/src/util/filesystem.rs @@ -499,7 +499,7 @@ impl Filesystem { if file.is_dir() { self.ensure_directory_exists(&target_path)?; } else { - result = result && copy(&file.get_pathname(), &target_path); + result = result && copy(file.get_pathname(), &target_path); } } @@ -944,7 +944,7 @@ impl Filesystem { let cwd = Platform::get_cwd(false).unwrap_or_default(); let relative_path = self.find_shortest_path(link, target, false, false); - chdir(&dirname(link)); + chdir(dirname(link)); let result = symlink(&relative_path, link); chdir(&cwd); diff --git a/crates/shirabe/src/util/http/curl_downloader.rs b/crates/shirabe/src/util/http/curl_downloader.rs index 89e9661e..1207dea0 100644 --- a/crates/shirabe/src/util/http/curl_downloader.rs +++ b/crates/shirabe/src/util/http/curl_downloader.rs @@ -296,7 +296,7 @@ impl CurlDownloader { crate::io::DEBUG, ); if let Some(filename) = filename { - unlink_silent(&format!("{}~", filename)); + unlink_silent(format!("{}~", filename)); } return Ok(Decision::Retry { url: url.to_string(), @@ -305,7 +305,7 @@ impl CurlDownloader { } if let Some(filename) = filename { - unlink_silent(&format!("{}~", filename)); + unlink_silent(format!("{}~", filename)); } // PHP throws a MaxFileSizeExceededException (a TransportException subclass) with // the raw "Maximum allowed download size reached..." message verbatim rather than @@ -368,7 +368,7 @@ impl CurlDownloader { .unwrap_or(0); attributes.insert("retries".to_string(), PhpMixed::Int(retries + 1)); if let Some(filename) = filename { - unlink_silent(&format!("{}~", filename)); + unlink_silent(format!("{}~", filename)); } return Ok(Decision::Retry { url: url.to_string(), @@ -392,7 +392,7 @@ impl CurlDownloader { Ok(location) if !location.is_empty() => { attributes.insert("redirects".to_string(), PhpMixed::Int(redirects + 1)); if let Some(filename) = filename { - unlink_silent(&format!("{}~", filename)); + unlink_silent(format!("{}~", filename)); } return Ok(Decision::Retry { url: location, @@ -402,7 +402,7 @@ impl CurlDownloader { Ok(_) => {} Err(e) => { if let Some(filename) = filename { - unlink_silent(&format!("{}~", filename)); + unlink_silent(format!("{}~", filename)); } return Ok(Decision::Failed(e)); } @@ -440,7 +440,7 @@ impl CurlDownloader { ); attributes.insert("retries".to_string(), PhpMixed::Int(retries + 1)); if let Some(filename) = filename { - unlink_silent(&format!("{}~", filename)); + unlink_silent(format!("{}~", filename)); } return Ok(Decision::Retry { url: url.to_string(), @@ -827,7 +827,7 @@ impl CurlDownloader { error_message: &str, ) -> TransportException { if let Some(filename) = filename { - unlink_silent(&format!("{}~", filename)); + unlink_silent(format!("{}~", filename)); } let mut details = String::new(); diff --git a/crates/shirabe/src/util/tar.rs b/crates/shirabe/src/util/tar.rs index 792d1c4f..f5b0ebbe 100644 --- a/crates/shirabe/src/util/tar.rs +++ b/crates/shirabe/src/util/tar.rs @@ -7,7 +7,7 @@ pub struct Tar; impl Tar { pub fn get_composer_json(path_to_archive: &str) -> anyhow::Result> { - let phar = PharData::new(path_to_archive.to_string())?; + let phar = PharData::new(path_to_archive)?; if !phar.valid() { return Ok(None); -- cgit v1.3.1