Skip to content

Commit

Permalink
config: Refactor escape_string function.
Browse files Browse the repository at this point in the history
The escape_string function used for pretty printing from now on has a
POOL_MEM as argument to store the escaped data in. It also now sizes the
buffer right based on the len argument given. This way the calling code
can be cleaned up quite a bit as it now does the same action multiple
times which leads to much more code maintenance.
  • Loading branch information
Marco van Wieringen committed Jul 28, 2016
1 parent 01cae66 commit cd2433e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 96 deletions.
61 changes: 26 additions & 35 deletions src/dird/dird_conf.c
Expand Up @@ -1231,13 +1231,9 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
if (bstrcasecmp(item->name, "runscript")) {
if (list != NULL) {
foreach_alist(runscript, list) {
int len;
POOLMEM *cmdbuf;
POOL_MEM esc;

len = strlen(runscript->command);
cmdbuf = get_pool_memory(PM_NAME);
cmdbuf = check_pool_memory_size(cmdbuf, len * 2);
escape_string(cmdbuf, runscript->command, len);
escape_string(esc, runscript->command, strlen(runscript->command));

/*
* Don't print runscript when its inherited from a JobDef.
Expand All @@ -1252,28 +1248,28 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
if (runscript->short_form) {
if (runscript->when == SCRIPT_Before && /* runbeforejob */
(bstrcmp(runscript->target, ""))) {
Mmsg(temp, "run before job = \"%s\"\n", cmdbuf);
Mmsg(temp, "run before job = \"%s\"\n", esc.c_str());
} else if (runscript->when == SCRIPT_After && /* runafterjob */
runscript->on_success &&
!runscript->on_failure &&
!runscript->fail_on_error &&
bstrcmp(runscript->target, "")) {
Mmsg(temp, "run after job = \"%s\"\n", cmdbuf);
Mmsg(temp, "run after job = \"%s\"\n", esc.c_str());
} else if (runscript->when == SCRIPT_After && /* client run after job */
runscript->on_success &&
!runscript->on_failure &&
!runscript->fail_on_error &&
!bstrcmp(runscript->target, "")) {
Mmsg(temp, "client run after job = \"%s\"\n", cmdbuf);
Mmsg(temp, "client run after job = \"%s\"\n", esc.c_str());
} else if (runscript->when == SCRIPT_Before && /* client run before job */
!bstrcmp(runscript->target, "")) {
Mmsg(temp, "client run before job = \"%s\"\n", cmdbuf);
Mmsg(temp, "client run before job = \"%s\"\n", esc.c_str());
} else if (runscript->when == SCRIPT_After && /* run after failed job */
runscript->on_failure &&
!runscript->on_success &&
!runscript->fail_on_error &&
bstrcmp(runscript->target, "")) {
Mmsg(temp, "run after failed job = \"%s\"\n", cmdbuf);
Mmsg(temp, "run after failed job = \"%s\"\n", esc.c_str());
}
indent_config_item(cfg_str, 1, temp.c_str());
} else {
Expand All @@ -1285,7 +1281,7 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
cmdstring = (char *)"console";
}

Mmsg(temp, "%s = \"%s\"\n", cmdstring, cmdbuf);
Mmsg(temp, "%s = \"%s\"\n", cmdstring, esc.c_str());
indent_config_item(cfg_str, 2, temp.c_str());

/*
Expand Down Expand Up @@ -1358,8 +1354,6 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)

indent_config_item(cfg_str, 1, "}\n");
}

free_pool_memory(cmdbuf);
}
} /* foreach runscript */
}
Expand Down Expand Up @@ -1786,12 +1780,6 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data)
POOL_MEM temp;
const char *p;

/* escape strings */
POOLMEM *buf;
int len;
buf = get_pool_memory(PM_NAME);


Dmsg0(200,"FILESETRES::print_config\n");

Mmsg(temp, "FileSet {\n");
Expand Down Expand Up @@ -2110,11 +2098,13 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data)
* File = entries.
*/
if (incexe->name_list.size()) {
char *entry;
POOL_MEM esc;

for (int l = 0; l < incexe->name_list.size(); l++) {
len = strlen((char*)incexe->name_list.get(l));
buf = check_pool_memory_size(buf, len * 2);
escape_string(buf, (char*)incexe->name_list.get(l), len);
Mmsg(temp, "File = \"%s\"\n", buf);
entry = (char *)incexe->name_list.get(l);
escape_string(esc, entry, strlen(entry));
Mmsg(temp, "File = \"%s\"\n", esc.c_str());
indent_config_item(cfg_str, 2, temp.c_str());
}
}
Expand All @@ -2123,11 +2113,13 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data)
* Plugin = entries.
*/
if (incexe->plugin_list.size()) {
char *entry;
POOL_MEM esc;

for (int l = 0; l < incexe->plugin_list.size(); l++) {
len = strlen((char*)incexe->plugin_list.get(l));
buf = check_pool_memory_size(buf, len * 2);
escape_string(buf, (char*)incexe->plugin_list.get(l), len);
Mmsg(temp, "Plugin = \"%s\"\n", buf);
entry = (char *)incexe->plugin_list.get(l);
escape_string(esc, entry, strlen(entry));
Mmsg(temp, "Plugin = \"%s\"\n", esc.c_str());
indent_config_item(cfg_str, 2, temp.c_str());
}
}
Expand Down Expand Up @@ -2158,13 +2150,14 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data)
INCEXE *incexe = exclude_items[j];

