Skip to content

Commit 50ce9de

Browse files
authored
Merge pull request #4124 from nrwahl2/nrwahl2-acls_first
Refactor: libcrmcommon: Use XML attribute foreach functions in more places
2 parents 8710847 + 9272bde commit 50ce9de

16 files changed

Lines changed: 821 additions & 330 deletions

include/crm/common/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <crm/common/servers_internal.h>
6161
#include <crm/common/tls_internal.h>
6262
#include <crm/common/utils_internal.h>
63+
// xml_attr_internal.h intentionally left out
6364
// xml_comment_internal.h intentionally left out
6465
// xml_element_internal.h intentionally left out
6566
// xml_idref_internal.h intentionally left out
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2025-2026 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__INCLUDED_CRM_COMMON_INTERNAL_H
11+
#error "Include <crm/common/internal.h> instead of <xml_attr_internal.h> " \
12+
"directly"
13+
#endif
14+
15+
#ifndef PCMK__CRM_COMMON_XML_ATTR_INTERNAL__H
16+
#define PCMK__CRM_COMMON_XML_ATTR_INTERNAL__H
17+
18+
/*
19+
* Internal-only wrappers for and extensions to libxml2 for processing XML
20+
* attributes
21+
*/
22+
23+
#include <stdbool.h> // bool
24+
25+
#include <libxml/tree.h> // xmlAttr
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
bool pcmk__xa_insert_dup(const xmlAttr *attr, void *user_data);
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif
36+
37+
#endif // PCMK__XML_ATTR_INTERNAL__H

include/crm/common/xml_element_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ xmlNode *pcmk__xe_first_child(const xmlNode *parent, const char *node_name,
4848
void pcmk__xe_remove_attr(xmlNode *element, const char *name);
4949
bool pcmk__xe_remove_attr_cb(xmlNode *xml, void *user_data);
5050
void pcmk__xe_remove_matching_attrs(xmlNode *element, bool force,
51-
bool (*match)(xmlAttrPtr, void *),
51+
bool (*match)(xmlAttr *, void *),
5252
void *user_data);
5353
int pcmk__xe_delete_match(xmlNode *xml, xmlNode *search);
5454
int pcmk__xe_replace_match(xmlNode *xml, xmlNode *replace);

include/crm/common/xml_internal.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <crm/common/xml_names.h> // PCMK_XA_ID, PCMK_XE_CLONE
2929

3030
// This file is a wrapper for other {xml_*,xpath}_internal.h headers
31+
#include <crm/common/xml_attr_internal.h>
3132
#include <crm/common/xml_comment_internal.h>
3233
#include <crm/common/xml_element_internal.h>
3334
#include <crm/common/xml_idref_internal.h>
@@ -432,11 +433,23 @@ void pcmk__xml_mark_changes(xmlNode *old_xml, xmlNode *new_xml);
432433
bool pcmk__xml_tree_foreach(xmlNode *xml, bool (*fn)(xmlNode *, void *),
433434
void *user_data);
434435

436+
/*!
437+
* \internal
438+
* \brief Get an XML attribute's value
439+
*
440+
* \param[in] attr XML attribute
441+
*
442+
* \return Value of \p attr, or \c NULL if \p attr is \c NULL or its value is
443+
* unset
444+
*/
435445
static inline const char *
436446
pcmk__xml_attr_value(const xmlAttr *attr)
437447
{
438-
return ((attr == NULL) || (attr->children == NULL))? NULL
439-
: (const char *) attr->children->content;
448+
if ((attr == NULL) || (attr->children == NULL)) {
449+
return NULL;
450+
}
451+
452+
return (const char *) attr->children->content;
440453
}
441454

442455
void pcmk__xml_patchset_add_digest(xmlNode *patchset, const xmlNode *target);

lib/common/acl.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,24 @@ is_mode_allowed(uint32_t flags, enum pcmk__xml_flags mode)
788788
}
789789
}
790790

