Skip to content

Commit a3bec9b

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 9bab17a commit a3bec9b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,25 @@ pub fn rmdir(p: &Path) -> io::Result<()> {
14061406
}
14071407

14081408
pub fn remove_dir_all(path: &Path) -> io::Result<()> {
1409+
#[cfg(target_vendor = "rust9x")]
1410+
{
1411+
// if the modern file/directory APIs are not available, we'll fall back to the old (unsafe, see
1412+
// https://github.com/rust-lang/rust/pull/93112) directory removal implementation
1413+
if !(c::NtOpenFile::available().is_some()
1414+
&& c::GetFileInformationByHandleEx::available().is_some()
1415+
&& c::SetFileInformationByHandle::available().is_some())
1416+
{
1417+
let filetype = lstat(path)?.file_type();
1418+
if filetype.is_symlink() {
1419+
// On Windows symlinks to files and directories are removed differently.
1420+
// rmdir only deletes dir symlinks and junctions, not file symlinks.
1421+
return rmdir(path);
1422+
} else {
1423+
return remove_dir_all::remove_dir_all_recursive_old(path);
1424+
}
1425+
}
1426+
}
1427+
14091428
// Open a file or directory without following symlinks.
14101429
let mut opts = OpenOptions::new();
14111430
opts.access_mode(c::FILE_LIST_DIRECTORY);

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

Lines changed: 18 additions & 0 deletions
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)