[gnome-software/wip/kalev/gnome-3-22: 82/96] Add support for file:// URIs to gs_plugin_download_file()



commit 8892033c30843ec27b42aef2c0c4f6334f5928a0
Author: Richard Hughes <richard hughsie com>
Date:   Fri Feb 24 16:49:07 2017 +0000

    Add support for file:// URIs to gs_plugin_download_file()
    
    In this case, just fall back to reading the entire file and writing the
    contents in an atomic way.

 src/gs-plugin.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)
---
diff --git a/src/gs-plugin.c b/src/gs-plugin.c
index daa591b..429a826 100644
--- a/src/gs-plugin.c
+++ b/src/gs-plugin.c
@@ -1075,6 +1075,24 @@ gs_plugin_download_data (GsPlugin *plugin,
        g_return_val_if_fail (uri != NULL, NULL);
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
+       /* local */
+       if (g_str_has_prefix (uri, "file://")) {
+               gsize length = 0;
+               g_autofree gchar *contents = NULL;
+               g_autoptr(GError) error_local = NULL;
+               g_debug ("copying %s from plugin %s", uri, priv->name);
+               if (!g_file_get_contents (uri + 7, &contents, &length, &error_local)) {
+                       g_set_error (error,
+                                    GS_PLUGIN_ERROR,
+                                    GS_PLUGIN_ERROR_DOWNLOAD_FAILED,
+                                    "failed to copy %s: %s",
+                                    uri, error_local->message);
+                       return NULL;
+               }
+               return g_bytes_new (contents, length);
+       }
+
+       /* remote */
        g_debug ("downloading %s from plugin %s", uri, priv->name);
        msg = soup_message_new (SOUP_METHOD_GET, uri);
        if (app != NULL) {
@@ -1138,6 +1156,31 @@ gs_plugin_download_file (GsPlugin *plugin,
        g_return_val_if_fail (filename != NULL, FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
+       /* local */
+       if (g_str_has_prefix (uri, "file://")) {
+               gsize length = 0;
+               g_autofree gchar *contents = NULL;
+               g_debug ("copying %s from plugin %s", uri, priv->name);
+               if (!g_file_get_contents (uri + 7, &contents, &length, &error_local)) {
+                       g_set_error (error,
+                                    GS_PLUGIN_ERROR,
+                                    GS_PLUGIN_ERROR_DOWNLOAD_FAILED,
+                                    "failed to copy %s: %s",
+                                    uri, error_local->message);
+                       return FALSE;
+               }
+               if (!g_file_set_contents (filename, contents, length, &error_local)) {
+                       g_set_error (error,
+                                    GS_PLUGIN_ERROR,
+                                    GS_PLUGIN_ERROR_WRITE_FAILED,
+                                    "Failed to save file: %s",
+                                    error_local->message);
+                       return FALSE;
+               }
+               return TRUE;
+       }
+
+       /* remote */
        g_debug ("downloading %s to %s from plugin %s", uri, filename, priv->name);
        msg = soup_message_new (SOUP_METHOD_GET, uri);
        if (app != NULL) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]