]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Using json-c instead of rapidjson
authorMartin Vidner <mvidner@suse.cz>
Wed, 11 Dec 2019 13:54:58 +0000 (14:54 +0100)
committerMartin Vidner <mvidner@suse.cz>
Tue, 17 Dec 2019 10:22:26 +0000 (11:22 +0100)
https://github.com/json-c/json-c
Already in SLE, is also a dependency for systemd via libcryptsetup.

Dockerfile.debian
Dockerfile.fedora
Dockerfile.leap
Dockerfile.tumbleweed
Dockerfile.ubuntu
configure.ac
snapper.spec.in
zypp-plugin/Makefile.am
zypp-plugin/snapper_zypp_plugin.cc
zypp-plugin/testsuite/4-badjson.test

index 8e6a89bd128364a6b2632a8dc8b5007debdca135..2ec2293000706159df9d26cf8e12e8a3bec055aa 100644 (file)
@@ -21,13 +21,13 @@ RUN apt-get update && \
   libboost-test-dev \
   libboost-thread-dev \
   libdbus-1-dev \
+  libjson-c-dev \
   libmount-dev \
   libpam-dev \
   libtool \
   libxml2-dev \
   libz-dev \
   locales-all \
-  rapidjson-dev \
   ruby-dbus \
   xsltproc
 
index ab56e3e74ba308c9325cb3387805d88b9a65af79..5bbe3211ef22e7262087573dd798db1670c92470 100644 (file)
@@ -12,6 +12,7 @@ RUN dnf -y install \
   gettext \
   glibc-langpack-de \
   glibc-langpack-en \
+  json-c-devel \
   libacl-devel \
   libmount-devel \
   libtool \
@@ -20,7 +21,6 @@ RUN dnf -y install \
   make \
   pam-devel \
   pkgconfig \
-  rapidjson-devel \
   rpm-build
 
 RUN mkdir -p /usr/src/app
index 5596a63dcc14740715f4f96b8e6f563e300c78e6..76f0f5d8c7917cb1d8fdeaf9f11f13b7e8d63596 100644 (file)
@@ -22,13 +22,13 @@ RUN RUBY_VERSION=ruby:`rpm --eval '%{rb_ver}'` && \
   libboost_test-devel \
   libboost_thread-devel \
   libbtrfs-devel \
+  libjson-c-devel \
   libmount-devel \
   libtool \
   libxml2-devel \
   libxslt \
   obs-service-source_validator \
   pam-devel \
-  rapidjson-devel \
   rpm-build \
   "rubygem($RUBY_VERSION:ruby-dbus)" \
   which
index 187ab754fdd9df0472e986be7f5c84eccdbc4b17..ea4cf4f6a971389ddbf4320ded70a5fc9ec21bca 100644 (file)
@@ -22,13 +22,13 @@ RUN RUBY_VERSION=ruby:`rpm --eval '%{rb_ver}'` && \
   libboost_test-devel \
   libboost_thread-devel \
   libbtrfs-devel \
+  libjson-c-devel \
   libmount-devel \
   libtool \
   libxml2-devel \
   libxslt \
   obs-service-source_validator \
   pam-devel \
-  rapidjson-devel \
   rpm-build \
   "rubygem($RUBY_VERSION:ruby-dbus)" \
   which
index 0cc6bdee0d1c1f71e1569e62195b60d692d97aaa..7f566f31da0653e843abf3f762e70d5abacaea20 100644 (file)
@@ -23,12 +23,12 @@ RUN apt-get update && \
   libboost-test-dev \
   libboost-thread-dev \
   libdbus-1-dev \
+  libjson-c-dev \
   libmount-dev \
   libpam-dev \
   libtool \
   libxml2-dev \
   libz-dev \
-  rapidjson-dev \
   ruby-dbus \
   xsltproc
 
index 4379a766f60b9c2aa23e612fe57341ec7acec097..0558ce42cf9781c9cfba2345788dac94361770ac 100644 (file)
@@ -148,6 +148,9 @@ fi
 
 PKG_CHECK_MODULES(DBUS, dbus-1)
 PKG_CHECK_MODULES(XML2, libxml-2.0)
+if test "x$with_zypp" = "xyes"; then
+   PKG_CHECK_MODULES(JSONC, json-c)
+fi
 
 AC_CHECK_HEADER(acl/libacl.h,[],[AC_MSG_ERROR([Cannout find libacl headers. Please install libacl-devel])])
 
index 553e5ded55eb4edce88f75cfbc5fc5eafcea27a7..dcdae40216baf33ab096d4db85dc7a01bb8612cd 100644 (file)
@@ -72,7 +72,11 @@ BuildRequires:  xsltproc
 BuildRequires:  libzypp(plugin:commit)
 %endif
 BuildRequires:  pam-devel
-BuildRequires:  rapidjson-devel
+%if 0%{?fedora_version}
+BuildRequires:  json-c-devel
+%else
+BuildRequires:  libjson-c-devel
+%endif
 Requires:       diffutils
 Requires:       libsnapper@LIBVERSION_MAJOR@ = %version
 %if 0%{?suse_version}
index 656873348da865b07f87b3651b842d06521b488b..7ce5339cc545a73ae9a0e6138d24a7a3140fa585 100644 (file)
@@ -5,7 +5,7 @@ if HAVE_ZYPP
 plugindir = /usr/lib/zypp/plugins/commit
 plugin_PROGRAMS = snapper-zypp-plugin
 
-AM_CPPFLAGS = $(DBUS_CFLAGS) $(XML2_CFLAGS)
+AM_CPPFLAGS = $(DBUS_CFLAGS) $(XML2_CFLAGS) $(JSONC_CFLAGS)
 AM_CXXFLAGS = -Wsuggest-override
 snapper_zypp_plugin_SOURCES = \
     snapper_zypp_plugin.cc \
