@@ -924,23 +924,12 @@ pub fn GetFinalPathNameByHandle(
924
924
out_buffer : []u16 ,
925
925
) GetFinalPathNameByHandleError ! []u16 {
926
926
// 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.. ]);
935
929
936
930
// 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.. ]);
944
933
945
934
const file_name = @ptrCast (* const FILE_NAME_INFORMATION , & path_buffer [0 ]);
946
935
const file_name_u16 = @ptrCast ([* ]const u16 , & file_name .FileName [0 ])[0 .. file_name .FileNameLength / 2 ];
@@ -1014,9 +1003,7 @@ pub fn GetFinalPathNameByHandle(
1014
1003
// with traditional DOS drive letters, so pick the first one available.
1015
1004
const prefix = &[_ ]u16 { '\\ ' , 'D' , 'o' , 's' , 'D' , 'e' , 'v' , 'i' , 'c' , 'e' , 's' , '\\ ' };
1016
1005
1017
- if (std .mem .indexOf (u16 , symlink , prefix )) | idx | {
1018
- if (idx != 0 ) continue ;
1019
-
1006
+ if (std .mem .startsWith (u16 , symlink , prefix )) {
1020
1007
const drive_letter = symlink [prefix .len .. ];
1021
1008
1022
1009
if (out_buffer .len < drive_letter .len + file_name_u16 .len ) return error .NameTooLong ;
@@ -1035,6 +1022,25 @@ pub fn GetFinalPathNameByHandle(
1035
1022
}
1036
1023
}
1037
1024
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
+
1038
1044
pub const GetFileSizeError = error {Unexpected };
1039
1045
1040
1046
pub fn GetFileSizeEx (hFile : HANDLE ) GetFileSizeError ! u64 {
0 commit comments