serializer: add option for checking for updates 26/203526/2
authorAdrian Szyndela <adrian.s@samsung.com>
Mon, 15 Apr 2019 11:47:37 +0000 (13:47 +0200)
committerAdrian Szyndela <adrian.s@samsung.com>
Tue, 16 Apr 2019 09:59:53 +0000 (11:59 +0200)
Developers may need to check if they already updated their
serialized policy file. This adds -j option which does not write
anything to the output file, but rather just checks if the generated
file would have the same contents as an existing output file.

Change-Id: I28ab3e992416d25c04206279275f007c0ca08919

src/dbuspolicy_serializer.cpp

index 7ca1c41..a575c6c 100644 (file)
@@ -1,8 +1,10 @@
 #include <unistd.h>
 #include <getopt.h>
 
+#include <algorithm>
 #include <iostream>
 #include <fstream>
+#include <sstream>
 #include "internal/serializer.hpp"
 #include "internal/naive_policy_checker.hpp"
 #include "dbuspolicy1/libdbuspolicy1.h"
@@ -17,23 +19,46 @@ static const struct option options[] = {
 
 static void print_help(const char *name) {
        cout << endl;
-       cout << "usage: " << name << " [-o output_filename] <input_filename>" << endl;
-       cout << "       " << name << " {--system|--session} [-o output_filename]" << endl;
+       cout << "usage: " << name << " [-j] [-o output_filename] <input_filename>" << endl;
+       cout << "       " << name << " {--system|--session} [-j] [-o output_filename]" << endl;
        cout << endl;
        cout << "If not specified, output_filename is input_filename.serialized" << endl;
+       cout << " -j - don't write anything, just check if the output file is valid and up to date" << endl;
        cout << endl;
 }
 
+static int check(const char *input_filename, const std::string &output_filename) {
+       bool ok = false;
+
+       cout << "Read from: " << input_filename << ", checking " << output_filename << "..." << endl;
+       ldp_xml_parser::Serializer serializer;
+       ostringstream output;
+       serializer.serialize(input_filename, output);
+
+       ifstream serialized(output_filename, ifstream::binary|ifstream::ate);
+
+       if (serialized && output.tellp() == serialized.tellg()) {
+               serialized.seekg(0, ifstream::beg);
+
+               ok = equal(istreambuf_iterator<char>(serialized),
+                                  istreambuf_iterator<char>(),
+                                  output.str().begin());
+       }
+       cout << output_filename << " is " << (ok ? "valid and up to date" : "not updated or invalid") << endl;
+       return ok ? 0 : 1;
+}
+
 int main(int argc, char *argv[])
 {
        bool need_input_filename = true;
+       bool just_check = false;
        std::string output_filename;
        const char *input_filename = system_bus_conf_file_primary();
        int c;
 
        while (1) {
                int option_index;
-               c = getopt_long(argc, argv, "o:", options, &option_index);
+               c = getopt_long(argc, argv, "o:j", options, &option_index);
                if (c == -1)
                        break;
 
@@ -46,6 +71,9 @@ int main(int argc, char *argv[])
                case 'o':
                        output_filename = optarg;
                        break;
+               case 'j':
+                       just_check = true;
+                       break;
                }
        }
 
@@ -61,6 +89,9 @@ int main(int argc, char *argv[])
        if (output_filename.empty())
                output_filename = std::string(input_filename) + ".serialized";
 
+       if (just_check)
+               return check(input_filename, output_filename);
+
        cout << "Read from: " << input_filename << " write to: " << output_filename << endl;
        ldp_xml_parser::Serializer serializer;
        ofstream output(output_filename, ofstream::binary);