13 #include <sys/types.h>
17 #include <libxml/tree.h>
48 pcmk__new_nvpair(
const char *
name,
const char *value)
57 nvpair->
name = strdup(name);
58 nvpair->
value = value? strdup(value) : NULL;
69 pcmk__free_nvpair(gpointer
data)
94 return g_slist_prepend(nvpairs, pcmk__new_nvpair(name, value));
105 g_slist_free_full(nvpairs, pcmk__free_nvpair);
118 pcmk__compare_nvpair(gconstpointer a, gconstpointer b)
130 rc = strcmp(pair_a->
name, pair_b->
name);
149 return g_slist_sort(list, pcmk__compare_nvpair);
164 GSList *result = NULL;
166 for (xmlAttrPtr iter = pcmk__xe_first_attr(xml); iter != NULL;
170 (
const char *) iter->name,
171 (
const char *) pcmk__xml_attr_value(iter));
187 pcmk__nvpair_add_xml_attr(gpointer data, gpointer user_data)
190 xmlNode *parent = user_data;
204 g_slist_foreach(list, pcmk__nvpair_add_xml_attr, xml);
227 if (sscanf(input,
"%m[^=]=%m[^\n]", name, value) <= 0) {
235 sep = strstr(optarg,
"=");
240 *name =
strndup(input, sep-input);
249 if (*(sep+1) !=
'\0') {
250 *value = strdup(sep+1);
252 if (*value == NULL) {
258 if (*name != NULL && *value != NULL) {
260 }
else if (*name != NULL || *value != NULL) {
327 xmlAttr *attr = NULL;
335 #if XML_PARANOIA_CHECKS
337 const char *old_value = NULL;
343 crm_err(
"Cannot reset %s with crm_xml_add(%s)", name, value);
351 if (old == NULL || value == NULL || strcmp(old, value) != 0) {
357 crm_trace(
"Cannot add %s=%s to %s", name, value, node->name);
366 CRM_CHECK(attr && attr->children && attr->children->content,
return NULL);
367 return (
char *)attr->children->content;
384 xmlAttr *attr = NULL;
385 const char *old_value = NULL;
388 CRM_CHECK(name != NULL && name[0] != 0,
return NULL);
393 CRM_CHECK(old_value != value,
return value);
397 crm_trace(
"Cannot replace %s=%s to %s", name, value, node->name);
400 }
else if (old_value && !value) {
406 if (!old_value || !value || !strcmp(old_value, value)) {
415 CRM_CHECK(attr && attr->children && attr->children->content,
return NULL);
416 return (
char *) attr->children->content;
434 char *number = pcmk__itoa(value);
435 const char *added =
crm_xml_add(node, name, number);
457 const char *added =
crm_xml_add(node, name, number);
507 const struct timeval *value)
509 const char *added = NULL;
511 if (xml && name_sec && value) {
513 if (added && name_usec) {
532 xmlAttr *attr = NULL;
535 crm_err(
"Couldn't find %s in NULL", name ? name :
"<null>");
539 }
else if (name == NULL) {
540 crm_err(
"Couldn't find NULL in %s", crm_element_name(data));
547 attr = xmlHasProp((xmlNode *) data, (
pcmkXmlStr) name);
548 if (!attr || !attr->children) {
551 return (
const char *) attr->children->content;
568 const char *value = NULL;
576 || (value_ll < INT_MIN) || (value_ll > INT_MAX)) {
579 *dest = (int) value_ll;
600 const char *value = NULL;
625 const char *value = NULL;
632 || (value_ll < 0) || (value_ll > G_MAXUINT)) {
635 *dest = (guint) value_ll;
653 long long value_ll = 0;
662 *dest = (time_t) value_ll;
681 const char *name_usec,
struct timeval *dest)
683 long long value_i = 0;
704 dest->tv_sec = (time_t) value_i;
710 dest->tv_usec = (suseconds_t) value_i;
729 char *value_copy = NULL;
733 value_copy = strdup(value);
754 const char *name = key;
755 const char *s_value = value;
757 xmlNode *xml_node = user_data;
759 if (isdigit(name[0])) {
767 crm_trace(
"dumped: %s=%s", name, s_value);
770 crm_trace(
"duplicate: %s=%s", name, s_value);
788 const char *name = key;
789 const char *s_value = value;
791 xmlNode *xml_node = user_data;
797 crm_trace(
"duplicate: %s=%s", name, s_value);
816 char *crm_name = NULL;
818 if (key == NULL || value == NULL) {
825 for (crm_name = key; *crm_name; ++crm_name) {
826 if ((*crm_name ==
'#') || (*crm_name ==
':')) {
865 const char *parent_id =
ID(parent);
889 const char *name = key;
890 const char *s_value = value;
891 xmlNode *xml_node = user_data;
894 crm_trace(
"dumped: name=%s value=%s", name, s_value);
914 xmlNode *child = NULL;
915 xmlAttrPtr pIter = NULL;
916 xmlNode *nvpair_list = NULL;
919 CRM_CHECK(parent != NULL,
return nvpair_hash);
922 if (nvpair_list == NULL) {
923 crm_trace(
"No attributes in %s", crm_element_name(parent));
929 for (pIter = pcmk__xe_first_attr(nvpair_list); pIter != NULL;
930 pIter = pIter->next) {
932 const char *p_name = (
const char *)pIter->name;
933 const char *p_value = pcmk__xml_attr_value(pIter);
935 crm_trace(
"Added %s=%s", p_name, p_value);
937 g_hash_table_insert(nvpair_hash, strdup(p_name), strdup(p_value));
940 for (child = pcmk__xml_first_child(nvpair_list); child != NULL;
941 child = pcmk__xml_next(child)) {
948 if (key != NULL && value != NULL) {
949 g_hash_table_insert(nvpair_hash, strdup(key), strdup(value));
#define CRM_CHECK(expr, failure_action)
xmlNode * find_xml_node(xmlNode *cib, const char *node_path, gboolean must_find)
GSList * pcmk_sort_nvpairs(GSList *list)
Sort a list of name/value pairs.
void hash2field(gpointer key, gpointer value, gpointer user_data)
Set XML attribute based on hash table entry.
void pcmk_free_nvpairs(GSList *nvpairs)
Free a list of name/value pairs.
const char * crm_xml_add_ms(xmlNode *node, const char *name, guint ms)
Create an XML attribute with specified name and unsigned value.
G_GNUC_INTERNAL void pcmk__mark_xml_attr_dirty(xmlAttr *a)
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
#define XML_NVPAIR_ATTR_NAME
#define CRM_LOG_ASSERT(expr)
GSList * pcmk_prepend_nvpair(GSList *nvpairs, const char *name, const char *value)
Prepend a name/value pair to a list.
#define XML_CIB_TAG_NVPAIR
int crm_element_value_ll(const xmlNode *data, const char *name, long long *dest)
Retrieve the long long integer value of an XML attribute.
GSList * pcmk_xml_attrs2nvpairs(xmlNode *xml)
Create a list of name/value pairs from an XML node's attributes.
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
char * pcmk__format_named_time(const char *name, time_t epoch_time)
void hash2smartfield(gpointer key, gpointer value, gpointer user_data)
Add hash table entry to XML as (possibly legacy) name/value.
char * strndup(const char *str, size_t len)
char * crm_meta_name(const char *field)
void hash2nvpair(gpointer key, gpointer value, gpointer user_data)
Add XML nvpair element based on hash table entry.
char * pcmk__format_nvpair(const char *name, const char *value, const char *units)
#define PCMK__PARSE_INT_DEFAULT
int pcmk__scan_ll(const char *text, long long *result, long long default_value)
bool pcmk__check_acl(xmlNode *xml, const char *name, enum xml_private_flags mode)
int crm_element_value_ms(const xmlNode *data, const char *name, guint *dest)
Retrieve the millisecond value of an XML attribute.
char * crm_element_value_copy(const xmlNode *data, const char *name)
Retrieve a copy of the value of an XML attribute.
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
int crm_element_value_timeval(const xmlNode *data, const char *name_sec, const char *name_usec, struct timeval *dest)
Retrieve the value of XML second/microsecond attributes as time.
char * pcmk_format_nvpair(const char *name, const char *value, const char *units)
const char * crm_xml_replace(xmlNode *node, const char *name, const char *value)
Replace an XML attribute with specified name and (possibly NULL) value.
#define crm_trace(fmt, args...)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
int pcmk__scan_nvpair(const char *input, char **name, char **value)
void crm_xml_set_id(xmlNode *xml, const char *format,...) G_GNUC_PRINTF(2
Wrappers for and extensions to libxml2.
xmlNode * create_xml_node(xmlNode *parent, const char *name)
int crm_element_value_epoch(const xmlNode *xml, const char *name, time_t *dest)
Retrieve the seconds-since-epoch value of an XML attribute.
int pcmk_scan_nvpair(const char *input, char **name, char **value)
const char * pcmk__epoch2str(time_t *when)
const char * crm_xml_add_timeval(xmlNode *xml, const char *name_sec, const char *name_usec, const struct timeval *value)
Create XML attributes for seconds and microseconds.
const char * crm_xml_add_ll(xmlNode *node, const char *name, long long value)
Create an XML attribute with specified name and long long int value.
const xmlChar * pcmkXmlStr
GHashTable * pcmk__strkey_table(GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
#define crm_err(fmt, args...)
void xml_remove_prop(xmlNode *obj, const char *name)
#define XML_NVPAIR_ATTR_VALUE
void hash2metafield(gpointer key, gpointer value, gpointer user_data)
Set XML attribute based on hash table entry, as meta-attribute name.
GHashTable * xml2list(xmlNode *parent)
Retrieve XML attributes as a hash table.
void pcmk_nvpairs2xml_attrs(GSList *list, xmlNode *xml)
Add XML attributes based on a list of name/value pairs.
#define crm_log_xml_trace(xml, text)
G_GNUC_INTERNAL bool pcmk__tracking_xml_changes(xmlNode *xml, bool lazy)
char * pcmk_format_named_time(const char *name, time_t epoch_time)
xmlNode * crm_create_nvpair_xml(xmlNode *parent, const char *id, const char *name, const char *value)
Create an XML name/value pair.
#define pcmk_err_bad_nvpair
Deprecated Pacemaker utilities.