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!
15541554 if (builtin.os.tag == .windows) {
15551555 const target_path_w = try windows.sliceToPrefixedFileW(target_path);
15561556 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);
15581558 }
15591559 const target_path_c = try toPosixPath(target_path);
15601560 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
15781578 if (builtin.os.tag == .windows) {
15791579 const target_path_w = try windows.cStrToPrefixedFileW(target_path);
15801580 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);
15821582 }
15831583 switch (errno(system.symlink(target_path, sym_link_path))) {
15841584 0 => return,
......@@ -2394,8 +2394,9 @@ pub const readlinkC = @compileError("deprecated: renamed to readlinkZ");
23942394/// See also `readlinkZ`.
23952395pub fn readlinkW(file_path: []const u16, out_buffer: []u8) ReadLinkError![]u8 {
23962396 const handle = windows.OpenFile(file_path, .{
2397 .access_mask = 0,
2398 .creation = windows.FILE_OPEN_REPARSE_POINT | windows.FILE_LIST_DIRECTORY,
2397 .access_mask = windows.GENERIC_READ,
2398 .creation = windows.FILE_OPEN,
2399 .options = windows.FILE_OPEN_REPARSE_POINT,
23992400 .io_mode = std.io.default_mode,
24002401 }) catch |err| {
24012402 switch (err) {
lib/std/os/test.zig+4-3
......@@ -45,7 +45,8 @@ test "readlink" {
4545 if (builtin.os.tag == .wasi) return error.SkipZigTest;
4646
4747 var tmp = tmpDir(.{});
48 defer tmp.cleanup();
48 //defer tmp.cleanup();
49 std.debug.print("tmp = {}\n", .{tmp.sub_path[0..]});
4950
5051 // create file
5152 try tmp.dir.writeFile("file.txt", "nonsense");
......@@ -59,8 +60,8 @@ test "readlink" {
5960 const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..]});
6061 break :blk try fs.realpathAlloc(&arena.allocator, relative_path);
6162 };
62 const target_path = try fs.path.join(&arena.allocator, &[_][]const u8{"file.txt"});
63 const symlink_path = try fs.path.join(&arena.allocator, &[_][]const u8{"symlinked"});
63 const target_path = try fs.path.join(&arena.allocator, &[_][]const u8{base_path, "file.txt"});
64 const symlink_path = try fs.path.join(&arena.allocator, &[_][]const u8{base_path, "symlinked"});
6465
6566 // create symbolic link by path
6667 try os.symlink(target_path, symlink_path);
lib/std/os/windows.zig+20-3
......@@ -110,6 +110,7 @@ pub const OpenFileOptions = struct {
110110 share_access: ULONG = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
111111 share_access_nonblocking: bool = false,
112112 creation: ULONG,
113 options: ?ULONG = null,
113114 io_mode: std.io.ModeOverride,
114115};
115116
......@@ -145,7 +146,15 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN
145146
146147 var delay: usize = 1;
147148 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;
149158 const rc = ntdll.NtCreateFile(
150159 &result,
151160 options.access_mask,
......@@ -155,7 +164,8 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN
155164 FILE_ATTRIBUTE_NORMAL,
156165 options.share_access,
157166 options.creation,
158 FILE_NON_DIRECTORY_FILE | blocking_flag,
167 // flags | blocking_flag,
168 flags,
159169 null,
160170 0,
161171 );
......@@ -601,7 +611,12 @@ pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 {
601611 return buffer[0..end_index];
602612}
603613
604pub const CreateSymbolicLinkError = error{AccessDenied, FileNotFound, Unexpected};
614pub const CreateSymbolicLinkError = error{
615 AccessDenied,
616 PathAlreadyExists,
617 FileNotFound,
618 Unexpected
619};
605620
606621pub const CreateSymbolicLinkFlags = enum(DWORD) {
607622 File = SYMBOLIC_LINK_FLAG_FILE,
......@@ -638,6 +653,7 @@ pub fn CreateSymbolicLinkW(
638653 .FILE_NOT_FOUND => return error.FileNotFound,
639654 .PATH_NOT_FOUND => return error.FileNotFound,
640655 .ACCESS_DENIED => return error.AccessDenied,
656 .ALREADY_EXISTS => return error.PathAlreadyExists,
641657 else => |err| return unexpectedError(err),
642658 }
643659 }
......@@ -647,6 +663,7 @@ pub fn CreateSymbolicLinkW(
647663 .FILE_NOT_FOUND => return error.FileNotFound,
648664 .PATH_NOT_FOUND => return error.FileNotFound,
649665 .ACCESS_DENIED => return error.AccessDenied,
666 .ALREADY_EXISTS => return error.PathAlreadyExists,
650667 else => |err| return unexpectedError(err),
651668 }
652669 }