[Libosinfo] [PATCH db-tools 04/11] Find RNG schema in default database locations
Fabiano Fidêncio
fidencio at redhat.com
Wed Jul 6 11:28:31 UTC 2016
On Tue, Jun 28, 2016 at 5:52 PM, Daniel P. Berrange <berrange at redhat.com> wrote:
> With the split out database, the RNG schema will no longer
> be installed by the code, instead it will be distributed
> as part of the database. Thus we should look in the database
> locations to find the schema.
>
> Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
> ---
> po/POTFILES.in | 1 +
> tools/Makefile.am | 2 +-
> tools/osinfo-db-util.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++
> tools/osinfo-db-util.h | 7 +++++++
> tools/osinfo-db-validate.c | 29 ++++++++++++++++++---------
> 5 files changed, 78 insertions(+), 10 deletions(-)
>
> diff --git a/po/POTFILES.in b/po/POTFILES.in
> index 279b26f..a2cfe55 100644
> --- a/po/POTFILES.in
> +++ b/po/POTFILES.in
> @@ -1,4 +1,5 @@
> tools/osinfo-db-export.c
> tools/osinfo-db-import.c
> tools/osinfo-db-path.c
> +tools/osinfo-db-util.c
> tools/osinfo-db-validate.c
> diff --git a/tools/Makefile.am b/tools/Makefile.am
> index af069a5..f415297 100644
> --- a/tools/Makefile.am
> +++ b/tools/Makefile.am
> @@ -26,7 +26,7 @@ COMMON_SOURCES = \
> osinfo-db-util.h \
> $(NULL)
>
> -osinfo_db_validate_SOURCES = osinfo-db-validate.c
> +osinfo_db_validate_SOURCES = osinfo-db-validate.c $(COMMON_SOURCES)
> osinfo_db_validate_LDADD = $(GOBJECT_LIBS) \
> $(GIO_LIBS) \
> $(GLIB_LIBS) \
> diff --git a/tools/osinfo-db-util.c b/tools/osinfo-db-util.c
> index 1d81d0c..bd4a813 100644
> --- a/tools/osinfo-db-util.c
> +++ b/tools/osinfo-db-util.c
> @@ -23,6 +23,8 @@
>
> #include <config.h>
>
> +#include <glib/gi18n.h>
> +
> #include "osinfo-db-util.h"
>
> GFile *osinfo_db_get_system_path(const gchar *root)
> @@ -101,6 +103,53 @@ GFile *osinfo_db_get_path(const char *root,
> }
> }
>
> +GFile *osinfo_db_get_file(const char *root,
> + gboolean user,
> + gboolean local,
> + gboolean system,
> + const char *custom,
> + const gchar *file,
> + GError **err)
> +{
> + GFile *ret = NULL;
> + gboolean tryAll = TRUE;
> + GFile *paths[4];
> + gsize npaths = 0;
> + gsize i;
> +
> + if (user || local || system || custom)
> + tryAll = FALSE;
> +
> + if (custom)
> + paths[npaths++] = osinfo_db_get_custom_path(custom, root);
> +
> + if (tryAll || user)
> + paths[npaths++] = osinfo_db_get_user_path(root);
> +
> + if (tryAll || local)
> + paths[npaths++] = osinfo_db_get_local_path(root);
> +
> + if (tryAll || system)
> + paths[npaths++] = osinfo_db_get_system_path(root);
> +
> + for (i = 0; i < npaths; i++) {
> + ret = g_file_resolve_relative_path(paths[i], file);
> + if (g_file_query_exists(ret, NULL))
> + break;
> + g_object_unref(ret);
> + ret = NULL;
> + }
> +
> + if (!ret) {
This is just my personal preference, but I really would like to have
explicit comparisons in all cases that you're not checking against a
boolean. But feel free to ignore, probably there are a lot of other
places doing the same.
> + g_set_error(err, 0, 0,
> + _("Unable to locate '%s' in any database location"),
> + file);
> + return NULL;
I would not use "return NULL" here. ret is NULL at this point and
returning ret would be okay.
> + }
> +
> + return ret;
> +}
> +
> /*
> * Local variables:
> * indent-tabs-mode: nil
> diff --git a/tools/osinfo-db-util.h b/tools/osinfo-db-util.h
> index acfcde6..d56c098 100644
> --- a/tools/osinfo-db-util.h
> +++ b/tools/osinfo-db-util.h
> @@ -36,6 +36,13 @@ GFile *osinfo_db_get_path(const char *root,
> gboolean local,
> gboolean system,
> const char *custom);
> +GFile *osinfo_db_get_file(const char *root,
> + gboolean user,
> + gboolean local,
> + gboolean system,
> + const char *custom,
> + const gchar *file,
> + GError **err);
>
> #endif /* OSINFO_DB_UTIL_H__ */
>
> diff --git a/tools/osinfo-db-validate.c b/tools/osinfo-db-validate.c
> index 965ed0e..6826a54 100644
> --- a/tools/osinfo-db-validate.c
> +++ b/tools/osinfo-db-validate.c
> @@ -23,13 +23,11 @@
>
> #include <config.h>
>
> -#include <glib.h>
> -#include <gio/gio.h>
> #include <libxml/relaxng.h>
> #include <locale.h>
> #include <glib/gi18n.h>
>
> -#define SCHEMA PKGDATADIR "/schemas/libosinfo.rng"
> +#include "osinfo-db-util.h"
>
> static gboolean verbose = FALSE;
>
> @@ -192,22 +190,24 @@ static gboolean validate_file(xmlRelaxNGValidCtxtPtr rngValid, GFile *file, GFil
> }
>
>
> -static gboolean validate_files(gint argc, gchar **argv, GError **error)
> +static gboolean validate_files(GFile *schema, gint argc, gchar **argv, GError **error)
> {
> xmlRelaxNGParserCtxtPtr rngParser = NULL;
> xmlRelaxNGPtr rng = NULL;
> xmlRelaxNGValidCtxtPtr rngValid = NULL;
> gboolean ret = FALSE;
> gsize i;
> + gchar *schemapath = NULL;
>
> xmlSetGenericErrorFunc(NULL, validate_generic_error_nop);
> xmlSetStructuredErrorFunc(NULL, validate_structured_error_nop);
>
> - rngParser = xmlRelaxNGNewParserCtxt(SCHEMA);
> + schemapath = g_file_get_path(schema);
> + rngParser = xmlRelaxNGNewParserCtxt(schemapath);
> if (!rngParser) {
> g_set_error(error, 0, 0,
> _("Unable to create RNG parser for %s"),
> - SCHEMA);
> + schemapath);
> goto cleanup;
> }
>
> @@ -215,7 +215,7 @@ static gboolean validate_files(gint argc, gchar **argv, GError **error)
> if (!rng) {
> g_set_error(error, 0, 0,
> _("Unable to parse RNG %s"),
> - SCHEMA);
> + schemapath);
> goto cleanup;
> }
>
> @@ -223,7 +223,7 @@ static gboolean validate_files(gint argc, gchar **argv, GError **error)
> if (!rngValid) {
> g_set_error(error, 0, 0,
> _("Unable to create RNG validation context %s"),
> - SCHEMA);
> + schemapath);
> goto cleanup;
> }
>
> @@ -239,6 +239,7 @@ static gboolean validate_files(gint argc, gchar **argv, GError **error)
> ret = TRUE;
>
> cleanup:
> + g_free(schemapath);
> xmlRelaxNGFreeValidCtxt(rngValid);
> xmlRelaxNGFreeParserCtxt(rngParser);
> xmlRelaxNGFree(rng);
> @@ -250,6 +251,7 @@ gint main(gint argc, gchar **argv)
> GOptionContext *context;
> GError *error = NULL;
> gint ret = EXIT_FAILURE;
> + GFile *schema = NULL;
>
> setlocale(LC_ALL, "");
> textdomain(GETTEXT_PACKAGE);
> @@ -267,7 +269,14 @@ gint main(gint argc, gchar **argv)
> goto error;
> }
>
> - if (!validate_files(argc - 1, argv + 1, &error)) {
> + schema = osinfo_db_get_file(NULL, FALSE, FALSE, FALSE, NULL,
> + "schema/osinfo.rng", &error);
> + if (!schema) {
> + g_printerr("%s\n", error->message);
> + goto error;
> + }
> +
> + if (!validate_files(schema, argc - 1, argv + 1, &error)) {
> g_printerr("%s\n", error->message);
> goto error;
> }
> @@ -275,6 +284,8 @@ gint main(gint argc, gchar **argv)
> ret = EXIT_SUCCESS;
>
> error:
> + if (schema)
> + g_object_unref(schema);
> g_clear_error(&error);
> g_option_context_free(context);
>
> --
> 2.7.4
>
> _______________________________________________
> Libosinfo mailing list
> Libosinfo at redhat.com
> https://www.redhat.com/mailman/listinfo/libosinfo
More information about the Libosinfo
mailing list