summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bbf3f23)
raw | patch | inline | side by side (parent: bbf3f23)
author | Igor Peshansky <igorpeshansky@github.com> | |
Sun, 11 Sep 2016 23:39:58 +0000 (19:39 -0400) | ||
committer | Igor Peshansky <igorpeshansky@github.com> | |
Sun, 11 Sep 2016 23:45:23 +0000 (19:45 -0400) |
- Code simplifications,
- Leak fixes,
- ERROR -> WARNING.
- Leak fixes,
- ERROR -> WARNING.
src/target_replace.c | patch | blob | history |
diff --git a/src/target_replace.c b/src/target_replace.c
index 975acbce7f3ec62842d41f6f2057c7a636d1da19..dba3a8cf83b7ad6d18003c6efa982cad5da85dab 100644 (file)
--- a/src/target_replace.c
+++ b/src/target_replace.c
{
regex_t re;
char *replacement;
- int may_be_empty;
+ _Bool may_be_empty;
tr_action_t *next;
};
} /* }}} void tr_meta_data_action_destroy */
static int tr_config_add_action (tr_action_t **dest, /* {{{ */
- const oconfig_item_t *ci, int may_be_empty)
+ const oconfig_item_t *ci, _Bool may_be_empty)
{
tr_action_t *act;
int status;
if (act->replacement == NULL)
{
ERROR ("tr_config_add_action: tr_strdup failed.");
- regfree (&act->re);
- sfree (act);
+ tr_action_destroy (act);
return (-ENOMEM);
}
} /* }}} int tr_config_add_action */
static int tr_config_add_meta_action (tr_meta_data_action_t **dest, /* {{{ */
- const oconfig_item_t *ci, int should_delete)
+ const oconfig_item_t *ci, _Bool should_delete)
{
tr_meta_data_action_t *act;
int status;
act->key = NULL;
act->replacement = NULL;
- act->key = tr_strdup (ci->values[0].value.string);
- if (act->key == NULL)
- {
- ERROR ("tr_config_add_meta_action: tr_strdup failed.");
- sfree (act);
- return (-ENOMEM);
- }
-
status = regcomp (&act->re, ci->values[1].value.string, REG_EXTENDED);
if (status != 0)
{
return (-EINVAL);
}
+ act->key = tr_strdup (ci->values[0].value.string);
+ if (act->key == NULL)
+ {
+ ERROR ("tr_config_add_meta_action: tr_strdup failed.");
+ tr_meta_data_action_destroy (act);
+ return (-ENOMEM);
+ }
+
if (!should_delete) {
act->replacement = tr_strdup (ci->values[2].value.string);
if (act->replacement == NULL)
{
ERROR ("tr_config_add_meta_action: tr_strdup failed.");
- sfree (act->key);
- regfree (&act->re);
- sfree (act);
+ tr_meta_data_action_destroy (act);
return (-ENOMEM);
}
}
} /* }}} int tr_config_add_meta_action */
static int tr_action_invoke (tr_action_t *act_head, /* {{{ */
- char *buffer_in, size_t buffer_in_size, int may_be_empty)
+ char *buffer_in, size_t buffer_in_size, _Bool may_be_empty)
{
int status;
char buffer[DATA_MAX_NAME_LEN];
int value_type;
int meta_data_status;
char *value;
+ meta_data_t *result;
value_type = meta_data_type (*dest, act->key);
if (value_type == 0) /* not found */
continue;
if (value_type != MD_TYPE_STRING)
{
- ERROR ("Target `replace': Attempting replace on metadata key `%s', "
+ WARNING ("Target `replace': Attempting replace on metadata key `%s', "
"which isn't a string.",
act->key);
continue;
STATIC_ARRAY_SIZE (matches), matches,
/* flags = */ 0);
if (status == REG_NOMATCH)
+ {
+ sfree (value);
continue;
+ }
else if (status != 0)
{
char errbuf[1024] = "";
regerror (status, &act->re, errbuf, sizeof (errbuf));
ERROR ("Target `replace': Executing a regular expression failed: %s.",
errbuf);
+ sfree (value);
continue;
}
- if (act->replacement != NULL)
+ if (act->replacement == NULL)
{
- meta_data_t *result;
+ /* no replacement; delete the key */
+ DEBUG ("target_replace plugin: tr_meta_data_action_invoke: "
+ "deleting `%s'", act->key);
+ meta_data_delete (*dest, act->key);
+ sfree (value);
+ continue;
+ }
- subst_status = subst (temp, sizeof (temp), value,
- (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
+ subst_status = subst (temp, sizeof (temp), value,
+ (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo, act->replacement);
+ if (subst_status == NULL)
+ {
+ ERROR ("Target `replace': subst (value = %s, start = %zu, end = %zu, "
+ "replacement = %s) failed.",
+ value, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
act->replacement);
- if (subst_status == NULL)
- {
- ERROR ("Target `replace': subst (value = %s, start = %zu, end = %zu, "
- "replacement = %s) failed.",
- value, (size_t) matches[0].rm_so, (size_t) matches[0].rm_eo,
- act->replacement);
- continue;
- }
-
- DEBUG ("target_replace plugin: tr_meta_data_action_invoke: `%s' "
- "value `%s' -> `%s'", act->key, value, temp);
-
- if ((result = meta_data_create()) == NULL)
- {
- ERROR ("Target `replace': failed to create metadata for `%s'.",
- act->key);
- return (-ENOMEM);
- }
-
- meta_data_status = meta_data_add_string (result, act->key, temp);
-
- if (meta_data_status != 0)
- {
- ERROR ("Target `replace': Unable to set metadata value for `%s'.",
- act->key);
- return (meta_data_status);
- }
-
- meta_data_clone_merge (dest, result);
- meta_data_destroy (result);
+ sfree (value);
+ continue;
}
- else /* no replacement; delete the key */
+
+ DEBUG ("target_replace plugin: tr_meta_data_action_invoke: `%s' "
+ "value `%s' -> `%s'", act->key, value, temp);
+
+ if ((result = meta_data_create()) == NULL)
{
- DEBUG ("target_replace plugin: tr_meta_data_action_invoke: "
- "deleting `%s'", act->key);
- meta_data_delete (*dest, act->key);
+ ERROR ("Target `replace': failed to create metadata for `%s'.",
+ act->key);
+ sfree (value);
+ return (-ENOMEM);
}
+
+ meta_data_status = meta_data_add_string (result, act->key, temp);
+ if (meta_data_status != 0)
+ {
+ ERROR ("Target `replace': Unable to set metadata value for `%s'.",
+ act->key);
+ meta_data_destroy (result);
+ sfree (value);
+ return (meta_data_status);
+ }
+
+ meta_data_clone_merge (dest, result);
+ meta_data_destroy (result);
+ sfree (value);
} /* for (act = act_head; act != NULL; act = act->next) */
return (0);