Factor common libvirt-sandbox-builder-{container,machine}.c code master
authorChristophe Fergeau <cfergeau@redhat.com>
Thu, 9 Jan 2014 11:18:53 +0000 (12:18 +0100)
committerChristophe Fergeau <cfergeau@redhat.com>
Fri, 24 Jan 2014 13:20:03 +0000 (14:20 +0100)
Some code added by the network filter support commit can easily be moved
to a generic helper in libvirt-sandbox-builder.c

libvirt-sandbox/Makefile.am
libvirt-sandbox/libvirt-sandbox-builder-container.c
libvirt-sandbox/libvirt-sandbox-builder-machine.c
libvirt-sandbox/libvirt-sandbox-builder-private.h [new file with mode: 0644]
libvirt-sandbox/libvirt-sandbox-builder.c

index 3dc1e32..ea485b7 100644 (file)
@@ -105,6 +105,7 @@ SANDBOX_SOURCE_FILES = \
                        libvirt-sandbox-builder-initrd.c \
                        libvirt-sandbox-builder-machine.c \
                        libvirt-sandbox-builder-container.c \
+                       libvirt-sandbox-builder-private.h \
                        libvirt-sandbox-console.c \
                        libvirt-sandbox-console-raw.c \
                        libvirt-sandbox-console-rpc.c \
index bac8c70..c3a58b2 100644 (file)
@@ -26,6 +26,7 @@
 #include <glib/gi18n.h>
 
 #include "libvirt-sandbox/libvirt-sandbox.h"
+#include "libvirt-sandbox/libvirt-sandbox-builder-private.h"
 
 /**
  * SECTION: libvirt-sandbox-builder-container
@@ -325,7 +326,6 @@ static gboolean gvir_sandbox_builder_container_construct_devices(GVirSandboxBuil
         const gchar *source, *mac;
         GVirSandboxConfigNetwork *network = GVIR_SANDBOX_CONFIG_NETWORK(tmp->data);
         GVirSandboxConfigNetworkFilterref *filterref;
-        GVirConfigDomainInterfaceFilterref *glib_fref;
 
         iface = gvir_config_domain_interface_network_new();
         source = gvir_sandbox_config_network_get_source(network);
@@ -344,35 +344,9 @@ static gboolean gvir_sandbox_builder_container_construct_devices(GVirSandboxBuil
 
         filterref = gvir_sandbox_config_network_get_filterref(network);
         if (filterref) {
-            GList *param_iter, *parameters;
-            const gchar *fref_name = gvir_sandbox_config_network_filterref_get_name(filterref);
-            glib_fref = gvir_config_domain_interface_filterref_new();
-            gvir_config_domain_interface_filterref_set_name(glib_fref, fref_name);
-            param_iter = parameters = gvir_sandbox_config_network_filterref_get_parameters(filterref);
-            while (param_iter) {
-                const gchar *name;
-                const gchar *value;
-                GVirSandboxConfigNetworkFilterrefParameter *param = GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF_PARAMETER(param_iter->data);
-                GVirConfigDomainInterfaceFilterrefParameter *glib_param;
-
-                name = gvir_sandbox_config_network_filterref_parameter_get_name(param);
-                value = gvir_sandbox_config_network_filterref_parameter_get_value(param);
-
-                glib_param = gvir_config_domain_interface_filterref_parameter_new();
-                gvir_config_domain_interface_filterref_parameter_set_name(glib_param, name);
-                gvir_config_domain_interface_filterref_parameter_set_value(glib_param, value);
-
-                gvir_config_domain_interface_filterref_add_parameter(glib_fref, glib_param);
-                g_object_unref(glib_param);
-
-                param_iter = param_iter->next;
-            }
-
-            g_list_foreach(parameters, (GFunc)g_object_unref, NULL);
-            g_list_free(parameters);
-
-            gvir_config_domain_interface_set_filterref(GVIR_CONFIG_DOMAIN_INTERFACE(iface), glib_fref);
-            g_object_unref(glib_fref);
+            gvir_sandbox_builder_set_filterref(builder,
+                                               GVIR_CONFIG_DOMAIN_INTERFACE(iface),
+                                               filterref);
         }
 
         g_object_unref(iface);
index 542663c..35a5816 100644 (file)
@@ -28,6 +28,7 @@
 #include <glib/gi18n.h>
 
 #include "libvirt-sandbox/libvirt-sandbox.h"
+#include "libvirt-sandbox/libvirt-sandbox-builder-private.h"
 
 /**
  * SECTION: libvirt-sandbox-builder-machine
@@ -582,7 +583,6 @@ static gboolean gvir_sandbox_builder_machine_construct_devices(GVirSandboxBuilde
         const gchar *source, *mac;
         GVirSandboxConfigNetwork *network = GVIR_SANDBOX_CONFIG_NETWORK(tmp->data);
         GVirSandboxConfigNetworkFilterref *filterref;
-        GVirConfigDomainInterfaceFilterref *glib_fref;
 
         source = gvir_sandbox_config_network_get_source(network);
         if (source) {
@@ -600,42 +600,14 @@ static gboolean gvir_sandbox_builder_machine_construct_devices(GVirSandboxBuilde
         gvir_config_domain_interface_set_model(iface,
                                                "virtio");
 
-        gvir_config_domain_add_device(domain,
-                                      GVIR_CONFIG_DOMAIN_DEVICE(iface));
-
         filterref = gvir_sandbox_config_network_get_filterref(network);
         if (filterref) {
-            GList *param_iter, *parameters;
-            const gchar *fref_name = gvir_sandbox_config_network_filterref_get_name(filterref);
-            glib_fref = gvir_config_domain_interface_filterref_new();
-            gvir_config_domain_interface_filterref_set_name(glib_fref, fref_name);
-            param_iter = parameters = gvir_sandbox_config_network_filterref_get_parameters(filterref);
-            while (param_iter) {
-                const gchar *name;
-                const gchar *value;
-                GVirSandboxConfigNetworkFilterrefParameter *param = GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF_PARAMETER(param_iter->data);
-                GVirConfigDomainInterfaceFilterrefParameter *glib_param;
-
-                name = gvir_sandbox_config_network_filterref_parameter_get_name(param);
-                value = gvir_sandbox_config_network_filterref_parameter_get_value(param);
-
-                glib_param = gvir_config_domain_interface_filterref_parameter_new();
-                gvir_config_domain_interface_filterref_parameter_set_name(glib_param, name);
-                gvir_config_domain_interface_filterref_parameter_set_value(glib_param, value);
-
-                gvir_config_domain_interface_filterref_add_parameter(glib_fref, glib_param);
-                g_object_unref(glib_param);
-
-                param_iter = param_iter->next;
-            }
-
-            g_list_foreach(parameters, (GFunc)g_object_unref, NULL);
-            g_list_free(parameters);
-
-            gvir_config_domain_interface_set_filterref(iface, glib_fref);
-            g_object_unref(glib_fref);
+            gvir_sandbox_builder_set_filterref(builder, iface, filterref);
         }
 
+        gvir_config_domain_add_device(domain,
+                                      GVIR_CONFIG_DOMAIN_DEVICE(iface));
+
         g_object_unref(iface);
 
         tmp = tmp->next;
diff --git a/libvirt-sandbox/libvirt-sandbox-builder-private.h b/libvirt-sandbox/libvirt-sandbox-builder-private.h
new file mode 100644 (file)
index 0000000..288ec40
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * libvirt-sandbox-builder-private.h: libvirt sandbox builder
+ *
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Christophe Fergeau <cfergeau@redhat.com>
+ */
+
+#if !defined(__LIBVIRT_SANDBOX_H__) && !defined(LIBVIRT_SANDBOX_BUILD)
+#error "Only <libvirt-sandbox/libvirt-sandbox.h> can be included directly."
+#endif
+
+#ifndef __LIBVIRT_SANDBOX_BUILDER_PRIVATE_H__
+#define __LIBVIRT_SANDBOX_BUILDER_PRIVATE_H__
+
+G_BEGIN_DECLS
+
+void gvir_sandbox_builder_set_filterref(GVirSandboxBuilder *builder,
+                                        GVirConfigDomainInterface *iface,
+                                        GVirSandboxConfigNetworkFilterref *filterref);
+
+G_END_DECLS
+
+#endif /* __LIBVIRT_SANDBOX_BUILDER_PRIVATE_H__ */
index ff25a0b..48b3acc 100644 (file)
@@ -24,6 +24,7 @@
 #include <string.h>
 
 #include "libvirt-sandbox/libvirt-sandbox.h"
