authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-15 09:06:01+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-22 08:51:22+02:00
log30f1176a545111e8bdce3362d6bdbf7ef75dce2a
tree1a069f08114d0756dab54ea450328a799df8f961
parent4894de2b326363c9a157411bf555d73b661c74c4

Add SymlinkFlags needed to create symlinks to dirs on Win


3 files changed, 19 insertions(+), 16 deletions(-)

lib/std/fs.zig+2-2
......@@ -69,7 +69,7 @@ pub const need_async_thread = std.io.is_async and switch (builtin.os.tag) {
6969
7070/// TODO remove the allocator requirement from this API
7171pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) !void {
72 if (symLink(existing_path, new_path)) {
72 if (symLink(existing_path, new_path, .{})) {
7373 return;
7474 } else |err| switch (err) {
7575 error.PathAlreadyExists => {},
......@@ -87,7 +87,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:
8787 try crypto.randomBytes(rand_buf[0..]);
8888 base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf);
8989
90 if (symLink(existing_path, tmp_path)) {
90 if (symLink(existing_path, tmp_path, .{})) {
9191 return rename(tmp_path, new_path);
9292 } else |err| switch (err) {
9393 error.PathAlreadyExists => continue,
lib/std/os.zig+15-12
......@@ -1520,6 +1520,14 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
15201520 }
15211521}
15221522
1523/// Use with `symlink` to specify whether the symlink will point to a file
1524/// or a directory. This value is ignored on all hosts except Windows where
1525/// creating symlinks to different resource types, requires different flags.
1526/// By default, symlink is assumed to point to a file.
1527pub const SymlinkFlags = struct{
1528 is_directory: bool = false,
1529};
1530
15231531pub const SymLinkError = error{
15241532 /// In WASI, this error may occur when the file descriptor does
15251533 /// not hold the required rights to create a new symbolic link relative to it.
......@@ -1541,39 +1549,34 @@ pub const SymLinkError = error{
15411549/// Creates a symbolic link named `sym_link_path` which contains the string `target_path`.
15421550/// A symbolic link (also known as a soft link) may point to an existing file or to a nonexistent
15431551/// one; the latter case is known as a dangling link.
1544/// On Windows, it is only legal to create a symbolic link to an existing resource. Furthermore,
1545/// this function will by default try creating a symbolic link to a file. If you would like to
1546/// create a symbolic link to a directory instead, see `symlinkW` for more information how to
1547/// do that.
15481552/// If `sym_link_path` exists, it will not be overwritten.
15491553/// See also `symlinkC` and `symlinkW`.
1550pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {
1554pub fn symlink(target_path: []const u8, sym_link_path: []const u8, flags: SymlinkFlags) SymLinkError!void {
15511555 if (builtin.os.tag == .wasi) {
15521556 @compileError("symlink is not supported in WASI; use symlinkat instead");
15531557 }
15541558 if (builtin.os.tag == .windows) {
15551559 const target_path_w = try windows.sliceToPrefixedFileW(target_path);
15561560 const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path);
1557 return symlinkW(target_path_w.span().ptr, sym_link_path_w.span().ptr);
1561 return symlinkW(target_path_w.span().ptr, sym_link_path_w.span().ptr, flags);
15581562 }
15591563 const target_path_c = try toPosixPath(target_path);
15601564 const sym_link_path_c = try toPosixPath(sym_link_path);
1561 return symlinkZ(&target_path_c, &sym_link_path_c);
1565 return symlinkZ(&target_path_c, &sym_link_path_c, flags);
15621566}
15631567
15641568pub const symlinkC = @compileError("deprecated: renamed to symlinkZ");
15651569
15661570/// Windows-only. Same as `symlink` except the parameters are null-terminated, WTF16 encoded.
15671571/// Note that this function will by default try creating a symbolic link to a file. If you would
1568/// like to create a symbolic link to a directory, use `std.os.windows.CreateSymbolicLinkW` directly
1569/// specifying as flags `std.os.windows.CreateSymbolicLinkFlags.Directory`.
1570pub fn symlinkW(target_path: [*:0]const u16, sym_link_path: [*:0]const u16) SymLinkError!void {
1571 return windows.CreateSymbolicLinkW(sym_link_path, target_path, false);
1572/// like to create a symbolic link to a directory, specify this with `SymlinkFlags{ .is_directory = true }`.
1573pub fn symlinkW(target_path: [*:0]const u16, sym_link_path: [*:0]const u16, flags: SymlinkFlags) SymLinkError!void {
1574 return windows.CreateSymbolicLinkW(sym_link_path, target_path, flags.is_directory);
15721575}
15731576
15741577/// This is the same as `symlink` except the parameters are null-terminated pointers.
15751578/// See also `symlink`.
1576pub fn symlinkZ(target_path: [*:0]const u8, sym_link_path: [*:0]const u8) SymLinkError!void {
1579pub fn symlinkZ(target_path: [*:0]const u8, sym_link_path: [*:0]const u8, flags: SymlinkFlags) SymLinkError!void {
15771580 if (builtin.os.tag == .windows) {
15781581 const target_path_w = try windows.cStrToPrefixedFileW(target_path);
15791582 const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path);
lib/std/os/test.zig+2-2
......@@ -48,7 +48,7 @@ test "readlink" {
4848 {
4949 var cwd = fs.cwd();
5050 try cwd.writeFile("file.txt", "nonsense");
51 try os.symlink("file.txt", "symlinked");
51 try os.symlink("file.txt", "symlinked", .{});
5252
5353 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
5454 const given = try os.readlink("symlinked", buffer[0..]);
......@@ -81,7 +81,7 @@ test "readlink" {
8181 std.debug.warn("symlink_path={}\n", .{symlink_path});
8282
8383 // create symbolic link by path
84 try os.symlink(target_path, symlink_path);
84 try os.symlink(target_path, symlink_path, .{});
8585
8686 // now, read the link and verify
8787 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;