Skip to content

Commit 4957f7a

Browse files
moosichusqueek502
andcommitted
Deal with NT paths in GetFinalPathNameByHandle
When calling QueryObjectName, NT namespaced paths can be returned. This change appropriately strips the prefix to turn it into an absolute path. (The above behaviour was observed at least in Wine so far) Co-authored-by: Ryan Liptak <[email protected]>
1 parent 7abf9b3 commit 4957f7a

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

lib/std/os/windows.zig

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,16 +1219,33 @@ pub fn GetFinalPathNameByHandle(
12191219
},
12201220
.Dos => {
12211221
// parse the string to separate volume path from file path
1222-
const expected_prefix = std.unicode.utf8ToUtf16LeStringLiteral("\\Device\\");
1223-
1224-
// TODO find out if a path can start with something besides `\Device\<volume name>`,
1225-
// and if we need to handle it differently
1226-
// (i.e. how to determine the start and end of the volume name in that case)
1227-
if (!mem.eql(u16, expected_prefix, final_path[0..expected_prefix.len])) return error.Unexpected;
1222+
const device_prefix = std.unicode.utf8ToUtf16LeStringLiteral("\\Device\\");
1223+
1224+
// We aren't entirely sure of the structure of the path returned by
1225+
// QueryObjectName in all contexts/environments.
1226+
// This code is written to cover the various cases that have
1227+
// been encountered and solved appropriately. But note that there's
1228+
// no easy way to verify that they have all been tackled!
1229+
// (Unless you, the reader knows of one then please do action that!)
1230+
if (!mem.eql(u16, device_prefix, final_path[0..device_prefix.len])) {
1231+
// Wine seems to return NT namespaced paths from QueryObjectName
1232+
// (e.g. `\??\Z:\some\path\to\a\file.txt`), in which case we can just strip the
1233+
// prefix to turn it into an absolute paths
1234+
const namespace_prefix = getNamespacePrefix(u16, final_path);
1235+
if (namespace_prefix == .nt) {
1236+
const unprefixed_path = final_path[NamespacePrefix.len..];
1237+
// TODO: Handle other possible path types, only drive absolute has been observed so far
1238+
if (getUnprefixedPathType(u16, unprefixed_path) != .drive_absolute) {
1239+
return error.Unexpected;
1240+
}
1241+
return unprefixed_path;
1242+
}
1243+
return error.Unexpected;
1244+
}
12281245

1229-
const file_path_begin_index = mem.indexOfPos(u16, final_path, expected_prefix.len, &[_]u16{'\\'}) orelse unreachable;
1246+
const file_path_begin_index = mem.indexOfPos(u16, final_path, device_prefix.len, &[_]u16{'\\'}) orelse unreachable;
12301247
const volume_name_u16 = final_path[0..file_path_begin_index];
1231-
const device_name_u16 = volume_name_u16[expected_prefix.len..];
1248+
const device_name_u16 = volume_name_u16[device_prefix.len..];
12321249
const file_name_u16 = final_path[file_path_begin_index..];
12331250

12341251
// MUP is Multiple UNC Provider, and indicates that the path is a UNC
@@ -2343,6 +2360,9 @@ pub const NamespacePrefix = enum {
23432360
fake_verbatim,
23442361
/// `\??\`
23452362
nt,
2363+
2364+
// The length of all the prefixes (except `none`)
2365+
pub const len: usize = 4;
23462366
};
23472367

23482368
/// If `T` is `u16`, then `path` should be encoded as UTF-16LE.

0 commit comments

Comments
 (0)