[libvirt] [sandbox PATCH 1/3] Add an utility function for guessing filetype from file extension
Daniel P. Berrange
berrange at redhat.com
Thu Jun 11 13:41:29 UTC 2015
On Wed, Jun 10, 2015 at 01:40:08PM +0200, Eren Yagdiran wrote:
> Consider the file name extension as the image type, except for .img that are usually RAW images
> ---
> libvirt-sandbox/Makefile.am | 1 +
> libvirt-sandbox/libvirt-sandbox-util.c | 79 ++++++++++++++++++++++++++++++++++
> libvirt-sandbox/libvirt-sandbox-util.h | 6 +++
> 3 files changed, 86 insertions(+)
> create mode 100644 libvirt-sandbox/libvirt-sandbox-util.c
>
> diff --git a/libvirt-sandbox/Makefile.am b/libvirt-sandbox/Makefile.am
> index 96302cb..6917f04 100644
> --- a/libvirt-sandbox/Makefile.am
> +++ b/libvirt-sandbox/Makefile.am
> @@ -84,6 +84,7 @@ SANDBOX_HEADER_FILES = \
> $(NULL)
> SANDBOX_SOURCE_FILES = \
> libvirt-sandbox-main.c \
> + libvirt-sandbox-util.c \
> libvirt-sandbox-config.c \
> libvirt-sandbox-config-network.c \
> libvirt-sandbox-config-network-address.c \
> diff --git a/libvirt-sandbox/libvirt-sandbox-util.c b/libvirt-sandbox/libvirt-sandbox-util.c
> new file mode 100644
> index 0000000..0ab4fac
> --- /dev/null
> +++ b/libvirt-sandbox/libvirt-sandbox-util.c
> @@ -0,0 +1,79 @@
> +/*
> + * libvirt-sandbox-util.c: libvirt sandbox util functions
> + *
> + * Copyright (C) 2015 Universitat Politècnica de Catalunya.
> + *
> + * 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: Eren Yagdiran <erenyagdiran at gmail.com>
> + */
> +
> +#include <config.h>
> +#include <string.h>
> +
> +#include "libvirt-sandbox/libvirt-sandbox.h"
> +
> +/* This array contains string values for GVirConfigDomainDiskFormat,
> + * order is important.*/
> +static const gchar *FORMATS_STRINGS[] = {
> + "raw",
> + "dir",
> + "bochs",
> + "cloop",
> + "cow",
> + "dmg",
> + "iso",
> + "qcow",
> + "qcow2",
> + "qed",
> + "vmdk",
> + "vpc",
> + "fat",
> + "vhd",
> + NULL
> +};
I'm not convinced we actually need this lookup table. The libvirt-gconfig
library defines a formal gobject enum type for GVirConfigDomainDiskFormat
which lets you do value <-> string lookups / conversions.
eg
GEnumClass *klass = g_type_class_ref(GVIR_CONFIG_DOMAIN_DISK_FORMAT);
GEnumValue *value = g_enum_get_value(klass, GVIR_CONFIG_DOMAIN_DISK_FORMAT_QCOW2)
value->value_nick now contains the string 'qcow2'
> +
> +gint gvir_sandbox_util_guess_image_format(const gchar *path){
We ought to have a GError ** parameter here to return an
error message.
> +
> + gchar *tmp;
> +
> + if ((tmp = strchr(path, '.')) == NULL) {
> + return -1;
> + }
> + tmp = tmp + 1;
> +
> + if (strcmp(tmp,"img")==0){
> + return GVIR_CONFIG_DOMAIN_DISK_FORMAT_RAW;
> + }
> +
> + return gvir_sandbox_util_disk_format_from_str(tmp);
> +}
> +
> +gint gvir_sandbox_util_disk_format_from_str(const gchar *value)
Same here with GError **
> +{
> + gint i = 0;
> +
> + while (FORMATS_STRINGS[i] != NULL) {
> + if (strcmp(FORMATS_STRINGS[i], value) == 0)
> + return i;
> + i++;
> + }
> + return -1;
> +}
> +
> +const gchar *gvir_sandbox_util_disk_format_to_str(GVirConfigDomainDiskFormat format)
> +{
> + return FORMATS_STRINGS[format];
> +}
This is redundant - the g_enum apis already let callers do this
conversion.
Regards,
Daniel
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org -o- http://virt-manager.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
More information about the libvir-list
mailing list