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

feat(bindings/c): add opendal_operator_blocking_delete method #2416

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 43 additions & 0 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,49 @@ enum opendal_code opendal_operator_blocking_write(struct opendal_operator_ptr pt
struct opendal_result_read opendal_operator_blocking_read(struct opendal_operator_ptr ptr,
const char *path);

/**
* \brief Blockingly delete the object in `path`.
*
* Delete the object in `path` blockingly by `op_ptr`, returns the opendal_code OPENDAL_OK
* if succeeds, others otherwise
*
* @param ptr The opendal_operator_ptr created previously
* @param path The designated path you want to delete
* @see opendal_operator_ptr
* @see opendal_code
* @return OPENDAL_OK if succeeds others otherwise
*
* # Example
*
* Following is an example
* ```C
* //...prepare your opendal_operator_ptr, named ptr for example
*
* // prepare your data
* char* data = "Hello, World!";
* opendal_bytes bytes = opendal_bytes { .data = (uint8_t*)data, .len = 13 };
* opendal_code code = opendal_operator_blocking_write(ptr, "/testpath", bytes);
*
* // now you can delete!
* opendal_code code = opendal_operator_blocking_delete(ptr, "/testpath");
*
* // Assert that this succeeds
* assert(code == OPENDAL_OK)
* ```
*
* # Safety
*
* It is **safe** under the cases below
* * The memory pointed to by `path` must contain a valid nul terminator at the end of
* the string.
*
* # Panic
*
* * If the `path` points to NULL, this function panics, i.e. exits with information
*/
enum opendal_code opendal_operator_blocking_delete(struct opendal_operator_ptr ptr,
const char *path);

/**
* \brief Check whether the path exists.
*
Expand Down
55 changes: 55 additions & 0 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,61 @@ pub unsafe extern "C" fn opendal_operator_blocking_read(
}
}

/// \brief Blockingly delete the object in `path`.
///
/// Delete the object in `path` blockingly by `op_ptr`, returns the opendal_code OPENDAL_OK
/// if succeeds, others otherwise
///
/// @param ptr The opendal_operator_ptr created previously
/// @param path The designated path you want to delete
/// @see opendal_operator_ptr
/// @see opendal_code
/// @return OPENDAL_OK if succeeds others otherwise
///
/// # Example
///
/// Following is an example
/// ```C
/// //...prepare your opendal_operator_ptr, named ptr for example
xyjixyjixyji marked this conversation as resolved.
Show resolved Hide resolved
///
/// // prepare your data
/// char* data = "Hello, World!";
/// opendal_bytes bytes = opendal_bytes { .data = (uint8_t*)data, .len = 13 };
/// opendal_code code = opendal_operator_blocking_write(ptr, "/testpath", bytes);
///
/// // now you can delete!
/// opendal_code code = opendal_operator_blocking_delete(ptr, "/testpath");
///
/// // Assert that this succeeds
/// assert(code == OPENDAL_OK)
/// ```
///
/// # Safety
///
/// It is **safe** under the cases below
/// * The memory pointed to by `path` must contain a valid nul terminator at the end of
/// the string.
///
/// # Panic
///
/// * If the `path` points to NULL, this function panics, i.e. exits with information
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_blocking_delete(
ptr: opendal_operator_ptr,
path: *const c_char,
) -> opendal_code {
if path.is_null() {
panic!("The path given is pointing at NULL");
}

let op = ptr.as_ref();
let path = unsafe { std::ffi::CStr::from_ptr(path).to_str().unwrap() };
match op.delete(path) {
Ok(_) => opendal_code::OPENDAL_OK,
xyjixyjixyji marked this conversation as resolved.
Show resolved Hide resolved
Err(e) => opendal_code::from_opendal_error(e),
}
}

/// \brief Check whether the path exists.
///
/// If the operation succeeds, no matter the path exists or not,
Expand Down
12 changes: 12 additions & 0 deletions bindings/c/tests/bdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ TEST_F(OpendalBddTest, FeatureTest)
for (int i = 0; i < r.data->len; i++) {
EXPECT_EQ(this->content[i], (char)(r.data->data[i]));
}

// The blocking file should be deleted
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
code = opendal_operator_blocking_delete(this->p, this->path.c_str());
EXPECT_EQ(code, OPENDAL_OK);
e = opendal_operator_is_exist(this->p, this->path.c_str());
EXPECT_EQ(e.code, OPENDAL_OK);
EXPECT_FALSE(e.is_exist);

// The deletion operation should be idempotent
code = opendal_operator_blocking_delete(this->p, this->path.c_str());
EXPECT_EQ(code, OPENDAL_OK);

opendal_bytes_free(r.data);
}

Expand Down