forked from ClusterLabs/pacemaker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml_diff.c
233 lines (199 loc) · 7.21 KB
/
xml_diff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <crm_internal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <crm/crm.h>
#include <crm/msg_xml.h>
#include <crm/common/xml.h>
#include <crm/common/ipc.h>
#include <crm/cib.h>
/* *INDENT-OFF* */
static struct crm_option long_options[] = {
/* Top-level Options */
{"help", 0, 0, '?', "\t\tThis text"},
{"version", 0, 0, '$', "\t\tVersion information" },
{"verbose", 0, 0, 'V', "\t\tIncrease debug output\n"},
{"-spacer-", 1, 0, '-', "\nOriginal XML:"},
{"original", 1, 0, 'o', "\tXML is contained in the named file"},
{"original-string", 1, 0, 'O', "XML is contained in the supplied string"},
{"-spacer-", 1, 0, '-', "\nOperation:"},
{"new", 1, 0, 'n', "\tCompare the original XML to the contents of the named file"},
{"new-string", 1, 0, 'N', "\tCompare the original XML to the contents of the supplied string"},
{"patch", 1, 0, 'p', "\tPatch the original XML with the contents of the named file"},
{"-spacer-", 1, 0, '-', "\nAdditional Options:"},
{"cib", 0, 0, 'c', "\t\tCompare/patch the inputs as a CIB (includes versions details)"},
{"filter", 0, 0, 'f', "\t\tSuppress irrelevant differences between the two inputs"},
{"stdin", 0, 0, 's', NULL, 1},
{"-spacer-", 1, 0, '-', "\nExamples:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', "Obtain the two different configuration files by running cibadmin on the two cluster setups to compare:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " cibadmin --query > cib-old.xml", pcmk_option_example},
{"-spacer-", 1, 0, '-', " cibadmin --query > cib-new.xml", pcmk_option_example},
{"-spacer-", 1, 0, '-', "Calculate and save the difference between the two files:", pcmk_option_paragraph},
{"-spacer-", 1, 0, '-', " crm_diff --original cib-old.xml --new cib-new.xml > patch.xml", pcmk_option_example },
{"-spacer-", 1, 0, '-', "Apply the patch to the original file:", pcmk_option_paragraph },
{"-spacer-", 1, 0, '-', " crm_diff --original cib-old.xml --patch patch.xml > updated.xml", pcmk_option_example },
{"-spacer-", 1, 0, '-', "Apply the patch to the running cluster:", pcmk_option_paragraph },
{"-spacer-", 1, 0, '-', " cibadmin --patch patch.xml", pcmk_option_example },
{0, 0, 0, 0}
};
/* *INDENT-ON* */
int
main(int argc, char **argv)
{
gboolean apply = FALSE;
gboolean raw_1 = FALSE;
gboolean raw_2 = FALSE;
gboolean filter = FALSE;
gboolean use_stdin = FALSE;
gboolean as_cib = FALSE;
int argerr = 0;
int flag;
xmlNode *object_1 = NULL;
xmlNode *object_2 = NULL;
xmlNode *output = NULL;
const char *xml_file_1 = NULL;
const char *xml_file_2 = NULL;
int option_index = 0;
crm_log_cli_init("crm_diff");
crm_set_options(NULL, "original_xml operation [options]", long_options,
"A utility for comparing Pacemaker configurations (XML format)\n\n"
"The tool produces a custom (diff-like) output which it can also apply like a patch\n");
if (argc < 2) {
crm_help('?', EX_USAGE);
}
while (1) {
flag = crm_get_option(argc, argv, &option_index);
if (flag == -1)
break;
switch (flag) {
case 'o':
xml_file_1 = optarg;
break;
case 'O':
xml_file_1 = optarg;
raw_1 = TRUE;
break;
case 'n':
xml_file_2 = optarg;
break;
case 'N':
xml_file_2 = optarg;
raw_2 = TRUE;
break;
case 'p':
xml_file_2 = optarg;
apply = TRUE;
break;
case 'f':
filter = TRUE;
break;
case 's':
use_stdin = TRUE;
break;
case 'c':
as_cib = TRUE;
break;
case 'V':
crm_bump_log_level(argc, argv);
break;
case '?':
case '$':
crm_help(flag, EX_OK);
break;
default:
printf("Argument code 0%o (%c)" " is not (?yet?) supported\n", flag, flag);
++argerr;
break;
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
if (optind > argc) {
++argerr;
}
if (argerr) {
crm_help('?', EX_USAGE);
}
if (raw_1) {
object_1 = string2xml(xml_file_1);
} else if (use_stdin) {
fprintf(stderr, "Input first XML fragment:");
object_1 = stdin2xml();
} else if (xml_file_1 != NULL) {
object_1 = filename2xml(xml_file_1);
}
if (raw_2) {
object_2 = string2xml(xml_file_2);
} else if (use_stdin) {
fprintf(stderr, "Input second XML fragment:");
object_2 = stdin2xml();
} else if (xml_file_2 != NULL) {
object_2 = filename2xml(xml_file_2);
}
if (object_1 == NULL) {
fprintf(stderr, "Could not parse the first XML fragment");
return 1;
}
if (object_2 == NULL) {
fprintf(stderr, "Could not parse the second XML fragment");
return 1;
}
if (apply) {
if (as_cib == FALSE) {
apply_xml_diff(object_1, object_2, &output);
} else {
apply_cib_diff(object_1, object_2, &output);
}
} else {
if (as_cib == FALSE) {
output = diff_xml_object(object_1, object_2, filter);
} else {
output = diff_cib_object(object_1, object_2, filter);
}
log_xml_diff(LOG_NOTICE, output, " ");
}
if (output != NULL) {
char *buffer = dump_xml_formatted(output);
fprintf(stdout, "%s\n", crm_str(buffer));
free(buffer);
fflush(stdout);
if (apply) {
const char *version = crm_element_value(output, XML_ATTR_CRM_VERSION);
buffer = calculate_xml_versioned_digest(output, FALSE, TRUE, version);
crm_trace("Digest: %s\n", crm_str(buffer));
free(buffer);
}
}
free_xml(object_1);
free_xml(object_2);
free_xml(output);
if (apply == FALSE && output != NULL) {
return 1;
}
return 0;
}