]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
iovec-wrapper: introduce iovw_replace_string_field()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 15 Oct 2025 00:29:18 +0000 (09:29 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 19 Oct 2025 01:01:46 +0000 (10:01 +0900)
src/basic/iovec-wrapper.c
src/basic/iovec-wrapper.h

index 5cc3cc2f93d5b9282b3935eece1530ad297294bd..0071893411efc8f9bc6f7b3850496635c15439f2 100644 (file)
@@ -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);
index 6bc4d3905a6676e1b82af38f4a49403cd7c8f5c0..33a91df1ca6abf1db548cefe31ea24f454beaf07 100644 (file)
@@ -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);