This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/fix-zig-ffi-safety in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 19ee8f24f16dfac899863ca1d3c619bd6139ebe0 Author: Xuanwo <[email protected]> AuthorDate: Tue Nov 24 09:45:29 2026 +0100 fix(bindings/zig): fix FFI memory safety, C signatures, and error handling --- bindings/zig/README.md | 3 +- bindings/zig/examples/getting_started.zig | 5 +- bindings/zig/src/opendal.zig | 389 +++++++++++++++++++++--------- 3 files changed, 284 insertions(+), 113 deletions(-) diff --git a/bindings/zig/README.md b/bindings/zig/README.md index 9b7e3a037d..209fe08f53 100644 --- a/bindings/zig/README.md +++ b/bindings/zig/README.md @@ -43,7 +43,8 @@ var op = try opendal.Operator.init("memory", null); defer op.deinit(); try op.write("/hello.txt", "Hello, World!"); -const data = try op.read("/hello.txt"); +var data = try op.read("/hello.txt"); +defer data.deinit(); ``` See the [User Guide](https://opendal.apache.org/docs/bindings/zig) for more. diff --git a/bindings/zig/examples/getting_started.zig b/bindings/zig/examples/getting_started.zig index 9b7f9af7d5..4d5b7018a2 100644 --- a/bindings/zig/examples/getting_started.zig +++ b/bindings/zig/examples/getting_started.zig @@ -32,8 +32,9 @@ pub fn main() !void { try op.write("/hello.txt", "Hello, World!"); // Read them back. - const data = try op.read("/hello.txt"); - std.debug.print("read: {s}\n", .{data}); + var data = try op.read("/hello.txt"); + defer data.deinit(); + std.debug.print("read: {s}\n", .{data.slice()}); // Inspect metadata. var meta = try op.stat("/hello.txt"); diff --git a/bindings/zig/src/opendal.zig b/bindings/zig/src/opendal.zig index d0345e39f0..6829c7c6c6 100644 --- a/bindings/zig/src/opendal.zig +++ b/bindings/zig/src/opendal.zig @@ -15,109 +15,159 @@ // specific language governing permissions and limitations // under the License. +const std = @import("std"); pub const c = @import("opendal_c_header"); +pub const OwnedString = struct { + ptr: [*c]u8, + + pub fn slice(self: OwnedString) []const u8 { + if (self.ptr == null) { + return ""; + } + return std.mem.span(self.ptr); + } + + pub fn deinit(self: *OwnedString) void { + if (self.ptr != null) { + c.opendal_string_free(self.ptr); + self.ptr = null; + } + } +}; + +pub const Bytes = struct { + inner: c.opendal_bytes, + + pub fn slice(self: Bytes) []const u8 { + if (self.inner.len == 0 or self.inner.data == null) { + return &.{}; + } + return self.inner.data[0..self.inner.len]; + } + + pub fn deinit(self: *Bytes) void { + c.opendal_bytes_free(&self.inner); + self.inner = .{ + .data = null, + .len = 0, + .capacity = 0, + }; + } +}; + pub const Operator = struct { inner: *c.opendal_operator, - pub fn init(scheme: []const u8, options: ?*c.opendal_operator_options) !Operator { + pub fn init(scheme: [:0]const u8, options: ?*c.opendal_operator_options) !Operator { var op: ?*c.opendal_operator = null; if (c.opendal_operator_new(scheme.ptr, options, @ptrCast(&op))) |err| { - c.opendal_error_free(err); - return error.OperatorInitFailed; + defer c.opendal_error_free(err); + return codeToError(err.*.code); } - if (op == null) { - return error.OperatorInitFailed; + if (op) |inner| { + return .{ .inner = inner }; } - return .{ - .inner = op.?, - }; + return error.OperatorInitFailed; } pub fn deinit(self: *Operator) void { c.opendal_operator_free(self.inner); } - pub fn write(self: *const Operator, path: []const u8, data: []const u8) !void { - const bytes = c.opendal_bytes{ - .data = @constCast(data.ptr), - .len = data.len, - .capacity = data.len, + pub fn write(self: *const Operator, path: [:0]const u8, data: []const u8) !void { + var bytes = c.opendal_bytes{ + .data = null, + .len = 0, + .capacity = 0, }; + if (data.len > 0) { + bytes.data = @constCast(data.ptr); + bytes.len = data.len; + bytes.capacity = data.len; + } if (c.opendal_operator_write(self.inner, path.ptr, &bytes)) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } } - pub fn read(self: *const Operator, path: []const u8) ![]const u8 { - var data: c.opendal_bytes = undefined; + pub fn read(self: *const Operator, path: [:0]const u8) !Bytes { + var data = c.opendal_bytes{ + .data = null, + .len = 0, + .capacity = 0, + }; if (c.opendal_operator_read(self.inner, path.ptr, &data)) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } - return data.data[0..data.len]; + return .{ .inner = data }; } - pub fn delete(self: *const Operator, path: []const u8) !void { + pub fn delete(self: *const Operator, path: [:0]const u8) !void { if (c.opendal_operator_delete(self.inner, path.ptr)) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } } - pub fn stat(self: *const Operator, path: []const u8) !Metadata { + pub fn stat(self: *const Operator, path: [:0]const u8) !Metadata { var meta: ?*c.opendal_metadata = null; if (c.opendal_operator_stat(self.inner, path.ptr, @ptrCast(&meta))) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } - return .{ .inner = meta.? }; + const m = meta orelse return error.Unexpected; + return .{ .inner = m }; } - pub fn exists(self: *const Operator, path: []const u8) !bool { + pub fn exists(self: *const Operator, path: [:0]const u8) !bool { var is_exist: bool = false; if (c.opendal_operator_is_exist(self.inner, path.ptr, &is_exist)) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } return is_exist; } - pub fn list(self: *const Operator, path: []const u8) !Lister { + pub fn list(self: *const Operator, path: [:0]const u8) !Lister { var lister: ?*c.opendal_lister = null; if (c.opendal_operator_list(self.inner, path.ptr, @ptrCast(&lister))) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } - return .{ .inner = lister.? }; + const l = lister orelse return error.Unexpected; + return .{ .inner = l }; } - pub fn createDir(self: *const Operator, path: []const u8) !void { + pub fn createDir(self: *const Operator, path: [:0]const u8) !void { if (c.opendal_operator_create_dir(self.inner, path.ptr)) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } } - pub fn rename(self: *const Operator, src: []const u8, dest: []const u8) !void { + pub fn rename(self: *const Operator, src: [:0]const u8, dest: [:0]const u8) !void { if (c.opendal_operator_rename(self.inner, src.ptr, dest.ptr)) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } } - pub fn copy(self: *const Operator, src: []const u8, dest: []const u8) !void { + pub fn copy(self: *const Operator, src: [:0]const u8, dest: [:0]const u8) !void { if (c.opendal_operator_copy(self.inner, src.ptr, dest.ptr)) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } } pub fn info(self: *const Operator) !OperatorInfo { - const info_new = c.opendal_operator_info_new(self.inner); - if (info_new == null) return error.InfoFailed; - return .{ .inner = info.? }; + const info_new: ?*c.opendal_operator_info = c.opendal_operator_info_new(self.inner); + if (info_new) |inner| { + return .{ .inner = inner }; + } + return error.InfoFailed; } }; @@ -128,22 +178,16 @@ pub const OperatorInfo = struct { c.opendal_operator_info_free(self.inner); } - pub fn scheme(self: *const OperatorInfo) []const u8 { - const ptr = c.opendal_operator_info_get_scheme(self.inner); - defer std.c.free(ptr); - return std.mem.span(ptr); + pub fn scheme(self: *const OperatorInfo) OwnedString { + return .{ .ptr = c.opendal_operator_info_get_scheme(self.inner) }; } - pub fn root(self: *const OperatorInfo) []const u8 { - const ptr = c.opendal_operator_info_get_root(self.inner); - defer std.c.free(ptr); - return std.mem.span(ptr); + pub fn root(self: *const OperatorInfo) OwnedString { + return .{ .ptr = c.opendal_operator_info_get_root(self.inner) }; } - pub fn name(self: *const OperatorInfo) []const u8 { - const ptr = c.opendal_operator_info_get_name(self.inner); - defer std.c.free(ptr); - return std.mem.span(ptr); + pub fn name(self: *const OperatorInfo) OwnedString { + return .{ .ptr = c.opendal_operator_info_get_name(self.inner) }; } pub fn capability(self: *const OperatorInfo) c.opendal_capability { @@ -163,8 +207,8 @@ pub const Lister = struct { pub fn next(self: *const Lister) !?Entry { var entry: ?*c.opendal_entry = null; if (c.opendal_lister_next(self.inner, @ptrCast(&entry))) |err| { - errdefer c.opendal_error_free(err); - try codeToError(err.*.code); + defer c.opendal_error_free(err); + return codeToError(err.*.code); } if (entry) |e| { return Entry{ .inner = e }; @@ -180,14 +224,12 @@ pub const Entry = struct { c.opendal_entry_free(self.inner); } - pub fn path(self: *const Entry) []const u8 { - const ptr = c.opendal_entry_path(self.inner); - return std.mem.span(ptr); + pub fn path(self: *const Entry) OwnedString { + return .{ .ptr = c.opendal_entry_path(self.inner) }; } - pub fn name(self: *const Entry) []const u8 { - const ptr = c.opendal_entry_name(self.inner); - return std.mem.span(ptr); + pub fn name(self: *const Entry) OwnedString { + return .{ .ptr = c.opendal_entry_name(self.inner) }; } }; @@ -214,22 +256,29 @@ pub const Metadata = struct { return c.opendal_metadata_content_length(self.inner); } - pub fn contentType(self: *const Metadata) ?[]const u8 { - var len: usize = undefined; - const ptr = c.opendal_metadata_content_type(self.inner, &len); - if (ptr == null) return null; - return ptr[0..len]; + pub fn contentType(self: *const Metadata) ?OwnedString { + const ptr = c.opendal_metadata_content_type(self.inner); + if (ptr == null) { + return null; + } + return OwnedString{ .ptr = ptr }; } - pub fn etag(self: *const Metadata) ?[]const u8 { - var len: usize = undefined; - const ptr = c.opendal_metadata_etag(self.inner, &len); - if (ptr == null) return null; - return ptr[0..len]; + pub fn etag(self: *const Metadata) ?OwnedString { + const ptr = c.opendal_metadata_etag(self.inner); + if (ptr == null) { + return null; + } + return OwnedString{ .ptr = ptr }; } - pub fn lastModified(self: *const Metadata) i64 { - return c.opendal_metadata_last_modified(self.inner); + /// Returns the last-modified timestamp in milliseconds since the Unix epoch, if present. + pub fn lastModified(self: *const Metadata) ?i64 { + const ms = c.opendal_metadata_last_modified_ms(self.inner); + if (ms == -1) { + return null; + } + return ms; } }; @@ -247,6 +296,7 @@ pub const Code = enum(c.opendal_code) { CONDITION_NOT_MATCH = c.OPENDAL_CONDITION_NOT_MATCH, RANGE_NOT_SATISFIED = c.OPENDAL_RANGE_NOT_SATISFIED, CONFLICT = c.OPENDAL_CONFLICT, + CANCELLED = c.OPENDAL_CANCELLED, }; pub const OpendalError = error{ @@ -263,9 +313,10 @@ pub const OpendalError = error{ ConditionNotMatch, RangeNotSatisfied, Conflict, + Cancelled, }; -pub fn codeToError(code: c.opendal_code) OpendalError!void { +pub fn codeToError(code: c.opendal_code) OpendalError { return switch (code) { c.OPENDAL_UNEXPECTED => error.Unexpected, c.OPENDAL_UNSUPPORTED => error.Unsupported, @@ -280,7 +331,8 @@ pub fn codeToError(code: c.opendal_code) OpendalError!void { c.OPENDAL_CONDITION_NOT_MATCH => error.ConditionNotMatch, c.OPENDAL_RANGE_NOT_SATISFIED => error.RangeNotSatisfied, c.OPENDAL_CONFLICT => error.Conflict, - else => {}, + c.OPENDAL_CANCELLED => error.Cancelled, + else => error.Unexpected, }; } @@ -299,26 +351,29 @@ pub fn errorToCode(err: OpendalError) c_int { error.ConditionNotMatch => c.OPENDAL_CONDITION_NOT_MATCH, error.RangeNotSatisfied => c.OPENDAL_RANGE_NOT_SATISFIED, error.Conflict => c.OPENDAL_CONFLICT, + error.Cancelled => c.OPENDAL_CANCELLED, }; } -const std = @import("std"); + const testing = std.testing; test "Error Tests" { // C code to Zig error - try testing.expectError(error.Unexpected, codeToError(c.OPENDAL_UNEXPECTED)); - try testing.expectError(error.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED)); - try testing.expectError(error.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID)); - try testing.expectError(error.NotFound, codeToError(c.OPENDAL_NOT_FOUND)); - try testing.expectError(error.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED)); - try testing.expectError(error.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY)); - try testing.expectError(error.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY)); - try testing.expectError(error.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS)); - try testing.expectError(error.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED)); - try testing.expectError(error.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE)); - try testing.expectError(error.ConditionNotMatch, codeToError(c.OPENDAL_CONDITION_NOT_MATCH)); - try testing.expectError(error.RangeNotSatisfied, codeToError(c.OPENDAL_RANGE_NOT_SATISFIED)); - try testing.expectError(error.Conflict, codeToError(c.OPENDAL_CONFLICT)); + try testing.expectEqual(OpendalError.Unexpected, codeToError(c.OPENDAL_UNEXPECTED)); + try testing.expectEqual(OpendalError.Unsupported, codeToError(c.OPENDAL_UNSUPPORTED)); + try testing.expectEqual(OpendalError.ConfigInvalid, codeToError(c.OPENDAL_CONFIG_INVALID)); + try testing.expectEqual(OpendalError.NotFound, codeToError(c.OPENDAL_NOT_FOUND)); + try testing.expectEqual(OpendalError.PermissionDenied, codeToError(c.OPENDAL_PERMISSION_DENIED)); + try testing.expectEqual(OpendalError.IsDirectory, codeToError(c.OPENDAL_IS_A_DIRECTORY)); + try testing.expectEqual(OpendalError.IsNotDirectory, codeToError(c.OPENDAL_NOT_A_DIRECTORY)); + try testing.expectEqual(OpendalError.AlreadyExists, codeToError(c.OPENDAL_ALREADY_EXISTS)); + try testing.expectEqual(OpendalError.RateLimited, codeToError(c.OPENDAL_RATE_LIMITED)); + try testing.expectEqual(OpendalError.IsSameFile, codeToError(c.OPENDAL_IS_SAME_FILE)); + try testing.expectEqual(OpendalError.ConditionNotMatch, codeToError(c.OPENDAL_CONDITION_NOT_MATCH)); + try testing.expectEqual(OpendalError.RangeNotSatisfied, codeToError(c.OPENDAL_RANGE_NOT_SATISFIED)); + try testing.expectEqual(OpendalError.Conflict, codeToError(c.OPENDAL_CONFLICT)); + try testing.expectEqual(OpendalError.Cancelled, codeToError(c.OPENDAL_CANCELLED)); + try testing.expectEqual(OpendalError.Unexpected, codeToError(999)); // Zig error to C code try testing.expectEqual(c.OPENDAL_UNEXPECTED, errorToCode(error.Unexpected)); @@ -334,10 +389,18 @@ test "Error Tests" { try testing.expectEqual(c.OPENDAL_CONDITION_NOT_MATCH, errorToCode(error.ConditionNotMatch)); try testing.expectEqual(c.OPENDAL_RANGE_NOT_SATISFIED, errorToCode(error.RangeNotSatisfied)); try testing.expectEqual(c.OPENDAL_CONFLICT, errorToCode(error.Conflict)); + try testing.expectEqual(c.OPENDAL_CANCELLED, errorToCode(error.Cancelled)); } test "Semantic Analyzer" { testing.refAllDecls(@This()); + testing.refAllDecls(OwnedString); + testing.refAllDecls(Bytes); + testing.refAllDecls(Operator); + testing.refAllDecls(OperatorInfo); + testing.refAllDecls(Lister); + testing.refAllDecls(Entry); + testing.refAllDecls(Metadata); } test "operator basic operations" { @@ -350,14 +413,15 @@ test "operator basic operations" { // Write this into path "/testpath" try op.write("/testpath", data); - defer _ = op.delete("/testpath") catch |err| { + defer op.delete("/testpath") catch |err| { std.debug.panic("Error deleting file: {}\n", .{err}); }; // We can read it out, make sure the data is the same - const read_bytes = try op.read("/testpath"); - try testing.expectEqual(read_bytes.len, 24); - try testing.expectEqualStrings(read_bytes, data); + var read_bytes = try op.read("/testpath"); + defer read_bytes.deinit(); + try testing.expectEqual(read_bytes.slice().len, 24); + try testing.expectEqualStrings(read_bytes.slice(), data); } test "operator advanced operations" { @@ -411,13 +475,16 @@ test "operator advanced operations" { var lister = try op.list("/testdir/"); defer lister.deinit(); var count: usize = 0; - while (try lister.next()) |entry| { - // defer entry.deinit(); + while (try lister.next()) |e| { + var entry = e; + defer entry.deinit(); count += 1; - try testing.expect(entry.path().len > 0); - try testing.expect(entry.name().len > 0); - // std.log.debug("entry name: {s}", .{entry.name()}); - // std.log.debug("entry path: {s}", .{entry.path()}); + var entry_path = entry.path(); + defer entry_path.deinit(); + var entry_name = entry.name(); + defer entry_name.deinit(); + try testing.expect(entry_path.slice().len > 0); + try testing.expect(entry_name.slice().len > 0); } try testing.expectEqual(count, 2); @@ -426,6 +493,106 @@ test "operator advanced operations" { try testing.expect(!try op.exists("/testdir/renamed.txt")); } +test "operator info" { + var op = try Operator.init("memory", null); + defer op.deinit(); + + var op_info = try op.info(); + defer op_info.deinit(); + + var scheme = op_info.scheme(); + defer scheme.deinit(); + try testing.expectEqualStrings("memory", scheme.slice()); + + var root = op_info.root(); + defer root.deinit(); + try testing.expectEqualStrings("/", root.slice()); + + var name = op_info.name(); + defer name.deinit(); + try testing.expect(name.slice().len > 0); + name.deinit(); + try testing.expectEqualStrings("", name.slice()); + + const cap = op_info.capability(); + try testing.expect(cap.read); + try testing.expect(cap.write); + try testing.expect(cap.stat); +} + +test "metadata optional fields" { + var op = try Operator.init("memory", null); + defer op.deinit(); + + try op.write("/meta.txt", "metadata test"); + defer op.delete("/meta.txt") catch {}; + + var meta = try op.stat("/meta.txt"); + defer meta.deinit(); + try testing.expect(meta.contentType() == null); + try testing.expect(meta.etag() == null); + try testing.expect(meta.lastModified() == null); + + const opts = c.opendal_write_options_new(); + defer c.opendal_write_options_free(opts); + c.opendal_write_options_set_content_type(opts, "text/plain"); + const payload: []const u8 = "typed payload"; + const raw_bytes = c.opendal_bytes{ + .data = @constCast(payload.ptr), + .len = payload.len, + .capacity = payload.len, + }; + if (c.opendal_operator_write_with(op.inner, "/typed.txt", &raw_bytes, opts)) |err| { + defer c.opendal_error_free(err); + return codeToError(err.*.code); + } + defer op.delete("/typed.txt") catch {}; + + var typed_meta = try op.stat("/typed.txt"); + defer typed_meta.deinit(); + var content_type = typed_meta.contentType() orelse return error.TestExpectedContentType; + defer content_type.deinit(); + try testing.expectEqualStrings("text/plain", content_type.slice()); +} + +test "empty write and read" { + var op = try Operator.init("memory", null); + defer op.deinit(); + + try op.write("/empty.txt", ""); + defer op.delete("/empty.txt") catch {}; + + var read_bytes = try op.read("/empty.txt"); + defer read_bytes.deinit(); + try testing.expectEqual(@as(usize, 0), read_bytes.slice().len); + try testing.expectEqualStrings("", read_bytes.slice()); + + read_bytes.deinit(); + try testing.expectEqual(@as(usize, 0), read_bytes.slice().len); +} + +test "runtime sentinel path" { + const allocator = testing.allocator; + + var op = try Operator.init("memory", null); + defer op.deinit(); + + const path = try std.fmt.allocPrintZ(allocator, "/runtime-{d}.txt", .{42}); + defer allocator.free(path); + + const payload = "runtime path payload"; + try op.write(path, payload); + defer op.delete(path) catch {}; + + var read_bytes = try op.read(path); + defer read_bytes.deinit(); + try testing.expectEqualStrings(payload, read_bytes.slice()); +} + +test "operator init invalid scheme" { + try testing.expectError(error.Unsupported, Operator.init("nonexistent_scheme", null)); +} + test "sync operations" { var op = try Operator.init("memory", null); defer op.deinit(); @@ -434,8 +601,9 @@ test "sync operations" { // Test first path try writeData(&op, "test_path", &data); - const read_bytes1 = try readData(&op, "test_path"); - try testing.expectEqualSlices(u8, &data, read_bytes1); + var read_bytes1 = try readData(&op, "test_path"); + defer read_bytes1.deinit(); + try testing.expectEqualSlices(u8, &data, read_bytes1.slice()); } // FIXME: test async operations @@ -458,16 +626,17 @@ test "sync operations" { // try testing.expectEqual(read_frame1.status(), .Suspended); // try coro.xawait(write_frame1); -// const read_bytes1 = try coro.xawait(read_frame1); +// var read_bytes1 = try coro.xawait(read_frame1); +// defer read_bytes1.deinit(); // try testing.expectEqual(write_frame1.status(), .Done); // try testing.expectEqual(read_frame1.status(), .Done); -// try testing.expectEqualSlices(u8, &data, read_bytes1); +// try testing.expectEqualSlices(u8, &data, read_bytes1.slice()); // } -fn writeData(op: *Operator, path: []const u8, data: []const u8) !void { +fn writeData(op: *Operator, path: [:0]const u8, data: []const u8) !void { try op.write(path, data); } -fn readData(op: *Operator, path: []const u8) ![]const u8 { +fn readData(op: *Operator, path: [:0]const u8) !Bytes { return try op.read(path); }
