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 <erenyagdi...@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
+};
+
+gint gvir_sandbox_util_guess_image_format(const gchar *path){
+
+    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)
+{
+    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];
+}
diff --git a/libvirt-sandbox/libvirt-sandbox-util.h 
b/libvirt-sandbox/libvirt-sandbox-util.h
index ae6b74b..fbd3785 100644
--- a/libvirt-sandbox/libvirt-sandbox-util.h
+++ b/libvirt-sandbox/libvirt-sandbox-util.h
@@ -29,6 +29,12 @@
 
 G_BEGIN_DECLS
 
+gint gvir_sandbox_util_guess_image_format(const gchar *path);
+
+const gchar *gvir_sandbox_util_disk_format_to_str(GVirConfigDomainDiskFormat 
format);
+
+gint gvir_sandbox_util_disk_format_from_str(const gchar *value);
+
 /**
  * LIBVIRT_SANDBOX_CLASS_PADDING: (skip)
  */
-- 
2.1.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to