+#include "libvirt-sandbox/libvirt-sandbox-builder-private.h"
 
 /**
  * SECTION: libvirt-sandbox-builder
@@ -446,6 +447,45 @@ gboolean gvir_sandbox_builder_clean_post_stop(GVirSandboxBuilder *builder,
     return klass->clean_post_stop(builder, config, statedir, error);
 }
 
+
+void gvir_sandbox_builder_set_filterref(GVirSandboxBuilder *builder,
+                                        GVirConfigDomainInterface *iface,
+                                        GVirSandboxConfigNetworkFilterref *filterref)
+{
+    GVirConfigDomainInterfaceFilterref *glib_fref;
+
+    GList *param_iter, *parameters;
+    const gchar *fref_name = gvir_sandbox_config_network_filterref_get_name(filterref);
+
+    glib_fref = gvir_config_domain_interface_filterref_new();
+    gvir_config_domain_interface_filterref_set_name(glib_fref, fref_name);
+    param_iter = parameters = gvir_sandbox_config_network_filterref_get_parameters(filterref);
+    while (param_iter) {
+        const gchar *name;
+        const gchar *value;
+        GVirSandboxConfigNetworkFilterrefParameter *param = GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF_PARAMETER(param_iter->data);
+        GVirConfigDomainInterfaceFilterrefParameter *glib_param;
+
+        name = gvir_sandbox_config_network_filterref_parameter_get_name(param);
+        value = gvir_sandbox_config_network_filterref_parameter_get_value(param);
+
+        glib_param = gvir_config_domain_interface_filterref_parameter_new();
+        gvir_config_domain_interface_filterref_parameter_set_name(glib_param, name);
+        gvir_config_domain_interface_filterref_parameter_set_value(glib_param, value);
+
+        gvir_config_domain_interface_filterref_add_parameter(glib_fref, glib_param);
+        g_object_unref(glib_param);
+
+        param_iter = param_iter->next;
+    }
+
+    g_list_foreach(parameters, (GFunc)g_object_unref, NULL);
+    g_list_free(parameters);
+
+    gvir_config_domain_interface_set_filterref(iface, glib_fref);
+    g_object_unref(glib_fref);
+}
+
 /*
  * Local variables:
  *  c-indent-level: 4