791+
/*!
792+
* \internal
793+
* \brief Check whether an XML attribute's name is \c PCMK_XA_ID
794+
*
795+
* \param[in] attr Attribute to check
796+
* \param[in] user_data Ignored
797+
*
798+
* \return \c true if the attribute's name is \c PCMK_XA_ID, or \c false
799+
* otherwise
800+
*
801+
* \note This is compatible with \c pcmk__xe_foreach_const_attr().
802+
*/
803+
static bool
804+
attr_is_id(const xmlAttr *attr, void *user_data)
805+
{
806+
return pcmk__str_eq((const char *) attr->name, PCMK_XA_ID, pcmk__str_none);
807+
}
808+
791809
/*!
792810
* \internal
793811
* \brief Check whether an XML attribute's name is not \c PCMK_XA_ID
@@ -803,7 +821,7 @@ is_mode_allowed(uint32_t flags, enum pcmk__xml_flags mode)
803821
static bool
804822
attr_is_not_id(xmlAttr *attr, void *user_data)
805823
{
806-
return !pcmk__str_eq((const char *) attr->name, PCMK_XA_ID, pcmk__str_none);
824+
return !attr_is_id(attr, user_data);
807825
}
808826

809827
/*!
@@ -952,14 +970,10 @@ xml_acl_filtered_copy(const char *user, xmlNode *acl_source, xmlNode *xml,
952970
* \return \c true if XML element is implicitly allowed, or \c false otherwise
953971
*/
954972
static bool
955-
implicitly_allowed(xmlNode *xml)
973+
implicitly_allowed(const xmlNode *xml)
956974
{
957-
for (xmlAttr *attr = pcmk__xe_first_attr(xml); attr != NULL;
958-
attr = attr->next) {
959-
960-
if (attr_is_not_id(attr, NULL)) {
961-
return false;
962-
}
975+
if (!pcmk__xe_foreach_const_attr(xml, attr_is_id, NULL)) {
976+
return false;
963977
}
964978

965979
/* Creation is not implicitly allowed for a descendant of PCMK_XE_ACLS, but

lib/common/crmcommon_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ G_GNUC_INTERNAL
176176
bool pcmk__marked_as_deleted(xmlAttrPtr a, void *user_data);
177177

178178
G_GNUC_INTERNAL
179-
void pcmk__dump_xml_attr(const xmlAttr *attr, GString *buffer);
179+
bool pcmk__dump_xml_attr(const xmlAttr *attr, void *user_data);
180180

181181
G_GNUC_INTERNAL
182182
int pcmk__xe_set_score(xmlNode *target, const char *name, const char *value);

lib/common/nvpair.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ GHashTable *
340340
xml2list(const xmlNode *parent)
341341
{
342342
xmlNode *child = NULL;
343-
xmlAttrPtr pIter = NULL;
344343
xmlNode *nvpair_list = NULL;
345344
GHashTable *nvpair_hash = pcmk__strkey_table(free, free);
346345

@@ -354,16 +353,7 @@ xml2list(const xmlNode *parent)
354353

355354
pcmk__log_xml_trace(nvpair_list, "Unpacking");
356355

357-
for (pIter = pcmk__xe_first_attr(nvpair_list); pIter != NULL;
358-
pIter = pIter->next) {
359-
360-
const char *p_name = (const char *)pIter->name;
361-
const char *p_value = pcmk__xml_attr_value(pIter);
362-
363-
pcmk__trace("Added %s=%s", p_name, p_value);
364-
365-
pcmk__insert_dup(nvpair_hash, p_name, p_value);
366-
}
356+
pcmk__xe_foreach_const_attr(nvpair_list, pcmk__xa_insert_dup, nvpair_hash);
367357

368358
for (child = pcmk__xe_first_child(nvpair_list, PCMK__XE_PARAM, NULL, NULL);
369359
child != NULL; child = pcmk__xe_next(child, PCMK__XE_PARAM)) {

0 commit comments

Comments
 (0)