[Libosinfo] [PATCH db-tools 01/11] Share code for getting database path locations

Daniel P. Berrange berrange at redhat.com
Tue Jun 28 15:52:27 UTC 2016


Both the osinfo-db-import and osinfo-db-export tools have
the same code for getting the database path locations. Pull
it out into a osinfo-db-util.c file for sharing.

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 tools/Makefile.am        |   9 +++-
 tools/osinfo-db-export.c |  80 ++--------------------------------
 tools/osinfo-db-import.c |  80 ++--------------------------------
 tools/osinfo-db-util.c   | 110 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/osinfo-db-util.h   |  48 +++++++++++++++++++++
 5 files changed, 171 insertions(+), 156 deletions(-)
 create mode 100644 tools/osinfo-db-util.c
 create mode 100644 tools/osinfo-db-util.h

diff --git a/tools/Makefile.am b/tools/Makefile.am
index 701fbe4..37f9abb 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -21,19 +21,24 @@ POD2MAN = pod2man -c "Virtualization Support" -r "$(PACKAGE)-$(VERSION)"
 %.1: %.c Makefile
 	$(AM_V_GEN)$(POD2MAN) $< $@
 
+COMMON_SOURCES = \
+		osinfo-db-util.c \
+		osinfo-db-util.h \
+		$(NULL)
+
 osinfo_db_validate_SOURCES = osinfo-db-validate.c
 osinfo_db_validate_LDADD = $(GOBJECT_LIBS)	\
 		      $(GIO_LIBS)		\
 		      $(GLIB_LIBS)		\
 		      $(LIBXML_LIBS)
 
-osinfo_db_import_SOURCES = osinfo-db-import.c
+osinfo_db_import_SOURCES = osinfo-db-import.c $(COMMON_SOURCES)
 osinfo_db_import_LDADD = $(GOBJECT_LIBS)	\
 		      $(GIO_LIBS)		\
 		      $(GLIB_LIBS)		\
 		      $(LIBARCHIVE_LIBS)
 
-osinfo_db_export_SOURCES = osinfo-db-export.c
+osinfo_db_export_SOURCES = osinfo-db-export.c $(COMMON_SOURCES)
 osinfo_db_export_LDADD = $(GOBJECT_LIBS)	\
 		      $(GIO_LIBS)		\
 		      $(GLIB_LIBS)		\
diff --git a/tools/osinfo-db-export.c b/tools/osinfo-db-export.c
index ea68645..256c793 100644
--- a/tools/osinfo-db-export.c
+++ b/tools/osinfo-db-export.c
@@ -23,89 +23,15 @@
 
 #include <config.h>
 
-#include <glib.h>
-#include <gio/gio.h>
 #include <locale.h>
 #include <glib/gi18n.h>
 #include <stdlib.h>
 #include <archive.h>
 #include <archive_entry.h>
 
-const char *argv0;
-
-static GFile *osinfo_db_export_get_system_path(const gchar *root)
-{
-    GFile *file;
-    gchar *dbdir;
-    const gchar *path = g_getenv("OSINFO_DATA_DIR");
-    if (!path)
-        path = DATA_DIR "/libosinfo";
-
-    dbdir = g_strdup_printf("%s%s/db", root, path);
-    file = g_file_new_for_path(dbdir);
-    g_free(dbdir);
-    return file;
-}
-
-static GFile *osinfo_db_export_get_local_path(const gchar *root)
-{
-    GFile *file;
-    gchar *dbdir;
-
-    dbdir = g_strdup_printf("%s" SYSCONFDIR "/libosinfo/db", root);
-    file = g_file_new_for_path(dbdir);
-    g_free(dbdir);
-    return file;
-}
-
-static GFile *osinfo_db_export_get_user_path(const gchar *root)
-{
-    GFile *file;
-    gchar *dbdir;
-    const gchar *configdir = g_get_user_config_dir();
-
-    dbdir = g_strdup_printf("%s%s/libosinfo/db", root, configdir);
-    file = g_file_new_for_path(dbdir);
-    g_free(dbdir);
-    return file;
-}
+#include "osinfo-db-util.h"
 
