Skip to content

Commit cd8e6b6

Browse files
committed
Address some review comments
1 parent 310aa87 commit cd8e6b6

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

lib/std/os/windows.zig

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -924,23 +924,12 @@ pub fn GetFinalPathNameByHandle(
924924
out_buffer: []u16,
925925
) GetFinalPathNameByHandleError![]u16 {
926926
// Get normalized path; doesn't include volume name though.
927-
var path_buffer: [PATH_MAX_WIDE * 2]u8 align(@alignOf(FILE_NAME_INFORMATION)) = undefined;
928-
var io: IO_STATUS_BLOCK = undefined;
929-
var rc = ntdll.NtQueryInformationFile(hFile, &io, &path_buffer, path_buffer.len, FILE_INFORMATION_CLASS.FileNormalizedNameInformation);
930-
switch (rc) {
931-
.SUCCESS => {},
932-
.INVALID_PARAMETER => unreachable,
933-
else => return unexpectedStatus(rc),
934-
}
927+
var path_buffer: [@sizeOf(FILE_NAME_INFORMATION) + PATH_MAX_WIDE * 2]u8 align(@alignOf(FILE_NAME_INFORMATION)) = undefined;
928+
try QueryInformationFile(hFile, FILE_INFORMATION_CLASS.FileNormalizedNameInformation, path_buffer[0..]);
935929

936930
// Get NT volume name.
937-
var volume_buffer: [MAX_PATH]u8 align(@alignOf(FILE_NAME_INFORMATION)) = undefined; // MAX_PATH bytes should be enough since it's Windows-defined name
938-
rc = ntdll.NtQueryInformationFile(hFile, &io, &volume_buffer, volume_buffer.len, FILE_INFORMATION_CLASS.FileVolumeNameInformation);
939-
switch (rc) {
940-
.SUCCESS => {},
941-
.INVALID_PARAMETER => unreachable,
942-
else => return unexpectedStatus(rc),
943-
}
931+
var volume_buffer: [@sizeOf(FILE_NAME_INFORMATION) + MAX_PATH]u8 align(@alignOf(FILE_NAME_INFORMATION)) = undefined; // MAX_PATH bytes should be enough since it's Windows-defined name
932+
try QueryInformationFile(hFile, FILE_INFORMATION_CLASS.FileVolumeNameInformation, volume_buffer[0..]);
944933

945934
const file_name = @ptrCast(*const FILE_NAME_INFORMATION, &path_buffer[0]);
946935
const file_name_u16 = @ptrCast([*]const u16, &file_name.FileName[0])[0 .. file_name.FileNameLength / 2];
@@ -1014,9 +1003,7 @@ pub fn GetFinalPathNameByHandle(
10141003
// with traditional DOS drive letters, so pick the first one available.
10151004
const prefix = &[_]u16{ '\\', 'D', 'o', 's', 'D', 'e', 'v', 'i', 'c', 'e', 's', '\\' };
10161005

1017-
if (std.mem.indexOf(u16, symlink, prefix)) |idx| {
1018-
if (idx != 0) continue;
1019-
1006+
if (std.mem.startsWith(u16, symlink, prefix)) {
10201007
const drive_letter = symlink[prefix.len..];
10211008

10221009
if (out_buffer.len < drive_letter.len + file_name_u16.len) return error.NameTooLong;
@@ -1035,6 +1022,25 @@ pub fn GetFinalPathNameByHandle(
10351022
}
10361023
}
10371024

1025+
pub const QueryInformationFileError = error{Unexpected};
1026+
1027+
pub fn QueryInformationFile(
1028+
handle: HANDLE,
1029+
info_class: FILE_INFORMATION_CLASS,
1030+
out_buffer: []u8,
1031+
) QueryInformationFileError!void {
1032+
var io: IO_STATUS_BLOCK = undefined;
1033+
const len_bytes = std.math.cast(u32, out_buffer.len) catch |err| switch (err) {
1034+
error.Overflow => std.math.maxInt(u32), // If the provided buffer is larger than what we can handle, set size to max what we can handle
1035+
};
1036+
const rc = ntdll.NtQueryInformationFile(handle, &io, out_buffer.ptr, len_bytes, info_class);
1037+
switch (rc) {
1038+
.SUCCESS => {},
1039+
.INVALID_PARAMETER => unreachable,
1040+
else => return unexpectedStatus(rc),
1041+
}
1042+
}
1043+
10381044
pub const GetFileSizeError = error{Unexpected};
10391045

10401046
pub fn GetFileSizeEx(hFile: HANDLE) GetFileSizeError!u64 {

lib/std/os/windows/bits.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,4 +1590,4 @@ pub const MOUNTMGR_MOUNT_POINTS = extern struct {
15901590
NumberOfMountPoints: ULONG,
15911591
MountPoints: [1]MOUNTMGR_MOUNT_POINT,
15921592
};
1593-
pub const IOCTL_MOUNTMGR_QUERY_POINTS: ULONG = 0x6d0008;
1593+
pub const IOCTL_MOUNTMGR_QUERY_POINTS: ULONG = 0x6d0008;

0 commit comments

Comments
 (0)