Xuanwo commented on code in PR #2249:
URL: 
https://github.com/apache/incubator-opendal/pull/2249#discussion_r1189527593


##########
bindings/c/include/opendal.h:
##########
@@ -234,11 +261,56 @@ struct opendal_result_read 
opendal_operator_blocking_read(opendal_operator_ptr o
 struct opendal_result_is_exist opendal_operator_is_exist(opendal_operator_ptr 
op_ptr,
                                                          const char *path);
 
+/*
+ Stat the path, return its metadata.
+
+ If the operation succeeds, no matter the path exists or not,
+ the error code should be opendal_code::OPENDAL_OK. Otherwise,
+ the field `meata` is filled with a NULL pointer, and the error code
+ is set correspondingly.
+
+ # 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
+ */
+struct opendal_result_stat opendal_operator_stat(opendal_operator_ptr op_ptr, 
const char *path);
+
+/*
+ Free the allocated operator pointed by [`opendal_operator_ptr`]
+ */
+void opendal_operator_free(const opendal_operator_ptr *self);
+
 /*
  Frees the heap memory used by the [`opendal_bytes`]
  */
 void opendal_bytes_free(const struct opendal_bytes *self);
 
+/*
+ Free the allocated metadata
+ */
+void opendal_meta_free(const opendal_metadata *self);

Review Comment:
   I perfer keep origin name: `opendal_metadata_free`



##########
bindings/c/src/types.rs:
##########
@@ -111,3 +114,67 @@ impl Into<bytes::Bytes> for opendal_bytes {
         bytes::Bytes::from_static(slice)
     }
 }
+
+/// Metadata carries all metadata associated with an path.
+///
+/// # Notes
+///
+/// mode and content_length are required metadata that all services
+/// should provide during `stat` operation. But in `list` operation,
+/// a.k.a., `Entry`'s content length could be NULL.
+#[repr(transparent)]
+pub struct opendal_metadata {
+    pub inner: *const od::Metadata,
+}
+
+impl opendal_metadata {
+    /// Free the allocated metadata
+    #[no_mangle]
+    pub extern "C" fn opendal_meta_free(&self) {
+        if self.inner.is_null() {
+            return;
+        }
+        let _ = unsafe { Box::from_raw(self.inner as *mut od::Metadata) };
+    }
+
+    /// Return the content_length of the metadata
+    #[no_mangle]
+    pub extern "C" fn opendal_meta_content_length(&self) -> u64 {
+        // Safety: the inner should never be null once constructed
+        // The use-after-free is undefined behavior
+        unsafe { (*self.inner).content_length() }
+    }
+
+    /// Return whether the path represents a file
+    #[no_mangle]
+    pub extern "C" fn opendal_meta_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() }
+    }
+
+    /// Return whether the path represents a directory
+    #[no_mangle]
+    pub extern "C" fn opendal_meta_is_dir(&self) -> bool {
+        // Safety: the inner should never be null once constructed
+        // The use-after-free is undefined behavior
+        unsafe { (*self.inner).is_dir() }
+    }
+}
+
+impl opendal_metadata {
+    /// Return a null metadata
+    pub(crate) fn null() -> Self {
+        Self {
+            inner: std::ptr::null(),
+        }
+    }
+
+    /// Convert a Rust core [`od::Metadata`] into a heap allocated C-compatible
+    /// [`opendal_metadata`]
+    pub(crate) fn from_meta(m: od::Metadata) -> Self {

Review Comment:
   Please use `from_metadata`, or simply `new`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to