authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-15 00:24:16+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-22 08:51:22+02:00
log99e3e29e2e611cf86941921770644401a3cf74b7
tree33729c1d9afa019af7c14716c05ce3fa9ca9ae4d
parentc47cb8d09f7cf1c02d3af59ebbca665ce78c85ca

Refactor


4 files changed, 33 insertions(+), 31 deletions(-)

lib/std/os.zig+20-9
......@@ -1568,8 +1568,7 @@ pub const symlinkC = @compileError("deprecated: renamed to symlinkZ");
15681568/// like to create a symbolic link to a directory, use `std.os.windows.CreateSymbolicLinkW` directly
15691569/// specifying as flags `std.os.windows.CreateSymbolicLinkFlags.Directory`.
15701570pub fn symlinkW(target_path: [*:0]const u16, sym_link_path: [*:0]const u16) SymLinkError!void {
1571 const flags = windows.CreateSymbolicLinkFlags.File;
1572 return windows.CreateSymbolicLinkW(sym_link_path, target_path, flags);
1571 return windows.CreateSymbolicLinkW(sym_link_path, target_path, false);
15731572}
15741573
15751574/// This is the same as `symlink` except the parameters are null-terminated pointers.
......@@ -2395,20 +2394,20 @@ pub const readlinkC = @compileError("deprecated: renamed to readlinkZ");
23952394pub fn readlinkW(file_path: [*:0]const u16, out_buffer: []u8) ReadLinkError![]u8 {
23962395 const w = windows;
23972396
2398 const dir = if (std.fs.path.isAbsoluteWindowsW(file_path)) null else std.fs.cwd().fd;
2399 const handle = w.OpenAsReparsePoint(dir, file_path) catch |err| {
2397 const sharing = w.FILE_SHARE_DELETE | w.FILE_SHARE_READ | w.FILE_SHARE_WRITE;
2398 const disposition = w.OPEN_EXISTING;
2399 const flags = w.FILE_FLAG_BACKUP_SEMANTICS | w.FILE_FLAG_OPEN_REPARSE_POINT;
2400 const handle = w.CreateFileW(file_path, 0, sharing, null, disposition, flags, null) catch |err| {
24002401 switch (err) {
24012402 error.SharingViolation => return error.AccessDenied,
24022403 error.PipeBusy => unreachable,
24032404 error.PathAlreadyExists => unreachable,
2404 error.NoDevice => return error.FileNotFound,
24052405 else => |e| return e,
24062406 }
24072407 };
24082408
24092409 var reparse_buf: [w.MAXIMUM_REPARSE_DATA_BUFFER_SIZE]u8 = undefined;
24102410 _ = try w.DeviceIoControl(handle, w.FSCTL_GET_REPARSE_POINT, null, reparse_buf[0..], null);
2411 // std.debug.warn("\n\n{x}\n\n", .{reparse_buf});
24122411 const reparse_struct = @ptrCast(*const w.REPARSE_DATA_BUFFER, @alignCast(@alignOf(w.REPARSE_DATA_BUFFER), &reparse_buf[0]));
24132412 switch (reparse_struct.ReparseTag) {
24142413 w.IO_REPARSE_TAG_SYMLINK => {
......@@ -2434,9 +2433,9 @@ pub fn readlinkW(file_path: [*:0]const u16, out_buffer: []u8) ReadLinkError![]u8
24342433}
24352434
24362435fn parseReadlinkPath(path: []const u16, is_relative: bool, out_buffer: []u8) []u8 {
2437 const out_len = std.unicode.utf16leToUtf8(out_buffer, path) catch unreachable;
2438 std.debug.warn("got symlink => utf8={}\n", .{out_buffer[0..out_len]});
2439 // TODO handle absolute paths and namespace prefix '/??/'
2436 const prefix = [_]u16{ '\\', '?', '?', '\\' };
2437 const start_index = if (mem.startsWith(u16, path, &prefix)) prefix.len else 0;
2438 const out_len = std.unicode.utf16leToUtf8(out_buffer, path[start_index..]) catch unreachable;
24402439 return out_buffer[0..out_len];
24412440}
24422441
......@@ -2502,6 +2501,18 @@ pub fn readlinkatWasi(dirfd: fd_t, file_path: []const u8, out_buffer: []u8) Read
25022501/// Windows-only. Same as `readlinkat` except `file_path` is null-terminated, WTF16 encoded.
25032502/// See also `readlinkat`.
25042503pub fn readlinkatW(dirfd: fd_t, file_path: [*:0]const u16, out_buffer: []u8) ReadLinkError![]u8 {
2504 const w = windows;
2505
2506 const handle = w.OpenReparsePoint(dir, file_path) catch |err| {
2507 switch (err) {
2508 error.SharingViolation => return error.AccessDenied,
2509 error.PathAlreadyExists => unreachable,
2510 error.PipeBusy => unreachable,
2511 error.PathAlreadyExists => unreachable,
2512 error.NoDevice => return error.FileNotFound,
2513 else => |e| return e,
2514 }
2515 };
25052516 @compileError("TODO implement on Windows");
25062517}
25072518
lib/std/os/test.zig+1
......@@ -86,6 +86,7 @@ test "readlink" {
8686 // now, read the link and verify
8787 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
8888 const given = try os.readlink(symlink_path, buffer[0..]);
89 std.debug.warn("given={}\n", .{given});
8990 expect(mem.eql(u8, symlink_path, given));
9091 }
9192}
lib/std/os/windows.zig+11-20
......@@ -602,43 +602,34 @@ pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 {
602602 return buffer[0..end_index];
603603}
604604
605pub const CreateSymbolicLinkError = error{
606 AccessDenied,
607 PathAlreadyExists,
608 FileNotFound,
609 Unexpected
610};
611
612pub const CreateSymbolicLinkFlags = enum(DWORD) {
613 File = SYMBOLIC_LINK_FLAG_FILE,
614 Directory = SYMBOLIC_LINK_FLAG_DIRECTORY,
615};
605pub const CreateSymbolicLinkError = error{ AccessDenied, PathAlreadyExists, FileNotFound, Unexpected };
616606
617607pub fn CreateSymbolicLink(
618608 sym_link_path: []const u8,
619609 target_path: []const u8,
620 flags: CreateSymbolicLinkFlags,
610 is_directory: bool,
621611) CreateSymbolicLinkError!void {
622612 const sym_link_path_w = try sliceToPrefixedFileW(sym_link_path);
623613 const target_path_w = try sliceToPrefixedFileW(target_path);
624 return CreateSymbolicLinkW(sym_link_path_w.span().ptr, target_path_w.span().ptr, flags);
614 return CreateSymbolicLinkW(sym_link_path_w.span().ptr, target_path_w.span().ptr, is_directory);
625615}
626616
627617pub fn CreateSymbolicLinkW(
628618 sym_link_path: [*:0]const u16,
629619 target_path: [*:0]const u16,
630 flags: CreateSymbolicLinkFlags,
620 is_directory: bool,
631621) CreateSymbolicLinkError!void {
632622 // Previously, until Win 10 Creators Update, creating symbolic links required
633623 // SeCreateSymbolicLink privilege. Currently, this is no longer required if the
634624 // OS is in Developer Mode; however, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
635625 // must be added to the input flags.
636 if (kernel32.CreateSymbolicLinkW(sym_link_path, target_path, @enumToInt(flags) | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) == 0) {
626 const flags = if (is_directory) SYMBOLIC_LINK_FLAG_DIRECTORY else 0;
627 if (kernel32.CreateSymbolicLinkW(sym_link_path, target_path, flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) == 0) {
637628 switch (kernel32.GetLastError()) {
638629 .INVALID_PARAMETER => {
639630 // If we're on Windows pre Creators Update, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
640631 // flag is an invalid parameter, in which case repeat without the flag.
641 if (kernel32.CreateSymbolicLinkW(sym_link_path, target_path, @enumToInt(flags)) == 0) {
632 if (kernel32.CreateSymbolicLinkW(sym_link_path, target_path, flags) == 0) {
642633 switch (kernel32.GetLastError()) {
643634 .PRIVILEGE_NOT_HELD => return error.AccessDenied,
644635 .FILE_NOT_FOUND => return error.FileNotFound,
......@@ -1372,7 +1363,7 @@ pub fn unexpectedStatus(status: NTSTATUS) std.os.UnexpectedError {
13721363 return error.Unexpected;
13731364}
13741365
1375pub const OpenAsReparsePointError = error {
1366pub const OpenReparsePointError = error{
13761367 FileNotFound,
13771368 NoDevice,
13781369 SharingViolation,
......@@ -1384,10 +1375,10 @@ pub const OpenAsReparsePointError = error {
13841375};
13851376
13861377/// Open file as a reparse point
1387pub fn OpenAsReparsePoint(
1378pub fn OpenReparsePoint(
13881379 dir: ?HANDLE,
13891380 sub_path_w: [*:0]const u16,
1390) OpenAsReparsePointError!HANDLE {
1381) OpenReparsePointError!HANDLE {
13911382 const path_len_bytes = math.cast(u16, mem.lenZ(sub_path_w) * 2) catch |err| switch (err) {
13921383 error.Overflow => return error.NameTooLong,
13931384 };
......@@ -1440,4 +1431,4 @@ pub fn OpenAsReparsePoint(
14401431 .FILE_IS_A_DIRECTORY => unreachable,
14411432 else => return unexpectedStatus(rc),
14421433 }
1443}
\ No newline at end of file
1434}
lib/std/os/windows/bits.zig+1-2
......@@ -1568,8 +1568,7 @@ pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: ULONG = 16 * 1024;
15681568pub const FSCTL_GET_REPARSE_POINT: DWORD = 0x900a8;
15691569pub const IO_REPARSE_TAG_SYMLINK: ULONG = 0xa000000c;
15701570pub const IO_REPARSE_TAG_MOUNT_POINT: ULONG = 0xa0000003;
1571pub const SYMLINK_FLAG_RELATIVE: ULONG = 0x00000001;
1571pub const SYMLINK_FLAG_RELATIVE: ULONG = 0x1;
15721572
1573pub const SYMBOLIC_LINK_FLAG_FILE: DWORD = 0x0;
15741573pub const SYMBOLIC_LINK_FLAG_DIRECTORY: DWORD = 0x1;
15751574pub const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: DWORD = 0x2;