[libvirt] [sandbox PATCH 01/10] Add an utility function for guessing filetype from file extension

Eren Yagdiran erenyagdiran at gmail.com
Thu Jun 25 11:27:21 UTC 2015


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 | 72 ++++++++++++++++++++++++++++++++++
 libvirt-sandbox/libvirt-sandbox-util.h |  5 +++
 po/POTFILES.in                         |  1 +
 4 files changed, 79 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..32d4c4e
--- /dev/null
+++ b/libvirt-sandbox/libvirt-sandbox-util.c
@@ -0,0 +1,72 @@
+/*
+ * 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 <errno.h>
+#include <glib/gi18n.h>
+
+#include "libvirt-sandbox/libvirt-sandbox.h"
+
+#define GVIR_SANDBOX_UTIL_ERROR gvir_sandbox_util_error_quark()
+
+static GQuark
+gvir_sandbox_util_error_quark(void)
+{
+    return g_quark_from_static_string("gvir-sandbox-util");
+}
+
+gint gvir_sandbox_util_guess_image_format(const gchar *path,
+                                          GError **error)
+{
+    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, error);
+}
+
+gint gvir_sandbox_util_disk_format_from_str(const gchar *value,
+                                            GError **error)
+{
+    GEnumClass *enum_class = g_type_class_ref(GVIR_CONFIG_TYPE_DOMAIN_DISK_FORMAT);
+    GEnumValue *enum_value;
+    gint ret = -1;
+
+    if (!(enum_value = g_enum_get_value_by_nick(enum_class, value))) {
+        g_set_error(error, GVIR_SANDBOX_UTIL_ERROR, 0,
+                    _("Unknown disk format '%s'"), value);
+        goto cleanup;
+    }
+    ret = enum_value->value;
+
+ cleanup:
+    g_type_class_unref(enum_class);
+    return ret;
+}
diff --git a/libvirt-sandbox/libvirt-sandbox-util.h b/libvirt-sandbox/libvirt-sandbox-util.h
index ae6b74b..890ae7f 100644
--- a/libvirt-sandbox/libvirt-sandbox-util.h
+++ b/libvirt-sandbox/libvirt-sandbox-util.h
@@ -29,6 +29,11 @@
 
 G_BEGIN_DECLS
 
+gint gvir_sandbox_util_guess_image_format(const gchar *path, GError **error);
+
+
+gint gvir_sandbox_util_disk_format_from_str(const gchar *value, GError **error);
+
 /**
  * LIBVIRT_SANDBOX_CLASS_PADDING: (skip)
  */
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 653abc5..f291b98 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -9,3 +9,4 @@ libvirt-sandbox/libvirt-sandbox-console-rpc.c
 libvirt-sandbox/libvirt-sandbox-context.c
 libvirt-sandbox/libvirt-sandbox-init-common.c
 libvirt-sandbox/libvirt-sandbox-rpcpacket.c
+libvirt-sandbox/libvirt-sandbox-util.c
-- 
2.1.0




More information about the libvir-list mailing list