host_env: Simplify, dedupe remove_dir_at (#8723)

This commit is contained in:
Joshua Megnauth authored and GitHub committed 2026-09-20 00:33:53 +09:00
1 parent 02c2a1dfc6
commit 391668b483
4 files changed
+10 -28

No files matched your search

-10
View File
@@ -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 = {
+8
View File
@@ -49,6 +49,14 @@ pub fn replace(
rename(from, from_fd, to, to_fd)
}
pub fn remove_dir_at(
dir_fd: Option<crt_fd::Borrowed<'_>>,
path: impl AsRef<Path>,
) -> 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<Path>,
dir_fd: Option<crt_fd::Borrowed<'_>>,
-5
View File
@@ -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::StatStruct> {
crate::fileutils::fstat(fd)
}
+2 -13
View File
@@ -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))
}