@@ -15,6 +15,7 @@ snapper_zypp_plugin_LDFLAGS = \
     ../client/libclient.la \
     ../snapper/libsnapper.la \
     ../dbus/libdbus.la \
+    $(JSONC_LIBS) \
     -lboost_regex
 
 endif
index 28aa27892a3e5b268924c762ea1cc5eb7cfe2218..9f27a90113a0f92756aefda7c73d13e9f406168c 100644 (file)
 #include <string>
 using namespace std;
 
-#include <rapidjson/document.h>
+#include <json.h>
+// a collision with client/errors.h
+#ifdef error_description
+#undef error_description
+#endif
 
 #include "dbus/DBusConnection.h"
 #include "snapper/Exception.h"
@@ -315,29 +319,64 @@ map<string, string> SnapperZyppPlugin::get_userdata(const Message& msg) {
     return result;
 }
 
+static
+json_object * object_get(json_object * obj, const char * name) {
+    json_object * result;
+    if (!json_object_object_get_ex(obj, name, &result)) {
+       cerr << "ERROR:" << '"' << name << "\" not found" << endl;
+       return NULL;
+    }
+    return result;
+}
+
 set<string> SnapperZyppPlugin::get_solvables(const Message& msg, Phase phase) {
     set<string> result;
 
-    rapidjson::Document doc;
-    const char * c_body = msg.body.c_str();
-    cerr << "DEBUG:" << "parsing zypp JSON: " << c_body << endl;
-    if (doc.Parse(c_body).HasParseError()) {
-       cerr << "ERROR:" << "parsing zypp JSON failed" << endl;
+    json_tokener * tok = json_tokener_new();
+    json_object * zypp = json_tokener_parse_ex(tok, msg.body.c_str(), msg.body.size());
+    json_tokener_error jerr = json_tokener_get_error(tok);
+    if (jerr != json_tokener_success) {
+       cerr << "ERROR:" << "parsing zypp JSON failed: "
+                        << json_tokener_error_desc(jerr) << endl;
        return result;
     }
+
+    // JSON structure:
+    // {"TransactionStepList":[{"type":"?","stage":"?","solvable":{"n":"mypackage"}}]}
     // https://doc.opensuse.org/projects/libzypp/SLE12SP2/plugin-commit.html
-    using rapidjson::Value;
-    const Value& steps = doc["TransactionStepList"];
-    for (Value::ConstValueIterator it = steps.Begin(); it != steps.End(); ++it) {
-       const Value& step = *it;
-       if (step.HasMember("type")) {
-           if (phase == Phase::BEFORE || step.HasMember("stage")) {
-               const Value& solvable = step["solvable"];
-               const Value& name = solvable["n"];
-               // FIXME: what happens when the doc structure is different?
-               result.insert(name.GetString());
-           }
-       }
+    json_object * steps = object_get(zypp, "TransactionStepList");
+    if (!steps)
+        return result;
+
+    if (json_object_get_type(steps) == json_type_array) {
+        size_t i, len = json_object_array_length(steps);
+        printf("steps: %zu\n", len);
+        for (i = 0; i < len; ++i) {
+            json_object * step = json_object_array_get_idx(steps, i);
+            bool have_type = json_object_object_get_ex(step, "type", NULL);
+            bool have_stage = json_object_object_get_ex(step, "stage", NULL);
+            if (have_type && (phase == Phase::BEFORE || have_stage)) {
+                json_object * solvable = object_get(step, "solvable");
+                if (!solvable) {
+                    cerr << "ERROR:" << "in item #" << i << endl;
+                    continue;
+                }
+                json_object * name = object_get(solvable, "n");
+                if (!name) {
+                    cerr << "ERROR:" << "in item #" << i << endl;
+                    continue;
+                }
+                if (json_object_get_type(name) != json_type_string) {
+                    cerr << "ERROR:" << "\"n\" is not a string" << endl;
+                    cerr << "ERROR:" << "in item #" << i << endl;
+                    continue;
+                }
+                else {
+                    const char * prize = json_object_get_string(name);
+                   result.insert(prize);
+                }
+            }
+        }
     }
 
     return result;
index 71333e8c4c769ebd598757351888ba16377a05ef..78744e90a3873fb1dca50e6904559f5911bb36c8 100755 (executable)
@@ -5,10 +5,8 @@ set -u
 
 . test-helper.sh
 
-mock_snapperd_setup || { echo "1..0 # SKIP"; exit; }
-
 # http://testanything.org/
-echo 1..1
+echo 1..2
 
 test_bad_json() {
     local JSON=("${1?JSON payload expected}")
@@ -19,10 +17,18 @@ test_bad_json() {
     stomp_message PLUGINEND "" ""
 }
 
-TEST="1 - It complains about bad JSON"
+TEST="1 - It complains about malformed JSON"
+STDERR=$(test_bad_json '{ malformed.1415926 ]' | runit "" 2>&1 >/dev/null || :)
+echo "$STDERR"
+if [[ ! "$STDERR" =~ parsing.*JSON.failed ]]; then
+    echo -n "not "
+fi
+echo "ok $TEST"
+
+TEST="2 - It complains about invalid JSON"
 STDERR=$(test_bad_json '{"well-formed": "but-invalid"}' | runit "" 2>&1 >/dev/null || :)
 echo "$STDERR"
-if [[ ! "$STDERR" =~ rapidjson.*Assertion.*failed ]]; then
+if [[ ! "$STDERR" =~ TransactionStepList.*not.found ]]; then
     echo -n "not "
 fi
 echo "ok $TEST"