authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-13 17:51:14+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-22 08:51:22+02:00
log92d11fd4e95b86d6ace166783cda69030647e688
tree5b1d297421025740852a5e9775bae4e031c2681c
parent791795a63a34bc69af59717d5eff5d4c76349756

Debug readlinkW using OpenFile


3 files changed, 29 insertions(+), 10 deletions(-)

lib/std/os.zig+5-4
...@@ -1554,7 +1554,7 @@ pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!...@@ -1554,7 +1554,7 @@ pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!
1554 if (builtin.os.tag == .windows) {1554 if (builtin.os.tag == .windows) {
1555 const target_path_w = try windows.sliceToPrefixedFileW(target_path);1555 const target_path_w = try windows.sliceToPrefixedFileW(target_path);
1556 const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path);1556 const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path);
1557 return symlinkW(sym_link_path_w.span().ptr, target_path_w.span().ptr);1557 return symlinkW(target_path_w.span().ptr, sym_link_path_w.span().ptr);
1558 }1558 }
1559 const target_path_c = try toPosixPath(target_path);1559 const target_path_c = try toPosixPath(target_path);
1560 const sym_link_path_c = try toPosixPath(sym_link_path);1560 const sym_link_path_c = try toPosixPath(sym_link_path);
...@@ -1578,7 +1578,7 @@ pub fn symlinkZ(target_path: [*:0]const u8, sym_link_path: [*:0]const u8) SymLin...@@ -1578,7 +1578,7 @@ pub fn symlinkZ(target_path: [*:0]const u8, sym_link_path: [*:0]const u8) SymLin
1578 if (builtin.os.tag == .windows) {1578 if (builtin.os.tag == .windows) {
1579 const target_path_w = try windows.cStrToPrefixedFileW(target_path);1579 const target_path_w = try windows.cStrToPrefixedFileW(target_path);
1580 const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path);1580 const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path);
1581 return windows.CreateSymbolicLinkW(sym_link_path_w.span().ptr, target_path_w.span().ptr, 0);1581 return symlinkW(target_path_w.span().ptr, sym_link_path_w.span().ptr);
1582 }1582 }
1583 switch (errno(system.symlink(target_path, sym_link_path))) {1583 switch (errno(system.symlink(target_path, sym_link_path))) {
1584 0 => return,1584 0 => return,
...@@ -2394,8 +2394,9 @@ pub const readlinkC = @compileError("deprecated: renamed to readlinkZ");...@@ -2394,8 +2394,9 @@ pub const readlinkC = @compileError("deprecated: renamed to readlinkZ");
2394/// See also `readlinkZ`.2394/// See also `readlinkZ`.
2395pub fn readlinkW(file_path: []const u16, out_buffer: []u8) ReadLinkError![]u8 {2395pub fn readlinkW(file_path: []const u16, out_buffer: []u8) ReadLinkError![]u8 {
2396 const handle = windows.OpenFile(file_path, .{2396 const handle = windows.OpenFile(file_path, .{
2397 .access_mask = 0,2397 .access_mask = windows.GENERIC_READ,
2398 .creation = windows.FILE_OPEN_REPARSE_POINT | windows.FILE_LIST_DIRECTORY,2398 .creation = windows.FILE_OPEN,
2399 .options = windows.FILE_OPEN_REPARSE_POINT,
2399 .io_mode = std.io.default_mode,2400 .io_mode = std.io.default_mode,
2400 }) catch |err| {2401 }) catch |err| {
2401 switch (err) {2402 switch (err) {
lib/std/os/test.zig+4-3
...@@ -45,7 +45,8 @@ test "readlink" {...@@ -45,7 +45,8 @@ test "readlink" {
45 if (builtin.os.tag == .wasi) return error.SkipZigTest;45 if (builtin.os.tag == .wasi) return error.SkipZigTest;
4646
47 var tmp = tmpDir(.{});47 var tmp = tmpDir(.{});
48 defer tmp.cleanup();48 //defer tmp.cleanup();
49 std.debug.print("tmp = {}\n", .{tmp.sub_path[0..]});
4950
50 // create file51 // create file
51 try tmp.dir.writeFile("file.txt", "nonsense");52 try tmp.dir.writeFile("file.txt", "nonsense");
...@@ -59,8 +60,8 @@ test "readlink" {...@@ -59,8 +60,8 @@ test "readlink" {
59 const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..]});60 const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..]});
60 break :blk try fs.realpathAlloc(&arena.allocator, relative_path);61 break :blk try fs.realpathAlloc(&arena.allocator, relative_path);
61 };62 };
62 const target_path = try fs.path.join(&arena.allocator, &[_][]const u8{"file.txt"});63 const target_path = try fs.path.join(&arena.allocator, &[_][]const u8{base_path, "file.txt"});
63 const symlink_path = try fs.path.join(&arena.allocator, &[_][]const u8{"symlinked"});64 const symlink_path = try fs.path.join(&arena.allocator, &[_][]const u8{base_path, "symlinked"});
6465
65 // create symbolic link by path66 // create symbolic link by path
66 try os.symlink(target_path, symlink_path);67 try os.symlink(target_path, symlink_path);
lib/std/os/windows.zig+20-3
...@@ -110,6 +110,7 @@ pub const OpenFileOptions = struct {...@@ -110,6 +110,7 @@ pub const OpenFileOptions = struct {
110 share_access: ULONG = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,110 share_access: ULONG = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
111 share_access_nonblocking: bool = false,111 share_access_nonblocking: bool = false,
112 creation: ULONG,112 creation: ULONG,
113 options: ?ULONG = null,
113 io_mode: std.io.ModeOverride,114 io_mode: std.io.ModeOverride,
114};115};
115116
...@@ -145,7 +146,15 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN...@@ -145,7 +146,15 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN
145146
146 var delay: usize = 1;147 var delay: usize = 1;
147 while (true) {148 while (true) {
148 const blocking_flag: ULONG = if (options.io_mode == .blocking) FILE_SYNCHRONOUS_IO_NONALERT else 0;149 var flags: ULONG = undefined;
150 if (options.options) |opt| {
151 flags = opt;
152 } else {
153 const blocking_flag: ULONG = if (options.io_mode == .blocking) FILE_SYNCHRONOUS_IO_NONALERT else 0;
154 flags = FILE_NON_DIRECTORY_FILE | blocking_flag;
155 }
156 // const blocking_flag: ULONG = if (options.io_mode == .blocking) FILE_SYNCHRONOUS_IO_NONALERT else 0;
157 // const flags = if (options.options) |opt| opt else FILE_NON_DIRECTORY_FILE;
149 const rc = ntdll.NtCreateFile(158 const rc = ntdll.NtCreateFile(
150 &result,159 &result,
151 options.access_mask,160 options.access_mask,
...@@ -155,7 +164,8 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN...@@ -155,7 +164,8 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN
155 FILE_ATTRIBUTE_NORMAL,164 FILE_ATTRIBUTE_NORMAL,
156 options.share_access,165 options.share_access,
157 options.creation,166 options.creation,
158 FILE_NON_DIRECTORY_FILE | blocking_flag,167 // flags | blocking_flag,
168 flags,
159 null,169 null,
160 0,170 0,
161 );171 );
...@@ -601,7 +611,12 @@ pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 {...@@ -601,7 +611,12 @@ pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 {
601 return buffer[0..end_index];611 return buffer[0..end_index];
602}612}
603613
604pub const CreateSymbolicLinkError = error{AccessDenied, FileNotFound, Unexpected};614pub const CreateSymbolicLinkError = error{
615 AccessDenied,
616 PathAlreadyExists,
617 FileNotFound,
618 Unexpected
619};
605620
606pub const CreateSymbolicLinkFlags = enum(DWORD) {621pub const CreateSymbolicLinkFlags = enum(DWORD) {
607 File = SYMBOLIC_LINK_FLAG_FILE,622 File = SYMBOLIC_LINK_FLAG_FILE,
...@@ -638,6 +653,7 @@ pub fn CreateSymbolicLinkW(...@@ -638,6 +653,7 @@ pub fn CreateSymbolicLinkW(
638 .FILE_NOT_FOUND => return error.FileNotFound,653 .FILE_NOT_FOUND => return error.FileNotFound,
639 .PATH_NOT_FOUND => return error.FileNotFound,654 .PATH_NOT_FOUND => return error.FileNotFound,
640 .ACCESS_DENIED => return error.AccessDenied,655 .ACCESS_DENIED => return error.AccessDenied,
656 .ALREADY_EXISTS => return error.PathAlreadyExists,
641 else => |err| return unexpectedError(err),657 else => |err| return unexpectedError(err),
642 }658 }
643 }659 }
...@@ -647,6 +663,7 @@ pub fn CreateSymbolicLinkW(...@@ -647,6 +663,7 @@ pub fn CreateSymbolicLinkW(
647 .FILE_NOT_FOUND => return error.FileNotFound,663 .FILE_NOT_FOUND => return error.FileNotFound,
648 .PATH_NOT_FOUND => return error.FileNotFound,664 .PATH_NOT_FOUND => return error.FileNotFound,
649 .ACCESS_DENIED => return error.AccessDenied,665 .ACCESS_DENIED => return error.AccessDenied,
666 .ALREADY_EXISTS => return error.PathAlreadyExists,
650 else => |err| return unexpectedError(err),667 else => |err| return unexpectedError(err),
651 }668 }
652 }669 }