root/include/crm/common/xml.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. crm_element_name
  2. crm_map_element_name
  3. numXpathResults

   1 /*
   2  * Copyright 2004-2021 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #ifndef PCMK__CRM_COMMON_XML__H
  11 #  define PCMK__CRM_COMMON_XML__H
  12 
  13 
  14 #  include <stdio.h>
  15 #  include <sys/types.h>
  16 #  include <unistd.h>
  17 
  18 #  include <stdlib.h>
  19 #  include <errno.h>
  20 #  include <fcntl.h>
  21 
  22 #  include <libxml/tree.h>
  23 #  include <libxml/xpath.h>
  24 
  25 #  include <crm/crm.h>
  26 #  include <crm/common/nvpair.h>
  27 
  28 #ifdef __cplusplus
  29 extern "C" {
  30 #endif
  31 
  32 /**
  33  * \file
  34  * \brief Wrappers for and extensions to libxml2
  35  * \ingroup core
  36  */
  37 
  38 /* Define compression parameters for IPC messages
  39  *
  40  * Compression costs a LOT, so we don't want to do it unless we're hitting
  41  * message limits. Currently, we use 128KB as the threshold, because higher
  42  * values don't play well with the heartbeat stack. With an earlier limit of
  43  * 10KB, compressing 184 of 1071 messages accounted for 23% of the total CPU
  44  * used by the cib.
  45  */
  46 #  define CRM_BZ2_BLOCKS                4
  47 #  define CRM_BZ2_WORK          20
  48 #  define CRM_BZ2_THRESHOLD     128 * 1024
  49 
  50 #  define XML_PARANOIA_CHECKS 0
  51 
  52 typedef const xmlChar *pcmkXmlStr;
  53 
  54 gboolean add_message_xml(xmlNode * msg, const char *field, xmlNode * xml);
  55 xmlNode *get_message_xml(xmlNode * msg, const char *field);
  56 
  57 xmlDoc *getDocPtr(xmlNode * node);
  58 
  59 /*
  60  * Replacement function for xmlCopyPropList which at the very least,
  61  * doesn't work the way *I* would expect it to.
  62  *
  63  * Copy all the attributes/properties from src into target.
  64  *
  65  * Not recursive, does not return anything.
  66  *
  67  */
  68 void copy_in_properties(xmlNode * target, xmlNode * src);
  69 void expand_plus_plus(xmlNode * target, const char *name, const char *value);
  70 void fix_plus_plus_recursive(xmlNode * target);
  71 
  72 /*
  73  * Create a node named "name" as a child of "parent"
  74  * If parent is NULL, creates an unconnected node.
  75  *
  76  * Returns the created node
  77  *
  78  */
  79 xmlNode *create_xml_node(xmlNode * parent, const char *name);
  80 
  81 /*
  82  * Create a node named "name" as a child of "parent", giving it the provided
  83  * text content.
  84  * If parent is NULL, creates an unconnected node.
  85  *
  86  * Returns the created node
  87  *
  88  */
  89 xmlNode *pcmk_create_xml_text_node(xmlNode * parent, const char *name, const char *content);
  90 
  91 /*
  92  * Create a new HTML node named "element_name" as a child of "parent", giving it the
  93  * provided text content.  Optionally, apply a CSS #id and #class.
  94  *
  95  * Returns the created node.
  96  */
  97 xmlNode *pcmk_create_html_node(xmlNode * parent, const char *element_name, const char *id,
  98                                const char *class_name, const char *text);
  99 
 100 /*
 101  *
 102  */
 103 void purge_diff_markers(xmlNode * a_node);
 104 
 105 /*
 106  * Returns a deep copy of src_node
 107  *
 108  */
 109 xmlNode *copy_xml(xmlNode * src_node);
 110 
 111 /*
 112  * Add a copy of xml_node to new_parent
 113  */
 114 xmlNode *add_node_copy(xmlNode * new_parent, xmlNode * xml_node);
 115 
 116 int add_node_nocopy(xmlNode * parent, const char *name, xmlNode * child);
 117 
 118 /*
 119  * XML I/O Functions
 120  *
 121  * Whitespace between tags is discarded.
 122  */
 123 xmlNode *filename2xml(const char *filename);
 124 
 125 xmlNode *stdin2xml(void);
 126 
 127 xmlNode *string2xml(const char *input);
 128 
 129 int write_xml_fd(xmlNode * xml_node, const char *filename, int fd, gboolean compress);
 130 int write_xml_file(xmlNode * xml_node, const char *filename, gboolean compress);
 131 
 132 char *dump_xml_formatted(xmlNode * msg);
 133 /* Also dump the text node with xml_log_option_text enabled */ 
 134 char *dump_xml_formatted_with_text(xmlNode * msg);
 135 
 136 char *dump_xml_unformatted(xmlNode * msg);
 137 
 138 /*
 139  * Diff related Functions
 140  */
 141 xmlNode *diff_xml_object(xmlNode * left, xmlNode * right, gboolean suppress);
 142 
 143 xmlNode *subtract_xml_object(xmlNode * parent, xmlNode * left, xmlNode * right,
 144                              gboolean full, gboolean * changed, const char *marker);
 145 
 146 gboolean can_prune_leaf(xmlNode * xml_node);
 147 
 148 /*
 149  * Searching & Modifying
 150  */
 151 xmlNode *find_xml_node(xmlNode * cib, const char *node_path, gboolean must_find);
 152 
 153 void xml_remove_prop(xmlNode * obj, const char *name);
 154 
 155 gboolean replace_xml_child(xmlNode * parent, xmlNode * child, xmlNode * update,
 156                            gboolean delete_only);
 157 
 158 gboolean update_xml_child(xmlNode * child, xmlNode * to_update);
 159 
 160 int find_xml_children(xmlNode ** children, xmlNode * root,
 161                       const char *tag, const char *field, const char *value,
 162                       gboolean search_matches);
 163 
 164 xmlNode *get_xpath_object(const char *xpath, xmlNode * xml_obj, int error_level);
 165 xmlNode *get_xpath_object_relative(const char *xpath, xmlNode * xml_obj, int error_level);
 166 
 167 static inline const char *
 168 crm_element_name(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170     return xml? (const char *)(xml->name) : NULL;
 171 }
 172 
 173 static inline const char *
 174 crm_map_element_name(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176     const char *name = crm_element_name(xml);
 177 
 178     if (strcmp(name, "master") == 0) {
 179         return "clone";
 180     } else {
 181         return name;
 182     }
 183 }
 184 
 185 gboolean xml_has_children(const xmlNode * root);
 186 
 187 char *calculate_on_disk_digest(xmlNode * local_cib);
 188 char *calculate_operation_digest(xmlNode * local_cib, const char *version);
 189 char *calculate_xml_versioned_digest(xmlNode * input, gboolean sort, gboolean do_filter,
 190                                      const char *version);
 191 
 192 /* schema-related functions (from schemas.c) */
 193 gboolean validate_xml(xmlNode * xml_blob, const char *validation, gboolean to_logs);
 194 gboolean validate_xml_verbose(xmlNode * xml_blob);
 195 
 196 /*!
 197  * \brief Update CIB XML to most recent schema version
 198  *
 199  * "Update" means either actively employ XSLT-based transformation(s)
 200  * (if intermediate product to transform valid per its declared schema version,
 201  * transformation available, proceeded successfully with a result valid per
 202  * expectated newer schema version), or just try to bump the marked validating
 203  * schema until all gradually rising schema versions attested or the first
 204  * such attempt subsequently fails to validate.   Which of the two styles will
 205  * be used depends on \p transform parameter (positive/negative, respectively).
 206  *
 207  * \param[in,out] xml_blob   XML tree representing CIB, may be swapped with
 208  *                           an "updated" one
 209  * \param[out]    best       The highest configuration version (per its index
 210  *                           in the global schemas table) it was possible to
 211  *                           reach during the update steps while ensuring
 212  *                           the validity of the result; if no validation
 213  *                           success was observed against possibly multiple
 214  *                           schemas, the value is less or equal the result
 215  *                           of \c get_schema_version applied on the input
 216  *                           \p xml_blob value (unless that function maps it
 217  *                           to -1, then 0 would be used instead)
 218  * \param[in]     max        When \p transform is positive, this allows to
 219  *                           set upper boundary schema (per its index in the
 220  *                           global schemas table) beyond which it's forbidden
 221  *                           to update by the means of XSLT transformation
 222  * \param[in]     transform  Whether to employ XSLT-based transformation so
 223  *                           as to allow overcoming possible incompatibilities
 224  *                           between major schema versions (see above)
 225  * \param[in]     to_logs    If true, output notable progress info to
 226  *                           internal log streams; if false, to stderr
 227  *
 228  * \return \c pcmk_ok if no non-recoverable error encountered (up to
 229  *         caller to evaluate if the update satisfies the requirements
 230  *         per returned \p best value), negative value carrying the reason
 231  *         otherwise
 232  */
 233 int update_validation(xmlNode **xml_blob, int *best, int max,
 234                       gboolean transform, gboolean to_logs);
 235 
 236 int get_schema_version(const char *name);
 237 const char *get_schema_name(int version);
 238 const char *xml_latest_schema(void);
 239 gboolean cli_config_update(xmlNode ** xml, int *best_version, gboolean to_logs);
 240 
 241 /*!
 242  * \brief Initialize the CRM XML subsystem
 243  *
 244  * This method sets global XML settings and loads pacemaker schemas into the cache.
 245  */
 246 void crm_xml_init(void);
 247 void crm_xml_cleanup(void);
 248 
 249 void pcmk_free_xml_subtree(xmlNode *xml);
 250 void free_xml(xmlNode * child);
 251 
 252 xmlNode *first_named_child(const xmlNode *parent, const char *name);
 253 xmlNode *crm_next_same_xml(const xmlNode *sibling);
 254 
 255 xmlNode *sorted_xml(xmlNode * input, xmlNode * parent, gboolean recursive);
 256 xmlXPathObjectPtr xpath_search(xmlNode * xml_top, const char *path);
 257 void crm_foreach_xpath_result(xmlNode *xml, const char *xpath,
 258                               void (*helper)(xmlNode*, void*), void *user_data);
 259 xmlNode *expand_idref(xmlNode * input, xmlNode * top);
 260 
 261 void freeXpathObject(xmlXPathObjectPtr xpathObj);
 262 xmlNode *getXpathResult(xmlXPathObjectPtr xpathObj, int index);
 263 void dedupXpathResults(xmlXPathObjectPtr xpathObj);
 264 
 265 static inline int numXpathResults(xmlXPathObjectPtr xpathObj)
     /* [previous][next][first][last][top][bottom][index][help] */
 266 {
 267     if(xpathObj == NULL || xpathObj->nodesetval == NULL) {
 268         return 0;
 269     }
 270     return xpathObj->nodesetval->nodeNr;
 271 }
 272 
 273 bool xml_tracking_changes(xmlNode * xml);
 274 bool xml_document_dirty(xmlNode *xml);
 275 void xml_track_changes(xmlNode * xml, const char *user, xmlNode *acl_source, bool enforce_acls);
 276 void xml_calculate_changes(xmlNode *old_xml, xmlNode *new_xml);
 277 void xml_calculate_significant_changes(xmlNode *old_xml, xmlNode *new_xml);
 278 void xml_accept_changes(xmlNode * xml);
 279 void xml_log_changes(uint8_t level, const char *function, xmlNode *xml);
 280 void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml);
 281 bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3]);
 282 
 283 xmlNode *xml_create_patchset(
 284     int format, xmlNode *source, xmlNode *target, bool *config, bool manage_version);
 285 int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version);
 286 
 287 void patchset_process_digest(xmlNode *patch, xmlNode *source, xmlNode *target, bool with_digest);
 288 
 289 void save_xml_to_file(xmlNode * xml, const char *desc, const char *filename);
 290 char *xml_get_path(xmlNode *xml);
 291 
 292 char * crm_xml_escape(const char *text);
 293 void crm_xml_sanitize_id(char *id);
 294 void crm_xml_set_id(xmlNode *xml, const char *format, ...) G_GNUC_PRINTF(2, 3);
 295 
 296 /*!
 297  * \brief xmlNode destructor which can be used in glib collections
 298  */
 299 void crm_destroy_xml(gpointer data);
 300 
 301 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
 302 #include <crm/common/xml_compat.h>
 303 #endif
 304 
 305 #ifdef __cplusplus
 306 }
 307 #endif
 308 
 309 #endif

/* [previous][next][first][last][top][bottom][index][help] */