]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Fix org#2579 About incorrect JSON generated from empty Messages resource
authorEric Bollengier <eric@baculasystems.com>
Fri, 8 Jan 2021 10:27:18 +0000 (11:27 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 24 Mar 2022 08:02:58 +0000 (09:02 +0100)
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

bacula/src/console/bbconsjson.c
bacula/src/dird/bdirjson.c
bacula/src/filed/bfdjson.c
bacula/src/lib/bjson.c
bacula/src/lib/protos.h
bacula/src/stored/bsdjson.c

index 1964b50a48064e5ef5fe80023144d08bfe003f6f..239f758123358b3696eb43d6d83f517af2b7b673 100644 (file)
@@ -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);
                }
index 475e3ba43146636056248ec01dace6e8399b4ef2..f37d06ed1eb32dccaf590b054205ed5d70f4b48c 100644 (file)
@@ -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) {
index 0dd91b2735ab6fa6f5b40825cb9df94b6c371d2b..47f665506775afeca980c71b928c02cf26de7669 100644 (file)
@@ -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) {
index f81a880dfa5308b2e48a134931be61b2eb76ebbe..20bbcf940e1b7d5a739d2eca1dddb17e9c41d380 100644 (file)
@@ -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
 }
 
 /*
index 38b69ffba5eb29870596e47bb105d87656ec27fd..6d5c567080f39fa029af0bf87461f6b6494b2d14 100644 (file)
@@ -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 */
index 3e8f3a56a24f81cc6eece29cd4a0ec194a5f5a15..5981234aff2f4bed13f7af6f627194ee9ac5d831 100644 (file)
@@ -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) {