#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"
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;
case 'o':
output_filename = optarg;
break;
+ case 'j':
+ just_check = true;
+ break;
}
}
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);