pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
cib.c
Go to the documentation of this file.
1 /*
2  * Original copyright 2004 International Business Machines
3  * Later changes copyright 2008-2024 the Pacemaker project contributors
4  *
5  * The version control history for this file may have further details.
6  *
7  * This source code is licensed under the GNU Lesser General Public License
8  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
9  */
10 
11 #include <crm_internal.h>
12 
13 #include <stdio.h>
14 #include <libxml/tree.h> // xmlNode
15 
16 #include <crm/common/xml.h>
17 #include <crm/common/cib.h>
19 
20 /*
21  * Functions to help find particular sections of the CIB
22  */
23 
24 // Map CIB element names to their parent elements and XPath searches
25 static struct {
26  const char *name; // Name of this CIB element
27  const char *parent; // CIB element that this element is a child of
28  const char *path; // XPath to find this CIB element
29 } cib_sections[] = {
30  {
31  // This first entry is also the default if a NULL is compared
33  NULL,
34  "//" PCMK_XE_CIB
35  },
36  {
38  "/" PCMK_XE_CIB,
40  },
41  {
43  "/" PCMK_XE_CIB,
45  },
46  {
50  },
51  {
55  },
56  {
60  },
61  {
65  },
66  {
70  },
71  {
75  },
76  {
80  },
81  {
85  },
86  {
90  },
91  {
95  },
96  {
98  NULL,
99  "//" PCMK_XE_CIB
100  },
101 };
102 
111 const char *
112 pcmk_cib_xpath_for(const char *element_name)
113 {
114  for (int lpc = 0; lpc < PCMK__NELEM(cib_sections); lpc++) {
115  // A NULL element_name will match the first entry
116  if (pcmk__str_eq(element_name, cib_sections[lpc].name,
118  return cib_sections[lpc].path;
119  }
120  }
121  return NULL;
122 }
123 
132 const char *
133 pcmk__cib_abs_xpath_for(const char *element)
134 {
135  const char *xpath = pcmk_cib_xpath_for(element);
136 
137  // XPaths returned by pcmk_cib_xpath_for() are relative (starting with "//")
138  return ((xpath != NULL)? (xpath + 1) : NULL);
139 }
140 
149 const char *
150 pcmk_cib_parent_name_for(const char *element_name)
151 {
152  for (int lpc = 0; lpc < PCMK__NELEM(cib_sections); lpc++) {
153  // A NULL element_name will match the first entry
154  if (pcmk__str_eq(element_name, cib_sections[lpc].name,
156  return cib_sections[lpc].parent;
157  }
158  }
159  return NULL;
160 }
161 
171 xmlNode *
172 pcmk_find_cib_element(xmlNode *cib, const char *element_name)
173 {
174  return get_xpath_object(pcmk_cib_xpath_for(element_name), cib, LOG_TRACE);
175 }
176 
183 int
184 pcmk__check_feature_set(const char *cib_version)
185 {
186  if (cib_version && compare_version(cib_version, CRM_FEATURE_SET) > 0) {
187  return EPROTONOSUPPORT;
188  }
189 
190  return pcmk_rc_ok;
191 }
#define LOG_TRACE
Definition: logging.h:38
#define PCMK_XE_FENCING_TOPOLOGY
Definition: xml_names.h:115
const char * pcmk_cib_xpath_for(const char *element_name)
Get the relative XPath needed to find a specified CIB element name.
Definition: cib.c:112
#define PCMK_XE_OP_DEFAULTS
Definition: xml_names.h:144
#define PCMK_XE_CIB
Definition: xml_names.h:79
#define PCMK_XE_STATUS
Definition: xml_names.h:199
const char * name
Definition: cib.c:26
#define PCMK_XE_ACLS
Definition: xml_names.h:59
#define PCMK_XE_CONSTRAINTS
Definition: xml_names.h:89
#define PCMK_XE_RSC_DEFAULTS
Definition: xml_names.h:182
#define CRM_FEATURE_SET
Definition: crm.h:72
#define PCMK_XE_ALERTS
Definition: xml_names.h:66
const char * pcmk_cib_parent_name_for(const char *element_name)
Get the parent element name of a given CIB element name.
Definition: cib.c:150
const char * pcmk__cib_abs_xpath_for(const char *element)
Definition: cib.c:133
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
Definition: xpath.c:189
xmlNode * pcmk_find_cib_element(xmlNode *cib, const char *element_name)
Find an element in the CIB.
Definition: cib.c:172
int pcmk__check_feature_set(const char *cib_version)
Definition: cib.c:184
Wrappers for and extensions to libxml2.
#define PCMK__NELEM(a)
Definition: internal.h:48
#define PCMK__XE_ALL
#define PCMK_XE_CONFIGURATION
Definition: xml_names.h:87
#define PCMK_XE_TAGS
Definition: xml_names.h:204
const char * path
Definition: cib.c:28
int compare_version(const char *version1, const char *version2)
Definition: utils.c:188
#define PCMK_XE_NODES
Definition: xml_names.h:139
const char * parent
Definition: cib.c:27
#define PCMK_XE_RESOURCES
Definition: xml_names.h:175
#define PCMK_XE_CRM_CONFIG
Definition: xml_names.h:91