13 #include <sys/types.h>
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
23 #include <libxml/xmlIO.h>
32 #define XML_BUFFER_SIZE 4096
33 #define XML_PARSER_DEBUG 0
44 #define PCMK__XML_PARSE_OPTS (XML_PARSE_NOBLANKS | XML_PARSE_RECOVER)
51 typedef struct xml_deleted_obj_s {
58 static filter_t filter[] = {
67 static xmlNode *subtract_xml_comment(xmlNode * parent, xmlNode * left, xmlNode * right, gboolean * changed);
68 static xmlNode *find_xml_comment(xmlNode * root, xmlNode * search_comment, gboolean exact);
69 static int add_xml_comment(xmlNode * parent, xmlNode * target, xmlNode * update);
71 #define CHUNK_SIZE 1024
76 if(xml == NULL || xml->doc == NULL || xml->doc->_private == NULL) {
80 }
else if (lazy && is_not_set(((
xml_private_t *)xml->doc->_private)->flags,
87 #define buffer_print(buffer, max, offset, fmt, args...) do { \
90 rc = snprintf((buffer) + (offset), (max) - (offset), fmt, ##args); \
92 if(buffer && rc < 0) { \
93 crm_perror(LOG_ERR, "snprintf failed at offset %d", offset); \
94 (buffer)[(offset)] = 0; \
96 } else if(rc >= ((max) - (offset))) { \
98 (max) = QB_MAX(CHUNK_SIZE, (max) * 2); \
99 tmp = realloc_safe((buffer), (max)); \
109 insert_prefix(
int options,
char **buffer,
int *offset,
int *max,
int depth)
112 size_t spaces = 2 * depth;
114 if ((*buffer) == NULL || spaces >= ((*max) - (*offset))) {
116 (*buffer) = realloc_safe((*buffer), (*max));
118 memset((*buffer) + (*offset),
' ', spaces);
124 set_parent_flag(xmlNode *xml,
long flag)
127 for(; xml; xml = xml->parent) {
143 if(xml && xml->doc && xml->doc->_private){
153 __xml_node_dirty(xmlNode *xml)
160 __xml_node_clean(xmlNode *xml)
162 xmlNode *cIter = NULL;
169 for (cIter = __xml_first_child(xml); cIter != NULL; cIter = __xml_next(cIter)) {
170 __xml_node_clean(cIter);
175 crm_node_created(xmlNode *xml)
177 xmlNode *cIter = NULL;
183 __xml_node_dirty(xml);
186 for (cIter = __xml_first_child(xml); cIter != NULL; cIter = __xml_next(cIter)) {
187 crm_node_created(cIter);
195 xmlNode *parent = a->parent;
204 __xml_node_dirty(parent);
207 int get_tag_name(
const char *input,
size_t offset,
size_t max);
208 int get_attr_name(
const char *input,
size_t offset,
size_t max);
212 static int add_xml_object(xmlNode * parent, xmlNode * target, xmlNode * update, gboolean as_diff);
214 #define XML_PRIVATE_MAGIC (long) 0x81726354
217 __xml_deleted_obj_free(
void *
data)
222 free(deleted_obj->path);
242 g_list_free_full(p->
deleted_objs, __xml_deleted_obj_free);
252 __xml_private_clean(p);
257 pcmkDeregisterNode(xmlNodePtr node)
266 if (node->type != XML_DOCUMENT_NODE || node->name == NULL
267 || node->name[0] !=
' ') {
268 __xml_private_free(node->_private);
273 pcmkRegisterNode(xmlNodePtr node)
278 case XML_ELEMENT_NODE:
279 case XML_DOCUMENT_NODE:
280 case XML_ATTRIBUTE_NODE:
281 case XML_COMMENT_NODE:
290 case XML_CDATA_SECTION_NODE:
294 crm_trace(
"Ignoring %p %d", node, node->type);
304 __xml_node_dirty(node);
312 crm_trace(
"Tracking changes%s to %p", enforce_acls?
" with ACLs":
"", xml);
315 if(acl_source == NULL) {
337 if(xml != NULL && xml->doc && xml->doc->_private) {
393 static int __xml_offset(xmlNode *xml)
396 xmlNode *cIter = NULL;
398 for(cIter = xml; cIter->prev; cIter = cIter->prev) {
409 static int __xml_offset_no_deletions(xmlNode *xml)
412 xmlNode *cIter = NULL;
414 for(cIter = xml; cIter->prev; cIter = cIter->prev) {
426 __xml_build_changes(xmlNode * xml, xmlNode *patchset)
428 xmlNode *cIter = NULL;
429 xmlAttr *pIter = NULL;
430 xmlNode *change = NULL;
438 sizeof(buffer)) > 0) {
439 int position = __xml_offset_no_deletions(xml);
452 for (pIter = pcmk__first_xml_attr(xml); pIter != NULL; pIter = pIter->next) {
453 xmlNode *attr = NULL;
465 sizeof(buffer)) > 0) {
490 xmlNode *result = NULL;
495 for (pIter = pcmk__first_xml_attr(xml); pIter != NULL; pIter = pIter->next) {
500 crm_xml_add(result, (
const char *)pIter->name, value);
505 for (cIter = __xml_first_child(xml); cIter != NULL; cIter = __xml_next(cIter)) {
506 __xml_build_changes(cIter, patchset);
514 crm_trace(
"%s.%s moved to position %d", xml->name,
ID(xml), __xml_offset(xml));
516 sizeof(buffer)) > 0) {
527 __xml_accept_changes(xmlNode * xml)
529 xmlNode *cIter = NULL;
530 xmlAttr *pIter = NULL;
534 pIter = pcmk__first_xml_attr(xml);
536 while (pIter != NULL) {
537 const xmlChar *name = pIter->name;
550 for (cIter = __xml_first_child(xml); cIter != NULL; cIter = __xml_next(cIter)) {
551 __xml_accept_changes(cIter);
556 is_config_change(xmlNode *xml)
563 p = config->_private;
569 if(xml->doc && xml->doc->_private) {
570 p = xml->doc->_private;
571 for(gIter = p->
deleted_objs; gIter; gIter = gIter->next) {
584 xml_repair_v1_diff(xmlNode * last, xmlNode * next, xmlNode * local_diff, gboolean changed)
588 xmlNode *diff_child = NULL;
590 const char *tag = NULL;
592 const char *vfields[] = {
598 if (local_diff == NULL) {
603 tag =
"diff-removed";
605 if (diff_child == NULL) {
615 for(lpc = 0; last && lpc <
DIMOF(vfields); lpc++){
619 if(changed || lpc == 2) {
626 if (diff_child == NULL) {
636 for(lpc = 0; next && lpc <
DIMOF(vfields); lpc++){
643 xmlAttrPtr xIter = NULL;
645 for (xIter = next->properties; xIter; xIter = xIter->next) {
646 const char *p_name = (
const char *)xIter->name;
657 xml_create_patchset_v1(xmlNode *source, xmlNode *target,
bool config,
bool suppress)
663 xml_repair_v1_diff(source, target, patchset, config);
670 xml_create_patchset_v2(xmlNode *source, xmlNode *target)
678 xmlNode *patchset = NULL;
679 const char *vfields[] = {
691 doc = target->doc->_private;
699 for(lpc = 0; lpc <
DIMOF(vfields); lpc++){
709 for(lpc = 0; lpc <
DIMOF(vfields); lpc++){
718 for(gIter = doc->
deleted_objs; gIter; gIter = gIter->next) {
724 if (deleted_obj->position >= 0) {
729 __xml_build_changes(target, patchset);
734 xml_create_patchset(
int format, xmlNode *source, xmlNode *target,
bool *config_changed,
bool manage_version)
738 xmlNode *patch = NULL;
747 config = is_config_change(target);
749 *config_changed = config;
752 if(manage_version && config) {
759 }
else if(manage_version) {
777 patch = xml_create_patchset_v1(source, target, config, FALSE);
780 patch = xml_create_patchset_v2(source, target);
783 crm_err(
"Unknown patch format: %d", format);
797 if (patch == NULL || source == NULL || target == NULL) {
806 if (format > 1 && with_digest == FALSE) {
820 __xml_log_element(
int log_level,
const char *file,
const char *
function,
int line,
821 const char *prefix, xmlNode *
data,
int depth,
int options);
827 xmlNode *child = NULL;
828 xmlNode *added = NULL;
829 xmlNode *removed = NULL;
830 gboolean is_first = TRUE;
832 int add[] = { 0, 0, 0 };
833 int del[] = { 0, 0, 0 };
835 const char *fmt = NULL;
836 const char *digest = NULL;
839 static struct qb_log_callsite *patchset_cs = NULL;
841 if (patchset_cs == NULL) {
842 patchset_cs = qb_log_callsite_get(
function, __FILE__,
"xml-patchset", log_level, __LINE__, 0);
845 if (patchset == NULL) {
849 }
else if (log_level == 0) {
859 if (add[2] != del[2] || add[1] != del[1] || add[0] != del[0]) {
861 "Diff: --- %d.%d.%d %s", del[0], del[1], del[2], fmt);
863 "Diff: +++ %d.%d.%d %s", add[0], add[1], add[2], digest);
865 }
else if (patchset != NULL && (add[0] || add[1] || add[2])) {
867 "%s: Local-only Change: %d.%d.%d",
function ?
function :
"",
868 add[0], add[1], add[2]);
873 xmlNode *change = NULL;
875 for (change = __xml_first_child(patchset); change != NULL; change = __xml_next(change)) {
880 }
else if(strcmp(op,
"create") == 0) {
881 int lpc = 0, max = 0;
884 max = strlen(prefix);
885 __xml_log_element(log_level, __FILE__,
function, __LINE__, prefix, change->children,
888 for(lpc = 2; lpc < max; lpc++) {
892 __xml_log_element(log_level, __FILE__,
function, __LINE__, prefix, change->children,
896 }
else if(strcmp(op,
"move") == 0) {
899 }
else if(strcmp(op,
"modify") == 0) {
908 for (child = __xml_first_child(clist); child != NULL; child = __xml_next(child)) {
913 }
else if(strcmp(op,
"set") == 0) {
919 o_set += snprintf(buffer_set + o_set,
XML_BUFFER_SIZE - o_set,
"@%s=%s", name, value);
921 }
else if(strcmp(op,
"unset") == 0) {
923 o_unset += snprintf(buffer_unset + o_unset,
XML_BUFFER_SIZE - o_unset,
", ");
925 o_unset += snprintf(buffer_unset + o_unset,
XML_BUFFER_SIZE - o_unset,
"@%s", name);
929 do_crm_log_alias(log_level, __FILE__,
function, __LINE__,
"+ %s: %s", xpath, buffer_set);
932 do_crm_log_alias(log_level, __FILE__,
function, __LINE__,
"-- %s: %s", xpath, buffer_unset);
935 }
else if(strcmp(op,
"delete") == 0) {
940 do_crm_log_alias(log_level, __FILE__,
function, __LINE__,
"-- %s (%d)", xpath, position);
950 if (log_level < LOG_DEBUG ||
function == NULL) {
955 for (child = __xml_first_child(removed); child != NULL; child = __xml_next(child)) {
967 for (child = __xml_first_child(added); child != NULL; child = __xml_next(child)) {
987 doc = xml->doc->_private;
992 for(gIter = doc->
deleted_objs; gIter; gIter = gIter->next) {
995 if (deleted_obj->position >= 0) {
997 deleted_obj->path, deleted_obj->position);
1012 xmlNode *top = NULL;
1019 crm_trace(
"Accepting changes to %p", xml);
1020 doc = xml->doc->_private;
1021 top = xmlDocGetRootElement(xml->doc);
1023 __xml_private_clean(xml->doc->_private);
1031 __xml_accept_changes(top);
1035 find_element(xmlNode *haystack, xmlNode *needle, gboolean exact)
1038 return (needle->type == XML_COMMENT_NODE)?
1039 find_xml_comment(haystack, needle, exact)
1040 :
find_entity(haystack, crm_element_name(needle),
ID(needle));
1045 __subtract_xml_object(xmlNode * target, xmlNode * patch)
1047 xmlNode *patch_child = NULL;
1048 xmlNode *cIter = NULL;
1049 xmlAttrPtr xIter = NULL;
1052 const char *name = NULL;
1053 const char *value = NULL;
1055 if (target == NULL || patch == NULL) {
1059 if (target->type == XML_COMMENT_NODE) {
1062 subtract_xml_comment(target->parent, target, patch, &dummy);
1065 name = crm_element_name(target);
1073 if (value != NULL && strcmp(value,
"removed:top") == 0) {
1074 crm_trace(
"We are the root of the deletion: %s.id=%s", name,
id);
1080 for (xIter = pcmk__first_xml_attr(patch); xIter != NULL; xIter = xIter->next) {
1081 const char *p_name = (
const char *)xIter->name;
1090 cIter = __xml_first_child(target);
1092 xmlNode *target_child = cIter;
1094 cIter = __xml_next(cIter);
1095 patch_child = find_element(patch, target_child, FALSE);
1096 __subtract_xml_object(target_child, patch_child);
1102 __add_xml_object(xmlNode * parent, xmlNode * target, xmlNode * patch)
1104 xmlNode *patch_child = NULL;
1105 xmlNode *target_child = NULL;
1106 xmlAttrPtr xIter = NULL;
1108 const char *
id = NULL;
1109 const char *name = NULL;
1110 const char *value = NULL;
1112 if (patch == NULL) {
1114 }
else if (parent == NULL && target == NULL) {
1122 && strcmp(value,
"added:top") == 0) {
1124 name = crm_element_name(patch);
1125 crm_trace(
"We are the root of the addition: %s.id=%s", name,
id);
1129 }
else if(target == NULL) {
1131 name = crm_element_name(patch);
1132 crm_err(
"Could not locate: %s.id=%s", name,
id);
1136 if (target->type == XML_COMMENT_NODE) {
1137 add_xml_comment(parent, target, patch);
1140 name = crm_element_name(target);
1145 for (xIter = pcmk__first_xml_attr(patch); xIter != NULL; xIter = xIter->next) {
1146 const char *p_name = (
const char *)xIter->name;
1154 for (patch_child = __xml_first_child(patch); patch_child != NULL;
1155 patch_child = __xml_next(patch_child)) {
1157 target_child = find_element(target, patch_child, FALSE);
1158 __add_xml_object(target, target_child, patch_child);
1174 find_patch_xml_node(xmlNode *patchset,
int format,
bool added,
1175 xmlNode **patch_node)
1182 label = added?
"diff-added" :
"diff-removed";
1185 if (cib_node != NULL) {
1186 *patch_node = cib_node;
1190 label = added?
"target" :
"source";
1195 crm_warn(
"Unknown patch format: %d", format);
1206 xmlNode *tmp = NULL;
1208 const char *vfields[] = {
1218 if (!find_patch_xml_node(patchset, format, FALSE, &tmp)) {
1222 for(lpc = 0; lpc <
DIMOF(vfields); lpc++) {
1224 crm_trace(
"Got %d for del[%s]", del[lpc], vfields[lpc]);
1229 if (!find_patch_xml_node(patchset, format, TRUE, &tmp)) {
1233 for(lpc = 0; lpc <
DIMOF(vfields); lpc++) {
1235 crm_trace(
"Got %d for add[%s]", add[lpc], vfields[lpc]);
1243 xml_patch_version_check(xmlNode *xml, xmlNode *patchset,
int format)
1246 bool changed = FALSE;
1248 int this[] = { 0, 0, 0 };
1249 int add[] = { 0, 0, 0 };
1250 int del[] = { 0, 0, 0 };
1252 const char *vfields[] = {
1258 for(lpc = 0; lpc <
DIMOF(vfields); lpc++) {
1260 crm_trace(
"Got %d for this[%s]",
this[lpc], vfields[lpc]);
1261 if (
this[lpc] < 0) {
1269 add[2] =
this[2] + 1;
1270 for(lpc = 0; lpc <
DIMOF(vfields); lpc++) {
1271 del[lpc] =
this[lpc];
1276 for(lpc = 0; lpc <
DIMOF(vfields); lpc++) {
1277 if(
this[lpc] < del[lpc]) {
1278 crm_debug(
"Current %s is too low (%d.%d.%d < %d.%d.%d --> %d.%d.%d)", vfields[lpc],
1279 this[0],
this[1],
this[2], del[0], del[1], del[2], add[0], add[1], add[2]);
1282 }
else if(
this[lpc] > del[lpc]) {
1283 crm_info(
"Current %s is too high (%d.%d.%d > %d.%d.%d --> %d.%d.%d) %p", vfields[lpc],
1284 this[0],
this[1],
this[2], del[0], del[1], del[2], add[0], add[1], add[2], patchset);
1290 for(lpc = 0; lpc <
DIMOF(vfields); lpc++) {
1291 if(add[lpc] > del[lpc]) {
1296 if(changed == FALSE) {
1297 crm_notice(
"Versions did not change in patch %d.%d.%d", add[0], add[1], add[2]);
1301 crm_debug(
"Can apply patch %d.%d.%d to %d.%d.%d",
1302 add[0], add[1], add[2],
this[0],
this[1],
this[2]);
1307 xml_apply_patchset_v1(xmlNode *xml, xmlNode *patchset)
1310 int root_nodes_seen = 0;
1312 xmlNode *child_diff = NULL;
1313 xmlNode *added =
find_xml_node(patchset,
"diff-added", FALSE);
1314 xmlNode *removed =
find_xml_node(patchset,
"diff-removed", FALSE);
1318 for (child_diff = __xml_first_child(removed); child_diff != NULL;
1319 child_diff = __xml_next(child_diff)) {
1320 CRM_CHECK(root_nodes_seen == 0, rc = FALSE);
1321 if (root_nodes_seen == 0) {
1322 __subtract_xml_object(xml, child_diff);
1327 if (root_nodes_seen > 1) {
1328 crm_err(
"(-) Diffs cannot contain more than one change set... saw %d", root_nodes_seen);
1332 root_nodes_seen = 0;
1335 xmlNode *child_diff = NULL;
1337 for (child_diff = __xml_first_child(added); child_diff != NULL;
1338 child_diff = __xml_next(child_diff)) {
1339 CRM_CHECK(root_nodes_seen == 0, rc = FALSE);
1340 if (root_nodes_seen == 0) {
1341 __add_xml_object(NULL, xml, child_diff);
1347 if (root_nodes_seen > 1) {
1348 crm_err(
"(+) Diffs cannot contain more than one change set... saw %d", root_nodes_seen);
1359 __first_xml_child_match(xmlNode *parent,
const char *name,
const char *
id,
int position)
1361 xmlNode *cIter = NULL;
1363 for (cIter = __xml_first_child(parent); cIter != NULL; cIter = __xml_next(cIter)) {
1364 if(strcmp((
const char *)cIter->name, name) != 0) {
1367 const char *cid =
ID(cIter);
1368 if(cid == NULL || strcmp(cid,
id) != 0) {
1374 if (cIter->type == XML_COMMENT_NODE
1376 && __xml_offset(cIter) != position) {
1399 __xml_find_path(xmlNode *top,
const char *key,
int target_position)
1401 xmlNode *target = (xmlNode*) top->doc;
1402 const char *current = key;
1412 key_len = strlen(key);
1418 remainder = calloc(key_len,
sizeof(
char));
1421 section = calloc(key_len,
sizeof(
char));
1424 id = calloc(key_len,
sizeof(
char));
1427 tag = calloc(key_len,
sizeof(
char));
1432 rc = sscanf(current,
"/%[^/]%s", section, remainder);
1435 int f = sscanf(section,
"%[^[][@id='%[^']", tag,
id);
1436 int current_position = -1;
1441 if ((rc == 1) && (target_position >= 0)) {
1442 current_position = target_position;
1447 target = __first_xml_child_match(target, tag, NULL, current_position);
1450 target = __first_xml_child_match(target, tag,
id, current_position);
1457 current = remainder;
1461 }
while ((rc == 2) && target);
1465 (path = (
char *) xmlGetNodePath(target)), key);
1478 typedef struct xml_change_obj_s {
1484 sort_change_obj_by_position(gconstpointer a, gconstpointer b)
1488 int position_a = -1;
1489 int position_b = -1;
1494 if (position_a < position_b) {
1497 }
else if (position_a > position_b) {
1505 xml_apply_patchset_v2(xmlNode *xml, xmlNode *patchset)
1508 xmlNode *change = NULL;
1512 for (change = __xml_first_child(patchset); change != NULL; change = __xml_next(change)) {
1513 xmlNode *match = NULL;
1522 crm_trace(
"Processing %s %s", change->name, op);
1525 if(strcmp(op,
"delete") == 0) {
1528 match = __xml_find_path(xml, xpath, position);
1529 crm_trace(
"Performing %s on %s with %p", op, xpath, match);
1531 if(match == NULL && strcmp(op,
"delete") == 0) {
1532 crm_debug(
"No %s match for %s in %p", op, xpath, xml->doc);
1535 }
else if(match == NULL) {
1536 crm_err(
"No %s match for %s in %p", op, xpath, xml->doc);
1540 }
else if (strcmp(op,
"create") == 0 || strcmp(op,
"move") == 0) {
1546 change_obj->change = change;
1547 change_obj->match = match;
1549 change_objs = g_list_append(change_objs, change_obj);
1551 if (strcmp(op,
"move") == 0) {
1553 if (match->parent != NULL && match->parent->last != NULL) {
1554 xmlAddNextSibling(match->parent->last, match);
1558 }
else if(strcmp(op,
"delete") == 0) {
1561 }
else if(strcmp(op,
"modify") == 0) {
1562 xmlAttr *pIter = pcmk__first_xml_attr(match);
1569 while(pIter != NULL) {
1570 const char *name = (
const char *)pIter->name;
1572 pIter = pIter->next;
1576 for (pIter = pcmk__first_xml_attr(attrs); pIter != NULL; pIter = pIter->next) {
1577 const char *name = (
const char *)pIter->name;
1584 crm_err(
"Unknown operation: %s", op);
1590 change_objs = g_list_sort(change_objs, sort_change_obj_by_position);
1592 for (gIter = change_objs; gIter; gIter = gIter->next) {
1594 xmlNode *match = change_obj->match;
1595 const char *op = NULL;
1596 const char *xpath = NULL;
1598 change = change_obj->change;
1603 crm_trace(
"Continue performing %s on %s with %p", op, xpath, match);
1605 if(strcmp(op,
"create") == 0) {
1607 xmlNode *child = NULL;
1608 xmlNode *match_child = NULL;
1610 match_child = match->children;
1613 while(match_child && position != __xml_offset(match_child)) {
1614 match_child = match_child->next;
1617 child = xmlDocCopyNode(change->children, match->doc, 1);
1619 crm_trace(
"Adding %s at position %d", child->name, position);
1620 xmlAddPrevSibling(match_child, child);
1622 }
else if(match->last) {
1623 crm_trace(
"Adding %s at position %d (end)", child->name, position);
1624 xmlAddNextSibling(match->last, child);
1627 crm_trace(
"Adding %s at position %d (first)", child->name, position);
1629 xmlAddChild(match, child);
1631 crm_node_created(child);
1633 }
else if(strcmp(op,
"move") == 0) {
1637 if(position != __xml_offset(match)) {
1638 xmlNode *match_child = NULL;
1641 if(p > __xml_offset(match)) {
1646 match_child = match->parent->children;
1648 while(match_child && p != __xml_offset(match_child)) {
1649 match_child = match_child->next;
1652 crm_trace(
"Moving %s to position %d (was %d, prev %p, %s %p)",
1653 match->name, position, __xml_offset(match), match->prev,
1654 match_child?
"next":
"last", match_child?match_child:match->parent->last);
1657 xmlAddPrevSibling(match_child, match);
1661 xmlAddNextSibling(match->parent->last, match);
1665 crm_trace(
"%s is already in position %d", match->name, position);
1668 if(position != __xml_offset(match)) {
1669 crm_err(
"Moved %s.%s to position %d instead of %d (%p)",
1670 match->name,
ID(match), __xml_offset(match), position, match->prev);
1676 g_list_free_full(change_objs, free);
1685 xmlNode *old = NULL;
1688 if(patchset == NULL) {
1696 rc = xml_patch_version_check(xml, patchset, format);
1710 rc = xml_apply_patchset_v1(xml, patchset);
1713 rc = xml_apply_patchset_v2(xml, patchset);
1716 crm_err(
"Unknown patch format: %d", format);
1722 static struct qb_log_callsite *digest_cs = NULL;
1724 char *new_digest = NULL;
1727 if (digest_cs == NULL) {
1729 qb_log_callsite_get(__func__, __FILE__,
"diff-digest",
LOG_TRACE, __LINE__,
1735 crm_info(
"v%d digest mis-match: expected %s, calculated %s", format, digest, new_digest);
1738 if (digest_cs && digest_cs->targets) {
1744 crm_trace(
"%p %.6x", digest_cs, digest_cs ? digest_cs->targets : 0);
1748 crm_trace(
"v%d digest matched: expected %s, calculated %s", format, digest, new_digest);
1760 xmlNode *a_child = NULL;
1761 const char *name =
"NULL";
1764 name = crm_element_name(root);
1767 if (search_path == NULL) {
1768 crm_warn(
"Will never find <NULL>");
1772 for (a_child = __xml_first_child(root); a_child != NULL; a_child = __xml_next(a_child)) {
1773 if (strcmp((
const char *)a_child->name, search_path) == 0) {
1780 crm_warn(
"Could not find %s in %s.", search_path, name);
1781 }
else if (root != NULL) {
1782 crm_trace(
"Could not find %s in %s.", search_path, name);
1784 crm_trace(
"Could not find %s in <NULL>.", search_path);
1794 find_entity_by_attr_or_just_name(xmlNode *parent,
const char *node_name,
1795 const char *attr_n,
const char *attr_v)
1800 CRM_CHECK(attr_n == NULL || attr_v != NULL,
return NULL);
1802 for (child = __xml_first_child(parent); child != NULL; child = __xml_next(child)) {
1804 if (node_name == NULL || !strcmp((
const char *) child->name, node_name)) {
1814 attr_n ? attr_n :
"",
1816 attr_n ? attr_v :
"",
1817 crm_element_name(parent));
1825 return find_entity_by_attr_or_just_name(parent, node_name,
1833 crm_warn(
"No node to copy properties from");
1835 }
else if (target == NULL) {
1836 crm_err(
"No node to copy properties into");
1839 xmlAttrPtr pIter = NULL;
1841 for (pIter = pcmk__first_xml_attr(src); pIter != NULL; pIter = pIter->next) {
1842 const char *p_name = (
const char *)pIter->name;
1843 const char *p_value = pcmk__xml_attr_value(pIter);
1856 xmlNode *child = NULL;
1857 xmlAttrPtr pIter = NULL;
1859 for (pIter = pcmk__first_xml_attr(target); pIter != NULL; pIter = pIter->next) {
1860 const char *p_name = (
const char *)pIter->name;
1861 const char *p_value = pcmk__xml_attr_value(pIter);
1865 for (child = __xml_first_child(target); child != NULL; child = __xml_next(child)) {
1878 const char *old_value = NULL;
1880 if (value == NULL || name == NULL) {
1886 if (old_value == NULL) {
1888 goto set_unexpanded;
1890 }
else if (strstr(value, name) != value) {
1891 goto set_unexpanded;
1894 name_len = strlen(name);
1895 value_len = strlen(value);
1896 if (value_len < (name_len + 2)
1897 || value[name_len] !=
'+' || (value[name_len + 1] !=
'+' && value[name_len + 1] !=
'=')) {
1898 goto set_unexpanded;
1904 if (old_value != value) {
1908 if (value[name_len + 1] !=
'+') {
1909 const char *offset_s = value + (name_len + 2);
1913 int_value += offset;
1923 if (old_value == value) {
1941 xmlDocSetRootElement(doc, node);
1942 xmlSetTreeDoc(node, doc);
1950 xmlNode *child = NULL;
1953 CRM_CHECK(src_node != NULL,
return NULL);
1955 child = xmlDocCopyNode(src_node, doc, 1);
1956 xmlAddChild(parent, child);
1957 crm_node_created(child);
1973 xmlNode *node = NULL;
1975 if (name == NULL || name[0] == 0) {
1976 CRM_CHECK(name != NULL && name[0] == 0,
return NULL);
1980 if (parent == NULL) {
1982 node = xmlNewDocRawNode(doc, NULL, (
pcmkXmlStr) name, NULL);
1983 xmlDocSetRootElement(doc, node);
1987 node = xmlNewDocRawNode(doc, NULL, (
pcmkXmlStr) name, NULL);
1988 xmlAddChild(parent, node);
1990 crm_node_created(node);
2000 xmlNodeSetContent(node, (
pcmkXmlStr) content);
2008 const char *class_name,
const char *text)
2012 if (class_name != NULL) {
2025 int offset,
size_t buffer_size)
2027 const char *
id =
ID(xml);
2029 if(offset == 0 && prefix == NULL && xml->parent) {
2035 offset += snprintf(buffer + offset, buffer_size - offset,
2036 "/%s[@id='%s']", (
const char *) xml->name,
id);
2037 }
else if(xml->name) {
2038 offset += snprintf(buffer + offset, buffer_size - offset,
2039 "/%s", (
const char *) xml->name);
2052 return strdup(buffer);
2070 free_xml_with_position(xmlNode * child,
int position)
2072 if (child != NULL) {
2073 xmlNode *top = NULL;
2074 xmlDoc *doc = child->doc;
2078 top = xmlDocGetRootElement(doc);
2081 if (doc != NULL && top == child) {
2100 sizeof(buffer)) > 0) {
2103 crm_trace(
"Deleting %s %p from %p", buffer, child, doc);
2105 deleted_obj->path = strdup(buffer);
2107 deleted_obj->position = -1;
2109 if (child->type == XML_COMMENT_NODE) {
2110 if (position >= 0) {
2111 deleted_obj->position = position;
2114 deleted_obj->position = __xml_offset(child);
2132 free_xml_with_position(child, -1);
2139 xmlNode *copy = xmlDocCopyNode(src, doc, 1);
2141 xmlDocSetRootElement(doc, copy);
2142 xmlSetTreeDoc(copy, doc);
2147 crm_xml_err(
void *ctx,
const char *fmt, ...)
2148 G_GNUC_PRINTF(2, 3);
2151 crm_xml_err(
void *ctx, const
char *fmt, ...)
2154 static struct qb_log_callsite *xml_error_cs = NULL;
2156 if (xml_error_cs == NULL) {
2157 xml_error_cs = qb_log_callsite_get(
2162 if (xml_error_cs && xml_error_cs->targets) {
2164 crm_abort(__FILE__, __PRETTY_FUNCTION__, __LINE__,
"xml library error",
2166 "XML Error: ", fmt, ap);
2176 xmlNode *xml = NULL;
2177 xmlDocPtr output = NULL;
2178 xmlParserCtxtPtr ctxt = NULL;
2179 xmlErrorPtr last_error = NULL;
2181 if (input == NULL) {
2182 crm_err(
"Can't parse NULL input");
2187 ctxt = xmlNewParserCtxt();
2190 xmlCtxtResetLastError(ctxt);
2191 xmlSetGenericErrorFunc(ctxt, crm_xml_err);
2192 output = xmlCtxtReadDoc(ctxt, (
pcmkXmlStr) input, NULL, NULL,
2195 xml = xmlDocGetRootElement(output);
2197 last_error = xmlCtxtGetLastError(ctxt);
2198 if (last_error && last_error->code != XML_ERR_OK) {
2204 crm_warn(
"Parsing failed (domain=%d, level=%d, code=%d): %s",
2205 last_error->domain, last_error->level, last_error->code, last_error->message);
2207 if (last_error->code == XML_ERR_DOCUMENT_EMPTY) {
2210 }
else if (last_error->code != XML_ERR_DOCUMENT_END) {
2211 crm_err(
"Couldn't%s parse %d chars: %s", xml ?
" fully" :
"", (
int)strlen(input),
2218 int len = strlen(input);
2222 crm_warn(
"Parse error[+%.3d]: %.80s", lpc, input+lpc);
2230 xmlFreeParserCtxt(ctxt);
2237 size_t data_length = 0;
2238 size_t read_chars = 0;
2240 char *xml_buffer = NULL;
2241 xmlNode *xml_obj = NULL;
2245 read_chars = fread(xml_buffer + data_length, 1,
XML_BUFFER_SIZE, stdin);
2246 data_length += read_chars;
2249 if (data_length == 0) {
2250 crm_warn(
"No XML supplied on stdin");
2255 xml_buffer[data_length] =
'\0';
2264 decompress_file(
const char *filename)
2266 char *buffer = NULL;
2270 size_t length = 0, read_len = 0;
2272 BZFILE *bz_file = NULL;
2273 FILE *input = fopen(filename,
"r");
2275 if (input == NULL) {
2276 crm_perror(LOG_ERR,
"Could not open %s for reading", filename);
2280 bz_file = BZ2_bzReadOpen(&rc, input, 0, 0, NULL, 0);
2282 crm_err(
"Could not prepare to read compressed %s: %s "
2284 BZ2_bzReadClose(&rc, bz_file);
2289 while (rc == BZ_OK) {
2291 read_len = BZ2_bzRead(&rc, bz_file, buffer + length,
XML_BUFFER_SIZE);
2293 crm_trace(
"Read %ld bytes from file: %d", (
long)read_len, rc);
2295 if (rc == BZ_OK || rc == BZ_STREAM_END) {
2300 buffer[length] =
'\0';
2302 if (rc != BZ_STREAM_END) {
2303 crm_err(
"Could not read compressed %s: %s "
2309 BZ2_bzReadClose(&rc, bz_file);
2313 crm_err(
"Could not read compressed %s: not built with bzlib support",
2322 xmlNode *iter = xml->children;
2325 xmlNode *next = iter->next;
2327 switch (iter->type) {
2333 case XML_ELEMENT_NODE:
2350 xmlNode *xml = NULL;
2351 xmlDocPtr output = NULL;
2352 gboolean uncompressed = TRUE;
2353 xmlParserCtxtPtr ctxt = NULL;
2354 xmlErrorPtr last_error = NULL;
2357 ctxt = xmlNewParserCtxt();
2360 xmlCtxtResetLastError(ctxt);
2361 xmlSetGenericErrorFunc(ctxt, crm_xml_err);
2367 if (filename == NULL) {
2369 output = xmlCtxtReadFd(ctxt, STDIN_FILENO,
"unknown.xml", NULL,
2372 }
else if (uncompressed) {
2376 char *input = decompress_file(filename);
2378 output = xmlCtxtReadDoc(ctxt, (
pcmkXmlStr) input, NULL, NULL,
2383 if (output && (xml = xmlDocGetRootElement(output))) {
2387 last_error = xmlCtxtGetLastError(ctxt);
2388 if (last_error && last_error->code != XML_ERR_OK) {
2394 crm_err(
"Parsing failed (domain=%d, level=%d, code=%d): %s",
2395 last_error->domain, last_error->level, last_error->code, last_error->message);
2397 if (last_error && last_error->code != XML_ERR_OK) {
2398 crm_err(
"Couldn't%s parse %s", xml ?
" fully" :
"", filename);
2405 xmlFreeParserCtxt(ctxt);
2422 now_str ? now_str :
"Could not determine current time");
2435 for (c =
id; *c; ++c) {
2460 va_start(ap, format);
2461 len = vasprintf(&
id, format, ap);
2482 write_xml_stream(xmlNode * xml_node,
const char *filename, FILE * stream, gboolean compress)
2485 char *buffer = NULL;
2486 unsigned int out = 0;
2499 unsigned int in = 0;
2500 BZFILE *bz_file = NULL;
2502 bz_file = BZ2_bzWriteOpen(&rc, stream, 5, 0, 30);
2504 crm_warn(
"Not compressing %s: could not prepare file stream: %s "
2507 BZ2_bzWrite(&rc, bz_file, buffer, strlen(buffer));
2509 crm_warn(
"Not compressing %s: could not compress data: %s "
2510 CRM_XS " bzerror=%d errno=%d",
2516 BZ2_bzWriteClose(&rc, bz_file, 0, &in, &out);
2518 crm_warn(
"Not compressing %s: could not write compressed data: %s "
2519 CRM_XS " bzerror=%d errno=%d",
2524 crm_trace(
"Compressed XML for %s from %u bytes to %u",
2529 crm_warn(
"Not compressing %s: not built with bzlib support", filename);
2534 res = fprintf(stream,
"%s", buffer);
2544 if (fflush(stream) != 0) {
2546 crm_perror(LOG_ERR,
"flushing %s", filename);
2550 if (fsync(fileno(stream)) < 0 && errno != EROFS && errno != EINVAL) {
2552 crm_perror(LOG_ERR,
"synchronizing %s", filename);
2557 crm_trace(
"Saved %d bytes%s to %s as XML",
2558 res, ((out > 0)?
" (compressed)" :
""), filename);
2575 write_xml_fd(xmlNode * xml_node,
const char *filename,
int fd, gboolean compress)
2577 FILE *stream = NULL;
2579 CRM_CHECK(xml_node && (fd > 0),
return -EINVAL);
2580 stream = fdopen(fd,
"w");
2581 if (stream == NULL) {
2584 return write_xml_stream(xml_node, filename, stream, compress);
2599 FILE *stream = NULL;
2601 CRM_CHECK(xml_node && filename,
return -EINVAL);
2602 stream = fopen(filename,
"w");
2603 if (stream == NULL) {
2606 return write_xml_stream(xml_node, filename, stream, compress);
2614 return __xml_first_child(tmp);
2627 crm_xml_escape_shuffle(
char *text,
int start,
int *length,
const char *replace)
2630 int offset = strlen(replace) - 1;
2633 text = realloc_safe(text, *length);
2635 for (lpc = (*length) - 1; lpc > (start + offset); lpc--) {
2636 text[lpc] = text[lpc - offset];
2639 memcpy(text + start, replace, offset + 1);
2648 int length = 1 + strlen(text);
2649 char *copy = strdup(text);
2666 for (index = 0; index < length; index++) {
2667 switch (copy[index]) {
2671 copy = crm_xml_escape_shuffle(copy, index, &length,
"<");
2675 copy = crm_xml_escape_shuffle(copy, index, &length,
">");
2679 copy = crm_xml_escape_shuffle(copy, index, &length,
""");
2683 copy = crm_xml_escape_shuffle(copy, index, &length,
"'");
2687 copy = crm_xml_escape_shuffle(copy, index, &length,
"&");
2692 copy = crm_xml_escape_shuffle(copy, index, &length,
" ");
2697 copy = crm_xml_escape_shuffle(copy, index, &length,
"\\n");
2701 copy = crm_xml_escape_shuffle(copy, index, &length,
"\\r");
2711 if(copy[index] <
' ' || copy[index] >
'~') {
2715 copy = crm_xml_escape_shuffle(copy, index, &length, replace);
2729 dump_xml_attr(xmlAttrPtr attr,
int options,
char **buffer,
int *offset,
int *max)
2731 char *p_value = NULL;
2732 const char *p_name = NULL;
2736 if (attr == NULL || attr->children == NULL) {
2745 p_name = (
const char *)attr->name;
2747 buffer_print(*buffer, *max, *offset,
" %s=\"%s\"", p_name, p_value);
2752 __xml_log_element(
int log_level,
const char *file,
const char *
function,
int line,
2753 const char *prefix, xmlNode *
data,
int depth,
int options)
2757 const char *name = NULL;
2758 const char *hidden = NULL;
2760 xmlNode *child = NULL;
2761 xmlAttrPtr pIter = NULL;
2767 name = crm_element_name(
data);
2770 char *buffer = NULL;
2772 insert_prefix(options, &buffer, &offset, &max, depth);
2774 if (
data->type == XML_COMMENT_NODE) {
2781 for (pIter = pcmk__first_xml_attr(
data); pIter != NULL; pIter = pIter->next) {
2783 const char *p_name = (
const char *)pIter->name;
2784 const char *p_value = pcmk__xml_attr_value(pIter);
2785 char *p_copy = NULL;
2794 }
else if (hidden != NULL && p_name[0] != 0 && strstr(hidden, p_name) != NULL) {
2795 p_copy = strdup(
"*****");
2801 buffer_print(buffer, max, offset,
" %s=\"%s\"", p_name, p_copy);
2816 do_crm_log_alias(log_level, file,
function, line,
"%s %s", prefix, buffer);
2820 if(
data->type == XML_COMMENT_NODE) {
2830 for (child = __xml_first_child(
data); child != NULL; child = __xml_next(child)) {
2836 char *buffer = NULL;
2838 insert_prefix(options, &buffer, &offset, &max, depth);
2841 do_crm_log_alias(log_level, file,
function, line,
"%s %s", prefix, buffer);
2847 __xml_log_change_element(
int log_level,
const char *file,
const char *
function,
int line,
2848 const char *prefix, xmlNode *
data,
int depth,
int options)
2851 char *prefix_m = NULL;
2852 xmlNode *child = NULL;
2853 xmlAttrPtr pIter = NULL;
2861 prefix_m = strdup(prefix);
2866 __xml_log_element(log_level, file,
function, line,
2870 char *spaces = calloc(80, 1);
2871 int s_count = 0, s_max = 80;
2872 char *prefix_del = NULL;
2873 char *prefix_moved = NULL;
2874 const char *
flags = prefix;
2876 insert_prefix(options, &spaces, &s_count, &s_max, depth);
2877 prefix_del = strdup(prefix);
2878 prefix_del[0] =
'-';
2879 prefix_del[1] =
'-';
2880 prefix_moved = strdup(prefix);
2881 prefix_moved[1] =
'~';
2884 flags = prefix_moved;
2889 __xml_log_element(log_level, file,
function, line,
2892 for (pIter = pcmk__first_xml_attr(
data); pIter != NULL; pIter = pIter->next) {
2893 const char *aname = (
const char*)pIter->name;
2895 p = pIter->_private;
2900 "%s %s @%s=%s",
flags, spaces, aname, value);
2912 flags = prefix_moved;
2918 "%s %s @%s=%s",
flags, spaces, aname, value);
2925 for (child = __xml_first_child(
data); child != NULL; child = __xml_next(child)) {
2926 __xml_log_change_element(log_level, file,
function, line, prefix, child, depth + 1, options);
2929 __xml_log_element(log_level, file,
function, line,
2933 for (child = __xml_first_child(
data); child != NULL; child = __xml_next(child)) {
2934 __xml_log_change_element(log_level, file,
function, line, prefix, child, depth + 1, options);
2944 const char *prefix, xmlNode *
data,
int depth,
int options)
2946 xmlNode *a_child = NULL;
2948 char *prefix_m = NULL;
2950 if (prefix == NULL) {
2957 "No data to dump as XML");
2962 __xml_log_change_element(log_level, file,
function, line, prefix,
data, depth, options);
2970 prefix_m = strdup(prefix);
2977 prefix_m = strdup(prefix);
2986 for (a_child = __xml_first_child(
data); a_child != NULL; a_child = __xml_next(a_child)) {
2987 log_data_element(log_level, file,
function, line, prefix, a_child, depth + 1, options);
2990 __xml_log_element(log_level, file,
function, line, prefix,
data, depth,
2997 dump_filtered_xml(xmlNode *
data,
int options,
char **buffer,
int *offset,
int *max)
3000 xmlAttrPtr xIter = NULL;
3001 static int filter_len =
DIMOF(filter);
3003 for (lpc = 0; options && lpc < filter_len; lpc++) {
3004 filter[lpc].found = FALSE;
3007 for (xIter = pcmk__first_xml_attr(
data); xIter != NULL; xIter = xIter->next) {
3009 const char *p_name = (
const char *)xIter->name;
3011 for (lpc = 0; skip == FALSE && lpc < filter_len; lpc++) {
3012 if (filter[lpc].found == FALSE && strcmp(p_name, filter[lpc].
string) == 0) {
3013 filter[lpc].found = TRUE;
3019 if (skip == FALSE) {
3020 dump_xml_attr(xIter, options, buffer, offset, max);
3026 dump_xml_element(xmlNode *
data,
int options,
char **buffer,
int *offset,
int *max,
int depth)
3028 const char *name = NULL;
3039 if (*buffer == NULL) {
3044 name = crm_element_name(
data);
3047 insert_prefix(options, buffer, offset, max, depth);
3051 dump_filtered_xml(
data, options, buffer, offset, max);
3054 xmlAttrPtr xIter = NULL;
3056 for (xIter = pcmk__first_xml_attr(
data); xIter != NULL; xIter = xIter->next) {
3057 dump_xml_attr(xIter, options, buffer, offset, max);
3061 if (
data->children == NULL) {
3072 if (
data->children) {
3073 xmlNode *xChild = NULL;
3074 for(xChild =
data->children; xChild != NULL; xChild = xChild->next) {
3075 crm_xml_dump(xChild, options, buffer, offset, max, depth + 1);
3078 insert_prefix(options, buffer, offset, max, depth);
3088 dump_xml_text(xmlNode *
data,
int options,
char **buffer,
int *offset,
int *max,
int depth)
3099 if (*buffer == NULL) {
3104 insert_prefix(options, buffer, offset, max, depth);
3114 dump_xml_cdata(xmlNode *
data,
int options,
char **buffer,
int *offset,
int *max,
int depth)
3125 if (*buffer == NULL) {
3130 insert_prefix(options, buffer, offset, max, depth);
3143 dump_xml_comment(xmlNode *
data,
int options,
char **buffer,
int *offset,
int *max,
int depth)
3154 if (*buffer == NULL) {
3159 insert_prefix(options, buffer, offset, max, depth);
3170 #define PCMK__XMLDUMP_STATS 0
3190 #if (PCMK__XMLDUMP_STATS - 0)
3191 time_t next,
new = time(NULL);
3194 xmlOutputBuffer *xml_buffer;
3200 xml_buffer = xmlAllocOutputBuffer(NULL);
3213 xmlNodeDumpOutput(xml_buffer, doc,
data, 0,
3215 xmlOutputBufferWrite(xml_buffer,
sizeof(
"\n") - 1,
"\n");
3216 if (xml_buffer->buffer != NULL) {
3218 (
char *) xmlBufContent(xml_buffer->buffer));
3221 #if (PCMK__XMLDUMP_STATS - 0)
3223 if ((now + 1) < next) {
3225 crm_err(
"xmlNodeDump() -> %dbytes took %ds", *max, next - now);
3229 xmlOutputBufferClose(xml_buffer);
3233 switch(
data->type) {
3234 case XML_ELEMENT_NODE:
3236 dump_xml_element(
data, options, buffer, offset, max, depth);
3241 dump_xml_text(
data, options, buffer, offset, max, depth);
3244 case XML_COMMENT_NODE:
3245 dump_xml_comment(
data, options, buffer, offset, max, depth);
3247 case XML_CDATA_SECTION_NODE:
3248 dump_xml_cdata(
data, options, buffer, offset, max, depth);
3286 char *buffer = NULL;
3287 int offset = 0, max = 0;
3291 &buffer, &offset, &max, 0);
3298 char *buffer = NULL;
3299 int offset = 0, max = 0;
3308 char *buffer = NULL;
3309 int offset = 0, max = 0;
3311 crm_xml_dump(an_xml_node, 0, &buffer, &offset, &max, 0);
3318 if (xml_root != NULL && xml_root->children != NULL) {
3328 crm_trace(
"Cannot remove %s from %s", name, obj->name);
3333 xmlAttr *attr = xmlHasProp(obj, (
pcmkXmlStr) name);
3348 xmlNode *child = NULL;
3353 for (child = __xml_first_child(a_node); child != NULL; child = __xml_next(child)) {
3363 if (filename == NULL) {
3371 crm_info(
"Saving %s to %s", desc, filename);
3379 gboolean result = TRUE;
3380 int root_nodes_seen = 0;
3381 static struct qb_log_callsite *digest_cs = NULL;
3385 xmlNode *child_diff = NULL;
3387 xmlNode *removed =
find_xml_node(diff,
"diff-removed", FALSE);
3389 CRM_CHECK(new_xml != NULL,
return FALSE);
3390 if (digest_cs == NULL) {
3392 qb_log_callsite_get(__func__, __FILE__,
"diff-digest",
LOG_TRACE, __LINE__,
3397 for (child_diff = __xml_first_child(removed); child_diff != NULL;
3398 child_diff = __xml_next(child_diff)) {
3399 CRM_CHECK(root_nodes_seen == 0, result = FALSE);
3400 if (root_nodes_seen == 0) {
3406 if (root_nodes_seen == 0) {
3409 }
else if (root_nodes_seen > 1) {
3410 crm_err(
"(-) Diffs cannot contain more than one change set..." " saw %d", root_nodes_seen);
3414 root_nodes_seen = 0;
3417 xmlNode *child_diff = NULL;
3419 for (child_diff = __xml_first_child(added); child_diff != NULL;
3420 child_diff = __xml_next(child_diff)) {
3421 CRM_CHECK(root_nodes_seen == 0, result = FALSE);
3422 if (root_nodes_seen == 0) {
3423 add_xml_object(NULL, *new_xml, child_diff, TRUE);
3429 if (root_nodes_seen > 1) {
3430 crm_err(
"(+) Diffs cannot contain more than one change set..." " saw %d", root_nodes_seen);
3433 }
else if (result && digest) {
3434 char *new_digest = NULL;
3439 crm_info(
"Digest mis-match: expected %s, calculated %s", digest, new_digest);
3442 crm_trace(
"%p %.6x", digest_cs, digest_cs ? digest_cs->targets : 0);
3443 if (digest_cs && digest_cs->targets) {
3450 crm_trace(
"Digest matched: expected %s, calculated %s", digest, new_digest);
3454 }
else if (result) {
3471 for (xmlAttr *attr = pcmk__first_xml_attr(xml); attr; attr = attr->next) {
3486 mark_attr_deleted(xmlNode *new_xml,
const char *element,
const char *attr_name,
3487 const char *old_value)
3490 xmlAttr *attr = NULL;
3506 crm_trace(
"XML attribute %s=%s was removed from %s",
3507 attr_name, old_value, element);
3515 mark_attr_changed(xmlNode *new_xml,
const char *element,
const char *attr_name,
3516 const char *old_value)
3520 crm_trace(
"XML attribute %s was changed from '%s' to '%s' in %s",
3521 attr_name, old_value, vcopy, element);
3536 mark_attr_moved(xmlNode *new_xml,
const char *element, xmlAttr *old_attr,
3537 xmlAttr *new_attr,
int p_old,
int p_new)
3541 crm_trace(
"XML attribute %s moved from position %d to %d in %s",
3542 old_attr->name, p_old, p_new, element);
3545 __xml_node_dirty(new_xml);
3550 p = (p_old > p_new)? old_attr->_private : new_attr->_private;
3559 xml_diff_old_attrs(xmlNode *old_xml, xmlNode *new_xml)
3561 xmlAttr *attr_iter = pcmk__first_xml_attr(old_xml);
3563 while (attr_iter != NULL) {
3564 xmlAttr *old_attr = attr_iter;
3565 xmlAttr *new_attr = xmlHasProp(new_xml, attr_iter->name);
3566 const char *name = (
const char *) attr_iter->name;
3569 attr_iter = attr_iter->next;
3570 if (new_attr == NULL) {
3571 mark_attr_deleted(new_xml, (
const char *) old_xml->name, name,
3576 int new_pos = __xml_offset((xmlNode*) new_attr);
3577 int old_pos = __xml_offset((xmlNode*) old_attr);
3583 if (strcmp(new_value, old_value) != 0) {
3584 mark_attr_changed(new_xml, (
const char *) old_xml->name, name,
3587 }
else if ((old_pos != new_pos)
3589 mark_attr_moved(new_xml, (
const char *) old_xml->name,
3590 old_attr, new_attr, old_pos, new_pos);
3601 mark_created_attrs(xmlNode *new_xml)
3603 xmlAttr *attr_iter = pcmk__first_xml_attr(new_xml);
3605 while (attr_iter != NULL) {
3606 xmlAttr *new_attr = attr_iter;
3609 attr_iter = attr_iter->next;
3611 const char *attr_name = (
const char *) new_attr->name;
3613 crm_trace(
"Created new attribute %s=%s in %s",
3624 xmlUnsetProp(new_xml, new_attr->name);
3635 xml_diff_attrs(xmlNode *old_xml, xmlNode *new_xml)
3638 xml_diff_old_attrs(old_xml, new_xml);
3639 mark_created_attrs(new_xml);
3652 mark_child_deleted(xmlNode *old_child, xmlNode *new_parent)
3658 __xml_node_clean(candidate);
3664 free_xml_with_position(candidate, __xml_offset(old_child));
3666 if (find_element(new_parent, old_child, TRUE) == NULL) {
3672 mark_child_moved(xmlNode *old_child, xmlNode *new_parent, xmlNode *new_child,
3673 int p_old,
int p_new)
3677 crm_trace(
"Child element %s with id='%s' moved from position %d to %d under %s",
3678 new_child->name, (
ID(new_child)?
ID(new_child) :
"<no id>"),
3679 p_old, p_new, new_parent->name);
3680 __xml_node_dirty(new_parent);
3683 if (p_old > p_new) {
3684 p = old_child->_private;
3686 p = new_child->_private;
3692 __xml_diff_object(xmlNode *old_xml, xmlNode *new_xml,
bool check_top)
3694 xmlNode *cIter = NULL;
3698 if (old_xml == NULL) {
3699 crm_node_created(new_xml);
3704 p = new_xml->_private;
3713 xml_diff_attrs(old_xml, new_xml);
3716 for (cIter = __xml_first_child(old_xml); cIter != NULL; ) {
3717 xmlNode *old_child = cIter;
3718 xmlNode *new_child = find_element(new_xml, cIter, TRUE);
3720 cIter = __xml_next(cIter);
3722 __xml_diff_object(old_child, new_child, TRUE);
3725 mark_child_deleted(old_child, new_xml);
3730 for (cIter = __xml_first_child(new_xml); cIter != NULL; ) {
3731 xmlNode *new_child = cIter;
3732 xmlNode *old_child = find_element(old_xml, cIter, TRUE);
3734 cIter = __xml_next(cIter);
3735 if(old_child == NULL) {
3737 p = new_child->_private;
3739 __xml_diff_object(old_child, new_child, TRUE);
3743 int p_new = __xml_offset(new_child);
3744 int p_old = __xml_offset(old_child);
3746 if(p_old != p_new) {
3747 mark_child_moved(old_child, new_xml, new_child, p_old, p_new);
3764 crm_element_name(new_xml)),
3772 __xml_diff_object(old_xml, new_xml, FALSE);
3778 xmlNode *tmp1 = NULL;
3795 if (added->children == NULL && removed->children == NULL) {
3806 xmlNode *cIter = NULL;
3807 xmlAttrPtr pIter = NULL;
3808 gboolean can_prune = TRUE;
3809 const char *name = crm_element_name(xml_node);
3818 for (pIter = pcmk__first_xml_attr(xml_node); pIter != NULL; pIter = pIter->next) {
3819 const char *p_name = (
const char *)pIter->name;
3827 cIter = __xml_first_child(xml_node);
3829 xmlNode *child = cIter;
3831 cIter = __xml_next(cIter);
3842 find_xml_comment(xmlNode * root, xmlNode * search_comment, gboolean exact)
3844 xmlNode *a_child = NULL;
3845 int search_offset = __xml_offset(search_comment);
3847 CRM_CHECK(search_comment->type == XML_COMMENT_NODE,
return NULL);
3849 for (a_child = __xml_first_child(root); a_child != NULL; a_child = __xml_next(a_child)) {
3851 int offset = __xml_offset(a_child);
3854 if (offset < search_offset) {
3857 }
else if (offset > search_offset) {
3866 if (a_child->type == XML_COMMENT_NODE
3867 &&
safe_str_eq((
const char *)a_child->content, (
const char *)search_comment->content)) {
3879 subtract_xml_comment(xmlNode * parent, xmlNode * left, xmlNode * right,
3883 CRM_CHECK(left->type == XML_COMMENT_NODE,
return NULL);
3886 ||
safe_str_neq((
const char *)left->content, (
const char *)right->content)) {
3887 xmlNode *deleted = NULL;
3900 gboolean full, gboolean * changed,
const char *marker)
3902 gboolean dummy = FALSE;
3903 gboolean skip = FALSE;
3904 xmlNode *diff = NULL;
3905 xmlNode *right_child = NULL;
3906 xmlNode *left_child = NULL;
3907 xmlAttrPtr xIter = NULL;
3909 const char *
id = NULL;
3910 const char *name = NULL;
3911 const char *value = NULL;
3912 const char *right_val = NULL;
3915 static int filter_len =
DIMOF(filter);
3917 if (changed == NULL) {
3925 if (left->type == XML_COMMENT_NODE) {
3926 return subtract_xml_comment(parent, left, right, changed);
3930 if (right == NULL) {
3931 xmlNode *deleted = NULL;
3933 crm_trace(
"Processing <%s id=%s> (complete copy)", crm_element_name(left),
id);
3941 name = crm_element_name(left);
3947 if (value != NULL && strcmp(value,
"removed:top") == 0) {
3948 crm_trace(
"We are the root of the deletion: %s.id=%s", name,
id);
3957 for (lpc = 0; lpc < filter_len; lpc++) {
3958 filter[lpc].found = FALSE;
3962 for (left_child = __xml_first_child(left); left_child != NULL;
3963 left_child = __xml_next(left_child)) {
3964 gboolean child_changed = FALSE;
3966 right_child = find_element(right, left_child, FALSE);
3968 if (child_changed) {
3973 if (*changed == FALSE) {
3977 xmlAttrPtr pIter = NULL;
3979 for (pIter = pcmk__first_xml_attr(left); pIter != NULL; pIter = pIter->next) {
3980 const char *p_name = (
const char *)pIter->name;
3981 const char *p_value = pcmk__xml_attr_value(pIter);
3991 for (xIter = pcmk__first_xml_attr(left); xIter != NULL; xIter = xIter->next) {
3992 const char *prop_name = (
const char *)xIter->name;
3993 xmlAttrPtr right_attr = NULL;
4003 for (lpc = 0; skip == FALSE && lpc < filter_len; lpc++) {
4004 if (filter[lpc].found == FALSE && strcmp(prop_name, filter[lpc].
string) == 0) {
4005 filter[lpc].found = TRUE;
4015 right_attr = xmlHasProp(right, (
pcmkXmlStr) prop_name);
4017 p = right_attr->_private;
4025 xmlAttrPtr pIter = NULL;
4027 for (pIter = pcmk__first_xml_attr(left); pIter != NULL; pIter = pIter->next) {
4028 const char *p_name = (
const char *)pIter->name;
4029 const char *p_value = pcmk__xml_attr_value(pIter);
4046 if (strcmp(left_value, right_val) == 0) {
4052 xmlAttrPtr pIter = NULL;
4054 crm_trace(
"Changes detected to %s in <%s id=%s>", prop_name,
4055 crm_element_name(left),
id);
4056 for (pIter = pcmk__first_xml_attr(left); pIter != NULL; pIter = pIter->next) {
4057 const char *p_name = (
const char *)pIter->name;
4058 const char *p_value = pcmk__xml_attr_value(pIter);
4065 crm_trace(
"Changes detected to %s (%s -> %s) in <%s id=%s>",
4066 prop_name, left_value, right_val, crm_element_name(left),
id);
4073 if (*changed == FALSE) {
4077 }
else if (full == FALSE &&
id) {
4085 add_xml_comment(xmlNode * parent, xmlNode * target, xmlNode * update)
4088 CRM_CHECK(update->type == XML_COMMENT_NODE,
return 0);
4090 if (target == NULL) {
4091 target = find_xml_comment(parent, update, FALSE);
4094 if (target == NULL) {
4098 }
else if (
safe_str_neq((
const char *)target->content, (
const char *)update->content)) {
4099 xmlFree(target->content);
4100 target->content = xmlStrdup(update->content);
4107 add_xml_object(xmlNode * parent, xmlNode * target, xmlNode * update, gboolean as_diff)
4109 xmlNode *a_child = NULL;
4110 const char *object_name = NULL,
4111 *object_href = NULL,
4112 *object_href_val = NULL;
4121 if (update->type == XML_COMMENT_NODE) {
4122 return add_xml_comment(parent, target, update);
4125 object_name = crm_element_name(update);
4126 object_href_val =
ID(update);
4127 if (object_href_val != NULL) {
4134 CRM_CHECK(object_name != NULL,
return 0);
4135 CRM_CHECK(target != NULL || parent != NULL,
return 0);
4137 if (target == NULL) {
4138 target = find_entity_by_attr_or_just_name(parent, object_name,
4139 object_href, object_href_val);
4142 if (target == NULL) {
4145 #if XML_PARSER_DEBUG
4147 object_href ?
" " :
"",
4148 object_href ? object_href :
"",
4149 object_href ?
"=" :
"",
4150 object_href ? object_href_val :
"");
4154 object_href ?
" " :
"",
4155 object_href ? object_href :
"",
4156 object_href ?
"=" :
"",
4157 object_href ? object_href_val :
"");
4163 if (as_diff == FALSE) {
4169 xmlAttrPtr pIter = NULL;
4171 for (pIter = pcmk__first_xml_attr(update); pIter != NULL; pIter = pIter->next) {
4172 const char *p_name = (
const char *)pIter->name;
4173 const char *p_value = pcmk__xml_attr_value(pIter);
4181 for (a_child = __xml_first_child(update); a_child != NULL; a_child = __xml_next(a_child)) {
4182 #if XML_PARSER_DEBUG
4184 object_href ?
" " :
"",
4185 object_href ? object_href :
"",
4186 object_href ?
"=" :
"",
4187 object_href ? object_href_val :
"");
4189 add_xml_object(target, NULL, a_child, as_diff);
4192 #if XML_PARSER_DEBUG
4194 object_href ?
" " :
"",
4195 object_href ? object_href :
"",
4196 object_href ?
"=" :
"",
4197 object_href ? object_href_val :
"");
4205 gboolean can_update = TRUE;
4206 xmlNode *child_of_child = NULL;
4209 CRM_CHECK(to_update != NULL,
return FALSE);
4211 if (
safe_str_neq(crm_element_name(to_update), crm_element_name(child))) {
4217 }
else if (can_update) {
4218 #if XML_PARSER_DEBUG
4221 add_xml_object(NULL, child, to_update, FALSE);
4224 for (child_of_child = __xml_first_child(child); child_of_child != NULL;
4225 child_of_child = __xml_next(child_of_child)) {
4238 const char *tag,
const char *field,
const char *value, gboolean search_matches)
4240 int match_found = 0;
4243 CRM_CHECK(children != NULL,
return FALSE);
4245 if (tag != NULL &&
safe_str_neq(tag, crm_element_name(root))) {
4250 if (*children == NULL) {
4257 if (search_matches || match_found == 0) {
4258 xmlNode *child = NULL;
4260 for (child = __xml_first_child(root); child != NULL; child = __xml_next(child)) {
4261 match_found +=
find_xml_children(children, child, tag, field, value, search_matches);
4271 gboolean can_delete = FALSE;
4272 xmlNode *child_of_child = NULL;
4274 const char *up_id = NULL;
4275 const char *child_id = NULL;
4276 const char *right_val = NULL;
4279 CRM_CHECK(update != NULL,
return FALSE);
4282 child_id =
ID(child);
4284 if (up_id == NULL || (child_id && strcmp(child_id, up_id) == 0)) {
4287 if (
safe_str_neq(crm_element_name(update), crm_element_name(child))) {
4290 if (can_delete && delete_only) {
4291 xmlAttrPtr pIter = NULL;
4293 for (pIter = pcmk__first_xml_attr(update); pIter != NULL; pIter = pIter->next) {
4294 const char *p_name = (
const char *)pIter->name;
4295 const char *p_value = pcmk__xml_attr_value(pIter);
4304 if (can_delete && parent != NULL) {
4306 if (delete_only || update == NULL) {
4311 xmlDoc *doc = tmp->doc;
4312 xmlNode *old = NULL;
4315 old = xmlReplaceNode(child, tmp);
4323 xmlDocSetRootElement(doc, old);
4329 }
else if (can_delete) {
4334 child_of_child = __xml_first_child(child);
4335 while (child_of_child) {
4336 xmlNode *next = __xml_next(child_of_child);
4342 child_of_child = NULL;
4344 child_of_child = next;
4354 xmlNode *child = NULL;
4355 GSList *nvpairs = NULL;
4356 xmlNode *result = NULL;
4357 const char *name = NULL;
4361 name = crm_element_name(input);
4370 for (child = __xml_first_child(input); child != NULL;
4371 child = __xml_next(child)) {
4386 xmlNode *match = NULL;
4388 for (match = __xml_first_child_element(parent); match != NULL;
4389 match = __xml_next_element(match)) {
4395 if (name == NULL || strcmp((
const char *)match->name, name) == 0) {
4412 xmlNode *match = __xml_next_element(sibling);
4413 const char *name = crm_element_name(sibling);
4415 while (match != NULL) {
4416 if (!strcmp(crm_element_name(match), name)) {
4419 match = __xml_next_element(match);
4427 static bool init = TRUE;
4436 xmlSetBufferAllocationScheme(XML_BUFFER_ALLOC_DOUBLEIT);
4439 xmlDeregisterNodeDefault(pcmkDeregisterNode);
4440 xmlRegisterNodeDefault(pcmkRegisterNode);
4449 crm_info(
"Cleaning up memory from libxml2");
4454 #define XPATH_MAX 512
4459 const char *tag = NULL;
4460 const char *ref = NULL;
4461 xmlNode *result = input;
4463 if (result == NULL) {
4466 }
else if (top == NULL) {
4470 tag = crm_element_name(result);
4477 if (result == NULL) {
4478 char *nodePath = (
char *)xmlGetNodePath(top);
4480 crm_err(
"No match for %s found in %s: Invalid configuration", xpath_string,
4498 static const char *base = NULL;
4502 base = getenv(
"PCMK_schema_directory");
4504 if (base == NULL || base[0] ==
'\0') {
4518 crm_err(
"XML artefact family specified as %u not recognized", ns);
4538 crm_err(
"XML artefact family specified as %u not recognized", ns);