Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix the build for zig binding #2493

Merged
merged 6 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ typedef struct opendal_metadata {
* The pointer to the opendal::Metadata in the Rust code.
* Only touch this on judging whether it is NULL.
*/
const struct Metadata *inner;
struct Metadata *inner;
} opendal_metadata;

/**
Expand All @@ -254,7 +254,7 @@ typedef struct opendal_result_stat {
/**
* The metadata output of the stat
*/
const struct opendal_metadata *meta;
struct opendal_metadata *meta;
/**
* The error code, should be OPENDAL_OK if succeeds
*/
Expand Down Expand Up @@ -558,7 +558,7 @@ void opendal_bytes_free(const struct opendal_bytes *self);
/**
* \brief Free the heap-allocated metadata used by opendal_metadata
*/
void opendal_metadata_free(const struct opendal_metadata *self);
void opendal_metadata_free(struct opendal_metadata *self);

/**
* \brief Return the content_length of the metadata
Expand Down
4 changes: 2 additions & 2 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ pub unsafe extern "C" fn opendal_operator_stat(
let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() };
match op.stat(path) {
Ok(m) => opendal_result_stat {
meta: opendal_metadata::from_metadata(m),
meta: Box::into_raw(Box::new(opendal_metadata::new(m))),
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
code: opendal_code::OPENDAL_OK,
},
Err(err) => opendal_result_stat {
meta: std::ptr::null(),
meta: std::ptr::null_mut(),
code: opendal_code::from_opendal_error(err),
},
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct opendal_result_is_exist {
#[repr(C)]
pub struct opendal_result_stat {
/// The metadata output of the stat
pub meta: *const opendal_metadata,
pub meta: *mut opendal_metadata,
/// The error code, should be OPENDAL_OK if succeeds
pub code: opendal_code,
}
36 changes: 20 additions & 16 deletions bindings/c/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

use std::collections::HashMap;
use std::mem;
use std::os::raw::c_char;

use ::opendal as od;
Expand Down Expand Up @@ -143,17 +144,28 @@ impl Into<bytes::Bytes> for opendal_bytes {
pub struct opendal_metadata {
/// The pointer to the opendal::Metadata in the Rust code.
/// Only touch this on judging whether it is NULL.
pub inner: *const od::Metadata,
pub inner: *mut od::Metadata,
}

impl opendal_metadata {
/// Convert a Rust core [`od::Metadata`] into a heap allocated C-compatible
/// [`opendal_metadata`]
pub(crate) fn new(m: od::Metadata) -> Self {
Self {
inner: Box::into_raw(Box::new(m)),
}
}

/// \brief Free the heap-allocated metadata used by opendal_metadata
#[no_mangle]
pub extern "C" fn opendal_metadata_free(&self) {
if self.inner.is_null() {
return;
pub extern "C" fn opendal_metadata_free(&mut self) {
if !self.inner.is_null() {
unsafe {
mem::drop(Box::from_raw(self.inner));
}
}
let _ = unsafe { Box::from_raw(self.inner as *mut od::Metadata) };

Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
unsafe { mem::drop(Box::from_raw(self as *mut Self)) }
}

/// \brief Return the content_length of the metadata
Expand Down Expand Up @@ -189,7 +201,9 @@ impl opendal_metadata {
pub extern "C" fn opendal_metadata_is_file(&self) -> bool {
// Safety: the inner should never be null once constructed
// The use-after-free is undefined behavior
unsafe { (*self.inner).is_file() }
let m = unsafe { &*self.inner };

m.is_file()
}

/// \brief Return whether the path represents a directory
Expand All @@ -216,16 +230,6 @@ impl opendal_metadata {
}
}

impl opendal_metadata {
/// Convert a Rust core [`od::Metadata`] into a heap allocated C-compatible
/// [`opendal_metadata`]
pub(crate) fn from_metadata(m: od::Metadata) -> *const Self {
&Self {
inner: Box::leak(Box::new(m)),
}
}
}

/// \brief The configuration for the initialization of opendal_operator_ptr.
///
/// \note This is also a heap-allocated struct, please free it after you use it
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/tests/bdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEST_F(OpendalBddTest, FeatureTest)
// The blocking file "test" entry mode must be file
opendal_result_stat s = opendal_operator_stat(this->p, this->path.c_str());
EXPECT_EQ(s.code, OPENDAL_OK);
const opendal_metadata *meta = s.meta;
opendal_metadata *meta = s.meta;
EXPECT_TRUE(opendal_metadata_is_file(meta));

// The blocking file "test" content length must be 13
Expand Down
21 changes: 10 additions & 11 deletions bindings/zig/test/bdd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test "Opendal BDD test" {
const c_str = [*:0]const u8; // define a type for 'const char*' in C

const OpendalBDDTest = struct {
p: opendal.c.opendal_operator_ptr,
p: [*c]const opendal.c.opendal_operator_ptr,
scheme: c_str,
path: c_str,
content: c_str,
Expand All @@ -35,19 +35,18 @@ test "Opendal BDD test" {
self.path = "test";
self.content = "Hello, World!";

var options: opendal.c.opendal_operator_options = opendal.c.opendal_operator_options_new();
defer opendal.c.opendal_operator_options_free(&options);
opendal.c.opendal_operator_options_set(&options, "root", "/myroot");
var options: [*c]opendal.c.opendal_operator_options = opendal.c.opendal_operator_options_new();
defer opendal.c.opendal_operator_options_free(options);
opendal.c.opendal_operator_options_set(options, "root", "/myroot");

// Given A new OpenDAL Blocking Operator
self.p = opendal.c.opendal_operator_new(self.scheme, &options);
std.debug.assert(self.p.ptr != null);
self.p = opendal.c.opendal_operator_new(self.scheme, options);

return self;
}

pub fn deinit(self: *Self) void {
opendal.c.opendal_operator_free(&self.p);
opendal.c.opendal_operator_free(self.p);
}

const Self = @This();
Expand All @@ -73,12 +72,12 @@ test "Opendal BDD test" {
// The blocking file "test" entry mode must be file
var s: opendal.c.opendal_result_stat = opendal.c.opendal_operator_stat(testkit.p, testkit.path);
try testing.expectEqual(s.code, @enumToInt(Code.OK));
var meta: opendal.c.opendal_metadata = s.meta;
try testing.expect(opendal.c.opendal_metadata_is_file(&meta));
var meta: [*c] opendal.c.opendal_metadata = s.meta;
try testing.expect(opendal.c.opendal_metadata_is_file(meta));

// The blocking file "test" content length must be 13
try testing.expectEqual(opendal.c.opendal_metadata_content_length(&meta), 13);
defer opendal.c.opendal_metadata_free(&meta);
try testing.expectEqual(opendal.c.opendal_metadata_content_length(meta), 13);
defer opendal.c.opendal_metadata_free(meta);

// The blocking file "test" must have content "Hello, World!"
var r: opendal.c.opendal_result_read = opendal.c.opendal_operator_blocking_read(testkit.p, testkit.path);
Expand Down