root/lib/common/xml_attr.c

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

DEFINITIONS

This source file includes following definitions.
  1. pcmk__xa_remove
  2. pcmk__mark_xml_attr_dirty
  3. pcmk__marked_as_deleted
  4. pcmk__dump_xml_attr

   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>
  13 #include <sys/types.h>
  14 #include <unistd.h>
  15 #include <time.h>
  16 #include <string.h>
  17 #include <stdlib.h>
  18 #include <stdarg.h>
  19 #include <bzlib.h>
  20 
  21 #include <libxml/parser.h>
  22 #include <libxml/tree.h>
  23 #include <libxml/xmlIO.h>  /* xmlAllocOutputBuffer */
  24 
  25 #include <crm/crm.h>
  26 #include <crm/common/xml.h>
  27 #include <crm/common/xml_internal.h>  // PCMK__XML_LOG_BASE, etc.
  28 #include "crmcommon_private.h"
  29 
  30 /*!
  31  * \internal
  32  * \brief Remove an XML attribute from its parent and free it
  33  *
  34  * \param[in,out] attr   XML attribute to remove
  35  * \param[in]     force  If \c true, remove the attribute immediately, ignoring
  36  *                       ACLs and change tracking
  37  *
  38  * \return Standard Pacemaker return code (\c EPERM if ACLs prevent removal, or
  39  *         or \c pcmk_rc_ok otherwise)
  40  *
  41  * \note If the attribute has no parent element, this function does not free it.
  42  *       This mimics \c xmlRemoveProp().
  43  */
  44 int
  45 pcmk__xa_remove(xmlAttr *attr, bool force)
     /* [previous][next][first][last][top][bottom][index][help] */
  46 {
  47     xmlNode *element = NULL;
  48 
  49     if ((attr == NULL) || (attr->parent == NULL)) {
  50         return pcmk_rc_ok;
  51     }
  52 
  53     element = attr->parent;
  54 
  55     if (!force && !pcmk__check_acl(element, NULL, pcmk__xf_acl_write)) {
  56         // ACLs apply to element, not to particular attributes
  57         crm_trace("ACLs prevent removal of attributes from %s element",
  58                   (const char *) element->name);
  59         return EPERM;
  60     }
  61 
  62     if (!force && pcmk__tracking_xml_changes(element, false)) {
  63         // Leave in place (marked for removal) until after diff is calculated
  64         pcmk__xml_set_parent_flags(element, pcmk__xf_dirty);
  65         pcmk__set_xml_flags((xml_node_private_t *) attr->_private,
  66                             pcmk__xf_deleted);
  67     } else {
  68         pcmk__xml_free_private_data((xmlNode *) attr);
  69         xmlRemoveProp(attr);
  70     }
  71     return pcmk_rc_ok;
  72 }
  73 
  74 void
  75 pcmk__mark_xml_attr_dirty(xmlAttr *a) 
     /* [previous][next][first][last][top][bottom][index][help] */
  76 {
  77     xmlNode *parent = a->parent;
  78     xml_node_private_t *nodepriv = a->_private;
  79 
  80     pcmk__set_xml_flags(nodepriv, pcmk__xf_dirty|pcmk__xf_modified);
  81     pcmk__clear_xml_flags(nodepriv, pcmk__xf_deleted);
  82     pcmk__mark_xml_node_dirty(parent);
  83 }
  84 
  85 // This also clears attribute's flags if not marked as deleted
  86 bool
  87 pcmk__marked_as_deleted(xmlAttrPtr a, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89     xml_node_private_t *nodepriv = a->_private;
  90 
  91     if (pcmk_is_set(nodepriv->flags, pcmk__xf_deleted)) {
  92         return true;
  93     }
  94     nodepriv->flags = pcmk__xf_none;
  95     return false;
  96 }
  97 
  98 /*!
  99  * \internal
 100  * \brief Append an XML attribute to a buffer
 101  *
 102  * \param[in]     attr     Attribute to append
 103  * \param[in,out] buffer   Where to append the content (must not be \p NULL)
 104  */
 105 void
 106 pcmk__dump_xml_attr(const xmlAttr *attr, GString *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108     const char *name = NULL;
 109     const char *value = NULL;
 110     gchar *value_esc = NULL;
 111     xml_node_private_t *nodepriv = NULL;
 112 
 113     if (attr == NULL || attr->children == NULL) {
 114         return;
 115     }
 116 
 117     nodepriv = attr->_private;
 118     if (nodepriv && pcmk_is_set(nodepriv->flags, pcmk__xf_deleted)) {
 119         return;
 120     }
 121 
 122     name = (const char *) attr->name;
 123     value = (const char *) attr->children->content;
 124     if (value == NULL) {
 125         /* Don't print anything for unset attribute. Any null-indicator value,
 126          * including the empty string, could also be a real value that needs to
 127          * be treated differently from "unset".
 128          */
 129         return;
 130     }
 131 
 132     if (pcmk__xml_needs_escape(value, pcmk__xml_escape_attr)) {
 133         value_esc = pcmk__xml_escape(value, pcmk__xml_escape_attr);
 134         value = value_esc;
 135     }
 136 
 137     pcmk__g_strcat(buffer, " ", name, "=\"", value, "\"", NULL);
 138     g_free(value_esc);
 139 }

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