Skip to content

Commit 9ed04ab

Browse files
committed
Fall back to old impl for remove_dir_all
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
1 parent ef56e7f commit 9ed04ab

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

library/std/src/sys/pal/windows/fs.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,25 @@ pub fn rmdir(p: &Path) -> io::Result<()> {
12591259
}
12601260

12611261
pub fn remove_dir_all(path: &Path) -> io::Result<()> {
1262+
#[cfg(target_vendor = "rust9x")]
1263+
{
1264+
// if the modern file/directory APIs are not available, we'll fall back to the old (unsafe, see
1265+
// https://github.com/rust-lang/rust/pull/93112) directory removal implementation
1266+
if !(c::NtCreateFile::available().is_some()
1267+
&& c::GetFileInformationByHandleEx::available().is_some()
1268+
&& c::SetFileInformationByHandle::available().is_some())
1269+
{
1270+
let filetype = lstat(path)?.file_type();
1271+
if filetype.is_symlink() {
1272+
// On Windows symlinks to files and directories are removed differently.
1273+
// rmdir only deletes dir symlinks and junctions, not file symlinks.
1274+
return rmdir(path);
1275+
} else {
1276+
return remove_dir_all::remove_dir_all_recursive_old(path);
1277+
}
1278+
}
1279+
}
1280+
12621281
// Open a file or directory without following symlinks.
12631282
let mut opts = OpenOptions::new();
12641283
opts.access_mode(c::FILE_LIST_DIRECTORY);

library/std/src/sys/pal/windows/fs/remove_dir_all.rs

+18
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,21 @@ pub fn remove_dir_all_iterative(dir: File) -> Result<(), WinError> {
203203
}
204204
Ok(())
205205
}
206+
207+
#[cfg(target_vendor = "rust9x")]
208+
pub fn remove_dir_all_recursive_old(path: &crate::path::Path) -> crate::io::Result<()> {
209+
use super::*;
210+
211+
for child in readdir(path)? {
212+
let child = child?;
213+
let child_type = child.file_type()?;
214+
if child_type.is_dir() {
215+
remove_dir_all_recursive_old(&child.path())?;
216+
} else if child_type.is_symlink_dir() {
217+
rmdir(&child.path())?;
218+
} else {
219+
unlink(&child.path())?;
220+
}
221+
}
222+
rmdir(path)
223+
}

0 commit comments

Comments
 (0)