if (incexe->name_list.size()) {
indent_config_item(cfg_str, 1, "Exclude {\n");
char *entry;
POOL_MEM esc;

indent_config_item(cfg_str, 1, "Exclude {\n");
for (int k = 0; k < incexe->name_list.size(); k++) {
len = strlen((char*)incexe->name_list.get(k));
buf = check_pool_memory_size(buf, len * 2);
escape_string(buf, (char*)incexe->name_list.get(k), len);
Mmsg(temp, "File = \"%s\"\n", buf);
entry = (char *)incexe->name_list.get(k);
escape_string(esc, entry, strlen(entry));
Mmsg(temp, "File = \"%s\"\n", esc.c_str());
indent_config_item(cfg_str, 2, temp.c_str());
}

Expand All @@ -2173,8 +2166,6 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data)
} /* loop over all exclude blocks */
}

free_pool_memory(buf);

pm_strcat(cfg_str, "}\n\n");
pm_strcat(buff, cfg_str.c_str());

Expand Down
2 changes: 1 addition & 1 deletion src/lib/protos.h
Expand Up @@ -384,7 +384,7 @@ void set_tls_enable(TLS_CONTEXT *ctx, bool value);
bool get_tls_verify_peer(TLS_CONTEXT *ctx);

/* util.c */
void escape_string(char *snew, char *old, int len);
void escape_string(POOL_MEM &snew, char *old, int len);
bool is_buf_zero(char *buf, int len);
void lcase(char *str);
void bash_spaces(char *str);
Expand Down
28 changes: 12 additions & 16 deletions src/lib/res.c
Expand Up @@ -1388,8 +1388,6 @@ static inline void print_config_time(RES_ITEM *item, POOL_MEM &cfg_str)

bool MSGSRES::print_config(POOL_MEM &buff, bool hide_sensitive_data)
{
int len;
POOLMEM *cmdbuf;
POOL_MEM cfg_str; /* configuration as string */
POOL_MEM temp;
MSGSRES *msgres;
Expand All @@ -1401,31 +1399,29 @@ bool MSGSRES::print_config(POOL_MEM &buff, bool hide_sensitive_data)
Mmsg(temp, " %s = \"%s\"\n", "Name", msgres->name());
pm_strcat(cfg_str, temp.c_str());

cmdbuf = get_pool_memory(PM_NAME);
if (msgres->mail_cmd) {
len = strlen(msgres->mail_cmd);
cmdbuf = check_pool_memory_size(cmdbuf, len * 2);
escape_string(cmdbuf, msgres->mail_cmd, len);
Mmsg(temp, " MailCommand = \"%s\"\n", cmdbuf);
POOL_MEM esc;

escape_string(esc, msgres->mail_cmd, strlen(msgres->mail_cmd));
Mmsg(temp, " MailCommand = \"%s\"\n", esc.c_str());
pm_strcat(cfg_str, temp.c_str());
}

if (msgres->operator_cmd) {
len = strlen(msgres->operator_cmd);
cmdbuf = check_pool_memory_size(cmdbuf, len * 2);
escape_string(cmdbuf, msgres->operator_cmd, len);
Mmsg(temp, " OperatorCommand = \"%s\"\n", cmdbuf);
POOL_MEM esc;

escape_string(esc, msgres->operator_cmd, strlen(msgres->operator_cmd));
Mmsg(temp, " OperatorCommand = \"%s\"\n", esc.c_str());
pm_strcat(cfg_str, temp.c_str());
}

if (msgres->timestamp_format) {
len = strlen(msgres->timestamp_format);
cmdbuf = check_pool_memory_size(cmdbuf, len * 2);
escape_string(cmdbuf, msgres->timestamp_format, len);
Mmsg(temp, " TimestampFormat = \"%s\"\n", cmdbuf);
POOL_MEM esc;

escape_string(esc, msgres->timestamp_format, strlen(msgres->timestamp_format));
Mmsg(temp, " TimestampFormat = \"%s\"\n", esc.c_str());
pm_strcat(cfg_str, temp.c_str());
}
free_pool_memory(cmdbuf);

for (d = msgres->dest_chain; d; d = d->next) {
int nr_set = 0;
Expand Down

0 comments on commit cd2433e

Please sign in to comment.