Skip to content

Commit ac02fcc

Browse files
committed
Use NtCreateFile instead of NtOpenFile to open a file
1 parent ecf7299 commit ac02fcc

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

library/std/src/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
20442044
///
20452045
/// This function currently corresponds to `openat`, `fdopendir`, `unlinkat` and `lstat` functions
20462046
/// on Unix (except for macOS before version 10.10 and REDOX) and the `CreateFileW`,
2047-
/// `GetFileInformationByHandleEx`, `SetFileInformationByHandle`, and `NtOpenFile` functions on
2047+
/// `GetFileInformationByHandleEx`, `SetFileInformationByHandle`, and `NtCreateFile` functions on
20482048
/// Windows. Note that, this [may change in the future][changes].
20492049
///
20502050
/// [changes]: io#platform-specific-behavior

library/std/src/sys/windows/c.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub const FILE_SHARE_DELETE: DWORD = 0x4;
8888
pub const FILE_SHARE_READ: DWORD = 0x1;
8989
pub const FILE_SHARE_WRITE: DWORD = 0x2;
9090

91+
pub const FILE_OPEN: ULONG = 0x00000001;
9192
pub const FILE_OPEN_REPARSE_POINT: ULONG = 0x200000;
9293
pub const OBJ_DONT_REPARSE: ULONG = 0x1000;
9394

@@ -1228,15 +1229,20 @@ compat_fn! {
12281229

12291230
compat_fn! {
12301231
"ntdll":
1231-
pub fn NtOpenFile(
1232+
pub fn NtCreateFile(
12321233
FileHandle: *mut HANDLE,
12331234
DesiredAccess: ACCESS_MASK,
12341235
ObjectAttributes: *const OBJECT_ATTRIBUTES,
12351236
IoStatusBlock: *mut IO_STATUS_BLOCK,
1237+
AllocationSize: *mut i64,
1238+
FileAttributes: ULONG,
12361239
ShareAccess: ULONG,
1237-
OpenOptions: ULONG
1240+
CreateDisposition: ULONG,
1241+
CreateOptions: ULONG,
1242+
EaBuffer: *mut c_void,
1243+
EaLength: ULONG
12381244
) -> NTSTATUS {
1239-
panic!("`NtOpenFile` not available");
1245+
panic!("`NtCreateFile` not available");
12401246
}
12411247
pub fn RtlNtStatusToDosError(
12421248
Status: NTSTATUS

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,11 @@ impl<'a> Iterator for DirBuffIter<'a> {
712712

713713
/// Open a link relative to the parent directory, ensure no symlinks are followed.
714714
fn open_link_no_reparse(parent: &File, name: &[u16], access: u32) -> io::Result<File> {
715-
// This is implemented using the lower level `NtOpenFile` function as
715+
// This is implemented using the lower level `NtCreateFile` function as
716716
// unfortunately opening a file relative to a parent is not supported by
717717
// win32 functions. It is however a fundamental feature of the NT kernel.
718718
//
719-
// See https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntopenfile
719+
// See https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntcreatefile
720720
unsafe {
721721
let mut handle = ptr::null_mut();
722722
let mut io_status = c::IO_STATUS_BLOCK::default();
@@ -732,14 +732,19 @@ fn open_link_no_reparse(parent: &File, name: &[u16], access: u32) -> io::Result<
732732
Attributes: ATTRIBUTES.load(Ordering::Relaxed),
733733
..c::OBJECT_ATTRIBUTES::default()
734734
};
735-
let status = c::NtOpenFile(
735+
let status = c::NtCreateFile(
736736
&mut handle,
737737
access,
738738
&object,
739739
&mut io_status,
740+
crate::ptr::null_mut(),
741+
0,
740742
c::FILE_SHARE_DELETE | c::FILE_SHARE_READ | c::FILE_SHARE_WRITE,
743+
c::FILE_OPEN,
741744
// If `name` is a symlink then open the link rather than the target.
742745
c::FILE_OPEN_REPARSE_POINT,
746+
crate::ptr::null_mut(),
747+
0,
743748
);
744749
// Convert an NTSTATUS to the more familiar Win32 error codes (aka "DosError")
745750
if c::nt_success(status) {

0 commit comments

Comments
 (0)