[libvirt] [libvirt-glib 6/6] Add gvir_domain_get_devices()

Christophe Fergeau cfergeau at redhat.com
Wed Feb 29 13:30:56 UTC 2012


On Tue, Feb 28, 2012 at 08:25:07PM +0200, Zeeshan Ali (Khattak) wrote:
> From: "Zeeshan Ali (Khattak)" <zeeshanak at gnome.org>
> 
> Currently we only support existing DomainDevice implementations:
> DomainDisk and DomainInterface.
> ---
>  .../libvirt-gobject-domain-device-private.h        |    2 +
>  libvirt-gobject/libvirt-gobject-domain-device.c    |   21 ++++++++++
>  libvirt-gobject/libvirt-gobject-domain.c           |   42 ++++++++++++++++++++
>  libvirt-gobject/libvirt-gobject-domain.h           |    3 +
>  libvirt-gobject/libvirt-gobject.sym                |    1 +
>  5 files changed, 69 insertions(+), 0 deletions(-)
> 
> diff --git a/libvirt-gobject/libvirt-gobject-domain-device-private.h b/libvirt-gobject/libvirt-gobject-domain-device-private.h
> index 72c660e..292a2ac 100644
> --- a/libvirt-gobject/libvirt-gobject-domain-device-private.h
> +++ b/libvirt-gobject/libvirt-gobject-domain-device-private.h
> @@ -24,6 +24,8 @@
>  
>  G_BEGIN_DECLS
>  
> +G_GNUC_INTERNAL GVirDomainDevice *gvir_domain_device_new(GVirConfigDomainDevice *config,
> +                                                         GVirDomain *domain);
>  virDomainPtr gvir_domain_device_get_domain_handle(GVirDomainDevice *self);
>  
>  G_END_DECLS
> diff --git a/libvirt-gobject/libvirt-gobject-domain-device.c b/libvirt-gobject/libvirt-gobject-domain-device.c
> index 85879d2..0ec5dad 100644
> --- a/libvirt-gobject/libvirt-gobject-domain-device.c
> +++ b/libvirt-gobject/libvirt-gobject-domain-device.c
> @@ -176,3 +176,24 @@ GVirDomain *gvir_domain_device_get_domain(GVirDomainDevice *device) {
>  GVirConfigDomainDevice *gvir_domain_device_get_config(GVirDomainDevice *device) {
>      return g_object_ref (device->priv->config);
>  }
> +
> +G_GNUC_INTERNAL GVirDomainDevice *gvir_domain_device_new(GVirConfigDomainDevice *config,
> +                                                         GVirDomain *domain)

gvir_domain_new_device(GVirDomain *domain, GVirConfigDomainDevice *config)
maybe ?
Having the argument that is always non-NULL first feels better to me.

> +{
> +    GType type;
> +
> +    g_return_val_if_fail(GVIR_CONFIG_IS_DOMAIN_DEVICE(config), NULL);
> +
> +    if (GVIR_CONFIG_IS_DOMAIN_DISK(config))
> +        type = GVIR_TYPE_DOMAIN_DISK;
> +    else if (GVIR_CONFIG_IS_DOMAIN_INTERFACE(config))
> +        type = GVIR_TYPE_DOMAIN_INTERFACE;
> +    else {
> +        g_debug("Unknown device type: %s", G_OBJECT_TYPE_NAME(config));
> +        return NULL;
> +    }

I'd have a slight preference for something like

switch (G_OBJECT_TYPE(config)) {
case GVIR_CONFIG_TYPE_DOMAIN_DISK:
    type = GVIR_TYPE_DOMAIN_DISK;
    break;
...

but I'm fine with either version.

> +
> +    return GVIR_DOMAIN_DEVICE(g_object_new(type,
> +                                           "config", config,
> +                                           "domain", domain, NULL));
> +}
> diff --git a/libvirt-gobject/libvirt-gobject-domain.c b/libvirt-gobject/libvirt-gobject-domain.c
> index 23ad882..3c66c9c 100644
> --- a/libvirt-gobject/libvirt-gobject-domain.c
> +++ b/libvirt-gobject/libvirt-gobject-domain.c
> @@ -29,6 +29,7 @@
>  #include "libvirt-glib/libvirt-glib.h"
>  #include "libvirt-gobject/libvirt-gobject.h"
>  #include "libvirt-gobject-compat.h"
> +#include "libvirt-gobject/libvirt-gobject-domain-device-private.h"
>  
>  #define GVIR_DOMAIN_GET_PRIVATE(obj)                         \
>          (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_DOMAIN, GVirDomainPrivate))
> @@ -868,3 +869,44 @@ gboolean gvir_domain_get_saved(GVirDomain *dom)
>  
>      return virDomainHasManagedSaveImage(dom->priv->handle, 0) == 1;
>  }
> +
> +/**
> + * gvir_domain_get_devices:
> + * @domain: the domain
> + * @err: place-holder for possible errors, or NULL
> + *
> + * Gets the list of devices attached to @domain
> + *
> + * Returns: (element-type LibvirtGObject.DomainDevice) (transfer full): a newly
> + * allocated #GList of #GVirDomainDevice.
> + */
> +GList *gvir_domain_get_devices(GVirDomain *domain,
> +                               GError **err)
> +{
> +    GVirConfigDomain *config;
> +    GList *config_devices;
> +    GList *node;
> +    GList *ret = NULL;
> +
> +    g_return_val_if_fail(GVIR_IS_DOMAIN(domain), NULL);
> +
> +    config = gvir_domain_get_config(domain, 0, err);
> +    if (config == NULL)
> +        return ret;
> +
> +    config_devices = gvir_config_domain_get_devices(config);
> +    for (node = config_devices; node != NULL; node = node->next) {
> +        GVirConfigDomainDevice *device_config;
> +        GVirDomainDevice *device;
> +
> +        device_config = GVIR_CONFIG_DOMAIN_DEVICE(node->data);
> +        device = gvir_domain_device_new(device_config, domain);
> +        if (device != NULL)
> +             ret = g_list_append(ret, device);

slight preference for g_list_prepend (much more efficient when the list is
big).
> +
> +        g_object_unref (device_config);
> +    }
> +    g_list_free (config_devices);

The individual elements need to be unref'ed too as well as config.

> +
> +    return ret;
> +}
> diff --git a/libvirt-gobject/libvirt-gobject-domain.h b/libvirt-gobject/libvirt-gobject-domain.h
> index 56500a8..8a4836e 100644
> --- a/libvirt-gobject/libvirt-gobject-domain.h
> +++ b/libvirt-gobject/libvirt-gobject-domain.h
> @@ -183,6 +183,9 @@ gboolean gvir_domain_save_finish (GVirDomain *dom,
>  gboolean gvir_domain_get_persistent(GVirDomain *dom);
>  gboolean gvir_domain_get_saved(GVirDomain *dom);
>  
> +GList *gvir_domain_get_devices(GVirDomain *domain,
> +                               GError **err);
> +
>  G_END_DECLS
>  
>  #endif /* __LIBVIRT_GOBJECT_DOMAIN_H__ */
> diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym
> index d6999dc..b7b95cb 100644
> --- a/libvirt-gobject/libvirt-gobject.sym
> +++ b/libvirt-gobject/libvirt-gobject.sym
> @@ -72,6 +72,7 @@ LIBVIRT_GOBJECT_0.0.4 {
>  	gvir_domain_get_persistent;
>  	gvir_domain_get_saved;
>  	gvir_domain_screenshot;
> +	gvir_domain_get_devices;

Try to get the list sorted/consistent with the other entries.

Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120229/31d06e1b/attachment-0001.sig>


More information about the libvir-list mailing list