13 #include <sys/types.h> 21 #include <libxml/tree.h> 29 static xmlNode *subtract_xml_comment(xmlNode *
parent, xmlNode *left,
30 xmlNode *right, gboolean *changed);
36 add_xml_changes_to_patchset(xmlNode *xml, xmlNode *patchset)
38 xmlNode *cIter = NULL;
39 xmlAttr *pIter = NULL;
40 xmlNode *change = NULL;
42 const char *value = NULL;
44 if (nodepriv == NULL) {
65 g_string_free(xpath, TRUE);
72 for (pIter = pcmk__xe_first_attr(xml); pIter != NULL;
73 pIter = pIter->next) {
76 nodepriv = pIter->_private;
91 g_string_free(xpath, TRUE);
104 value = pcmk__xml_attr_value(pIter);
115 for (pIter = pcmk__xe_first_attr(xml); pIter != NULL;
116 pIter = pIter->next) {
117 nodepriv = pIter->_private;
126 for (cIter = pcmk__xml_first_child(xml); cIter != NULL;
127 cIter = pcmk__xml_next(cIter)) {
128 add_xml_changes_to_patchset(cIter, patchset);
131 nodepriv = xml->_private;
145 g_string_free(xpath, TRUE);
151 is_config_change(xmlNode *xml)
159 nodepriv = config->_private;
165 if ((xml->doc != NULL) && (xml->doc->_private != NULL)) {
166 docpriv = xml->doc->_private;
167 for (gIter = docpriv->
deleted_objs; gIter; gIter = gIter->next) {
170 if (strstr(deleted_obj->
path,
180 xml_repair_v1_diff(xmlNode *last, xmlNode *next, xmlNode *local_diff,
185 xmlNode *diff_child = NULL;
187 const char *tag = NULL;
189 const char *vfields[] = {
195 if (local_diff == NULL) {
202 if (diff_child == NULL) {
212 for (lpc = 0; (last != NULL) && (lpc <
PCMK__NELEM(vfields)); lpc++) {
216 if (changed || lpc == 2) {
223 if (diff_child == NULL) {
233 for (lpc = 0; next && lpc <
PCMK__NELEM(vfields); lpc++) {
239 for (xmlAttrPtr a = pcmk__xe_first_attr(next); a != NULL; a = a->next) {
241 const char *p_value = pcmk__xml_attr_value(a);
243 xmlSetProp(cib, a->name, (
pcmkXmlStr) p_value);
250 xml_create_patchset_v1(xmlNode *source, xmlNode *
target,
bool config,
257 xml_repair_v1_diff(source,
target, patchset, config);
264 xml_create_patchset_v2(xmlNode *source, xmlNode *
target)
272 xmlNode *patchset = NULL;
273 const char *vfields[] = {
285 docpriv =
target->doc->_private;
312 for (gIter = docpriv->
deleted_objs; gIter; gIter = gIter->next) {
323 add_xml_changes_to_patchset(
target, patchset);
329 bool *config_changed,
bool manage_version)
333 xmlNode *patch = NULL;
342 config = is_config_change(
target);
343 if (config_changed) {
344 *config_changed = config;
347 if (manage_version && config) {
354 }
else if (manage_version) {
356 crm_trace(
"Status changed %d - %d %s", format, counter,
372 patch = xml_create_patchset_v1(source,
target, config, FALSE);
375 patch = xml_create_patchset_v2(source,
target);
378 crm_err(
"Unknown patch format: %d", format);
392 if ((patch == NULL) || (source == NULL) || (
target == NULL)) {
402 if ((format > 1) && !with_digest) {
417 not_id(xmlAttrPtr attr,
void *user_data)
419 return strcmp((
const char *) attr->name,
XML_ATTR_ID) != 0;
424 process_v1_removals(xmlNode *
target, xmlNode *patch)
426 xmlNode *patch_child = NULL;
427 xmlNode *cIter = NULL;
430 const char *value = NULL;
432 if ((
target == NULL) || (patch == NULL)) {
436 if (
target->type == XML_COMMENT_NODE) {
448 if ((value != NULL) && (strcmp(value,
"removed:top") == 0)) {
449 crm_trace(
"We are the root of the deletion: %s.id=%s",
460 cIter = pcmk__xml_first_child(
target);
462 xmlNode *target_child = cIter;
464 cIter = pcmk__xml_next(cIter);
466 process_v1_removals(target_child, patch_child);
473 process_v1_additions(xmlNode *
parent, xmlNode *
target, xmlNode *patch)
475 xmlNode *patch_child = NULL;
476 xmlNode *target_child = NULL;
477 xmlAttrPtr xIter = NULL;
479 const char *
id = NULL;
480 const char *
name = NULL;
481 const char *value = NULL;
490 name = (
const char *) patch->name;
492 if ((
target == NULL) && (value != NULL)
493 && (strcmp(value,
"added:top") == 0)) {
495 crm_trace(
"We are the root of the addition: %s.id=%s",
name,
id);
499 }
else if (
target == NULL) {
505 if (
target->type == XML_COMMENT_NODE) {
512 for (xIter = pcmk__xe_first_attr(patch); xIter != NULL;
513 xIter = xIter->next) {
514 const char *p_name = (
const char *) xIter->name;
515 const char *p_value = pcmk__xml_attr_value(xIter);
522 for (patch_child = pcmk__xml_first_child(patch); patch_child != NULL;
523 patch_child = pcmk__xml_next(patch_child)) {
526 process_v1_additions(
target, target_child, patch_child);
542 find_patch_xml_node(
const xmlNode *patchset,
int format,
bool added,
543 xmlNode **patch_node)
553 if (cib_node != NULL) {
554 *patch_node = cib_node;
558 label = added?
"target" :
"source";
563 crm_warn(
"Unknown patch format: %d", format);
578 const char *vfields[] = {
588 if (!find_patch_xml_node(patchset, format, FALSE, &tmp)) {
594 crm_trace(
"Got %d for del[%s]", del[lpc], vfields[lpc]);
599 if (!find_patch_xml_node(patchset, format, TRUE, &tmp)) {
605 crm_trace(
"Got %d for add[%s]", add[lpc], vfields[lpc]);
621 xml_patch_version_check(
const xmlNode *xml,
const xmlNode *patchset)
624 bool changed = FALSE;
626 int this[] = { 0, 0, 0 };
627 int add[] = { 0, 0, 0 };
628 int del[] = { 0, 0, 0 };
630 const char *vfields[] = {
638 crm_trace(
"Got %d for this[%s]",
this[lpc], vfields[lpc]);
647 add[2] =
this[2] + 1;
649 del[lpc] =
this[lpc];
655 if (
this[lpc] < del[lpc]) {
656 crm_debug(
"Current %s is too low (%d.%d.%d < %d.%d.%d --> %d.%d.%d)",
657 vfields[lpc],
this[0],
this[1],
this[2],
658 del[0], del[1], del[2], add[0], add[1], add[2]);
661 }
else if (
this[lpc] > del[lpc]) {
662 crm_info(
"Current %s is too high (%d.%d.%d > %d.%d.%d --> %d.%d.%d) %p",
663 vfields[lpc],
this[0],
this[1],
this[2],
664 del[0], del[1], del[2], add[0], add[1], add[2], patchset);
671 if (add[lpc] > del[lpc]) {
677 crm_notice(
"Versions did not change in patch %d.%d.%d",
678 add[0], add[1], add[2]);
682 crm_debug(
"Can apply patch %d.%d.%d to %d.%d.%d",
683 add[0], add[1], add[2],
this[0],
this[1],
this[2]);
697 apply_v1_patchset(xmlNode *xml,
const xmlNode *patchset)
700 int root_nodes_seen = 0;
702 xmlNode *child_diff = NULL;
708 for (child_diff = pcmk__xml_first_child(removed); child_diff != NULL;
709 child_diff = pcmk__xml_next(child_diff)) {
710 CRM_CHECK(root_nodes_seen == 0, rc = FALSE);
711 if (root_nodes_seen == 0) {
712 process_v1_removals(xml, child_diff);
717 if (root_nodes_seen > 1) {
718 crm_err(
"(-) Diffs cannot contain more than one change set... saw %d",
726 xmlNode *child_diff = NULL;
728 for (child_diff = pcmk__xml_first_child(added); child_diff != NULL;
729 child_diff = pcmk__xml_next(child_diff)) {
730 CRM_CHECK(root_nodes_seen == 0, rc = FALSE);
731 if (root_nodes_seen == 0) {
732 process_v1_additions(NULL, xml, child_diff);
738 if (root_nodes_seen > 1) {
739 crm_err(
"(+) Diffs cannot contain more than one change set... saw %d",
752 first_matching_xml_child(
const xmlNode *
parent,
const char *
name,
753 const char *
id,
int position)
755 xmlNode *cIter = NULL;
757 for (cIter = pcmk__xml_first_child(
parent); cIter != NULL;
758 cIter = pcmk__xml_next(cIter)) {
759 if (strcmp((
const char *) cIter->name,
name) != 0) {
762 const char *cid =
ID(cIter);
764 if ((cid == NULL) || (strcmp(cid,
id) != 0)) {
770 if ((cIter->type == XML_COMMENT_NODE)
795 search_v2_xpath(
const xmlNode *top,
const char *key,
int target_position)
797 xmlNode *
target = (xmlNode *) top->doc;
798 const char *current = key;
808 key_len = strlen(key);
814 remainder = calloc(key_len,
sizeof(
char));
817 section = calloc(key_len,
sizeof(
char));
820 id = calloc(key_len,
sizeof(
char));
823 tag = calloc(key_len,
sizeof(
char));
828 rc = sscanf(current,
"/%[^/]%s", section, remainder);
831 int f = sscanf(section,
"%[^[][@" XML_ATTR_ID "='%[^']", tag,
id);
832 int current_position = -1;
837 if ((rc == 1) && (target_position >= 0)) {
838 current_position = target_position;
859 }
while ((rc == 2) &&
target);
863 (
path = (
char *) xmlGetNodePath(
target)), key);
876 typedef struct xml_change_obj_s {
877 const xmlNode *change;
882 sort_change_obj_by_position(gconstpointer a, gconstpointer b)
892 if (position_a < position_b) {
895 }
else if (position_a > position_b) {
912 apply_v2_patchset(xmlNode *xml,
const xmlNode *patchset)
915 const xmlNode *change = NULL;
916 GList *change_objs = NULL;
919 for (change = pcmk__xml_first_child(patchset); change != NULL;
920 change = pcmk__xml_next(change)) {
921 xmlNode *match = NULL;
930 crm_trace(
"Processing %s %s", change->name, op);
933 if (strcmp(op,
"delete") == 0) {
936 match = search_v2_xpath(xml, xpath, position);
937 crm_trace(
"Performing %s on %s with %p", op, xpath, match);
939 if ((match == NULL) && (strcmp(op,
"delete") == 0)) {
940 crm_debug(
"No %s match for %s in %p", op, xpath, xml->doc);
943 }
else if (match == NULL) {
944 crm_err(
"No %s match for %s in %p", op, xpath, xml->doc);
948 }
else if ((strcmp(op,
"create") == 0) || (strcmp(op,
"move") == 0)) {
954 change_obj->change = change;
955 change_obj->match = match;
957 change_objs = g_list_append(change_objs, change_obj);
959 if (strcmp(op,
"move") == 0) {
961 if ((match->parent != NULL) && (match->parent->last != NULL)) {
962 xmlAddNextSibling(match->parent->last, match);
966 }
else if (strcmp(op,
"delete") == 0) {
969 }
else if (strcmp(op,
"modify") == 0) {
970 xmlNode *attrs = NULL;
980 for (xmlAttrPtr pIter = pcmk__xe_first_attr(attrs); pIter != NULL;
981 pIter = pIter->next) {
982 const char *
name = (
const char *) pIter->name;
983 const char *value = pcmk__xml_attr_value(pIter);
989 crm_err(
"Unknown operation: %s", op);
995 change_objs = g_list_sort(change_objs, sort_change_obj_by_position);
997 for (gIter = change_objs; gIter; gIter = gIter->next) {
999 xmlNode *match = change_obj->match;
1000 const char *op = NULL;
1001 const char *xpath = NULL;
1003 change = change_obj->change;
1008 crm_trace(
"Continue performing %s on %s with %p", op, xpath, match);
1010 if (strcmp(op,
"create") == 0) {
1012 xmlNode *child = NULL;
1013 xmlNode *match_child = NULL;
1015 match_child = match->children;
1018 while ((match_child != NULL)
1020 match_child = match_child->next;
1023 child = xmlDocCopyNode(change->children, match->doc, 1);
1024 if (child == NULL) {
1029 crm_trace(
"Adding %s at position %d", child->name, position);
1030 xmlAddPrevSibling(match_child, child);
1032 }
else if (match->last) {
1033 crm_trace(
"Adding %s at position %d (end)",
1034 child->name, position);
1035 xmlAddNextSibling(match->last, child);
1038 crm_trace(
"Adding %s at position %d (first)",
1039 child->name, position);
1041 xmlAddChild(match, child);
1045 }
else if (strcmp(op,
"move") == 0) {
1050 xmlNode *match_child = NULL;
1058 match_child = match->parent->children;
1060 while ((match_child != NULL)
1062 match_child = match_child->next;
1065 crm_trace(
"Moving %s to position %d (was %d, prev %p, %s %p)",
1066 match->name, position,
1068 match->prev, (match_child?
"next":
"last"),
1069 (match_child? match_child : match->parent->last));
1072 xmlAddPrevSibling(match_child, match);
1076 xmlAddNextSibling(match->parent->last, match);
1080 crm_trace(
"%s is already in position %d",
1081 match->name, position);
1085 crm_err(
"Moved %s.%s to position %d instead of %d (%p)",
1086 match->name,
ID(match),
1088 position, match->prev);
1094 g_list_free_full(change_objs, free);
1103 xmlNode *old = NULL;
1104 const char *digest = NULL;
1106 if (patchset == NULL) {
1112 if (check_version) {
1120 if (digest != NULL) {
1137 crm_err(
"Unknown patch format: %d", format);
1142 if ((rc ==
pcmk_ok) && (digest != NULL)) {
1143 char *new_digest = NULL;
1148 crm_info(
"v%d digest mis-match: expected %s, calculated %s",
1149 format, digest, new_digest);
1161 crm_trace(
"v%d digest matched: expected %s, calculated %s",
1162 format, digest, new_digest);
1174 xmlNode *child = NULL;
1179 for (child = pcmk__xml_first_child(a_node); child != NULL;
1180 child = pcmk__xml_next(child)) {
1188 xmlNode *tmp1 = NULL;
1205 if ((added->children == NULL) && (removed->children == NULL)) {
1214 subtract_xml_comment(xmlNode *
parent, xmlNode *left, xmlNode *right,
1218 CRM_CHECK(left->type == XML_COMMENT_NODE,
return NULL);
1220 if ((right == NULL) || !pcmk__str_eq((
const char *)left->content,
1221 (
const char *)right->content,
1223 xmlNode *deleted = NULL;
1236 gboolean full, gboolean *changed,
const char *marker)
1238 gboolean
dummy = FALSE;
1239 xmlNode *diff = NULL;
1240 xmlNode *right_child = NULL;
1241 xmlNode *left_child = NULL;
1242 xmlAttrPtr xIter = NULL;
1244 const char *
id = NULL;
1245 const char *
name = NULL;
1246 const char *value = NULL;
1247 const char *right_val = NULL;
1249 if (changed == NULL) {
1257 if (left->type == XML_COMMENT_NODE) {
1258 return subtract_xml_comment(
parent, left, right, changed);
1262 name = (
const char *) left->name;
1263 if (right == NULL) {
1264 xmlNode *deleted = NULL;
1276 CRM_CHECK(pcmk__xe_is(left, (
const char *) right->name),
return NULL);
1280 if ((value != NULL) && (strcmp(value,
"removed:top") == 0)) {
1281 crm_trace(
"We are the root of the deletion: %s.id=%s",
name,
id);
1290 for (left_child = pcmk__xml_first_child(left); left_child != NULL;
1291 left_child = pcmk__xml_next(left_child)) {
1292 gboolean child_changed = FALSE;
1297 if (child_changed) {
1306 xmlAttrPtr pIter = NULL;
1308 for (pIter = pcmk__xe_first_attr(left); pIter != NULL;
1309 pIter = pIter->next) {
1310 const char *p_name = (
const char *)pIter->name;
1311 const char *p_value = pcmk__xml_attr_value(pIter);
1321 for (xIter = pcmk__xe_first_attr(left); xIter != NULL;
1322 xIter = xIter->next) {
1323 const char *prop_name = (
const char *) xIter->name;
1324 xmlAttrPtr right_attr = NULL;
1337 right_attr = xmlHasProp(right, (
pcmkXmlStr) prop_name);
1339 nodepriv = right_attr->_private;
1347 xmlAttrPtr pIter = NULL;
1349 for (pIter = pcmk__xe_first_attr(left); pIter != NULL;
1350 pIter = pIter->next) {
1351 const char *p_name = (
const char *) pIter->name;
1352 const char *p_value = pcmk__xml_attr_value(pIter);
1359 const char *left_value = pcmk__xml_attr_value(xIter);
1367 const char *left_value = pcmk__xml_attr_value(xIter);
1369 if (strcmp(left_value, right_val) == 0) {
1375 xmlAttrPtr pIter = NULL;
1379 for (pIter = pcmk__xe_first_attr(left); pIter != NULL;
1380 pIter = pIter->next) {
1381 const char *p_name = (
const char *) pIter->name;
1382 const char *p_value = pcmk__xml_attr_value(pIter);
1390 crm_trace(
"Changes detected to %s (%s -> %s) in " 1392 prop_name, left_value, right_val,
name,
id);
1403 }
else if (!full && (
id != NULL)) {
1419 int root_nodes_seen = 0;
1423 xmlNode *child_diff = NULL;
1427 CRM_CHECK(new_xml != NULL,
return FALSE);
1430 for (child_diff = pcmk__xml_first_child(removed); child_diff != NULL;
1431 child_diff = pcmk__xml_next(child_diff)) {
1433 if (root_nodes_seen == 0) {
1440 if (root_nodes_seen == 0) {
1443 }
else if (root_nodes_seen > 1) {
1444 crm_err(
"(-) Diffs cannot contain more than one change set... saw %d",
1449 root_nodes_seen = 0;
1452 xmlNode *child_diff = NULL;
1454 for (child_diff = pcmk__xml_first_child(added); child_diff != NULL;
1455 child_diff = pcmk__xml_next(child_diff)) {
1457 if (root_nodes_seen == 0) {
1464 if (root_nodes_seen > 1) {
1465 crm_err(
"(+) Diffs cannot contain more than one change set... saw %d",
1469 }
else if (
result && (digest != NULL)) {
1470 char *new_digest = NULL;
1476 crm_info(
"Digest mis-match: expected %s, calculated %s",
1477 digest, new_digest);
1490 crm_trace(
"Digest matched: expected %s, calculated %s",
1491 digest, new_digest);
#define CRM_CHECK(expr, failure_action)
#define crm_notice(fmt, args...)
void patchset_process_digest(xmlNode *patch, xmlNode *source, xmlNode *target, bool with_digest)
#define pcmk__if_tracing(if_action, else_action)
#define XML_ATTR_NUMUPDATES
int pcmk_rc2legacy(int rc)
xmlNode * first_named_child(const xmlNode *parent, const char *name)
G_GNUC_INTERNAL int pcmk__xml_position(const xmlNode *xml, enum xml_private_flags ignore_if_set)
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.
xmlNode * find_xml_node(const xmlNode *root, const char *search_path, gboolean must_find)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
#define XML_NVPAIR_ATTR_NAME
#define CRM_LOG_ASSERT(expr)
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Deprecated Pacemaker XML API.
#define XML_ATTR_GENERATION
#define XML_TAG_DIFF_ADDED
xmlNode * copy_xml(xmlNode *src_node)
#define crm_warn(fmt, args...)
#define pcmk_err_diff_failed
G_GNUC_INTERNAL void pcmk__xc_update(xmlNode *parent, xmlNode *target, xmlNode *update)
#define crm_debug(fmt, args...)
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
struct xml_change_obj_s xml_change_obj_t
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
void pcmk__xe_remove_matching_attrs(xmlNode *element, bool(*match)(xmlAttrPtr, void *), void *user_data)
#define crm_trace(fmt, args...)
#define crm_log_xml_explicit(xml, text)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
xmlNode * add_node_copy(xmlNode *new_parent, xmlNode *xml_node)
Wrappers for and extensions to libxml2.
bool xml_document_dirty(xmlNode *xml)
xmlNode * create_xml_node(xmlNode *parent, const char *name)
#define XML_DIFF_POSITION
void purge_diff_markers(xmlNode *a_node)
void free_xml(xmlNode *child)
xmlNode * xml_create_patchset(int format, xmlNode *source, xmlNode *target, bool *config_changed, bool manage_version)
G_GNUC_INTERNAL xmlNode * pcmk__xml_match(const xmlNode *haystack, const xmlNode *needle, bool exact)
const xmlChar * pcmkXmlStr
G_GNUC_INTERNAL bool pcmk__xa_filterable(const char *name)
gboolean apply_xml_diff(xmlNode *old_xml, xmlNode *diff, xmlNode **new_xml)
xmlNode * diff_xml_object(xmlNode *old, xmlNode *new, gboolean suppress)
void save_xml_to_file(const xmlNode *xml, const char *desc, const char *filename)
pcmk__action_result_t result
#define crm_err(fmt, args...)
void xml_remove_prop(xmlNode *obj, const char *name)
char * calculate_xml_versioned_digest(xmlNode *input, gboolean sort, gboolean do_filter, const char *version)
Calculate and return digest of XML tree.
gboolean can_prune_leaf(xmlNode *xml_node)
void xml_acl_disable(xmlNode *xml)
int compare_version(const char *version1, const char *version2)
#define crm_log_xml_info(xml, text)
#define XML_ATTR_GENERATION_ADMIN
#define XML_NVPAIR_ATTR_VALUE
#define XML_ATTR_CRM_VERSION
bool xml_patch_versions(const xmlNode *patchset, int add[3], int del[3])
GString * pcmk__element_xpath(const xmlNode *xml)
#define pcmk__log_xml_patchset(level, patchset)
#define XML_TAG_DIFF_REMOVED
#define XML_CIB_TAG_CONFIGURATION
xmlNode * subtract_xml_object(xmlNode *parent, xmlNode *left, xmlNode *right, gboolean full, gboolean *changed, const char *marker)
int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version)
G_GNUC_INTERNAL void pcmk__mark_xml_created(xmlNode *xml)
G_GNUC_INTERNAL void pcmk__xml_update(xmlNode *parent, xmlNode *target, xmlNode *update, bool as_diff)
#define crm_info(fmt, args...)