1 /* Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <getopt.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "knot/catalog/catalog_db.h"
22 #include "utils/common/params.h"
23
24 #define PROGRAM_NAME "kcatalogprint"
25
print_help(void)26 static void print_help(void)
27 {
28 printf("Usage: %s [parameters] <catalog_dir>\n"
29 "\n"
30 "Parameters:\n"
31 " -h, --help Print the program help.\n"
32 " -V, --version Print the program version.\n",
33 PROGRAM_NAME);
34 }
35
print_dname(const knot_dname_t * d)36 static void print_dname(const knot_dname_t *d)
37 {
38 knot_dname_txt_storage_t tmp;
39 knot_dname_to_str(tmp, d, sizeof(tmp));
40 printf("%s ", tmp);
41 }
42
catalog_print_cb(const knot_dname_t * mem,const knot_dname_t * ow,const knot_dname_t * cz,const char * group,void * ctx)43 static int catalog_print_cb(const knot_dname_t *mem, const knot_dname_t *ow,
44 const knot_dname_t *cz, const char *group, void *ctx)
45 {
46 print_dname(mem);
47 print_dname(ow);
48 print_dname(cz);
49 printf("%s\n", group);
50 (*(ssize_t *)ctx)++;
51 return KNOT_EOK;
52 }
53
catalog_print(catalog_t * cat)54 static void catalog_print(catalog_t *cat)
55 {
56 ssize_t total = 0;
57
58 printf(";; <member zone> <record owner> <catalog zone> <group>\n");
59
60 if (cat != NULL) {
61 int ret = catalog_open(cat);
62 if (ret == KNOT_EOK) {
63 ret = catalog_apply(cat, NULL, catalog_print_cb, &total, false);
64 }
65 if (ret != KNOT_EOK) {
66 printf("Catalog print failed (%s)\n", knot_strerror(ret));
67 return;
68 }
69 }
70
71 printf("Total records: %zd\n", total);
72 }
73
main(int argc,char * argv[])74 int main(int argc, char *argv[])
75 {
76 struct option options[] = {
77 { "help", no_argument, NULL, 'h' },
78 { "version", no_argument, NULL, 'V' },
79 { NULL }
80 };
81
82 int opt = 0;
83 while ((opt = getopt_long(argc, argv, "hV", options, NULL)) != -1) {
84 switch (opt) {
85 case 'h':
86 print_help();
87 return EXIT_SUCCESS;
88 case 'V':
89 print_version(PROGRAM_NAME);
90 return EXIT_SUCCESS;
91 default:
92 print_help();
93 return EXIT_FAILURE;
94 }
95 }
96
97 if (argc != 2) {
98 print_help();
99 return EXIT_FAILURE;
100 }
101
102 catalog_t c = { { 0 } };
103
104 catalog_init(&c, argv[1], 0); // mapsize grows automatically
105 catalog_print(&c);
106 if (catalog_deinit(&c) != KNOT_EOK) {
107 return EXIT_FAILURE;
108 }
109
110 return EXIT_SUCCESS;
111 }
112