[libvirt] [osinfo-db-tools PATCH v2 0/2] RFC: Make osinfo-db-import aware of URLs

This is a simple draft of a work that has been discussed back in July as part of this thread[0]. There are a few things which are not clear to me whether we want (or whether we do *not* want). For now I'm not using any progress callback to print the download status and I don't even know whether it would be desired to have. But this is something that could be easily done. Also, for now the operation is synchronous and there's no cancellable used ... but that's something that could be changed (although I don't see the need for that right now). In order to give it a try, please, just build our source with the patch included and try: ./tools/osinfo-db-import https://releases.pagure.org/libosinfo/osinfo-db-20180920.tar.xz ./tools/osinfo-db-import https://foo.bar/foo.bar.tar.xz [0]: https://www.redhat.com/archives/libosinfo/2018-July/msg00002.html Changes since v1: - Use Gio instead of curl; - An additional patch removing a unused var; Fabiano Fidêncio (2): import: Learn how to deal with URLs import: Remove unused variable tools/osinfo-db-import.c | 91 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 7 deletions(-) -- 2.19.1

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com> --- tools/osinfo-db-import.c | 88 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/tools/osinfo-db-import.c b/tools/osinfo-db-import.c index 0d0bdd9..289a85d 100644 --- a/tools/osinfo-db-import.c +++ b/tools/osinfo-db-import.c @@ -135,6 +135,55 @@ static GFile *osinfo_db_import_get_file(GFile *target, return g_file_resolve_relative_path(target, tmp); } +static int +osinfo_db_import_download_file(const gchar *url, + gchar **source_file) +{ + GFile *in = NULL; + GFile *out = NULL; + GError *err = NULL; + gchar *filename = NULL; + GFileCopyFlags flags = G_FILE_COPY_OVERWRITE | G_FILE_COPY_BACKUP; + int ret = -1; + + in = g_file_new_for_uri(url); + if (in == NULL) + return -1; + + filename = g_file_get_basename(in); + if (filename == NULL) + goto cleanup; + + *source_file = g_strdup_printf("%s/%s", g_get_user_cache_dir(), filename); + if (*source_file == NULL) + goto cleanup; + + out = g_file_new_for_path(*source_file); + if (out == NULL) + goto cleanup; + + if (!g_file_copy(in, out, flags, NULL, NULL, NULL, &err)) { + g_printerr("Could not download the \"%s\" file: %s\n", + url, err->message); + goto cleanup; + } + + ret = 0; + + cleanup: + g_free(filename); + if (in != NULL) + g_object_unref(in); + if (out != NULL) + g_object_unref(out); + if (err != NULL) + g_error_free(err); + if (ret != 0) + unlink(*source_file); + + return ret; +} + static int osinfo_db_import_extract(GFile *target, const char *source, gboolean verbose) @@ -145,6 +194,8 @@ static int osinfo_db_import_extract(GFile *target, int r; GFile *file = NULL; GError *err = NULL; + gchar *source_file = NULL; + const gchar *uri_schemes[] = {"ftp", "http", NULL}; arc = archive_read_new(); @@ -154,9 +205,37 @@ static int osinfo_db_import_extract(GFile *target, if (source != NULL && g_str_equal(source, "-")) source = NULL; - if ((r = archive_read_open_filename(arc, source, 10240)) != ARCHIVE_OK) { + if (source != NULL) { + gboolean download_file = FALSE; + + file = g_file_new_for_uri(source); + if (file == NULL) + goto cleanup; + + /* The supported uri schemes here are "ftp", "http" and "https". + * However, "https" is not set as part of the array as it matches with + * "http". */ + for (gint i = 0; uri_schemes[i] != NULL; i++) { + if (g_file_has_uri_scheme(file, uri_schemes[i])) { + download_file = TRUE; + break; + } + } + + if (download_file) { + if (osinfo_db_import_download_file(source, &source_file) < 0) + goto cleanup; + } else { + source_file = g_strdup(source); + } + + if (source_file == NULL) + goto cleanup; + } + + if ((r = archive_read_open_filename(arc, source_file, 10240)) != ARCHIVE_OK) { g_printerr("%s: cannot open archive %s: %s\n", - argv0, source, archive_error_string(arc)); + argv0, source_file, archive_error_string(arc)); goto cleanup; } @@ -166,7 +245,7 @@ static int osinfo_db_import_extract(GFile *target, break; if (r != ARCHIVE_OK) { g_printerr("%s: cannot read next archive entry in %s: %s\n", - argv0, source, archive_error_string(arc)); + argv0, source_file, archive_error_string(arc)); goto cleanup; } @@ -180,7 +259,7 @@ static int osinfo_db_import_extract(GFile *target, if (archive_read_close(arc) != ARCHIVE_OK) { g_printerr("%s: cannot finish reading archive %s: %s\n", - argv0, source, archive_error_string(arc)); + argv0, source_file, archive_error_string(arc)); goto cleanup; } @@ -191,6 +270,7 @@ static int osinfo_db_import_extract(GFile *target, g_error_free(err); if (file) g_object_unref(file); + g_free(source_file); return ret; } -- 2.19.1

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com> --- tools/osinfo-db-import.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/tools/osinfo-db-import.c b/tools/osinfo-db-import.c index 289a85d..c0b4931 100644 --- a/tools/osinfo-db-import.c +++ b/tools/osinfo-db-import.c @@ -193,7 +193,6 @@ static int osinfo_db_import_extract(GFile *target, int ret = -1; int r; GFile *file = NULL; - GError *err = NULL; gchar *source_file = NULL; const gchar *uri_schemes[] = {"ftp", "http", NULL}; @@ -266,8 +265,6 @@ static int osinfo_db_import_extract(GFile *target, ret = 0; cleanup: archive_read_free(arc); - if (err) - g_error_free(err); if (file) g_object_unref(file); g_free(source_file); -- 2.19.1

On Mon, 2018-10-08 at 10:53 +0200, Fabiano Fidêncio wrote:
This is a simple draft of a work that has been discussed back in July as part of this thread[0].
There are a few things which are not clear to me whether we want (or whether we do *not* want).
For now I'm not using any progress callback to print the download status and I don't even know whether it would be desired to have. But this is something that could be easily done.
Also, for now the operation is synchronous and there's no cancellable used ... but that's something that could be changed (although I don't see the need for that right now).
In order to give it a try, please, just build our source with the patch included and try: ./tools/osinfo-db-import https://releases.pagure.org/libosinfo/osinfo-db-20180920.tar.xz ./tools/osinfo-db-import https://foo.bar/foo.bar.tar.xz
[0]: https://www.redhat.com/archives/libosinfo/2018-July/msg00002.html
Changes since v1: - Use Gio instead of curl; - An additional patch removing a unused var;
Fabiano Fidêncio (2): import: Learn how to deal with URLs import: Remove unused variable
tools/osinfo-db-import.c | 91 ++++++++++++++++++++++++++++++++++++ ---- 1 file changed, 84 insertions(+), 7 deletions(-)
Oops, please, ignore those patches as they've been sent to wrong list, sorry for the noise!
participants (1)
-
Fabiano Fidêncio