This is the mail archive of the
cluster-cvs@sourceware.org
mailing list for the cluster.
master - ccs: libccs major rework pass 3
- From: "Fabio M. Di Nitto" <fabbione at fedoraproject dot org>
- To: cluster-cvs-relay at redhat dot com
- Date: Tue, 28 Oct 2008 19:29:41 +0000 (UTC)
- Subject: master - ccs: libccs major rework pass 3
Gitweb: http://git.fedorahosted.org/git/cluster.git?p=cluster.git;a=commitdiff;h=e7027327dac72cafba61fef267a279e68d1f801b
Commit: e7027327dac72cafba61fef267a279e68d1f801b
Parent: 91f1f653cc24972f2599449084e3b4e7ed565ac9
Author: Fabio M. Di Nitto <fdinitto@redhat.com>
AuthorDate: Tue Oct 28 20:08:05 2008 +0100
Committer: Fabio M. Di Nitto <fdinitto@redhat.com>
CommitterDate: Tue Oct 28 20:29:04 2008 +0100
ccs: libccs major rework pass 3
split fullxpath code into fullxpath.c
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
---
config/libs/libccsconfdb/Makefile | 1 +
config/libs/libccsconfdb/ccs_internal.h | 12 ++
config/libs/libccsconfdb/fullxpath.c | 308 +++++++++++++++++++++++++++++++
config/libs/libccsconfdb/libccs.c | 300 +------------------------------
4 files changed, 323 insertions(+), 298 deletions(-)
diff --git a/config/libs/libccsconfdb/Makefile b/config/libs/libccsconfdb/Makefile
index 3216d62..d75710f 100644
--- a/config/libs/libccsconfdb/Makefile
+++ b/config/libs/libccsconfdb/Makefile
@@ -4,6 +4,7 @@ INCDIRT=ccs.h
OBJS= $(TARGET).o \
xpathlite.o \
+ fullxpath.o \
extras.o
include ../../../make/defines.mk
diff --git a/config/libs/libccsconfdb/ccs_internal.h b/config/libs/libccsconfdb/ccs_internal.h
index 1acac71..0328245 100644
--- a/config/libs/libccsconfdb/ccs_internal.h
+++ b/config/libs/libccsconfdb/ccs_internal.h
@@ -2,9 +2,21 @@
#define __CCS_INTERNAL_DOT_H__
/* NOTE: use __attribute__ to hide the internal API */
+
+/* from xpathlite.c */
char * _ccs_get_xpathlite(confdb_handle_t handle, unsigned int connection_handle, const char *query, int list) __attribute__ ((visibility ("hidden")));
+
+/* from libccs.c */
void reset_iterator(confdb_handle_t handle, unsigned int connection_handle) __attribute__ ((visibility ("hidden")));
int get_previous_query(confdb_handle_t handle, unsigned int connection_handle, char *previous_query, unsigned int *query_handle) __attribute__ ((visibility ("hidden")));
int set_previous_query(confdb_handle_t handle, unsigned int connection_handle, char *previous_query, unsigned int query_handle) __attribute__ ((visibility ("hidden")));
+/* from xpathlite.c */
+char * _ccs_get_xpathlite(confdb_handle_t handle, unsigned int connection_handle, const char *query, int list) __attribute__ ((visibility ("hidden")));
+
+/* from fullxpath.c */
+char * _ccs_get_fullxpath(confdb_handle_t handle, unsigned int connection_handle, const char *query, int list) __attribute__ ((visibility ("hidden")));
+int xpathfull_init(confdb_handle_t handle, int ccs_handle) __attribute__ ((visibility ("hidden")));
+void xpathfull_finish() __attribute__ ((visibility ("hidden")));
+
#endif /* __CCS_INTERNAL_DOT_H__ */
diff --git a/config/libs/libccsconfdb/fullxpath.c b/config/libs/libccsconfdb/fullxpath.c
new file mode 100644
index 0000000..58349ea
--- /dev/null
+++ b/config/libs/libccsconfdb/fullxpath.c
@@ -0,0 +1,308 @@
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <corosync/saAis.h>
+#include <corosync/confdb.h>
+#include <libxml/parser.h>
+#include <libxml/xpath.h>
+
+#include "ccs.h"
+#include "ccs_internal.h"
+
+#ifndef XMLBUFSIZE
+#define XMLBUFSIZE 64000
+#endif
+
+int fullxpath = 0;
+
+static xmlDocPtr doc = NULL;
+static xmlXPathContextPtr ctx = NULL;
+
+static int add_to_buffer(char *data, char **buffer, int *bufsize)
+{
+ int datalen = 0, bufferlen = 0;
+ char *newbuf = NULL;
+
+ datalen = strlen(data);
+ bufferlen = strlen(*buffer);
+
+ if(datalen) {
+ if((bufferlen + datalen) >= *bufsize) {
+ newbuf = malloc((*bufsize * 2));
+ if(!newbuf) {
+ errno = ENOMEM;
+ return -1;
+ }
+ *bufsize = *bufsize * 2;
+ memset(newbuf, 0, *bufsize);
+ memcpy(newbuf, *buffer, bufferlen);
+ free(*buffer);
+ *buffer = newbuf;
+ }
+ strncpy(*buffer + bufferlen, data, datalen);
+ }
+ return 0;
+}
+
+static int dump_objdb_buff(confdb_handle_t dump_handle, unsigned int parent_object_handle, char **buffer, int *bufsize)
+{
+ unsigned int object_handle;
+ char temp[PATH_MAX];
+ char object_name[PATH_MAX];
+ int object_name_len;
+ char key_name[PATH_MAX];
+ int key_name_len;
+ char key_value[PATH_MAX];
+ int key_value_len;
+ int res;
+
+ res = confdb_key_iter_start(dump_handle, parent_object_handle);
+ if (res != SA_AIS_OK) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ if (!*buffer || ((*buffer) && !strlen(*buffer))) {
+ snprintf(temp, PATH_MAX - 1, "<?xml version=\"1.0\"?>\n<objdbmaindoc>\n");
+ if(add_to_buffer(temp, buffer, bufsize))
+ return -1;
+ }
+
+ while ( (res = confdb_key_iter(dump_handle, parent_object_handle, key_name, &key_name_len,
+ key_value, &key_value_len)) == SA_AIS_OK) {
+ key_name[key_name_len] = '\0';
+ key_value[key_value_len] = '\0';
+
+ if (!strncmp(key_name, "service_id", key_name_len))
+ continue;
+ if (!strncmp(key_name, "handle", key_name_len))
+ continue;
+ if (!strncmp(key_name, "next_handle", key_name_len))
+ continue;
+
+ snprintf(temp, PATH_MAX - 1, " %s=\"%s\"", key_name, key_value);
+ if(add_to_buffer(temp, buffer, bufsize))
+ return -1;
+ }
+
+ if (parent_object_handle > 0) {
+ snprintf(temp, PATH_MAX - 1, ">\n");
+ if(add_to_buffer(temp, buffer, bufsize))
+ return -1;
+ }
+
+ res = confdb_object_iter_start(dump_handle, parent_object_handle);
+ if (res != SA_AIS_OK) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ while ( (res = confdb_object_iter(dump_handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == SA_AIS_OK) {
+ unsigned int parent;
+
+ res = confdb_object_parent_get(dump_handle, object_handle, &parent);
+ if (res != SA_AIS_OK) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ object_name[object_name_len] = '\0';
+
+ /* we need to skip the top level services because they have invalid
+ * xml chars */
+
+ snprintf(temp, PATH_MAX - 1, "<%s", object_name);
+ if(add_to_buffer(temp, buffer, bufsize))
+ return -1;
+
+ res = dump_objdb_buff(dump_handle, object_handle, buffer, bufsize);
+ if(res) {
+ errno = res;
+ return res;
+ }
+
+ if (object_handle != parent_object_handle) {
+ snprintf(temp, PATH_MAX - 1, "</%s>\n", object_name);
+ if(add_to_buffer(temp, buffer, bufsize))
+ return -1;
+ } else {
+ snprintf(temp, PATH_MAX - 1, ">\n");
+ if(add_to_buffer(temp, buffer, bufsize))
+ return -1;
+ }
+ }
+
+ if (parent_object_handle == OBJECT_PARENT_HANDLE) {
+ snprintf(temp, PATH_MAX - 1, "</objdbmaindoc>\n");
+ if(add_to_buffer(temp, buffer, bufsize))
+ return -1;
+ }
+
+ return 0;
+}
+
+int xpathfull_init(confdb_handle_t handle, int ccs_handle) {
+ int size = XMLBUFSIZE;
+ char *buffer, *newbuf;
+
+ newbuf = buffer = malloc(XMLBUFSIZE);
+ if(!buffer) {
+ errno = ENOMEM;
+ goto fail;
+ }
+
+ memset(buffer, 0, XMLBUFSIZE);
+
+ if (dump_objdb_buff(handle, OBJECT_PARENT_HANDLE, &newbuf, &size))
+ goto fail;
+
+ if (newbuf != buffer) {
+ buffer = newbuf;
+ newbuf = NULL;
+ }
+
+ doc = xmlParseMemory(buffer,strlen(buffer));
+ if(!doc)
+ goto fail;
+
+ free(buffer);
+
+ ctx = xmlXPathNewContext(doc);
+ if(!ctx) {
+ xmlFreeDoc(doc);
+ goto fail;
+ }
+
+ return 0;
+
+fail:
+ return -1;
+}
+
+void xpathfull_finish() {
+ if (ctx) {
+ xmlXPathFreeContext(ctx);
+ ctx = NULL;
+ }
+ if (doc) {
+ xmlFreeDoc(doc);
+ doc = NULL;
+ }
+ return;
+}
+
+/**
+ * _ccs_get_fullxpath
+ * @desc:
+ * @query:
+ * @rtn: value returned
+ * @list: 1 to operate in list fashion
+ *
+ * This function will allocate space for the value that is the result
+ * of the given query. It is the user's responsibility to ensure that
+ * the data returned is freed.
+ *
+ * Returns: 0 on success, < 0 on failure
+ */
+char * _ccs_get_fullxpath(confdb_handle_t handle, unsigned int connection_handle, const char *query, int list)
+{
+ xmlXPathObjectPtr obj = NULL;
+ char realquery[PATH_MAX + 16];
+ char previous_query[PATH_MAX];
+ unsigned int list_handle = 0;
+ unsigned int xmllistindex = 0;
+ int prev = 0;
+ char *rtn = NULL;
+
+ errno = 0;
+
+ if(strncmp(query, "/", 1)) {
+ errno = EINVAL;
+ goto fail;
+ }
+
+ memset(previous_query, 0, PATH_MAX);
+
+ prev = get_previous_query(handle, connection_handle, previous_query, &list_handle);
+
+ if (list && !prev && !strcmp(query, previous_query)) {
+ if (confdb_key_increment(handle, connection_handle, "iterator_tracker", strlen("iterator_tracker"), &xmllistindex) != SA_AIS_OK) {
+ xmllistindex = 0;
+ } else {
+ xmllistindex--;
+ }
+ } else {
+ reset_iterator(handle, connection_handle);
+ xmllistindex = 0;
+ }
+
+ memset(realquery, 0, PATH_MAX + 16);
+ snprintf(realquery, PATH_MAX + 16 - 1, "/objdbmaindoc%s", query);
+
+ obj = xmlXPathEvalExpression((xmlChar *)realquery, ctx);
+
+ if(!obj) {
+ errno = EINVAL;
+ goto fail;
+ }
+
+ if (obj->nodesetval && (obj->nodesetval->nodeNr > 0)) {
+ xmlNodePtr node;
+ int size = 0, nnv = 0;
+
+ if(xmllistindex >= obj->nodesetval->nodeNr){
+ reset_iterator(handle, connection_handle);
+ errno = ENODATA;
+ goto fail;
+ }
+
+ node = obj->nodesetval->nodeTab[xmllistindex];
+
+ if(!node) {
+ errno = ENODATA;
+ goto fail;
+ }
+
+ if (((node->type == XML_ATTRIBUTE_NODE) && strstr(query, "@*")) ||
+ ((node->type == XML_ELEMENT_NODE) && strstr(query, "child::*"))) {
+ if (node->children && node->children->content)
+ size = strlen((char *)node->children->content) +
+ strlen((char *)node->name)+2;
+ else
+ size = strlen((char *)node->name)+2;
+
+ nnv = 1;
+ } else {
+ if (node->children && node->children->content)
+ size = strlen((char *)node->children->content)+1;
+
+ else {
+ errno = ENODATA;
+ goto fail;
+ }
+ }
+
+ rtn = malloc(size);
+
+ if (!rtn) {
+ errno = ENOMEM;
+ goto fail;
+ }
+
+ if (nnv)
+ sprintf(rtn, "%s=%s", node->name, node->children ? (char *)node->children->content:"");
+ else
+ sprintf(rtn, "%s", node->children ? node->children->content : node->name);
+
+ if(list)
+ set_previous_query(handle, connection_handle, (char *)query, OBJECT_PARENT_HANDLE);
+
+ } else
+ errno = EINVAL;
+
+fail:
+ if(obj)
+ xmlXPathFreeObject(obj);
+
+ return rtn;
+}
diff --git a/config/libs/libccsconfdb/libccs.c b/config/libs/libccsconfdb/libccs.c
index 60ae159..8484ffa 100644
--- a/config/libs/libccsconfdb/libccs.c
+++ b/config/libs/libccsconfdb/libccs.c
@@ -6,8 +6,6 @@
#include <limits.h>
#include <corosync/saAis.h>
#include <corosync/confdb.h>
-#include <libxml/parser.h>
-#include <libxml/xpath.h>
#ifdef EXPERIMENTAL_BUILD
#include <time.h>
#endif
@@ -21,19 +19,10 @@
#endif
#endif
-#ifndef XMLBUFSIZE
-#define XMLBUFSIZE 64000
-#endif
-
#ifdef EXPERIMENTAL_BUILD
int ccs_persistent_conn = 0;
#endif
-int fullxpath = 0;
-
-static xmlDocPtr doc = NULL;
-static xmlXPathContextPtr ctx = NULL;
-
/* Callbacks are not supported - we will use them to update fullxml doc/ctx */
static confdb_callbacks_t callbacks = {
};
@@ -233,167 +222,6 @@ static int clean_stalled_ccs_handles(confdb_handle_t handle)
}
#endif
-static int add_to_buffer(char *data, char **buffer, int *bufsize)
-{
- int datalen = 0, bufferlen = 0;
- char *newbuf = NULL;
-
- datalen = strlen(data);
- bufferlen = strlen(*buffer);
-
- if(datalen) {
- if((bufferlen + datalen) >= *bufsize) {
- newbuf = malloc((*bufsize * 2));
- if(!newbuf) {
- errno = ENOMEM;
- return -1;
- }
- *bufsize = *bufsize * 2;
- memset(newbuf, 0, *bufsize);
- memcpy(newbuf, *buffer, bufferlen);
- free(*buffer);
- *buffer = newbuf;
- }
- strncpy(*buffer + bufferlen, data, datalen);
- }
- return 0;
-}
-
-static int dump_objdb_buff(confdb_handle_t dump_handle, unsigned int parent_object_handle, char **buffer, int *bufsize)
-{
- unsigned int object_handle;
- char temp[PATH_MAX];
- char object_name[PATH_MAX];
- int object_name_len;
- char key_name[PATH_MAX];
- int key_name_len;
- char key_value[PATH_MAX];
- int key_value_len;
- int res;
-
- res = confdb_key_iter_start(dump_handle, parent_object_handle);
- if (res != SA_AIS_OK) {
- errno = ENOMEM;
- return -1;
- }
-
- if (!*buffer || ((*buffer) && !strlen(*buffer))) {
- snprintf(temp, PATH_MAX - 1, "<?xml version=\"1.0\"?>\n<objdbmaindoc>\n");
- if(add_to_buffer(temp, buffer, bufsize))
- return -1;
- }
-
- while ( (res = confdb_key_iter(dump_handle, parent_object_handle, key_name, &key_name_len,
- key_value, &key_value_len)) == SA_AIS_OK) {
- key_name[key_name_len] = '\0';
- key_value[key_value_len] = '\0';
-
- if (!strncmp(key_name, "service_id", key_name_len))
- continue;
- if (!strncmp(key_name, "handle", key_name_len))
- continue;
- if (!strncmp(key_name, "next_handle", key_name_len))
- continue;
-
- snprintf(temp, PATH_MAX - 1, " %s=\"%s\"", key_name, key_value);
- if(add_to_buffer(temp, buffer, bufsize))
- return -1;
- }
-
- if (parent_object_handle > 0) {
- snprintf(temp, PATH_MAX - 1, ">\n");
- if(add_to_buffer(temp, buffer, bufsize))
- return -1;
- }
-
- res = confdb_object_iter_start(dump_handle, parent_object_handle);
- if (res != SA_AIS_OK) {
- errno = ENOMEM;
- return -1;
- }
-
- while ( (res = confdb_object_iter(dump_handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == SA_AIS_OK) {
- unsigned int parent;
-
- res = confdb_object_parent_get(dump_handle, object_handle, &parent);
- if (res != SA_AIS_OK) {
- errno = EINVAL;
- return -1;
- }
-
- object_name[object_name_len] = '\0';
-
- /* we need to skip the top level services because they have invalid
- * xml chars */
-
- snprintf(temp, PATH_MAX - 1, "<%s", object_name);
- if(add_to_buffer(temp, buffer, bufsize))
- return -1;
-
- res = dump_objdb_buff(dump_handle, object_handle, buffer, bufsize);
- if(res) {
- errno = res;
- return res;
- }
-
- if (object_handle != parent_object_handle) {
- snprintf(temp, PATH_MAX - 1, "</%s>\n", object_name);
- if(add_to_buffer(temp, buffer, bufsize))
- return -1;
- } else {
- snprintf(temp, PATH_MAX - 1, ">\n");
- if(add_to_buffer(temp, buffer, bufsize))
- return -1;
- }
- }
-
- if (parent_object_handle == OBJECT_PARENT_HANDLE) {
- snprintf(temp, PATH_MAX - 1, "</objdbmaindoc>\n");
- if(add_to_buffer(temp, buffer, bufsize))
- return -1;
- }
-
- return 0;
-}
-
-static int xpathfull_init(confdb_handle_t handle, int ccs_handle) {
- int size = XMLBUFSIZE;
- char *buffer, *newbuf;
-
- newbuf = buffer = malloc(XMLBUFSIZE);
- if(!buffer) {
- errno = ENOMEM;
- goto fail;
- }
-
- memset(buffer, 0, XMLBUFSIZE);
-
- if (dump_objdb_buff(handle, OBJECT_PARENT_HANDLE, &newbuf, &size))
- goto fail;
-
- if (newbuf != buffer) {
- buffer = newbuf;
- newbuf = NULL;
- }
-
- doc = xmlParseMemory(buffer,strlen(buffer));
- if(!doc)
- goto fail;
-
- free(buffer);
-
- ctx = xmlXPathNewContext(doc);
- if(!ctx) {
- xmlFreeDoc(doc);
- goto fail;
- }
-
- return 0;
-
-fail:
- return -1;
-}
-
/**
* ccs_connect
*
@@ -524,16 +352,8 @@ int ccs_disconnect(int desc)
} else
fullxpathint = atoi(data);
- if (fullxpathint) {
- if (ctx) {
- xmlXPathFreeContext(ctx);
- ctx = NULL;
- }
- if (doc) {
- xmlFreeDoc(doc);
- doc = NULL;
- }
- }
+ if (fullxpathint)
+ xpathfull_finish();
ret = destroy_ccs_handle(handle, connection_handle);
confdb_disconnect(handle);
@@ -611,122 +431,6 @@ void reset_iterator(confdb_handle_t handle, unsigned int connection_handle)
}
/**
- * _ccs_get_fullxpath
- * @desc:
- * @query:
- * @rtn: value returned
- * @list: 1 to operate in list fashion
- *
- * This function will allocate space for the value that is the result
- * of the given query. It is the user's responsibility to ensure that
- * the data returned is freed.
- *
- * Returns: 0 on success, < 0 on failure
- */
-static char * _ccs_get_fullxpath(confdb_handle_t handle, unsigned int connection_handle, const char *query, int list)
-{
- xmlXPathObjectPtr obj = NULL;
- char realquery[PATH_MAX + 16];
- char previous_query[PATH_MAX];
- unsigned int list_handle = 0;
- unsigned int xmllistindex = 0;
- int prev = 0;
- char *rtn = NULL;
-
- errno = 0;
-
- if(strncmp(query, "/", 1)) {
- errno = EINVAL;
- goto fail;
- }
-
- memset(previous_query, 0, PATH_MAX);
-
- prev = get_previous_query(handle, connection_handle, previous_query, &list_handle);
-
- if (list && !prev && !strcmp(query, previous_query)) {
- if (confdb_key_increment(handle, connection_handle, "iterator_tracker", strlen("iterator_tracker"), &xmllistindex) != SA_AIS_OK) {
- xmllistindex = 0;
- } else {
- xmllistindex--;
- }
- } else {
- reset_iterator(handle, connection_handle);
- xmllistindex = 0;
- }
-
- memset(realquery, 0, PATH_MAX + 16);
- snprintf(realquery, PATH_MAX + 16 - 1, "/objdbmaindoc%s", query);
-
- obj = xmlXPathEvalExpression((xmlChar *)realquery, ctx);
-
- if(!obj) {
- errno = EINVAL;
- goto fail;
- }
-
- if (obj->nodesetval && (obj->nodesetval->nodeNr > 0)) {
- xmlNodePtr node;
- int size = 0, nnv = 0;
-
- if(xmllistindex >= obj->nodesetval->nodeNr){
- reset_iterator(handle, connection_handle);
- errno = ENODATA;
- goto fail;
- }
-
- node = obj->nodesetval->nodeTab[xmllistindex];
-
- if(!node) {
- errno = ENODATA;
- goto fail;
- }
-
- if (((node->type == XML_ATTRIBUTE_NODE) && strstr(query, "@*")) ||
- ((node->type == XML_ELEMENT_NODE) && strstr(query, "child::*"))) {
- if (node->children && node->children->content)
- size = strlen((char *)node->children->content) +
- strlen((char *)node->name)+2;
- else
- size = strlen((char *)node->name)+2;
-
- nnv = 1;
- } else {
- if (node->children && node->children->content)
- size = strlen((char *)node->children->content)+1;
-
- else {
- errno = ENODATA;
- goto fail;
- }
- }
-
- rtn = malloc(size);
-
- if (!rtn) {
- errno = ENOMEM;
- goto fail;
- }
-
- if (nnv)
- sprintf(rtn, "%s=%s", node->name, node->children ? (char *)node->children->content:"");
- else
- sprintf(rtn, "%s", node->children ? node->children->content : node->name);
-
- if(list)
- set_previous_query(handle, connection_handle, (char *)query, OBJECT_PARENT_HANDLE);
-
- } else
- errno = EINVAL;
-
-fail:
- if(obj)
- xmlXPathFreeObject(obj);
-
- return rtn;
-}
-
-/**
* _ccs_get
* @desc:
* @query: