Skip to content

Commit

Permalink
Fall back to old impl for remove_dir_all
Browse files Browse the repository at this point in the history
The recursive directory removal implementation falls back to the old one
if the necessary APIs are not available (`NtCreateFile`,
`GetFileInformationByHandleEx`, `SetFileInformationByHandle`). The APIs
are available on Vista/Server 2008.

See notes on `fileextd.lib` above to extend the support to Windows
XP/Server 2003. **This might cause security issues**, see
rust-lang#93112
  • Loading branch information
seritools committed Dec 1, 2024
1 parent 1ca54e4 commit 0414fca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions library/std/src/sys/pal/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,25 @@ pub fn rmdir(p: &Path) -> io::Result<()> {
}

pub fn remove_dir_all(path: &Path) -> io::Result<()> {
#[cfg(target_vendor = "rust9x")]
{
// if the modern file/directory APIs are not available, we'll fall back to the old (unsafe, see
// https://github.com/rust-lang/rust/pull/93112) directory removal implementation
if !(c::NtCreateFile::available().is_some()
&& c::GetFileInformationByHandleEx::available().is_some()
&& c::SetFileInformationByHandle::available().is_some())
{
let filetype = lstat(path)?.file_type();
if filetype.is_symlink() {
// On Windows symlinks to files and directories are removed differently.
// rmdir only deletes dir symlinks and junctions, not file symlinks.
return rmdir(path);
} else {
return remove_dir_all::remove_dir_all_recursive_old(path);
}
}
}

// Open a file or directory without following symlinks.
let mut opts = OpenOptions::new();
opts.access_mode(c::FILE_LIST_DIRECTORY);
Expand Down
18 changes: 18 additions & 0 deletions library/std/src/sys/pal/windows/fs/remove_dir_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,21 @@ pub fn remove_dir_all_iterative(dir: File) -> Result<(), WinError> {
}
Ok(())
}

#[cfg(target_vendor = "rust9x")]
pub fn remove_dir_all_recursive_old(path: &crate::path::Path) -> crate::io::Result<()> {
use super::*;

for child in readdir(path)? {
let child = child?;
let child_type = child.file_type()?;
if child_type.is_dir() {
remove_dir_all_recursive_old(&child.path())?;
} else if child_type.is_symlink_dir() {
rmdir(&child.path())?;
} else {
unlink(&child.path())?;
}
}
rmdir(path)
}

0 comments on commit 0414fca

Please sign in to comment.