-
-static GFile *osinfo_db_export_get_custom_path(const gchar *dir,
-                                               const gchar *root)
-{
-    GFile *file;
-    gchar *dbdir;
-
-    dbdir = g_strdup_printf("%s%s", root, dir);
-    file = g_file_new_for_path(dbdir);
-    g_free(dbdir);
-    return file;
-}
-
-
-static GFile *osinfo_db_export_get_path(const char *root,
-                                        gboolean user,
-                                        gboolean local,
-                                        gboolean system,
-                                        const char *custom)
-{
-    if (custom) {
-        return osinfo_db_export_get_custom_path(custom, root);
-    } else if (user) {
-        return osinfo_db_export_get_user_path(root);
-    } else if (local) {
-        return osinfo_db_export_get_local_path(root);
-    } else if (system) {
-        return osinfo_db_export_get_system_path(root);
-#ifndef WIN32
-    } else if (geteuid() == 0) {
-        return osinfo_db_export_get_local_path(root);
-#endif
-    } else {
-        return osinfo_db_export_get_user_path(root);
-    }
-}
+const char *argv0;
 
 
 static int osinfo_db_export_create_file(const gchar *prefix,
@@ -481,7 +407,7 @@ gint main(gint argc, gchar **argv)
 
     prefix = osinfo_db_export_prefix(version);
     archive = argc == 2 ? argv[1] : NULL;
-    dir = osinfo_db_export_get_path(root, user, local, system, custom);
+    dir = osinfo_db_get_path(root, user, local, system, custom);
     if (osinfo_db_export_create(prefix, dir, archive, verbose) < 0)
         goto error;
 
diff --git a/tools/osinfo-db-import.c b/tools/osinfo-db-import.c
index 327562a..b621d2b 100644
--- a/tools/osinfo-db-import.c
+++ b/tools/osinfo-db-import.c
@@ -23,89 +23,15 @@
 
 #include <config.h>
 
-#include <glib.h>
-#include <gio/gio.h>
 #include <locale.h>
 #include <glib/gi18n.h>
 #include <stdlib.h>
 #include <archive.h>
 #include <archive_entry.h>
 
-const char *argv0;
-
-static GFile *osinfo_db_import_get_system_path(const gchar *root)
-{
-    GFile *file;
-    gchar *dbdir;
-    const gchar *path = g_getenv("OSINFO_DATA_DIR");
-    if (!path)
-        path = DATA_DIR "/libosinfo";
-
-    dbdir = g_strdup_printf("%s%s/db", root, path);
-    file = g_file_new_for_path(dbdir);
-    g_free(dbdir);
-    return file;
-}
-
-static GFile *osinfo_db_import_get_local_path(const gchar *root)
-{
-    GFile *file;
-    gchar *dbdir;
-
-    dbdir = g_strdup_printf("%s" SYSCONFDIR "/libosinfo/db", root);
-    file = g_file_new_for_path(dbdir);
-    g_free(dbdir);
-    return file;
-}
-
-static GFile *osinfo_db_import_get_user_path(const gchar *root)
-{
-    GFile *file;
-    gchar *dbdir;
-    const gchar *configdir = g_get_user_config_dir();
-
-    dbdir = g_strdup_printf("%s%s/libosinfo/db", root, configdir);
-    file = g_file_new_for_path(dbdir);
-    g_free(dbdir);
-    return file;
-}
-
-static GFile *osinfo_db_import_get_custom_path(const gchar *dir,
-                                               const gchar *root)
-{
-    GFile *file;
-    gchar *dbdir;
-
-    dbdir = g_strdup_printf("%s%s", root, dir);
-    file = g_file_new_for_path(dbdir);
-    g_free(dbdir);
-    return file;
-}
-
-
-static GFile *osinfo_db_import_get_path(const char *root,
-                                        gboolean user,
-                                        gboolean local,
-                                        gboolean system,
-                                        const char *custom)
-{
-    if (custom) {
-        return osinfo_db_import_get_custom_path(custom, root);
-    } else if (user) {
-        return osinfo_db_import_get_user_path(root);
-    } else if (local) {
-        return osinfo_db_import_get_local_path(root);
-    } else if (system) {
-        return osinfo_db_import_get_system_path(root);
-#ifndef WIN32
-    } else if (geteuid() == 0) {
-        return osinfo_db_import_get_local_path(root);
-#endif
-    } else {
-        return osinfo_db_import_get_user_path(root);
-    }
-}
+#include "osinfo-db-util.h"
 
+const char *argv0;
 
 static int osinfo_db_import_create_reg(GFile *file,
                                        struct archive *arc,
@@ -335,7 +261,7 @@ gint main(gint argc, gchar **argv)
     }
 
     archive = argc == 2 ? argv[1] : NULL;
-    dir = osinfo_db_import_get_path(root, user, local, system, custom);
+    dir = osinfo_db_get_path(root, user, local, system, custom);
     if (osinfo_db_import_extract(dir, archive, verbose) < 0)
         goto error;
 
diff --git a/tools/osinfo-db-util.c b/tools/osinfo-db-util.c
new file mode 100644
index 0000000..1d81d0c
--- /dev/null
+++ b/tools/osinfo-db-util.c
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * osinfo-db-util: misc helper APIs
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Authors:
+ *   Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#include <config.h>
+
+#include "osinfo-db-util.h"
+
+GFile *osinfo_db_get_system_path(const gchar *root)
+{
+    GFile *file;
+    gchar *dbdir;
+    const gchar *path = g_getenv("OSINFO_DATA_DIR");
+    if (!path)
+        path = DATA_DIR "/libosinfo";
+
+    dbdir = g_strdup_printf("%s%s/db", root, path);
+    file = g_file_new_for_path(dbdir);
+    g_free(dbdir);
+    return file;
+}
+
+
+GFile *osinfo_db_get_local_path(const gchar *root)
+{
+    GFile *file;
+    gchar *dbdir;
+
+    dbdir = g_strdup_printf("%s" SYSCONFDIR "/libosinfo/db", root);
+    file = g_file_new_for_path(dbdir);
+    g_free(dbdir);
+    return file;
+}
+
+
+GFile *osinfo_db_get_user_path(const gchar *root)
+{
+    GFile *file;
+    gchar *dbdir;
+    const gchar *configdir = g_get_user_config_dir();
+
+    dbdir = g_strdup_printf("%s%s/libosinfo/db", root, configdir);
+    file = g_file_new_for_path(dbdir);
+    g_free(dbdir);
+    return file;
+}
+
+
+GFile *osinfo_db_get_custom_path(const gchar *dir,
+                                 const gchar *root)
+{
+    GFile *file;
+    gchar *dbdir;
+
+    dbdir = g_strdup_printf("%s%s", root, dir);
+    file = g_file_new_for_path(dbdir);
+    g_free(dbdir);
+    return file;
+}
+
+
+GFile *osinfo_db_get_path(const char *root,
+                          gboolean user,
+                          gboolean local,
+                          gboolean system,
+                          const char *custom)
+{
+    if (custom) {
+        return osinfo_db_get_custom_path(custom, root);
+    } else if (user) {
+        return osinfo_db_get_user_path(root);
+    } else if (local) {
+        return osinfo_db_get_local_path(root);
+    } else if (system) {
+        return osinfo_db_get_system_path(root);
+#ifndef WIN32
+    } else if (geteuid() == 0) {
+        return osinfo_db_get_local_path(root);
+#endif
+    } else {
+        return osinfo_db_get_user_path(root);
+    }
+}
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ * End:
+ */
diff --git a/tools/osinfo-db-util.h b/tools/osinfo-db-util.h
new file mode 100644
index 0000000..acfcde6
--- /dev/null
+++ b/tools/osinfo-db-util.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * osinfo-db-util: misc helper APIs
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Authors:
+ *   Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#ifndef OSINFO_DB_UTIL_H__
+#define OSINFO_DB_UTIL_H__
+
+#include <gio/gio.h>
+
+GFile *osinfo_db_get_system_path(const gchar *root);
+GFile *osinfo_db_get_local_path(const gchar *root);
+GFile *osinfo_db_get_user_path(const gchar *root);
+GFile *osinfo_db_get_custom_path(const gchar *dir,
+                                 const gchar *root);
+GFile *osinfo_db_get_path(const char *root,
+                          gboolean user,
+                          gboolean local,
+                          gboolean system,
+                          const char *custom);
+
+#endif /* OSINFO_DB_UTIL_H__ */
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ * End:
+ */
-- 
2.7.4




More information about the Libosinfo mailing list