compiletest: use std::fs::remove_dir_all
now that it is available by jieyouxu · Pull Request #129302 · rust-lang/rust (original) (raw)
I'm asking because docs.rs/remove_dir_all/latest/remove_dir_all explicitly mentions that it handles the readonly flag on Windows, so it seems like the
std::fs
version might not do that.
Based on
/// Delete using POSIX semantics. |
---|
/// |
/// Files will be deleted as soon as the handle is closed. This is supported |
/// for Windows 10 1607 (aka RS1) and later. However some filesystem |
/// drivers will not support it even then, e.g. FAT32. |
/// |
/// If the operation is not supported for this filesystem or OS version |
/// then errors will be `ERROR_NOT_SUPPORTED` or `ERROR_INVALID_PARAMETER`. |
fn posix_delete(&self) -> io::Result<()> { |
let info = c::FILE_DISPOSITION_INFO_EX { |
Flags: c::FILE_DISPOSITION_FLAG_DELETE |
| c::FILE_DISPOSITION_FLAG_POSIX_SEMANTICS |
| c::FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE, |
}; |
api::set_file_information_by_handle(self.handle.as_raw_handle(), &info).io_result() |
} |
I believe it is bypassing the read-only flag with FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE
(undocumented API on MSDN). But cc @ChrisDenton as you know way more about this...
However, that seems only to be true for the posix delete semantics path which is available for Windows 10 RS1 and beyond, but not Windows 7.
At least in #129187 when I added the test cases (I later removed them because std already has tests for them), I tested combination of symlinks and read-onlyn files/directories on Windows, and std::fs::remove_dir_all
handled them no problem locally on Windows 11.
EDIT: asked Chris, yes it does, see https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/ns-ntddk-_file_disposition_information_ex, specifically
FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE
Allows read-only files to be deleted.
EDIT 2: for Unix, read-only files can be deleted without special handling. Note that unlink
the syscall is not rm
; the rm
command does extra read-only checking.