9 #include "internal/serializer.hpp"
10 #include "dbuspolicy1/libdbuspolicy1.h"
11 #include "libdbuspolicy1-private.h"
13 #include <openssl/sha.h>
17 static const struct option options[] = {
18 {"system", no_argument, 0, 0 },
19 {"session", no_argument, 0, 0},
23 static void print_help(const char *name) {
25 cout << "usage: " << name << " [-j] [-o output_filename] <input_filename>" << endl;
26 cout << " " << name << " {--system|--session} [-j] [-o output_filename]" << endl;
28 cout << "If not specified, output_filename is input_filename.serialized" << endl;
29 cout << " -j - don't write anything, just check if the output file is valid and up to date" << endl;
33 static int check(const char *input_filename, const std::string &output_filename) {
36 cout << "Read from: " << input_filename << ", checking " << output_filename << "..." << endl;
37 ldp_serializer::Serializer serializer;
39 serializer.serialize(input_filename, output);
41 ifstream serialized(output_filename, ifstream::binary|ifstream::ate);
43 if (serialized && output.tellp() == serialized.tellg()) {
44 serialized.seekg(0, ifstream::beg);
46 ok = equal(istreambuf_iterator<char>(serialized),
47 istreambuf_iterator<char>(),
48 output.str().begin());
50 cout << output_filename << " is " << (ok ? "valid and up to date" : "not updated or invalid") << endl;
51 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
54 int main(int argc, char *argv[])
56 bool need_input_filename = true;
57 bool just_check = false;
58 std::string output_filename;
59 const char *input_filename = system_bus_conf_file_primary();
64 c = getopt_long(argc, argv, "o:j", options, &option_index);
70 if (option_index == 1)
71 input_filename = session_bus_conf_file_primary();
72 need_input_filename = false;
75 output_filename = optarg;
83 if (need_input_filename) {
85 input_filename = argv[optind];
92 if (output_filename.empty())
93 output_filename = std::string(input_filename) + ".serialized";
96 return check(input_filename, output_filename);
98 cout << "Read from: " << input_filename << " write to: " << output_filename << endl;
99 ldp_serializer::Serializer serializer;
100 ofstream output(output_filename, ofstream::binary);
102 uint8_t *data = serializer.serialize(input_filename, output);
105 cout << "Write FAILED." << endl;
109 cout << "Write " << output.tellp() << " bytes" << endl;
111 array<unsigned char, SHA_DIGEST_LENGTH> digest;
112 string sha1_filename = output_filename + ".sha1";
113 ofstream output_sha1(sha1_filename);
115 SHA1(data, output.tellp(), digest.data());
118 for_each(digest.begin(), digest.end(), [&](uint16_t c) { output_sha1 << setw(2) << setfill('0') << c; });
119 output_sha1 << " *" << output_filename << endl;
121 cout << "SHA1 written to " << sha1_filename << endl;