691ea20d5f53b43c8765a3ae8f86925458e6743f
[platform/core/system/libdbuspolicy.git] / src / dbuspolicy_printer.cpp
1 #include "internal/print_content.hpp"
2 #include "internal/storage_backend_serialized.hpp"
3 #include <iostream>
4 #include <getopt.h>
5 #include "libdbuspolicy1-private.h"
6
7 using namespace std;
8
9 static const struct option options[] = {
10         {"system", no_argument, 0, 0 },
11         {"session", no_argument, 0, 0},
12         {0, 0, 0, 0}
13 };
14
15 static void print_help(const char *name) {
16         cout << endl;
17         cout << "usage: " << name << " [-i serialized filename] [-v]" << endl;
18         cout << "       " << name << " {--system|--session} [-v]" << endl;
19         cout << " -v - just verify, don't print anything" << endl;
20         cout << endl;
21 }
22
23 int main(int argc, char *argv[]) {
24         std::string input_filename;
25         int c;
26         bool just_verify = false;
27
28         if (argc < 2) {
29                 print_help(argv[0]);
30                 return EXIT_FAILURE;
31         }
32
33         while (1) {
34                 int option_index;
35                 c = getopt_long(argc, argv, "i:v", options, &option_index);
36                 if (c == -1)
37                         break;
38
39                 switch(c) {
40                 case 0:
41                         if (option_index == 0)
42                                 input_filename = system_bus_conf_file_primary();
43                         else
44                                 input_filename = session_bus_conf_file_primary();
45                         input_filename.append(".serialized");
46                         break;
47                 case 'i':
48                         input_filename = optarg;
49                         break;
50                 case 'v':
51                         just_verify = true;
52                         break;
53                 case '?':
54                         print_help(argv[0]);
55                         return EXIT_FAILURE;
56                 }
57         }
58
59         if (optind < argc)
60                 input_filename = argv[optind];
61
62         if (input_filename.empty()) {
63                 cout << "No input filename" << endl;
64                 print_help(argv[0]);
65                 return EXIT_FAILURE;
66         }
67
68         tslog::init(tslog::ldp_log_level::DEFAULT);
69
70         ldp_serialized::StorageBackendSerialized storage;
71
72         if (!storage.init(input_filename.c_str(), true)) {
73                 if (just_verify)
74                         cout << input_filename << ": FAILED" << endl;
75                 return EXIT_FAILURE;
76         }
77
78         if (just_verify) {
79                 cout << input_filename << ": OK" << endl;
80                 return EXIT_SUCCESS;
81         }
82
83         storage.printContent();
84
85         return EXIT_SUCCESS;
86 }