-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix paths on Windows #7537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Fix paths on Windows #7537
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1528,6 +1528,16 @@ pub const PathSpace = struct { | |
} | ||
}; | ||
|
||
/// Decode a utf8 encoded path to a "wide character" path. Returns the path by value via PathSpace | ||
/// which can accomodate paths up to `PATH_MAX_WIDE` characters long. | ||
pub fn utf8ToWPathSpace(utf8_path: []const u8) !PathSpace { | ||
// TODO https://github.com/ziglang/zig/issues/2765 | ||
var path_space : PathSpace = undefined; | ||
path_space.len = try std.unicode.utf8ToUtf16Le(&path_space.data, utf8_path); | ||
path_space.data[path_space.len] = 0; | ||
return path_space; | ||
} | ||
|
||
/// Same as `sliceToPrefixedFileW` but accepts a pointer | ||
/// to a null-terminated path. | ||
pub fn cStrToPrefixedFileW(s: [*:0]const u8) !PathSpace { | ||
|
@@ -1569,6 +1579,48 @@ pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace { | |
return path_space; | ||
} | ||
|
||
/// Convert `dos_path` to an NT path that can be passed to Nt* functions like NtCreateFile. | ||
pub const NtPath = struct { | ||
str: UNICODE_STRING, | ||
pub fn init(dos_path: [*:0]const u16) !NtPath { | ||
var str : UNICODE_STRING = undefined; | ||
const status = ntdll.RtlDosPathNameToNtPathName_U_WithStatus(dos_path, &str, null, null); | ||
if (status != .SUCCESS) return unexpectedStatus(status); | ||
Comment on lines
+1587
to
+1588
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This introduces unnecessary heap allocation, causing |
||
return NtPath { .str = str }; | ||
} | ||
// TODO: not sure if the path is guaranteed to be NULL-terminted | ||
pub fn span(self: NtPath) []const u16 { | ||
return self.str.Buffer[0 .. self.str.Length / 2]; | ||
} | ||
pub fn deinit(self: NtPath) void { | ||
if (TRUE != kernel32.HeapFree(kernel32.GetProcessHeap().?, 0, self.str.Buffer)) { | ||
std.debug.panic("in NtPath, HeapFree failed with {}\n", .{kernel32.GetLastError()}); | ||
} | ||
} | ||
}; | ||
|
||
/// return the relative path of `nt_path` based on CWD | ||
pub fn toRelativeNtPath(nt_path: UNICODE_STRING) !UNICODE_STRING { | ||
const cwd_nt_path = try NtPath.init(&[_:0]u16 {'.'}); | ||
defer cwd_nt_path.deinit(); | ||
const cwd_span = cwd_nt_path.span(); | ||
std.debug.assert(mem.startsWith(u16, unicodeSpan(nt_path), cwd_span)); | ||
std.debug.assert(nt_path.Buffer[cwd_span.len] == '\\'); | ||
return unicodeSubstring(nt_path, @intCast(c_ushort, cwd_span.len + 1)); | ||
} | ||
|
||
pub fn unicodeSpan(str: UNICODE_STRING) []u16 { | ||
return str.Buffer[0..str.Length/2]; | ||
} | ||
pub fn unicodeSubstring(str: UNICODE_STRING, char_offset: c_ushort) UNICODE_STRING { | ||
std.debug.assert(char_offset * 2 <= str.Length); | ||
return .{ | ||
.Buffer = str.Buffer + char_offset, | ||
.MaximumLength = str.MaximumLength - (char_offset*2), | ||
.Length = str.Length - (char_offset*2), | ||
}; | ||
} | ||
|
||
/// Assumes an absolute path. | ||
pub fn wToPrefixedFileW(s: []const u16) !PathSpace { | ||
// TODO https://github.com/ziglang/zig/issues/2765 | ||
|
@@ -1637,3 +1689,9 @@ pub fn unexpectedStatus(status: NTSTATUS) std.os.UnexpectedError { | |
} | ||
return error.Unexpected; | ||
} | ||
|
||
test "" { | ||
if (builtin.os.tag == .windows) { | ||
_ = @import("windows/test.zig"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) 2015-2020 Zig Contributors | ||
// This file is part of [zig](https://ziglang.org/), which is MIT licensed. | ||
// The MIT license requires this copyright notice to be included in all copies | ||
// and substantial portions of the software. | ||
const std = @import("../../std.zig"); | ||
const builtin = @import("builtin"); | ||
const windows = std.os.windows; | ||
const mem = std.mem; | ||
const testing = std.testing; | ||
const expect = testing.expect; | ||
|
||
fn testNtPath(input: []const u8, expected: []const u8) !void { | ||
const input_w = try std.unicode.utf8ToUtf16LeWithNull(testing.allocator, input); | ||
defer testing.allocator.free(input_w); | ||
const expected_w = try std.unicode.utf8ToUtf16LeWithNull(testing.allocator, expected); | ||
defer testing.allocator.free(expected_w); | ||
|
||
const nt_path = try windows.NtPath.init(input_w); | ||
defer nt_path.deinit(); | ||
const relative_path = try windows.toRelativeNtPath(nt_path.str); | ||
expect(mem.eql(u16, windows.unicodeSpan(relative_path), expected_w)); | ||
} | ||
|
||
test "NtPath" { | ||
try testNtPath("a", "a"); | ||
try testNtPath("a\\b", "a\\b"); | ||
try testNtPath("a\\.\\b", "a\\b"); | ||
try testNtPath("a\\..\\b", "b"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we delete this function now? The idea of this function is that it's slightly more efficient if you already have wide strings you want to pass, but if we have to do the conversion anyway, there's no reason for this function to exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we delete it, then applications that have a
u16
string will now need to do 2 conversions, one tou8
and then the second internal conversion.