[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 10/21] Change virNetDaemonGetServerNames to virNetDaemonGetServers



For now it does not matter which ones we return as the code is similarly
complex, however it will fit in with other constructs in the future,
mainly when we will be able to generate dispatch helpers.

Signed-off-by: Martin Kletzander <mkletzan redhat com>
---
 daemon/admin_server.c   | 18 +-----------------
 src/libvirt_remote.syms |  1 +
 src/rpc/virnetdaemon.c  | 43 +++++++++++++++++++++++++++++--------------
 src/rpc/virnetdaemon.h  |  2 +-
 4 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/daemon/admin_server.c b/daemon/admin_server.c
index 0ccb39e91c77..a35f33130607 100644
--- a/daemon/admin_server.c
+++ b/daemon/admin_server.c
@@ -42,33 +42,17 @@ adminDaemonListServers(virNetDaemonPtr dmn,
                        unsigned int flags)
 {
     int ret = -1;
-    const char **srv_names = NULL;
     virNetServerPtr *srvs = NULL;
-    size_t i;
-    ssize_t nsrvs = 0;

     virCheckFlags(0, -1);

-    if ((nsrvs = virNetDaemonGetServerNames(dmn, &srv_names)) < 0)
+    if ((ret = virNetDaemonGetServers(dmn, &srvs)) < 0)
         goto cleanup;

     if (servers) {
-        if (VIR_ALLOC_N(srvs, nsrvs) < 0)
-            goto cleanup;
-
-        for (i = 0; i < nsrvs; i++) {
-            if (!(srvs[i] = virNetDaemonGetServer(dmn, srv_names[i])))
-                goto cleanup;
-        }
-
         *servers = srvs;
         srvs = NULL;
     }
-
-    ret = nsrvs;
-
  cleanup:
-    VIR_FREE(srv_names);
-    virObjectListFree(srvs);
     return ret;
 }
diff --git a/src/libvirt_remote.syms b/src/libvirt_remote.syms
index 4a3c5dfc8835..66f93833afda 100644
--- a/src/libvirt_remote.syms
+++ b/src/libvirt_remote.syms
@@ -65,6 +65,7 @@ virNetDaemonAddSignalHandler;
 virNetDaemonAutoShutdown;
 virNetDaemonClose;
 virNetDaemonGetServer;
+virNetDaemonGetServers;
 virNetDaemonHasClients;
 virNetDaemonIsPrivileged;
 virNetDaemonNew;
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index 9181ad7c474b..aeeeff23cb91 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -196,39 +196,54 @@ virNetDaemonGetServer(virNetDaemonPtr dmn,
 }


+struct collectData {
+    virNetServerPtr **servers;
+    size_t nservers;
+};
+
+
+static int
+collectServers(void *payload,
+               const void *name ATTRIBUTE_UNUSED,
+               void *opaque)
+{
+    virNetServerPtr srv = virObjectRef(payload);
+    struct collectData *data = opaque;
+
+    if (!srv)
+        return -1;
+
+    return VIR_APPEND_ELEMENT(*data->servers, data->nservers, srv);
+}
+
+
 /*
  * Returns number of names allocated in *servers, on error sets
  * *servers to NULL and returns -1.  List of *servers must be free()d,
  * but not the items in it (similarly to virHashGetItems).
  */
 ssize_t
-virNetDaemonGetServerNames(virNetDaemonPtr dmn,
-                           const char ***servers)
+virNetDaemonGetServers(virNetDaemonPtr dmn,
+                       virNetServerPtr **servers)
 {
-    virHashKeyValuePairPtr items = NULL;
-    size_t nservers = 0;
+    struct collectData data = { servers, 0 };
     ssize_t ret = -1;
-    size_t i;

     *servers = NULL;

     virObjectLock(dmn);

-    items = virHashGetItems(dmn->servers, NULL);
-    if (!items)
+    if (virHashForEach(dmn->servers, collectServers, &data) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Cannot get all servers from daemon"));
         goto cleanup;
-
-    for (i = 0; items[i].key; i++) {
-        if (VIR_APPEND_ELEMENT(*servers, nservers, items[i].key) < 0)
-            goto cleanup;
     }

-    ret = nservers;
+    ret = data.nservers;

  cleanup:
     if (ret < 0)
-        VIR_FREE(*servers);
-    VIR_FREE(items);
+        virObjectListFree(*servers);
     virObjectUnlock(dmn);
     return ret;
 }
diff --git a/src/rpc/virnetdaemon.h b/src/rpc/virnetdaemon.h
index 0084dfd6527a..211009c5030a 100644
--- a/src/rpc/virnetdaemon.h
+++ b/src/rpc/virnetdaemon.h
@@ -82,6 +82,6 @@ bool virNetDaemonHasClients(virNetDaemonPtr dmn);

 virNetServerPtr virNetDaemonGetServer(virNetDaemonPtr dmn,
                                       const char *serverName);
-ssize_t virNetDaemonGetServerNames(virNetDaemonPtr dmn, const char ***servers);
+ssize_t virNetDaemonGetServers(virNetDaemonPtr dmn, virNetServerPtr **servers);

 #endif /* __VIR_NET_DAEMON_H__ */
-- 
2.7.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]