Skip to content

Commit

Permalink
feat(bindings/c): add opendal_operator_blocking_delete method
Browse files Browse the repository at this point in the history
Signed-off-by: Enwei Jiao <[email protected]>
  • Loading branch information
jiaoew1991 committed Jun 5, 2023
1 parent fe21ea7 commit 99389fe
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
38 changes: 38 additions & 0 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,44 @@ 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 `path`.
*
* Write the `bytes` into the `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
*
* // 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
50 changes: 50 additions & 0 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,56 @@ pub unsafe extern "C" fn opendal_operator_blocking_read(
}
}

/// \brief Blockingly delete `path`.
///
/// Write the `bytes` into the `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
///
/// // 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,
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
8 changes: 8 additions & 0 deletions bindings/c/tests/bdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ 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
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);

opendal_bytes_free(r.data);
}

Expand Down

0 comments on commit 99389fe

Please sign in to comment.