authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-13 08:01:01+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-22 08:51:22+02:00
log515c663cd6e31d5f8ae07e804f2b32b99d025895
tree5950337a1fe1a4362594b73e9879369480e0fc42
parentcc83d92b0b12f5b3fa6462ed878cd7bec7412940

Add readlink smoke test


3 files changed, 49 insertions(+), 12 deletions(-)

lib/std/os.zig+17-11
......@@ -2355,6 +2355,8 @@ pub const ReadLinkError = error{
23552355 FileNotFound,
23562356 SystemResources,
23572357 NotDir,
2358 InvalidUtf8,
2359 BadPathName,
23582360 /// Windows-only.
23592361 UnsupportedSymlinkType,
23602362} || UnexpectedError;
......@@ -2366,7 +2368,7 @@ pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
23662368 @compileError("readlink is not supported in WASI; use readlinkat instead");
23672369 } else if (builtin.os.tag == .windows) {
23682370 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
2369 return readlinkW(file_path_w.span().ptr, out_buffer);
2371 return readlinkW(file_path_w.span(), out_buffer);
23702372 } else {
23712373 const file_path_c = try toPosixPath(file_path);
23722374 return readlinkZ(&file_path_c, out_buffer);
......@@ -2377,11 +2379,11 @@ pub const readlinkC = @compileError("deprecated: renamed to readlinkZ");
23772379
23782380/// Windows-only. Same as `readlink` except `file_path` is null-terminated, WTF16 encoded.
23792381/// See also `readlinkZ`.
2380pub fn readlinkW(file_path: [*:0]const u16, out_buffer: []u8) ReadLinkError![]u8 {
2382pub fn readlinkW(file_path: []const u16, out_buffer: []u8) ReadLinkError![]u8 {
23812383 const handle = windows.OpenFile(file_path, .{
23822384 .access_mask = 0,
2383 .creation = c.FILE_OPEN_REPARSE_POINT | c.FILE_LIST_DIRECTORY,
2384 .io_mode = 0,
2385 .creation = windows.FILE_OPEN_REPARSE_POINT | windows.FILE_LIST_DIRECTORY,
2386 .io_mode = std.io.default_mode,
23852387 }) catch |err| {
23862388 switch (err) {
23872389 error.IsDir => unreachable,
......@@ -2390,28 +2392,32 @@ pub fn readlinkW(file_path: [*:0]const u16, out_buffer: []u8) ReadLinkError![]u8
23902392 error.PipeBusy => unreachable,
23912393 error.PathAlreadyExists => unreachable,
23922394 error.WouldBlock => unreachable,
2393 else => return err,
2395 else => |e| return e,
23942396 }
23952397 };
23962398 var reparse_buf: [windows.MAXIMUM_REPARSE_DATA_BUFFER_SIZE]u8 = undefined;
23972399 _ = try windows.DeviceIoControl(handle, windows.FSCTL_GET_REPARSE_POINT, null, reparse_buf[0..], null);
2398 const reparse_struct = @bitCast(windows._REPARSE_DATA_BUFFER, reparse_buf[0..@sizeOf(windows._REPARSE_DATA_BUFFER)]);
2400 const reparse_struct = mem.bytesAsValue(windows._REPARSE_DATA_BUFFER, reparse_buf[0..@sizeOf(windows._REPARSE_DATA_BUFFER)]);
23992401 switch (reparse_struct.ReparseTag) {
2400 windows.IO_REPARSE_TAG_SYMLINK => {},
2401 windows.IO_REPARSE_TAG_MOUNT_POINT => {},
2402 windows.IO_REPARSE_TAG_SYMLINK => {
2403 std.debug.warn("got symlink!", .{});
2404 },
2405 windows.IO_REPARSE_TAG_MOUNT_POINT => {
2406 std.debug.warn("got mount point!", .{});
2407 },
24022408 else => |value| {
2403 std.debug.print("unsupported symlink type: {}", value);
2409 std.debug.warn("unsupported symlink type: {}", .{value});
24042410 return error.UnsupportedSymlinkType;
24052411 },
24062412 }
2407 @compileError("TODO implement readlink for Windows");
2413 @panic("Oh no!");
24082414}
24092415
24102416/// Same as `readlink` except `file_path` is null-terminated.
24112417pub fn readlinkZ(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8 {
24122418 if (builtin.os.tag == .windows) {
24132419 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
2414 return readlinkW(file_path_w.span().ptr, out_buffer);
2420 return readlinkW(file_path_w.span(), out_buffer);
24152421 }
24162422 const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len);
24172423 switch (errno(rc)) {
lib/std/os/test.zig+31
......@@ -17,6 +17,7 @@ const AtomicRmwOp = builtin.AtomicRmwOp;
1717const AtomicOrder = builtin.AtomicOrder;
1818const tmpDir = std.testing.tmpDir;
1919const Dir = std.fs.Dir;
20const ArenaAllocator = std.heap.ArenaAllocator;
2021
2122test "fstatat" {
2223 // enable when `fstat` and `fstatat` are implemented on Windows
......@@ -40,6 +41,36 @@ test "fstatat" {
4041 expectEqual(stat, statat);
4142}
4243
44test "readlink" {
45 if (builtin.os.tag == .wasi) return error.SkipZigTest;
46
47 var tmp = tmpDir(.{});
48 defer tmp.cleanup();
49
50 // create file
51 try tmp.dir.writeFile("file.txt", "nonsense");
52
53 // get paths
54 // TODO: use Dir's realpath function once that exists
55 var arena = ArenaAllocator.init(testing.allocator);
56 defer arena.deinit();
57
58 const base_path = blk: {
59 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 };
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"});
64
65 // create symbolic link by path
66 try os.symlink(target_path, symlink_path);
67
68 // now, read the link and verify
69 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
70 const given = try os.readlink(symlink_path, buffer[0..]);
71 expect(mem.eql(u8, symlink_path, given));
72}
73
4374test "readlinkat" {
4475 // enable when `readlinkat` and `symlinkat` are implemented on Windows
4576 if (builtin.os.tag == .windows) return error.SkipZigTest;
lib/std/os/windows/bits.zig+1-1
......@@ -488,7 +488,7 @@ pub const FILE_OPEN_BY_FILE_ID = 0x00002000;
488488pub const FILE_OPEN_FOR_BACKUP_INTENT = 0x00004000;
489489pub const FILE_NO_COMPRESSION = 0x00008000;
490490pub const FILE_RESERVE_OPFILTER = 0x00100000;
491pub const FILE_TRANSACTED_MODE = 0x00200000;
491pub const FILE_OPEN_REPARSE_POINT = 0x00200000;
492492pub const FILE_OPEN_OFFLINE_FILE = 0x00400000;
493493pub const FILE_OPEN_FOR_FREE_SPACE_QUERY = 0x00800000;
494494