From: Yu Watanabe Date: Wed, 15 Oct 2025 00:29:18 +0000 (+0900) Subject: iovec-wrapper: introduce iovw_replace_string_field() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a5cdc2be707c6fb4b269f824bceb4c9ea5746244;p=thirdparty%2Fsystemd.git iovec-wrapper: introduce iovw_replace_string_field() --- diff --git a/src/basic/iovec-wrapper.c b/src/basic/iovec-wrapper.c index 5cc3cc2f93d..0071893411e 100644 --- a/src/basic/iovec-wrapper.c +++ b/src/basic/iovec-wrapper.c @@ -59,7 +59,7 @@ int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) { return 0; } -int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) { +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; @@ -69,6 +69,14 @@ int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const c if (!x) return -ENOMEM; + if (replace) + FOREACH_ARRAY(iovec, iovw->iovec, iovw->count) + if (memory_startswith(iovec->iov_base, iovec->iov_len, field)) { + iovec->iov_len = strlen(x); + free_and_replace(iovec->iov_base, x); + return 0; + } + r = iovw_put(iovw, x, strlen(x)); if (r >= 0) TAKE_PTR(x); diff --git a/src/basic/iovec-wrapper.h b/src/basic/iovec-wrapper.h index 6bc4d3905a6..33a91df1ca6 100644 --- a/src/basic/iovec-wrapper.h +++ b/src/basic/iovec-wrapper.h @@ -33,7 +33,13 @@ static inline bool iovw_isempty(const struct iovec_wrapper *iovw) { return !iovw || iovw->count == 0; } -int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value); +int iovw_put_string_field_full(struct iovec_wrapper *iovw, bool replace, const char *field, const char *value); +static inline int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) { + return iovw_put_string_field_full(iovw, false, field, value); +} +static inline int iovw_replace_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) { + return iovw_put_string_field_full(iovw, true, field, value); +} 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);