iovec-wrapper: rename iovw_append() to iovw_extend()

The naming is consistent with strv_extend().

This also
- introduces tiny iovw_extend_iov() wrapper,
- refuse when the source and target points to the same object,
- check the final count before extending in iovw_extend_iovw().
This commit is contained in:
Yu Watanabe
2026-04-19 06:02:48 +09:00
parent d49c509d9b
commit 6d14614f4d
5 changed files with 115 additions and 71 deletions
+47 -29
View File
@@ -74,7 +74,7 @@ int iovw_consume(struct iovec_wrapper *iovw, void *data, size_t len) {
return r;
}
int iovw_append(struct iovec_wrapper *iovw, const void *data, size_t len) {
int iovw_extend(struct iovec_wrapper *iovw, const void *data, size_t len) {
if (len == 0)
return 0;
@@ -85,6 +85,52 @@ int iovw_append(struct iovec_wrapper *iovw, const void *data, size_t len) {
return iovw_consume(iovw, c, len);
}
int iovw_extend_iov(struct iovec_wrapper *iovw, const struct iovec *iov) {
assert(iovw);
if (!iovec_is_set(iov))
return 0;
return iovw_extend(iovw, iov->iov_base, iov->iov_len);
}
int iovw_extend_iovw(struct iovec_wrapper *iovw, const struct iovec_wrapper *source) {
int r;
assert(iovw);
/* This duplicates the source and merges it into the iovw. */
if (iovw_isempty(source))
return 0;
/* iovw->iovec will be reallocated in the loop below, hence source cannot point to the same object. */
if (iovw == source)
return -EINVAL;
if (iovw->count > SIZE_MAX - source->count)
return -E2BIG;
if (iovw->count + source->count > IOV_MAX)
return -E2BIG;
size_t original_count = iovw->count;
FOREACH_ARRAY(iovec, source->iovec, source->count) {
r = iovw_extend_iov(iovw, iovec);
if (r < 0)
goto rollback;
}
return 0;
rollback:
for (size_t i = original_count; i < iovw->count; i++)
iovec_done(iovw->iovec + i);
iovw->count = original_count;
return r;
}
int iovw_put_string_field_full(struct iovec_wrapper *iovw, bool replace, const char *field, const char *value) {
_cleanup_free_ char *x = NULL;
int r;
@@ -147,34 +193,6 @@ size_t iovw_size(const struct iovec_wrapper *iovw) {
return iovec_total_size(iovw->iovec, iovw->count);
}
int iovw_append_iovw(struct iovec_wrapper *target, const struct iovec_wrapper *source) {
int r;
assert(target);
/* This duplicates the source and merges it into the target. */
if (iovw_isempty(source))
return 0;
size_t original_count = target->count;
FOREACH_ARRAY(iovec, source->iovec, source->count) {
r = iovw_append(target, iovec->iov_base, iovec->iov_len);
if (r < 0)
goto rollback;
}
return 0;
rollback:
for (size_t i = original_count; i < target->count; i++)
iovec_done(target->iovec + i);
target->count = original_count;
return r;
}
int iovw_concat(const struct iovec_wrapper *iovw, struct iovec *ret) {
assert(iovw);
assert(ret);
+3 -2
View File
@@ -18,7 +18,9 @@ static inline bool iovw_equal(const struct iovec_wrapper *a, const struct iovec_
int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len);
int iovw_consume(struct iovec_wrapper *iovw, void *data, size_t len);
int iovw_append(struct iovec_wrapper *iovw, const void *data, size_t len);
int iovw_extend(struct iovec_wrapper *iovw, const void *data, size_t len);
int iovw_extend_iov(struct iovec_wrapper *iovw, const struct iovec *iov);
int iovw_extend_iovw(struct iovec_wrapper *iovw, const struct iovec_wrapper *source);
static inline bool iovw_isempty(const struct iovec_wrapper *iovw) {
return !iovw || iovw->count == 0;
@@ -37,6 +39,5 @@ int iovw_put_string_fieldf_full(struct iovec_wrapper *iovw, bool replace, const
int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value);
void iovw_rebase(struct iovec_wrapper *iovw, void *old, void *new);
size_t iovw_size(const struct iovec_wrapper *iovw);
int iovw_append_iovw(struct iovec_wrapper *target, const struct iovec_wrapper *source);
int iovw_concat(const struct iovec_wrapper *iovw, struct iovec *ret);
char* iovw_to_cstring(const struct iovec_wrapper *iovw);