Blob Blame History Raw
From 0d7d69ef2dc65cc9bbfe63b9f6304aa9232636d8 Mon Sep 17 00:00:00 2001
Message-Id: <0d7d69ef2dc65cc9bbfe63b9f6304aa9232636d8@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Wed, 26 Feb 2014 14:54:49 +0100
Subject: [PATCH] conf: Add functions to copy and free network disk source
 definitions

https://bugzilla.redhat.com/show_bug.cgi?id=1032370

To simplify operations on virDomainDiskHostDef arrays we will need deep
copy and freeing functions. Add and properly export them.

(cherry picked from commit ae361674aca64b97288b81874b8e565e12f685e0)

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/conf/domain_conf.c   | 55 +++++++++++++++++++++++++++++++++++++++++++++---
 src/conf/domain_conf.h   |  3 +++
 src/libvirt_private.syms |  2 ++
 3 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index cec0236..4e182f1 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1211,9 +1211,7 @@ void virDomainDiskDefFree(virDomainDiskDefPtr def)
         VIR_FREE(def->seclabels);
     }
 
-    for (i = 0; i < def->nhosts; i++)
-        virDomainDiskHostDefClear(&def->hosts[i]);
-    VIR_FREE(def->hosts);
+    virDomainDiskHostDefFree(def->nhosts, def->hosts);
 
     VIR_FREE(def);
 }
@@ -1228,6 +1226,57 @@ void virDomainDiskHostDefClear(virDomainDiskHostDefPtr def)
     VIR_FREE(def->socket);
 }
 
+
+void
+virDomainDiskHostDefFree(size_t nhosts,
+                         virDomainDiskHostDefPtr hosts)
+{
+    size_t i;
+
+    if (!hosts)
+        return;
+
+    for (i = 0; i < nhosts; i++)
+        virDomainDiskHostDefClear(&hosts[i]);
+
+    VIR_FREE(hosts);
+}
+
+
+virDomainDiskHostDefPtr
+virDomainDiskHostDefCopy(size_t nhosts,
+                         virDomainDiskHostDefPtr hosts)
+{
+    virDomainDiskHostDefPtr ret = NULL;
+    size_t i;
+
+    if (VIR_ALLOC_N(ret, nhosts) < 0)
+        goto error;
+
+    for (i = 0; i < nhosts; i++) {
+        virDomainDiskHostDefPtr src = &hosts[i];
+        virDomainDiskHostDefPtr dst = &ret[i];
+
+        dst->transport = src->transport;
+
+        if (VIR_STRDUP(dst->name, src->name) < 0)
+            goto error;
+
+        if (VIR_STRDUP(dst->port, src->port) < 0)
+            goto error;
+
+        if (VIR_STRDUP(dst->socket, src->socket) < 0)
+            goto error;
+    }
+
+    return ret;
+
+error:
+    virDomainDiskHostDefFree(nhosts, ret);
+    return NULL;
+}
+
+
 void virDomainControllerDefFree(virDomainControllerDefPtr def)
 {
     if (!def)
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index fa81fa1..3d8666a 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2210,6 +2210,9 @@ void virDomainInputDefFree(virDomainInputDefPtr def);
 void virDomainDiskDefFree(virDomainDiskDefPtr def);
 void virDomainLeaseDefFree(virDomainLeaseDefPtr def);
 void virDomainDiskHostDefClear(virDomainDiskHostDefPtr def);
+void virDomainDiskHostDefFree(size_t nhosts, virDomainDiskHostDefPtr hosts);
+virDomainDiskHostDefPtr virDomainDiskHostDefCopy(size_t nhosts,
+                                                 virDomainDiskHostDefPtr hosts);
 int virDomainDeviceFindControllerModel(virDomainDefPtr def,
                                        virDomainDeviceInfoPtr info,
                                        int controllerType);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4d80130..827b1ec 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -196,6 +196,8 @@ virDomainDiskFindByBusAndDst;
 virDomainDiskGeometryTransTypeFromString;
 virDomainDiskGeometryTransTypeToString;
 virDomainDiskHostDefClear;
+virDomainDiskHostDefCopy;
+virDomainDiskHostDefFree;
 virDomainDiskIndexByName;
 virDomainDiskInsert;
 virDomainDiskInsertPreAlloced;
-- 
1.9.0