authorgravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2021-12-12 18:43:31+01:00
committergravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2021-12-12 18:43:31+01:00
log5dd53c1986039ddf93d7eada8ceaa7ff2ad2b9bb
tree594d3adf342359dbb83548dc04ce4d28a31705b3
parent1fd0542bee467e3810b39c7d755a8152806c7818

os/linux/io_uring: implement symlinkat


1 files changed, 80 insertions(+), 0 deletions(-)

lib/std/os/linux/io_uring.zig+80
...@@ -794,6 +794,21 @@ pub const IO_Uring = struct {...@@ -794,6 +794,21 @@ pub const IO_Uring = struct {
794 return sqe;794 return sqe;
795 }795 }
796796
797 /// Queues (but does not submit) an SQE to perform a `symlinkat(2)`.
798 /// Returns a pointer to the SQE.
799 pub fn symlinkat(
800 self: *IO_Uring,
801 user_data: u64,
802 target: [*:0]const u8,
803 new_dir_fd: os.fd_t,
804 link_path: [*:0]const u8,
805 ) !*io_uring_sqe {
806 const sqe = try self.get_sqe();
807 io_uring_prep_symlinkat(sqe, target, new_dir_fd, link_path);
808 sqe.user_data = user_data;
809 return sqe;
810 }
811
797 /// Registers an array of file descriptors.812 /// Registers an array of file descriptors.
798 /// Every time a file descriptor is put in an SQE and submitted to the kernel, the kernel must813 /// Every time a file descriptor is put in an SQE and submitted to the kernel, the kernel must
799 /// retrieve a reference to the file, and once I/O has completed the file reference must be814 /// retrieve a reference to the file, and once I/O has completed the file reference must be
...@@ -1389,6 +1404,22 @@ pub fn io_uring_prep_mkdirat(...@@ -1389,6 +1404,22 @@ pub fn io_uring_prep_mkdirat(
1389 io_uring_prep_rw(.MKDIRAT, sqe, dir_fd, @ptrToInt(path), mode, 0);1404 io_uring_prep_rw(.MKDIRAT, sqe, dir_fd, @ptrToInt(path), mode, 0);
1390}1405}
13911406
1407pub fn io_uring_prep_symlinkat(
1408 sqe: *io_uring_sqe,
1409 target: [*:0]const u8,
1410 new_dir_fd: os.fd_t,
1411 link_path: [*:0]const u8,
1412) void {
1413 io_uring_prep_rw(
1414 .SYMLINKAT,
1415 sqe,
1416 new_dir_fd,
1417 @ptrToInt(target),
1418 0,
1419 @ptrToInt(link_path),
1420 );
1421}
1422
1392test "structs/offsets/entries" {1423test "structs/offsets/entries" {
1393 if (builtin.os.tag != .linux) return error.SkipZigTest;1424 if (builtin.os.tag != .linux) return error.SkipZigTest;
13941425
...@@ -2523,3 +2554,52 @@ test "mkdirat" {...@@ -2523,3 +2554,52 @@ test "mkdirat" {
2523 // Validate that the directory exist2554 // Validate that the directory exist
2524 _ = try std.fs.cwd().openDir(path, .{});2555 _ = try std.fs.cwd().openDir(path, .{});
2525}2556}
2557
2558test "symlinkat" {
2559 if (builtin.os.tag != .linux) return error.SkipZigTest;
2560
2561 var ring = IO_Uring.init(1, 0) catch |err| switch (err) {
2562 error.SystemOutdated => return error.SkipZigTest,
2563 error.PermissionDenied => return error.SkipZigTest,
2564 else => return err,
2565 };
2566 defer ring.deinit();
2567
2568 const path = "test_io_uring_symlinkat";
2569 const link_path = "test_io_uring_symlinkat_link";
2570
2571 const file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });
2572 defer {
2573 file.close();
2574 std.fs.cwd().deleteFile(path) catch {};
2575 std.fs.cwd().deleteFile(link_path) catch {};
2576 }
2577
2578 // Submit symlinkat
2579
2580 var sqe = try ring.symlinkat(
2581 0x12121212,
2582 path,
2583 linux.AT.FDCWD,
2584 link_path,
2585 );
2586 try testing.expectEqual(linux.IORING_OP.SYMLINKAT, sqe.opcode);
2587 try testing.expectEqual(@as(i32, linux.AT.FDCWD), sqe.fd);
2588 try testing.expectEqual(@as(u32, 1), try ring.submit());
2589
2590 const cqe = try ring.copy_cqe();
2591 switch (cqe.err()) {
2592 .SUCCESS => {},
2593 // This kernel's io_uring does not yet implement symlinkat (kernel version < 5.15)
2594 .INVAL => return error.SkipZigTest,
2595 else => |errno| std.debug.panic("unhandled errno: {}", .{errno}),
2596 }
2597 try testing.expectEqual(linux.io_uring_cqe{
2598 .user_data = 0x12121212,
2599 .res = 0,
2600 .flags = 0,
2601 }, cqe);
2602
2603 // Validate that the symlink exist
2604 _ = try std.fs.cwd().openFile(link_path, .{});
2605}