Skip to content
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
183 changes: 183 additions & 0 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,81 @@ typedef struct opendal_operator_options {
void *inner;
} opendal_operator_options;

/**
* \brief A key-value pair for write user metadata.
*/
typedef struct opendal_write_user_metadata_pair {
/**
* The metadata key.
*/
const char *key;
/**
* The metadata value.
*/
const char *value;
} opendal_write_user_metadata_pair;

/**
* \brief The options for write operations.
*
* Use `opendal_write_options_new()` to construct and
* `opendal_write_options_free()` to free.
*/
typedef struct opendal_write_options {
/**
* Append data to the existing file.
*/
bool append;
/**
* Cache-Control header value.
*/
const char *cache_control;
/**
* Content-Type header value.
*/
const char *content_type;
/**
* Content-Disposition header value.
*/
const char *content_disposition;
/**
* Content-Encoding header value.
*/
const char *content_encoding;
/**
* If-Match header value.
*/
const char *if_match;
/**
* If-None-Match header value.
*/
const char *if_none_match;
/**
* Only write if target does not exist.
*/
bool if_not_exists;
/**
* Concurrent write operations. `0` means sequential writes
*/
uintptr_t concurrent;
/**
* Whether `chunk` has been set.
*/
bool has_chunk;
/**
* Chunk size for buffered writes.
*/
uintptr_t chunk;
/**
* User metadata pairs.
*/
const struct opendal_write_user_metadata_pair *user_metadata;
/**
* User metadata pairs length.
*/
uintptr_t user_metadata_len;
} opendal_write_options;

/**
* \brief The result type returned by opendal's read operation.
*
Expand Down Expand Up @@ -560,10 +635,30 @@ typedef struct opendal_capability {
* If operator supports write with content disposition.
*/
bool write_with_content_disposition;
/**
* If operator supports write with content encoding.
*/
bool write_with_content_encoding;
/**
* If operator supports write with cache control.
*/
bool write_with_cache_control;
/**
* If operator supports write with if match.
*/
bool write_with_if_match;
/**
* If operator supports write with if none match.
*/
bool write_with_if_none_match;
/**
* If operator supports write with if not exists.
*/
bool write_with_if_not_exists;
/**
* If operator supports write with user metadata.
*/
bool write_with_user_metadata;
/**
* write_multi_max_size is the max size that services support in write_multi.
*
Expand Down Expand Up @@ -1030,6 +1125,14 @@ struct opendal_error *opendal_operator_write(const struct opendal_operator *op,
const char *path,
const struct opendal_bytes *bytes);

/**
* \brief Blocking write raw bytes to `path` with options.
*/
struct opendal_error *opendal_operator_write_with(const struct opendal_operator *op,
const char *path,
const struct opendal_bytes *bytes,
const struct opendal_write_options *opts);

/**
* \brief Blocking read the data from `path`.
*
Expand Down Expand Up @@ -1156,6 +1259,13 @@ struct opendal_result_operator_reader opendal_operator_reader(const struct opend
struct opendal_result_operator_writer opendal_operator_writer(const struct opendal_operator *op,
const char *path);

/**
* \brief Blocking create a writer for the specified path with options.
*/
struct opendal_result_operator_writer opendal_operator_writer_with(const struct opendal_operator *op,
const char *path,
const struct opendal_write_options *opts);

/**
* \brief Blocking delete the object in `path`.
*
Expand Down Expand Up @@ -1685,6 +1795,79 @@ void opendal_list_options_set_recursive(struct opendal_list_options *opts, bool
*/
void opendal_list_options_free(struct opendal_list_options *opts);

/**
* \brief Construct a heap-allocated opendal_write_options with default values.
*/
struct opendal_write_options *opendal_write_options_new(void);

/**
* \brief Free the heap memory used by opendal_write_options.
*/
void opendal_write_options_free(struct opendal_write_options *opts);

/**
* \brief Set append mode.
*/
void opendal_write_options_set_append(struct opendal_write_options *opts, bool append);

/**
* \brief Set Cache-Control.
*/
void opendal_write_options_set_cache_control(struct opendal_write_options *opts,
const char *cache_control);

/**
* \brief Set Content-Type.
*/
void opendal_write_options_set_content_type(struct opendal_write_options *opts,
const char *content_type);

/**
* \brief Set Content-Disposition.
*/
void opendal_write_options_set_content_disposition(struct opendal_write_options *opts,
const char *content_disposition);

/**
* \brief Set Content-Encoding.
*/
void opendal_write_options_set_content_encoding(struct opendal_write_options *opts,
const char *content_encoding);

/**
* \brief Set If-Match.
*/
void opendal_write_options_set_if_match(struct opendal_write_options *opts, const char *if_match);

/**
* \brief Set If-None-Match.
*/
void opendal_write_options_set_if_none_match(struct opendal_write_options *opts,
const char *if_none_match);

