diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2020-07-15 17:55:02 +0200 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2020-07-22 08:51:22 +0200 |
| commit | cc9c5c5b0e7c37f96d7ea4c6bd22118ea72a0265 (patch) | |
| tree | 82947e5569da4fcbd0a63e81cc7160000659c1f0 /lib/std | |
| parent | a8a02dfbfaf4b9bd303712c853e202bd07837371 (diff) | |
| download | zig-cc9c5c5b0e7c37f96d7ea4c6bd22118ea72a0265.tar.gz zig-cc9c5c5b0e7c37f96d7ea4c6bd22118ea72a0265.zip | |
Handle relative/absolute symlinks; add more tests
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/os.zig | 5 | ||||
| -rw-r--r-- | lib/std/os/test.zig | 41 |
2 files changed, 33 insertions, 13 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig index 6e614b8006..5243c1155a 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -2437,7 +2437,10 @@ pub fn readlinkW(file_path: [*:0]const u16, out_buffer: []u8) ReadLinkError![]u8 fn parseReadlinkPath(path: []const u16, is_relative: bool, out_buffer: []u8) []u8 { const prefix = [_]u16{ '\\', '?', '?', '\\' }; - const start_index = if (mem.startsWith(u16, path, &prefix)) prefix.len else 0; + var start_index: usize = 0; + if (!is_relative and mem.startsWith(u16, path, &prefix)) { + start_index = prefix.len; + } const out_len = std.unicode.utf16leToUtf8(out_buffer, path[start_index..]) catch unreachable; return out_buffer[0..out_len]; } diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index 98a2cb7fae..056d06d975 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -70,29 +70,46 @@ test "readlink" { var arena = ArenaAllocator.init(testing.allocator); defer arena.deinit(); + const base_path = blk: { const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] }); break :blk try fs.realpathAlloc(&arena.allocator, relative_path); }; + const allocator = &arena.allocator; - try testReadlink(&arena.allocator, base_path, "file.txt", "symlink1", false); - try testReadlink(&arena.allocator, base_path, "subdir", "symlink2", true); -} + { + const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "file.txt" }); + const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "symlink1" }); + std.debug.warn("\ntarget_path={}\n", .{target_path}); + std.debug.warn("symlink_path={}\n", .{symlink_path}); -fn testReadlink(allocator: *mem.Allocator, base_path: []const u8, target_name: []const u8, symlink_name: []const u8, is_dir: bool) !void { - const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, target_name }); - const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, symlink_name }); + // Create symbolic link by path + try os.symlink(target_path, symlink_path, .{ .is_directory = false }); + try testReadlink(target_path, symlink_path); + } + { + const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "subdir" }); + const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "symlink2" }); std.debug.warn("\ntarget_path={}\n", .{target_path}); std.debug.warn("symlink_path={}\n", .{symlink_path}); // Create symbolic link by path - try os.symlink(target_path, symlink_path, .{ .is_directory = is_dir }); + try os.symlink(target_path, symlink_path, .{ .is_directory = true }); + try testReadlink(target_path, symlink_path); + } - // Read the link and verify - var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; - const given = try os.readlink(symlink_path, buffer[0..]); - std.debug.warn("given={}\n", .{given}); - expect(mem.eql(u8, target_path, given)); + if (builtin.os.tag == .windows) { + try testReadlink("C:\\ProgramData", "C:\\Users\\All Users"); + try testReadlink("C:\\Users\\Default", "C:\\Users\\Default User"); + try testReadlink("C:\\Users", "C:\\Documents and Settings"); + } +} + +fn testReadlink(target_path: []const u8, symlink_path: []const u8) !void { + var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; + const given = try os.readlink(symlink_path, buffer[0..]); + std.debug.warn("given={}\n", .{given}); + expect(mem.eql(u8, target_path, given)); } test "readlinkat" { |
