pacemaker  1.1.12-561c4cf
Scalable High-Availability cluster resource manager
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
xml.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This software is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #ifndef CRM_COMMON_XML__H
19 # define CRM_COMMON_XML__H
20 
27 # include <stdio.h>
28 # include <sys/types.h>
29 # include <unistd.h>
30 
31 # include <stdlib.h>
32 # include <errno.h>
33 # include <fcntl.h>
34 
35 # include <crm/crm.h>
36 
37 # include <libxml/tree.h>
38 # include <libxml/xpath.h>
39 
40 /* Encryption costs a LOT, don't do it unless we're hitting message limits
41  *
42  * For now, use 256k as the lower size, which means we can have 4 big data fields
43  * before we hit heartbeat's message limit
44  *
45  * The previous limit was 10k, compressing 184 of 1071 messages accounted for 23%
46  * of the total CPU used by the cib
47  */
48 # define CRM_BZ2_BLOCKS 4
49 # define CRM_BZ2_WORK 20
50 # define CRM_BZ2_THRESHOLD 128 * 1024
51 
52 # define XML_PARANOIA_CHECKS 0
53 
54 gboolean add_message_xml(xmlNode * msg, const char *field, xmlNode * xml);
55 xmlNode *get_message_xml(xmlNode * msg, const char *field);
56 GHashTable *xml2list(xmlNode * parent);
57 
58 void hash2nvpair(gpointer key, gpointer value, gpointer user_data);
59 void hash2field(gpointer key, gpointer value, gpointer user_data);
60 void hash2metafield(gpointer key, gpointer value, gpointer user_data);
61 void hash2smartfield(gpointer key, gpointer value, gpointer user_data);
62 
63 xmlDoc *getDocPtr(xmlNode * node);
64 
65 /*
66  * Replacement function for xmlCopyPropList which at the very least,
67  * doesnt work the way *I* would expect it to.
68  *
69  * Copy all the attributes/properties from src into target.
70  *
71  * Not recursive, does not return anything.
72  *
73  */
74 void copy_in_properties(xmlNode * target, xmlNode * src);
75 void expand_plus_plus(xmlNode * target, const char *name, const char *value);
76 void fix_plus_plus_recursive(xmlNode * target);
77 
78 /*
79  * Create a node named "name" as a child of "parent"
80  * If parent is NULL, creates an unconnected node.
81  *
82  * Returns the created node
83  *
84  */
85 xmlNode *create_xml_node(xmlNode * parent, const char *name);
86 
87 /*
88  * Make a copy of name and value and use the copied memory to create
89  * an attribute for node.
90  *
91  * If node, name or value are NULL, nothing is done.
92  *
93  * If name or value are an empty string, nothing is done.
94  *
95  * Returns FALSE on failure and TRUE on success.
96  *
97  */
98 const char *crm_xml_add(xmlNode * node, const char *name, const char *value);
99 
100 const char *crm_xml_replace(xmlNode * node, const char *name, const char *value);
101 
102 const char *crm_xml_add_int(xmlNode * node, const char *name, int value);
103 
104 /*
105  * Unlink the node and set its doc pointer to NULL so free_xml()
106  * will act appropriately
107  */
108 void unlink_xml_node(xmlNode * node);
109 
110 /*
111  *
112  */
113 void purge_diff_markers(xmlNode * a_node);
114 
115 /*
116  * Returns a deep copy of src_node
117  *
118  */
119 xmlNode *copy_xml(xmlNode * src_node);
120 
121 /*
122  * Add a copy of xml_node to new_parent
123  */
124 xmlNode *add_node_copy(xmlNode * new_parent, xmlNode * xml_node);
125 
126 int add_node_nocopy(xmlNode * parent, const char *name, xmlNode * child);
127 
128 /*
129  * XML I/O Functions
130  *
131  * Whitespace between tags is discarded.
132  */
133 xmlNode *filename2xml(const char *filename);
134 
135 xmlNode *stdin2xml(void);
136 
137 xmlNode *string2xml(const char *input);
138 
139 int write_xml_fd(xmlNode * xml_node, const char *filename, int fd, gboolean compress);
140 int write_xml_file(xmlNode * xml_node, const char *filename, gboolean compress);
141 
142 char *dump_xml_formatted(xmlNode * msg);
143 
144 char *dump_xml_unformatted(xmlNode * msg);
145 
146 /*
147  * Diff related Functions
148  */
149 xmlNode *diff_xml_object(xmlNode * left, xmlNode * right, gboolean suppress);
150 
151 xmlNode *subtract_xml_object(xmlNode * parent, xmlNode * left, xmlNode * right,
152  gboolean full, gboolean * changed, const char *marker);
153 
154 gboolean can_prune_leaf(xmlNode * xml_node);
155 
156 void print_xml_diff(FILE * where, xmlNode * diff);
157 
158 gboolean apply_xml_diff(xmlNode * old, xmlNode * diff, xmlNode ** new);
159 
160 /*
161  * Searching & Modifying
162  */
163 xmlNode *find_xml_node(xmlNode * cib, const char *node_path, gboolean must_find);
164 
165 xmlNode *find_entity(xmlNode * parent, const char *node_name, const char *id);
166 
167 void xml_remove_prop(xmlNode * obj, const char *name);
168 
169 gboolean replace_xml_child(xmlNode * parent, xmlNode * child, xmlNode * update,
170  gboolean delete_only);
171 
172 gboolean update_xml_child(xmlNode * child, xmlNode * to_update);
173 
174 int find_xml_children(xmlNode ** children, xmlNode * root,
175  const char *tag, const char *field, const char *value,
176  gboolean search_matches);
177 
178 int crm_element_value_int(xmlNode * data, const char *name, int *dest);
179 char *crm_element_value_copy(xmlNode * data, const char *name);
180 int crm_element_value_const_int(const xmlNode * data, const char *name, int *dest);
181 const char *crm_element_value_const(const xmlNode * data, const char *name);
182 xmlNode *get_xpath_object(const char *xpath, xmlNode * xml_obj, int error_level);
183 xmlNode *get_xpath_object_relative(const char *xpath, xmlNode * xml_obj, int error_level);
184 
185 # define crm_element_name(xml) (xml)?(const char *)(xml)->name:NULL
186 
187 const char *crm_element_value(xmlNode * data, const char *name);
188 
189 void xml_validate(const xmlNode * root);
190 
191 gboolean xml_has_children(const xmlNode * root);
192 
193 char *calculate_on_disk_digest(xmlNode * local_cib);
194 char *calculate_operation_digest(xmlNode * local_cib, const char *version);
195 char *calculate_xml_versioned_digest(xmlNode * input, gboolean sort, gboolean do_filter,
196  const char *version);
197 
198 gboolean validate_xml(xmlNode * xml_blob, const char *validation, gboolean to_logs);
199 gboolean validate_xml_verbose(xmlNode * xml_blob);
200 int update_validation(xmlNode ** xml_blob, int *best, int max, gboolean transform, gboolean to_logs);
201 int get_schema_version(const char *name);
202 const char *get_schema_name(int version);
203 
204 void crm_xml_init(void);
205 void crm_xml_cleanup(void);
206 
207 static inline xmlNode *
208 __xml_first_child(xmlNode * parent)
209 {
210  xmlNode *child = NULL;
211 
212  if (parent) {
213  child = parent->children;
214  while (child && child->type == XML_TEXT_NODE) {
215  child = child->next;
216  }
217  }
218  return child;
219 }
220 
221 static inline xmlNode *
222 __xml_next(xmlNode * child)
223 {
224  if (child) {
225  child = child->next;
226  while (child && child->type == XML_TEXT_NODE) {
227  child = child->next;
228  }
229  }
230  return child;
231 }
232 
233 void free_xml(xmlNode * child);
234 
235 xmlNode *first_named_child(xmlNode * parent, const char *name);
236 
237 xmlNode *sorted_xml(xmlNode * input, xmlNode * parent, gboolean recursive);
238 xmlXPathObjectPtr xpath_search(xmlNode * xml_top, const char *path);
239 gboolean cli_config_update(xmlNode ** xml, int *best_version, gboolean to_logs);
240 xmlNode *expand_idref(xmlNode * input, xmlNode * top);
241 
242 void freeXpathObject(xmlXPathObjectPtr xpathObj);
243 xmlNode *getXpathResult(xmlXPathObjectPtr xpathObj, int index);
244 
245 static inline int numXpathResults(xmlXPathObjectPtr xpathObj)
246 {
247  if(xpathObj == NULL || xpathObj->nodesetval == NULL) {
248  return 0;
249  }
250  return xpathObj->nodesetval->nodeNr;
251 }
252 
253 const char *xml_latest_schema(void);
254 
255 bool xml_acl_enabled(xmlNode *xml);
256 void xml_acl_disable(xmlNode *xml);
257 bool xml_acl_denied(xmlNode *xml); /* Part or all of a change was rejected */
258 bool xml_acl_filtered_copy(const char *user, xmlNode* acl_source, xmlNode *xml, xmlNode ** result);
259 
260 bool xml_tracking_changes(xmlNode * xml);
261 bool xml_document_dirty(xmlNode *xml);
262 void xml_track_changes(xmlNode * xml, const char *user, xmlNode *acl_source, bool enforce_acls);
263 void xml_calculate_changes(xmlNode * old, xmlNode * new); /* For comparing two documents after the fact */
264 void xml_accept_changes(xmlNode * xml);
265 void xml_log_changes(uint8_t level, const char *function, xmlNode *xml);
266 void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml);
267 bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3]);
268 
269 xmlNode *xml_create_patchset(
270  int format, xmlNode *source, xmlNode *target, bool *config, bool manage_version, bool with_digest);
271 int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version);
272 
273 void save_xml_to_file(xmlNode * xml, const char *desc, const char *filename);
274 char *xml_get_path(xmlNode *xml);
275 
276 #endif
xmlNode * find_xml_node(xmlNode *cib, const char *node_path, gboolean must_find)
xmlNode * get_xpath_object_relative(const char *xpath, xmlNode *xml_obj, int error_level)
void unlink_xml_node(xmlNode *node)
A dumping ground.
xmlNode * get_message_xml(xmlNode *msg, const char *field)
GHashTable * xml2list(xmlNode *parent)
void xml_validate(const xmlNode *root)
int crm_element_value_const_int(const xmlNode *data, const char *name, int *dest)
void xml_calculate_changes(xmlNode *old, xmlNode *new)
void crm_xml_cleanup(void)
xmlNode * find_entity(xmlNode *parent, const char *node_name, const char *id)
void xml_track_changes(xmlNode *xml, const char *user, xmlNode *acl_source, bool enforce_acls)
bool xml_acl_denied(xmlNode *xml)
void crm_xml_init(void)
int write_xml_file(xmlNode *xml_node, const char *filename, gboolean compress)
char * crm_element_value_copy(xmlNode *data, const char *name)
int get_schema_version(const char *name)
void print_xml_diff(FILE *where, xmlNode *diff)
void copy_in_properties(xmlNode *target, xmlNode *src)
void xml_accept_changes(xmlNode *xml)
xmlNode * filename2xml(const char *filename)
void purge_diff_markers(xmlNode *a_node)
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
xmlNode * string2xml(const char *input)
gboolean validate_xml(xmlNode *xml_blob, const char *validation, gboolean to_logs)
xmlDoc * getDocPtr(xmlNode *node)
bool xml_acl_enabled(xmlNode *xml)
const char * crm_xml_replace(xmlNode *node, const char *name, const char *value)
gboolean validate_xml_verbose(xmlNode *xml_blob)
void expand_plus_plus(xmlNode *target, const char *name, const char *value)
bool xml_tracking_changes(xmlNode *xml)
void hash2smartfield(gpointer key, gpointer value, gpointer user_data)
xmlNode * copy_xml(xmlNode *src_node)
void hash2field(gpointer key, gpointer value, gpointer user_data)
char * calculate_operation_digest(xmlNode *local_cib, const char *version)
xmlNode * stdin2xml(void)
int xml_apply_patchset(xmlNode *xml, xmlNode *patchset, bool check_version)
xmlNode * xml_create_patchset(int format, xmlNode *source, xmlNode *target, bool *config, bool manage_version, bool with_digest)
gboolean update_xml_child(xmlNode *child, xmlNode *to_update)
xmlNode * add_node_copy(xmlNode *new_parent, xmlNode *xml_node)
const char * crm_element_value_const(const xmlNode *data, const char *name)
xmlNode * expand_idref(xmlNode *input, xmlNode *top)
bool xml_document_dirty(xmlNode *xml)
xmlNode * create_xml_node(xmlNode *parent, const char *name)
int crm_element_value_int(xmlNode *data, const char *name, int *dest)
char * dump_xml_formatted(xmlNode *msg)
const char * crm_element_value(xmlNode *data, const char *name)
void xml_acl_disable(xmlNode *xml)
gboolean add_message_xml(xmlNode *msg, const char *field, xmlNode *xml)
void free_xml(xmlNode *child)
gboolean xml_has_children(const xmlNode *root)
char * calculate_on_disk_digest(xmlNode *local_cib)
xmlNode * sorted_xml(xmlNode *input, xmlNode *parent, gboolean recursive)
const char * xml_latest_schema(void)
void hash2metafield(gpointer key, gpointer value, gpointer user_data)
int find_xml_children(xmlNode **children, xmlNode *root, const char *tag, const char *field, const char *value, gboolean search_matches)
bool xml_patch_versions(xmlNode *patchset, int add[3], int del[3])
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
void xml_log_changes(uint8_t level, const char *function, xmlNode *xml)
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
gboolean apply_xml_diff(xmlNode *old, xmlNode *diff, xmlNode **new)
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
void xml_remove_prop(xmlNode *obj, const char *name)
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
char * calculate_xml_versioned_digest(xmlNode *input, gboolean sort, gboolean do_filter, const char *version)
gboolean can_prune_leaf(xmlNode *xml_node)
char * xml_get_path(xmlNode *xml)
char * dump_xml_unformatted(xmlNode *msg)
bool xml_acl_filtered_copy(const char *user, xmlNode *acl_source, xmlNode *xml, xmlNode **result)
char data[0]
Definition: internal.h:58
void save_xml_to_file(xmlNode *xml, const char *desc, const char *filename)
int update_validation(xmlNode **xml_blob, int *best, int max, gboolean transform, gboolean to_logs)
xmlNode * diff_xml_object(xmlNode *left, xmlNode *right, gboolean suppress)
int add_node_nocopy(xmlNode *parent, const char *name, xmlNode *child)
xmlNode * first_named_child(xmlNode *parent, const char *name)
xmlNode * subtract_xml_object(xmlNode *parent, xmlNode *left, xmlNode *right, gboolean full, gboolean *changed, const char *marker)
void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml)
int write_xml_fd(xmlNode *xml_node, const char *filename, int fd, gboolean compress)
gboolean cli_config_update(xmlNode **xml, int *best_version, gboolean to_logs)
void hash2nvpair(gpointer key, gpointer value, gpointer user_data)
void fix_plus_plus_recursive(xmlNode *target)
void freeXpathObject(xmlXPathObjectPtr xpathObj)
gboolean replace_xml_child(xmlNode *parent, xmlNode *child, xmlNode *update, gboolean delete_only)
const char * get_schema_name(int version)