diff --git a/crates/host_env/src/posix.rs b/crates/host_env/src/posix.rs index aa8e86b92..808e74705 100644 --- a/crates/host_env/src/posix.rs +++ b/crates/host_env/src/posix.rs @@ -210,16 +210,6 @@ pub fn link_paths(src: &CStr, dst: &CStr, follow_symlinks: bool) -> std::io::Res } } -#[cfg(all(not(windows), not(target_os = "redox")))] -pub fn remove_dir_at(dir_fd: i32, path: &CStr) -> std::io::Result<()> { - let ret = unsafe { libc::unlinkat(dir_fd, path.as_ptr(), libc::AT_REMOVEDIR) }; - if ret < 0 { - Err(std::io::Error::last_os_error()) - } else { - Ok(()) - } -} - #[cfg(all(unix, not(target_os = "redox")))] fn statvfs_info_from_raw(st: libc::statvfs) -> StatVfsInfo { let f_fsid = { diff --git a/crates/host_env/src/posix_unix_like.rs b/crates/host_env/src/posix_unix_like.rs index cdace41f6..e318fdbac 100644 --- a/crates/host_env/src/posix_unix_like.rs +++ b/crates/host_env/src/posix_unix_like.rs @@ -49,6 +49,14 @@ pub fn replace( rename(from, from_fd, to, to_fd) } +pub fn remove_dir_at( + dir_fd: Option>, + path: impl AsRef, +) -> io::Result<()> { + let dir_fd = dir_fd.as_ref().map_or(fs::CWD, AsFd::as_fd); + fs::unlinkat(dir_fd, path.as_ref(), AtFlags::REMOVEDIR).map_err(Into::into) +} + pub fn stat_path( path: impl AsRef, dir_fd: Option>, diff --git a/crates/host_env/src/posix_wasi.rs b/crates/host_env/src/posix_wasi.rs index 791991e98..df09c08bc 100644 --- a/crates/host_env/src/posix_wasi.rs +++ b/crates/host_env/src/posix_wasi.rs @@ -7,11 +7,6 @@ pub use super::posix_unix_like::*; use crate::{crt_fd, os::CheckLibcResult}; -pub fn remove_dir_at(dir_fd: i32, path: &CStr) -> io::Result<()> { - unsafe { libc::unlinkat(dir_fd, path.as_ptr(), libc::AT_REMOVEDIR) }.check_libc_neg()?; - Ok(()) -} - pub fn stat_fd(fd: crate::crt_fd::Borrowed<'_>) -> io::Result { crate::fileutils::fstat(fd) } diff --git a/crates/vm/src/stdlib/os.rs b/crates/vm/src/stdlib/os.rs index e23b5da49..093e89b81 100644 --- a/crates/vm/src/stdlib/os.rs +++ b/crates/vm/src/stdlib/os.rs @@ -245,7 +245,7 @@ pub(super) mod _os { pub(crate) const SYMLINK_DIR_FD: bool = cfg!(not(any(windows, target_os = "redox"))); pub(crate) const UNLINK_DIR_FD: bool = cfg!(not(windows)); const RENAME_DIR_FD: bool = cfg!(any(unix, target_os = "wasi")); - const RMDIR_DIR_FD: bool = cfg!(not(any(windows, target_os = "redox"))); + const RMDIR_DIR_FD: bool = cfg!(not(windows)); const SCANDIR_FD: bool = cfg!(all(unix, not(target_os = "redox"))); #[pyattr] @@ -449,18 +449,7 @@ pub(super) mod _os { dir_fd: DirFd<'_, { RMDIR_DIR_FD as usize }>, vm: &VirtualMachine, ) -> PyResult<()> { - #[cfg(not(target_os = "redox"))] - if let Some(fd) = dir_fd.raw_opt() { - let c_path = path.clone().into_cstring(vm)?; - return if let Err(err) = crate::host_env::posix::remove_dir_at(fd, c_path.as_c_str()) { - Err(OSErrorBuilder::with_filename(&err, path, vm)) - } else { - Ok(()) - }; - } - #[cfg(target_os = "redox")] - let [] = dir_fd.0; - crate::host_env::fs::remove_dir(&path) + crate::host_env::posix::remove_dir_at(dir_fd.get_opt(), &path.path) .map_err(|err| OSErrorBuilder::with_filename(&err, path, vm)) }