From: Eric Bollengier Date: Fri, 8 Jan 2021 10:27:18 +0000 (+0100) Subject: Fix org#2579 About incorrect JSON generated from empty Messages resource X-Git-Tag: Release-11.3.2~931 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fbf9302a850076b3c6bd1d05284bdef922e39ccd;p=thirdparty%2Fbacula.git Fix org#2579 About incorrect JSON generated from empty Messages resource The broken JSON from bdirjson output is caused by defining in Messages resource message types with negation but without using 'All' keyword at the beginning of the message type list. It is: Append = /var/log/bacula/bacula.log = !Debug, !Saved, !Skipped Console = !Debug, !Saved, !Skipped instead of Append = /var/log/bacula/bacula.log = All, !Debug, !Saved, !Skipped Console = All, !Debug, !Saved, !Skipped --- diff --git a/bacula/src/console/bbconsjson.c b/bacula/src/console/bbconsjson.c index 1964b50a4..239f75812 100644 --- a/bacula/src/console/bbconsjson.c +++ b/bacula/src/console/bbconsjson.c @@ -380,8 +380,14 @@ static void dump_json(display_filter *filter) hpkt.ritem = &items[item]; if (bit_is_set(item, res_all.hdr.item_present)) { if (!first_directive) printf(","); - if (display_global_item(hpkt)) { + /* 1: found, 0: not found, -1 found but empty */ + int ret = display_global_item(hpkt); + if (ret == -1) { + /* Do not print a comma after this empty directive */ + first_directive = 0; + } else if (ret == 1) { /* Fall-through wanted */ + } else { printf("\n \"%s\": null", items[item].name); } diff --git a/bacula/src/dird/bdirjson.c b/bacula/src/dird/bdirjson.c index 475e3ba43..f37d06ed1 100644 --- a/bacula/src/dird/bdirjson.c +++ b/bacula/src/dird/bdirjson.c @@ -1036,10 +1036,18 @@ static void dump_json(display_filter *filter) } continue; } + if (first_directive++ > 0) { + sendit(NULL, ","); + } - if (first_directive++ > 0) sendit(NULL, ","); - if (display_global_item(hpkt)) { + /* 1: found, 0: not found, -1 found but empty */ + int ret = display_global_item(hpkt); + if (ret == -1) { + /* Do not print a comma after this empty directive */ + first_directive = 0; + } else if (ret == 1) { /* Fall-through wanted */ + } else if (items[item].handler == store_jobtype) { display_jobtype(hpkt); } else if (items[item].handler == store_label) { diff --git a/bacula/src/filed/bfdjson.c b/bacula/src/filed/bfdjson.c index 0dd91b273..47f665506 100644 --- a/bacula/src/filed/bfdjson.c +++ b/bacula/src/filed/bfdjson.c @@ -495,8 +495,14 @@ static void dump_json(display_filter *filter) hpkt.ritem = &items[item]; if (bit_is_set(item, res_all.hdr.item_present)) { if (first_directive++ > 0) printf(","); - if (display_global_item(hpkt)) { + /* 1: found, 0: not found, -1 found but empty */ + int ret = display_global_item(hpkt); + if (ret == -1) { + /* Do not print a comma after this empty directive */ + first_directive = 0; + } else if (ret == 1) { /* Fall-through wanted */ + } else if (items[item].handler == store_cipher_type) { display_cipher(hpkt); } else if (items[item].handler == store_digest_type) { diff --git a/bacula/src/lib/bjson.c b/bacula/src/lib/bjson.c index f81a880df..20bbcf940 100644 --- a/bacula/src/lib/bjson.c +++ b/bacula/src/lib/bjson.c @@ -203,10 +203,11 @@ void edit_msg_types(HPKT &hpkt, DEST *dest) pm_strcat(hpkt.edbuf, "]"); } -bool display_global_item(HPKT &hpkt) +/* -1 nothing displayed, 1 found, 0 not found */ +int display_global_item(HPKT &hpkt) { bool found = true; - + bool has_something = true; if (hpkt.ritem->handler == store_res) { display_res(hpkt); } else if (hpkt.ritem->handler == store_str || @@ -227,24 +228,28 @@ bool display_global_item(HPKT &hpkt) } else if (hpkt.ritem->handler == store_bool) { display_bool_pair(hpkt); } else if (hpkt.ritem->handler == store_msgs) { - display_msgs(hpkt); + has_something = display_msgs(hpkt); } else if (hpkt.ritem->handler == store_bit) { display_bit_pair(hpkt); } else if (hpkt.ritem->handler == store_alist_res) { - found = display_alist_res(hpkt); /* In some cases, the list is null... */ + has_something = display_alist_res(hpkt); /* In some cases, the list is null... */ } else if (hpkt.ritem->handler == store_alist_str) { - found = display_alist_str(hpkt); /* In some cases, the list is null... */ + has_something = display_alist_str(hpkt); /* In some cases, the list is null... */ } else { found = false; } - return found; + if (found) { + return has_something ? 1 : -1; + } else { + return 0; + } } /* * Called here for each store_msgs resource */ -void display_msgs(HPKT &hpkt) +bool display_msgs(HPKT &hpkt) { MSGS *msgs = (MSGS *)hpkt.ritem->value; /* Message res */ DEST *dest; /* destination chain */ @@ -293,9 +298,12 @@ void display_msgs(HPKT &hpkt) sendit(NULL, " \"Command\": %s\n }", quote_string(hpkt.edbuf, dest->mail_cmd)); break; + default: + Dmsg1(50, "got %d\n", hpkt.ritem->code); } } } + return (first == false); // We found nothing to display } /* diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index 38b69ffba..6d5c56708 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -48,7 +48,7 @@ int base64_to_bin(char *dest, int destlen, char *src, int srclen); void strip_long_opts(char *out, const char *in); void edit_alist(HPKT &hpkt); void edit_msg_types(HPKT &hpkt, DEST *dest); -void display_msgs(HPKT &hpkt); +bool display_msgs(HPKT &hpkt); void display_alist(HPKT &hpkt); bool display_alist_str(HPKT &hpkt); bool display_alist_res(HPKT &hpkt); @@ -63,7 +63,7 @@ void display_bit_array(char *array, int num); void display_last(HPKT &hpkt); void init_hpkt(HPKT &hpkt); void term_hpkt(HPKT &hpkt); -bool display_global_item(HPKT &hpkt); +int display_global_item(HPKT &hpkt); // void display_collector_types(HPKT &hpkt); /* bsys.c */ diff --git a/bacula/src/stored/bsdjson.c b/bacula/src/stored/bsdjson.c index 3e8f3a56a..5981234af 100644 --- a/bacula/src/stored/bsdjson.c +++ b/bacula/src/stored/bsdjson.c @@ -465,8 +465,15 @@ static void dump_json(display_filter *filter) hpkt.ritem = &items[item]; if (bit_is_set(item, res_all.hdr.item_present)) { if (first_directive++ > 0) printf(","); - if (display_global_item(hpkt)) { + + /* 1: found, 0: not found, -1 found but empty */ + int ret = display_global_item(hpkt); + if (ret == -1) { + /* Do not print a comma after this empty directive */ + first_directive = 0; + } else if (ret == 1) { /* Fall-through wanted */ + } else if (items[item].handler == store_maxblocksize) { display_int32_pair(hpkt); } else if (items[item].handler == store_devtype) {