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-2023 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 typedef const xmlChar *pcmkXmlStr;
  51 
  52 gboolean add_message_xml(xmlNode * msg, const char *field, xmlNode * xml);
  53 xmlNode *get_message_xml(const xmlNode *msg, const char *field);
  54 
  55 xmlDoc *getDocPtr(xmlNode * node);
  56 
  57 /*
  58  * \brief xmlCopyPropList ACLs-sensitive replacement expading i++ notation
  59  *
  60  * The gist is the same as with \c{xmlCopyPropList(target, src->properties)}.
  61  * The function exits prematurely when any attribute cannot be copied for
  62  * ACLs violation.  Even without bailing out, the result can possibly be
  63  * incosistent with expectations in that case, hence the caller shall,
  64  * aposteriori, verify that no document-level-tracked denial was indicated
  65  * with \c{xml_acl_denied(target)} and drop whole such intermediate object.
  66  *
  67  * \param[in,out] target  Element to receive attributes from #src element
  68  * \param[in]     src     Element carrying attributes to copy over to #target
  69  *
  70  * \note Original commit 1c632c506 sadly haven't stated which otherwise
  71  *       assumed behaviours of xmlCopyPropList were missing beyond otherwise
  72  *       custom extensions like said ACLs and "atomic increment" (that landed
  73  *       later on, anyway).
  74  */
  75 void copy_in_properties(xmlNode *target, const xmlNode *src);
  76 
  77 void expand_plus_plus(xmlNode * target, const char *name, const char *value);
  78 void fix_plus_plus_recursive(xmlNode * target);
  79 
  80 /*
  81  * Create a node named "name" as a child of "parent"
  82  * If parent is NULL, creates an unconnected node.
  83  *
  84  * Returns the created node
  85  *
  86  */
  87 xmlNode *create_xml_node(xmlNode * parent, const char *name);
  88 
  89 /*
  90  * Create a node named "name" as a child of "parent", giving it the provided
  91  * text content.
  92  * If parent is NULL, creates an unconnected node.
  93  *
  94  * Returns the created node
  95  *
  96  */
  97 xmlNode *pcmk_create_xml_text_node(xmlNode * parent, const char *name, const char *content);
  98 
  99 /*
 100  * Create a new HTML node named "element_name" as a child of "parent", giving it the
 101  * provided text content.  Optionally, apply a CSS #id and #class.
 102  *
 103  * Returns the created node.
 104  */
 105 xmlNode *pcmk_create_html_node(xmlNode * parent, const char *element_name, const char *id,
 106                                const char *class_name, const char *text);
 107 
 108 /*
 109  *
 110  */
 111 void purge_diff_markers(xmlNode * a_node);
 112 
 113 /*
 114  * Returns a deep copy of src_node
 115  *
 116  */
 117 xmlNode *copy_xml(xmlNode * src_node);
 118 
 119 /*
 120  * Add a copy of xml_node to new_parent
 121  */
 122 xmlNode *add_node_copy(xmlNode * new_parent, xmlNode * xml_node);
 123 
 124 /*
 125  * XML I/O Functions
 126  *
 127  * Whitespace between tags is discarded.
 128  */
 129 xmlNode *filename2xml(const char *filename);
 130 
 131 xmlNode *stdin2xml(void);
 132 
 133 xmlNode *string2xml(const char *input);
 134 
 135 int write_xml_fd(xmlNode * xml_node, const char *filename, int fd, gboolean compress);
 136 int write_xml_file(xmlNode * xml_node, const char *filename, gboolean compress);
 137 
 138 char *dump_xml_formatted(xmlNode * msg);
 139 char *dump_xml_formatted_with_text(xmlNode * msg);
 140 char *dump_xml_unformatted(xmlNode * msg);
 141 
 142 /*
 143  * Diff related Functions
 144  */
 145 xmlNode *diff_xml_object(xmlNode * left, xmlNode * right, gboolean suppress);
 146 
 147 xmlNode *subtract_xml_object(xmlNode * parent, xmlNode * left, xmlNode * right,
 148                              gboolean full, gboolean * changed, const char *marker);
 149 
 150 gboolean can_prune_leaf(xmlNode * xml_node);
 151 
 152 /*
 153  * Searching & Modifying
 154  */
 155 xmlNode *find_xml_node(const xmlNode *root, const char *search_path,
 156                        gboolean must_find);
 157 
 158 void xml_remove_prop(xmlNode * obj, const char *name);
 159 
 160 gboolean replace_xml_child(xmlNode * parent, xmlNode * child, xmlNode * update,
 161                            gboolean delete_only);
 162 
 163 gboolean update_xml_child(xmlNode * child, xmlNode * to_update);
 164 
 165 int find_xml_children(xmlNode ** children, xmlNode * root,
 166                       const char *tag, const char *field, const char *value,
 167                       gboolean search_matches);
 168 
 169 xmlNode *get_xpath_object(const char *xpath, xmlNode * xml_obj, int error_level);
 170 xmlNode *get_xpath_object_relative(const char *xpath, xmlNode * xml_obj, int error_level);
 171 
 172 static inline const char *
 173 crm_element_name(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175     return xml? (const char *)(xml->name) : NULL;
 176 }
 177 
 178 static inline const char *
 179 crm_map_element_name(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181     const char *name = crm_element_name(xml);
 182 
 183     if (strcmp(name, "master") == 0) {
 184         return "clone";
 185     } else {
 186         return name;
 187     }
 188 }
 189 
 190 gboolean xml_has_children(const xmlNode * root);
 191 
 192 char *calculate_on_disk_digest(xmlNode * local_cib);
 193 char *calculate_operation_digest(xmlNode * local_cib, const char *version);
 194 char *calculate_xml_versioned_digest(xmlNode * input, gboolean sort, gboolean do_filter,
 195                                      const char *version);
 196 
 197 /* schema-related functions (from schemas.c) */
 198 gboolean validate_xml(xmlNode * xml_blob, const char *validation, gboolean to_logs);
 199 gboolean validate_xml_verbose(xmlNode * xml_blob);
 200 
 201 /*!
 202  * \brief Update CIB XML to most recent schema version
 203  *
 204  * "Update" means either actively employ XSLT-based transformation(s)
 205  * (if intermediate product to transform valid per its declared schema version,
 206  * transformation available, proceeded successfully with a result valid per
 207  * expectated newer schema version), or just try to bump the marked validating
 208  * schema until all gradually rising schema versions attested or the first
 209  * such attempt subsequently fails to validate.   Which of the two styles will
 210  * be used depends on \p transform parameter (positive/negative, respectively).
 211  *
 212  * \param[in,out] xml_blob   XML tree representing CIB, may be swapped with
 213  *                           an "updated" one
 214  * \param[out]    best       The highest configuration version (per its index
 215  *                           in the global schemas table) it was possible to
 216  *                           reach during the update steps while ensuring
 217  *                           the validity of the result; if no validation
 218  *                           success was observed against possibly multiple
 219  *                           schemas, the value is less or equal the result
 220  *                           of \c get_schema_version applied on the input
 221  *                           \p xml_blob value (unless that function maps it
 222  *                           to -1, then 0 would be used instead)
 223  * \param[in]     max        When \p transform is positive, this allows to
 224  *                           set upper boundary schema (per its index in the
 225  *                           global schemas table) beyond which it's forbidden
 226  *                           to update by the means of XSLT transformation
 227  * \param[in]     transform  Whether to employ XSLT-based transformation so
 228  *                           as to allow overcoming possible incompatibilities
 229  *                           between major schema versions (see above)
 230  * \param[in]     to_logs    If true, output notable progress info to
 231  *                           internal log streams; if false, to stderr
 232  *
 233  * \return \c pcmk_ok if no non-recoverable error encountered (up to
 234  *         caller to evaluate if the update satisfies the requirements
 235  *         per returned \p best value), negative value carrying the reason
 236  *         otherwise
 237  */
 238 int update_validation(xmlNode **xml_blob, int *best, int max,
 239                       gboolean transform, gboolean to_logs);
 240 
 241 int get_schema_version(const char *name);
 242 const char *get_schema_name(int version);
 243 const char *xml_latest_schema(void);
 244 gboolean cli_config_update(xmlNode ** xml, int *best_version, gboolean to_logs);
 245 
 246 /*!
 247  * \brief Initialize the CRM XML subsystem
 248  *
 249  * This method sets global XML settings and loads pacemaker schemas into the cache.
 250  */
 251 void crm_xml_init(void);
 252 void crm_xml_cleanup(void);
 253 
 254 void pcmk_free_xml_subtree(xmlNode *xml);
 255 void free_xml(xmlNode * child);
 256 
 257 xmlNode *first_named_child(const xmlNode *parent, const char *name);
 258 xmlNode *crm_next_same_xml(const xmlNode *sibling);
 259 
 260 xmlNode *sorted_xml(xmlNode * input, xmlNode * parent, gboolean recursive);
 261 xmlXPathObjectPtr xpath_search(xmlNode * xml_top, const char *path);
 262 void crm_foreach_xpath_result(xmlNode *xml, const char *xpath,
 263                               void (*helper)(xmlNode*, void*), void *user_data);
 264 xmlNode *expand_idref(xmlNode * input, xmlNode * top);
 265 
 266 void freeXpathObject(xmlXPathObjectPtr xpathObj);
 267 xmlNode *getXpathResult(xmlXPathObjectPtr xpathObj, int index);
 268 void dedupXpathResults(xmlXPathObjectPtr xpathObj);
 269 
 270 static inline int numXpathResults(xmlXPathObjectPtr xpathObj)
     /* [previous][next][first][last][top][bottom][index][help] */
 271 {
 272     if(xpathObj == NULL || xpathObj->nodesetval == NULL) {
 273         return 0;
 274     }
 275     return xpathObj->nodesetval->nodeNr;
 276 }
 277 
 278 bool xml_tracking_changes(xmlNode * xml);
 279 bool xml_document_dirty(xmlNode *xml);
 280 void xml_track_changes(xmlNode * xml, const char *user, xmlNode *acl_source, bool enforce_acls);
 281 void xml_calculate_changes(xmlNode *old_xml, xmlNode *new_xml);
 282 void xml_calculate_significant_changes(xmlNode *old_xml, xmlNode *new_xml);
 283 void xml_accept_changes(xmlNode * xml);
 284 bool xml_patch_versions(const xmlNode *patchset, int add[3], int del[3]);
 285 
 286 xmlNode *xml_create_patchset(
 287     int format, xmlNode *source, xmlNode *target, bool *config, bool manage_version);
 288 int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version);
 289 
 290 void patchset_process_digest(xmlNode *patch, xmlNode *source, xmlNode *target, bool with_digest);
 291 
 292 void save_xml_to_file(xmlNode * xml, const char *desc, const char *filename);
 293 
 294 char * crm_xml_escape(const char *text);
 295 void crm_xml_sanitize_id(char *id);
 296 void crm_xml_set_id(xmlNode *xml, const char *format, ...) G_GNUC_PRINTF(2, 3);
 297 
 298 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
 299 #include <crm/common/xml_compat.h>
 300 #endif
 301 
 302 #ifdef __cplusplus
 303 }
 304 #endif
 305 
 306 #endif

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