diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-10 02:59:50 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-10 03:05:26 +0900 |
| commit | 264feb1093d587e5df457fac023852a0e578b22f (patch) | |
| tree | fc23db88f628bd421c2a9819ebbdb96b05ed99de /crates | |
| parent | 54760d5c73ca20de2e155df274d576cec9ea0ed0 (diff) | |
| download | php-shirabe-264feb1093d587e5df457fac023852a0e578b22f.tar.gz php-shirabe-264feb1093d587e5df457fac023852a0e578b22f.tar.zst php-shirabe-264feb1093d587e5df457fac023852a0e578b22f.zip | |
fix(php-shim): return an error from fileperms instead of 0
fileperms swallowed metadata errors and reported a mode of 0, which the
callers could not tell apart from a real (if implausible) mode: the zip
archiver stored entries with no permission bits, and Filesystem::copy
chmod-ed the target to 0. It now returns Result<u32, io::Error> so both
callers propagate the failure. The switch to u32 also drops the casts
around the mode arithmetic and ZipArchive's entry attributes.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/shirabe-php-shim/src/fs.rs | 9 | ||||
| -rw-r--r-- | crates/shirabe-php-shim/src/zip.rs | 8 | ||||
| -rw-r--r-- | crates/shirabe-symfony-filesystem/src/filesystem.rs | 6 | ||||
| -rw-r--r-- | crates/shirabe/src/package/archiver/zip_archiver.rs | 2 |
4 files changed, 11 insertions, 14 deletions
diff --git a/crates/shirabe-php-shim/src/fs.rs b/crates/shirabe-php-shim/src/fs.rs index a948a6f2..6aa8eeee 100644 --- a/crates/shirabe-php-shim/src/fs.rs +++ b/crates/shirabe-php-shim/src/fs.rs @@ -791,13 +791,10 @@ pub fn chmod(path: impl AsRef<std::path::Path>, mode: u32) -> bool { std::fs::set_permissions(path.as_ref(), std::fs::Permissions::from_mode(mode)).is_ok() } -pub fn fileperms(path: impl AsRef<std::path::Path>) -> i64 { +pub fn fileperms(path: impl AsRef<std::path::Path>) -> Result<u32, std::io::Error> { use std::os::unix::fs::MetadataExt; - // PHP returns the full st_mode (file type bits included). - // TODO(phase-c): PHP returns false on error; this i64 signature reports 0 instead. - std::fs::metadata(path.as_ref()) - .map(|m| m.mode() as i64) - .unwrap_or(0) + // TODO(phase-e): PHP returns the full st_mode (file type bits included). + std::fs::metadata(path.as_ref()).map(|m| m.mode()) } pub fn filesize(path: impl AsRef<std::path::Path>) -> Option<i64> { diff --git a/crates/shirabe-php-shim/src/zip.rs b/crates/shirabe-php-shim/src/zip.rs index f028ffd0..4fab9aad 100644 --- a/crates/shirabe-php-shim/src/zip.rs +++ b/crates/shirabe-php-shim/src/zip.rs @@ -218,7 +218,7 @@ impl ZipArchive { ))) } - pub fn add_empty_dir(&self, local_name: &str, opsys: i64, attr: i64) -> bool { + pub fn add_empty_dir(&self, local_name: &str, opsys: i64, attr: u32) -> bool { let mut state = self.state.borrow_mut(); let ZipState::Writer { writer, .. } = &mut *state else { return false; @@ -233,7 +233,7 @@ impl ZipArchive { filepath: impl AsRef<std::path::Path>, local_name: &str, opsys: i64, - attr: i64, + attr: u32, ) -> bool { let contents = match std::fs::read(filepath.as_ref()) { Ok(c) => c, @@ -257,14 +257,14 @@ impl ZipArchive { /// keeps only the low 9 mode bits, on top of which the crate restores `S_IFREG` for /// files and `S_IFDIR` for directories; libzip stores `attr` verbatim, so /// setuid/setgid/sticky bits and the remaining file types do not survive. - fn entry_options(opsys: i64, attr: i64) -> SimpleFileOptions { + fn entry_options(opsys: i64, attr: u32) -> SimpleFileOptions { let system = match opsys { Self::OPSYS_UNIX => zip::System::Unix, _ => todo!(), }; SimpleFileOptions::default() .system(system) - .unix_permissions((attr >> 16) as u32) + .unix_permissions(attr >> 16) } pub fn get_status_string(&self) -> String { diff --git a/crates/shirabe-symfony-filesystem/src/filesystem.rs b/crates/shirabe-symfony-filesystem/src/filesystem.rs index 737c3bea..0bb54efd 100644 --- a/crates/shirabe-symfony-filesystem/src/filesystem.rs +++ b/crates/shirabe-symfony-filesystem/src/filesystem.rs @@ -55,9 +55,9 @@ impl Filesystem { // on the target instead, so the mode fopen would have left is captured here and put // back below. let target_perms = if shirabe_php_shim::is_file(target_file) { - shirabe_php_shim::fileperms(target_file) + shirabe_php_shim::fileperms(target_file)? } else { - 0o666 & !(shirabe_php_shim::umask() as i64) + 0o666 & !shirabe_php_shim::umask() }; if !shirabe_php_shim::copy(origin_file, target_file) { @@ -85,7 +85,7 @@ impl Filesystem { // Like `cp`, preserve executable permission bits. shirabe_php_shim::chmod( target_file, - (target_perms | (shirabe_php_shim::fileperms(origin_file) & 0o111)) as u32, + (target_perms | (shirabe_php_shim::fileperms(origin_file)? & 0o111)) as u32, ); // Like `cp`, preserve the file modification time. diff --git a/crates/shirabe/src/package/archiver/zip_archiver.rs b/crates/shirabe/src/package/archiver/zip_archiver.rs index 5604c07d..2ef9473e 100644 --- a/crates/shirabe/src/package/archiver/zip_archiver.rs +++ b/crates/shirabe/src/package/archiver/zip_archiver.rs @@ -70,7 +70,7 @@ impl ArchiverInterface for ZipArchiver { } // Ensure to preserve the permission umasks for the filepath in the archive. - let perms = fileperms(&filepath); + let perms = fileperms(&filepath)?; if filepath.is_dir() { zip.add_empty_dir( |
