Skip to content

Commit fc39ff1

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 4c7caeb commit fc39ff1

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
@@ -1341,6 +1341,25 @@ pub fn rmdir(p: &Path) -> io::Result<()> {
13411341
}
13421342

13431343
pub fn remove_dir_all(path: &Path) -> io::Result<()> {
1344+
#[cfg(target_vendor = "rust9x")]
1345+
{
1346+
// if the modern file/directory APIs are not available, we'll fall back to the old (unsafe, see
1347+
// https://github.com/rust-lang/rust/pull/93112) directory removal implementation
1348+
if !(c::NtOpenFile::available().is_some()
1349+
&& c::GetFileInformationByHandleEx::available().is_some()
1350+
&& c::SetFileInformationByHandle::available().is_some())
1351+
{
1352+
let filetype = lstat(path)?.file_type();
1353+
if filetype.is_symlink() {
1354+
// On Windows symlinks to files and directories are removed differently.
1355+
// rmdir only deletes dir symlinks and junctions, not file symlinks.
1356+
return rmdir(path);
1357+
} else {
1358+
return remove_dir_all::remove_dir_all_recursive_old(path);
1359+
}
1360+
}
1361+
}
1362+
13441363
// Open a file or directory without following symlinks.
13451364
let mut opts = OpenOptions::new();
13461365
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)