pacemaker  3.0.0-d8340737c4
Scalable High-Availability cluster resource manager
xml_idref.c
Go to the documentation of this file.
1 /*
2  * Copyright 2004-2024 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 #include <crm_internal.h>
11 
12 #include <stdio.h> // NULL
13 #include <stdlib.h> // free()
14 #include <glib.h> // GList, GHashTable, etc.
15 #include <libxml/tree.h> // xmlNode
16 
17 #include <crm/crm.h>
18 #include <crm/common/xml.h> // get_xpath_object(), PCMK_XA_ID_REF
19 
31 void
32 pcmk__add_idref(GHashTable *table, const char *id, const char *referrer)
33 {
34  pcmk__idref_t *idref = NULL;
35 
36  pcmk__assert((table != NULL) && (id != NULL) && (referrer != NULL));
37 
38  idref = g_hash_table_lookup(table, id);
39  if (idref == NULL) {
40  idref = pcmk__assert_alloc(1, sizeof(pcmk__idref_t));
41  idref->id = pcmk__str_copy(id);
42  g_hash_table_insert(table, pcmk__str_copy(id), idref);
43  }
44  for (GList *iter = idref->refs; iter != NULL; iter = iter->next) {
45  if (pcmk__str_eq(referrer, (const char *) iter->data,
46  pcmk__str_none)) {
47  return; // Already present
48  }
49  }
50  idref->refs = g_list_append(idref->refs, pcmk__str_copy(referrer));
51  crm_trace("Added ID %s referrer %s", id, referrer);
52 }
53 
60 void
62 {
63  pcmk__idref_t *idref = data;
64 
65  if (idref != NULL) {
66  free(idref->id);
67  g_list_free_full(idref->refs, free);
68  free(idref);
69  }
70 }
71 
84 xmlNode *
85 pcmk__xe_resolve_idref(xmlNode *xml, xmlNode *search)
86 {
87  char *xpath = NULL;
88  const char *ref = NULL;
89  xmlNode *result = NULL;
90 
91  if (xml == NULL) {
92  return NULL;
93  }
94 
96  if (ref == NULL) {
97  return xml;
98  }
99 
100  if (search == NULL) {
101  search = xml;
102  }
103 
104  xpath = crm_strdup_printf("//%s[@" PCMK_XA_ID "='%s']", xml->name, ref);
105  result = get_xpath_object(xpath, search, LOG_DEBUG);
106  if (result == NULL) {
107  // Not possible with schema validation enabled
108  pcmk__config_err("Ignoring invalid %s configuration: "
109  PCMK_XA_ID_REF " '%s' does not reference "
110  "a valid object " QB_XS " xpath=%s",
111  xml->name, ref, xpath);
112  }
113  free(xpath);
114  return result;
115 }
A dumping ground.
char data[0]
Definition: cpg.c:58
xmlNode * pcmk__xe_resolve_idref(xmlNode *xml, xmlNode *search)
Definition: xml_idref.c:85
#define pcmk__config_err(fmt...)
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
Definition: xpath.c:189
void pcmk__free_idref(gpointer data)
Definition: xml_idref.c:61
#define PCMK_XA_ID_REF
Definition: xml_names.h:303
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
Definition: xml_element.c:1168
#define crm_trace(fmt, args...)
Definition: logging.h:372
Wrappers for and extensions to libxml2.
#define PCMK_XA_ID
Definition: xml_names.h:301
#define pcmk__str_copy(str)
void pcmk__add_idref(GHashTable *table, const char *id, const char *referrer)
Definition: xml_idref.c:32
#define pcmk__assert(expr)
pcmk__action_result_t result
Definition: pcmk_fence.c:37
#define pcmk__assert_alloc(nmemb, size)
Definition: internal.h:257
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1