]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
text protocol: Require quotes for string metadata.
authorRafael Marinheiro <marinheiro@google.com>
Mon, 21 May 2018 15:49:39 +0000 (11:49 -0400)
committerDagobert Michelsen <dam@opencsw.org>
Wed, 19 Feb 2020 17:13:24 +0000 (18:13 +0100)
This changes the metadata option to require quotes for string values.
This also adds tests for the new option.

I also ran clang-format on utils_cmds_test.c.

src/utils/cmds/cmds_test.c
src/utils/cmds/putval.c

index edbf5c95eef9df47583f1284357f12488081512d..9e7882031f509fd62dd946992e793ae225458da3 100644 (file)
@@ -204,6 +204,18 @@ static struct {
         CMD_OK,
         CMD_PUTVAL,
     },
+    {
+        "PUTVAL myhost/magic/MAGIC meta:KEY=\"string_value\" 1234:42",
+        NULL,
+        CMD_OK,
+        CMD_PUTVAL,
+    },
+    {
+        "PUTVAL myhost/magic/MAGIC meta:KEY='string_value' 1234:42",
+        NULL,
+        CMD_OK,
+        CMD_PUTVAL,
+    },
 
     /* Invalid PUTVAL commands. */
     {
index 11f0fcb61edf4dacfec4a4000980066f2a12723b..77debe352404cabb9684164d7a997ea0b8ff4d86 100644 (file)
  * private helper functions
  */
 
+static int is_quoted(const char *str, size_t len) {
+  if (len < 2) {
+    return 0;
+  }
+
+  return (str[0] == '"' && str[len - 1] == '"') ||
+         (str[0] == '\'' && str[len - 1] == '\'');
+}
+
 static int set_option(value_list_t *vl, const char *key, const char *value) {
   if ((vl == NULL) || (key == NULL) || (value == NULL))
     return -1;
@@ -50,13 +59,21 @@ static int set_option(value_list_t *vl, const char *key, const char *value) {
     if ((errno == 0) && (endptr != NULL) && (endptr != value) && (tmp > 0.0))
       vl->interval = DOUBLE_TO_CDTIME_T(tmp);
   } else if (strncasecmp("meta:", key, 5) == 0) {
+    const char *meta_key = key + 5;
+    size_t value_len = strlen(value);
+
     if (vl->meta == NULL) {
       vl->meta = meta_data_create();
       if (vl->meta == NULL) {
         return 1;
       }
     }
-    return meta_data_add_string(vl->meta, key + 5, value);
+
+    if (is_quoted(value, value_len)) {
+      const char *value_str = strndup(value + 1, value_len - 2);
+      return meta_data_add_string(vl->meta, meta_key, value_str);
+    }
+    return 1;
   } else {
     return 1;
   }