]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: add helper to extend strv from both sides
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 22 Feb 2024 09:47:23 +0000 (10:47 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Mar 2024 17:49:44 +0000 (18:49 +0100)
Also, use the more correct type of 'const char* const*' for the input strv.
This requires adding the cast in a few places, but also allows to remove some
casts in others.

src/basic/conf-files.c
src/basic/path-lookup.c
src/basic/strv.c
src/basic/strv.h
src/test/test-strv.c
src/tmpfiles/tmpfiles.c
src/xdg-autostart-generator/xdg-autostart-generator.c

index 9cb66c099b0c09d9b02e0cd9a572ba034770b5a9..7fdcc71356f1147d6ac240b4231723e0bfa551ef 100644 (file)
@@ -369,7 +369,7 @@ int conf_files_list_dropins(
         assert(dirs);
 
         suffix = strjoina("/", dropin_dirname);
-        r = strv_extend_strv_concat(&dropin_dirs, (char**) dirs, suffix);
+        r = strv_extend_strv_concat(&dropin_dirs, dirs, suffix);
         if (r < 0)
                 return r;
 
index 1b31a4e23a52c463fdb840499486f8d9a5afda9b..8a03e29f65ad94351feb4457517094aec4227ddd 100644 (file)
@@ -214,7 +214,7 @@ static char** user_dirs(
                             persistent_config) < 0)
                 return NULL;
 
-        if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
+        if (strv_extend_strv_concat(&res, (const char* const*) config_dirs, "/systemd/user") < 0)
                 return NULL;
 
         /* global config has lower priority than the user config of the same type */
@@ -232,7 +232,7 @@ static char** user_dirs(
                             data_home) < 0)
                 return NULL;
 
-        if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
+        if (strv_extend_strv_concat(&res, (const char* const*) data_dirs, "/systemd/user") < 0)
                 return NULL;
 
         if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0)
index 37199ebf045d7f4193a6684573dbfde1ee9d9587..b7946c8ba2ff9f1b4efa581d16ab9069854e225b 100644 (file)
@@ -242,13 +242,13 @@ rollback:
         return -ENOMEM;
 }
 
-int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix) {
+int strv_extend_strv_biconcat(char ***a, const char *prefix, const char* const *b, const char *suffix) {
         int r;
 
         STRV_FOREACH(s, b) {
                 char *v;
 
-                v = strjoin(*s, suffix);
+                v = strjoin(strempty(prefix), *s, suffix);
                 if (!v)
                         return -ENOMEM;
 
index 91337b9287086027e41ae56ecd7303c44ca95218..169737d1d8c18f72cdb3991cf7994b278168796b 100644 (file)
@@ -43,7 +43,10 @@ int strv_copy_unless_empty(char * const *l, char ***ret);
 size_t strv_length(char * const *l) _pure_;
 
 int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates);
-int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix);
+int strv_extend_strv_biconcat(char ***a, const char *prefix, const char* const *b, const char *suffix);
+static inline int strv_extend_strv_concat(char ***a, const char* const *b, const char *suffix) {
+        return strv_extend_strv_biconcat(a, NULL, b, suffix);
+}
 int strv_prepend(char ***l, const char *value);
 
 /* _with_size() are lower-level functions where the size can be provided externally,
index da8721214a2f42ce9f8b97029032e50c691e9310..47ad0eb639c30407b91b2ec06350afccc8953176 100644 (file)
@@ -527,6 +527,22 @@ TEST(strv_sort) {
         assert_se(streq(input_table[4], "durian"));
 }
 
+TEST(strv_extend_strv_biconcat) {
+        _cleanup_strv_free_ char **a = NULL, **b = NULL;
+
+        a = strv_new("without", "suffix");
+        b = strv_new("with", "suffix");
+        assert_se(a);
+        assert_se(b);
+
+        assert_se(strv_extend_strv_biconcat(&a, "prefix_", (const char* const*) b, "_suffix") >= 0);
+
+        assert_se(streq(a[0], "without"));
+        assert_se(streq(a[1], "suffix"));
+        assert_se(streq(a[2], "prefix_with_suffix"));
+        assert_se(streq(a[3], "prefix_suffix_suffix"));
+}
+
 TEST(strv_extend_strv_concat) {
         _cleanup_strv_free_ char **a = NULL, **b = NULL;
 
@@ -535,7 +551,7 @@ TEST(strv_extend_strv_concat) {
         assert_se(a);
         assert_se(b);
 
-        assert_se(strv_extend_strv_concat(&a, b, "_suffix") >= 0);
+        assert_se(strv_extend_strv_concat(&a, (const char* const*) b, "_suffix") >= 0);
 
         assert_se(streq(a[0], "without"));
         assert_se(streq(a[1], "suffix"));
index 958341bc5b8bbae5e4664805c9231015c5889d55..e4854f31b5ddb724e8691bd853932939852fba09 100644 (file)
@@ -369,7 +369,7 @@ static int user_config_paths(char*** ret) {
         if (r < 0 && !ERRNO_IS_NOINFO(r))
                 return r;
 
-        r = strv_extend_strv_concat(&res, config_dirs, "/user-tmpfiles.d");
+        r = strv_extend_strv_concat(&res, (const char* const*) config_dirs, "/user-tmpfiles.d");
         if (r < 0)
                 return r;
 
@@ -381,7 +381,7 @@ static int user_config_paths(char*** ret) {
         if (r < 0)
                 return r;
 
-        r = strv_extend_strv_concat(&res, data_dirs, "/user-tmpfiles.d");
+        r = strv_extend_strv_concat(&res, (const char* const*) data_dirs, "/user-tmpfiles.d");
         if (r < 0)
                 return r;
 
index 616c0173577784bcbd2b369db182b3574565277b..71e1a664351ffeb048ae540008e56d6c45e7221a 100644 (file)
@@ -37,7 +37,7 @@ static int enumerate_xdg_autostart(Hashmap *all_services) {
         r = xdg_user_dirs(&config_dirs, &data_dirs);
         if (r < 0)
                 return r;
-        r = strv_extend_strv_concat(&autostart_dirs, config_dirs, "/autostart");
+        r = strv_extend_strv_concat(&autostart_dirs, (const char* const*) config_dirs, "/autostart");
         if (r < 0)
                 return r;