/**
* \brief Set if_not_exists.
*/
void opendal_write_options_set_if_not_exists(struct opendal_write_options *opts,
bool if_not_exists);

/**
* \brief Set concurrent.
*/
void opendal_write_options_set_concurrent(struct opendal_write_options *opts, uintptr_t concurrent);

/**
* \brief Set chunk.
*/
void opendal_write_options_set_chunk(struct opendal_write_options *opts, uintptr_t chunk);

/**
* \brief Set user metadata.
*/
void opendal_write_options_set_user_metadata(struct opendal_write_options *opts,
const struct opendal_write_user_metadata_pair *pairs,
uintptr_t len);

/**
* \brief Construct a heap-allocated opendal_operator_options
*
Expand Down
2 changes: 2 additions & 0 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ mod types;
pub use types::opendal_bytes;
pub use types::opendal_list_options;
pub use types::opendal_operator_options;
pub use types::opendal_write_options;
pub use types::opendal_write_user_metadata_pair;

mod entry;
pub use entry::opendal_entry;
Expand Down
59 changes: 57 additions & 2 deletions bindings/c/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ pub unsafe extern "C" fn opendal_operator_write(
}
}

/// \brief Blocking write raw bytes to `path` with options.
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_write_with(
op: &opendal_operator,
path: *const c_char,
bytes: &opendal_bytes,
opts: *const opendal_write_options,
) -> *mut opendal_error {
assert!(!path.is_null());
let path = std::ffi::CStr::from_ptr(path)
.to_str()
.expect("malformed path");
let opts = if opts.is_null() {
core::options::WriteOptions::default()
} else {
(&*opts).into()
};
match op.deref().write_options(path, bytes, opts) {
Ok(_) => std::ptr::null_mut(),
Err(e) => opendal_error::new(e),
}
}

/// \brief Blocking read the data from `path`.
///
/// Read the data out from `path` blocking by operator.
Expand Down Expand Up @@ -392,7 +415,7 @@ pub unsafe extern "C" fn opendal_operator_reader(
return opendal_result_operator_reader {
reader: std::ptr::null_mut(),
error: opendal_error::new(err),
}
};
}
};

Expand Down Expand Up @@ -459,7 +482,39 @@ pub unsafe extern "C" fn opendal_operator_writer(
return opendal_result_operator_writer {
writer: std::ptr::null_mut(),
error: opendal_error::new(err),
}
};
}
};

opendal_result_operator_writer {
writer: Box::into_raw(Box::new(opendal_writer::new(writer))),
error: std::ptr::null_mut(),
}
}

/// \brief Blocking create a writer for the specified path with options.
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_writer_with(
op: &opendal_operator,
path: *const c_char,
opts: *const opendal_write_options,
) -> opendal_result_operator_writer {
assert!(!path.is_null());
let path = std::ffi::CStr::from_ptr(path)
.to_str()
.expect("malformed path");
let opts = if opts.is_null() {
core::options::WriteOptions::default()
} else {
(&*opts).into()
};
let writer = match op.deref().writer_options(path, opts) {
Ok(writer) => writer,
Err(err) => {
return opendal_result_operator_writer {
writer: std::ptr::null_mut(),
error: opendal_error::new(err),
};
}
};

Expand Down
15 changes: 15 additions & 0 deletions bindings/c/src/operator_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,18 @@ pub struct opendal_capability {
pub write_with_content_type: bool,
/// If operator supports write with content disposition.
pub write_with_content_disposition: bool,
/// If operator supports write with content encoding.
pub write_with_content_encoding: bool,
/// If operator supports write with cache control.
pub write_with_cache_control: bool,
/// If operator supports write with if match.
pub write_with_if_match: bool,
/// If operator supports write with if none match.
pub write_with_if_none_match: bool,
/// If operator supports write with if not exists.
pub write_with_if_not_exists: bool,
/// If operator supports write with user metadata.
pub write_with_user_metadata: bool,
/// write_multi_max_size is the max size that services support in write_multi.
///
/// For example, AWS S3 supports 5GiB as max in write_multi.
Expand Down Expand Up @@ -239,7 +249,12 @@ impl From<core::Capability> for opendal_capability {
write_can_append: value.write_can_append,
write_with_content_type: value.write_with_content_type,
write_with_content_disposition: value.write_with_content_disposition,
write_with_content_encoding: value.write_with_content_encoding,
write_with_cache_control: value.write_with_cache_control,
write_with_if_match: value.write_with_if_match,
write_with_if_none_match: value.write_with_if_none_match,
write_with_if_not_exists: value.write_with_if_not_exists,
write_with_user_metadata: value.write_with_user_metadata,
write_multi_max_size: value.write_multi_max_size.unwrap_or(0),
write_multi_min_size: value.write_multi_min_size.unwrap_or(0),
write_total_max_size: value.write_total_max_size.unwrap_or(0),
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl opendal_reader {
core::ErrorKind::Unexpected,
"undefined whence",
)),
}
};
}
};

Expand Down
Loading
Loading