builder: Break out download_uri helper function in SourceFile

tingping/wmclass
Alexander Larsson 2015-12-09 11:37:09 +01:00
parent 3fb4226c81
commit e39192b3d6
1 changed files with 48 additions and 36 deletions

View File

@ -219,36 +219,15 @@ get_download_location (BuilderSourceArchive *self,
return g_steal_pointer (&file);
}
static gboolean
builder_source_archive_download (BuilderSource *source,
BuilderContext *context,
GError **error)
static GBytes *
download_uri (const char *orig_url,
const char *base_name,
BuilderContext *context,
GError **error)
{
BuilderSourceArchive *self = BUILDER_SOURCE_ARCHIVE (source);
g_autoptr (GFile) file = NULL;
g_autoptr (GFile) dir = NULL;
g_autoptr(SoupURI) uri = NULL;
SoupSession *session;
g_autofree char *url = NULL;
g_autofree char *dir_path = NULL;
g_autofree char *sha256 = NULL;
g_autofree char *base_name = NULL;
g_autoptr(SoupMessage) msg = NULL;
file = get_download_location (self, context, error);
if (file == NULL)
return FALSE;
if (g_file_query_exists (file, NULL))
return TRUE;
base_name = g_file_get_basename (file);
uri = get_uri (self, error);
if (uri == NULL)
return FALSE;
url = g_strdup (self->url);
g_autofree char *url = g_strdup (orig_url);
session = builder_context_get_soup_session (context);
@ -256,7 +235,7 @@ builder_source_archive_download (BuilderSource *source,
{
g_clear_object (&msg);
msg = soup_message_new ("GET", url);
g_debug ("GET %s", self->url);
g_debug ("GET %s", url);
g_print ("Downloading %s...", base_name);
soup_session_send_message (session, msg);
g_print ("done\n");
@ -270,7 +249,7 @@ builder_source_archive_download (BuilderSource *source,
{
g_autoptr(SoupURI) new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), header);
g_free (url);
url = soup_uri_to_string (uri, FALSE);
url = soup_uri_to_string (new_uri, FALSE);
g_debug (" -> %s", header);
continue;
}
@ -279,15 +258,50 @@ builder_source_archive_download (BuilderSource *source,
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to download %s (error %d): %s", base_name, msg->status_code, msg->reason_phrase);
return FALSE;
return NULL;
}
break; /* No redirection */
}
return g_bytes_new_with_free_func (msg->response_body->data,
msg->response_body->length,
g_object_unref,
g_object_ref (msg));
}
static gboolean
builder_source_archive_download (BuilderSource *source,
BuilderContext *context,
GError **error)
{
BuilderSourceArchive *self = BUILDER_SOURCE_ARCHIVE (source);
g_autoptr (GFile) file = NULL;
g_autoptr (GFile) dir = NULL;
g_autofree char *dir_path = NULL;
g_autofree char *sha256 = NULL;
g_autofree char *base_name = NULL;
g_autoptr(GBytes) content = NULL;
file = get_download_location (self, context, error);
if (file == NULL)
return FALSE;
if (g_file_query_exists (file, NULL))
return TRUE;
base_name = g_file_get_basename (file);
content = download_uri (self->url,
base_name,
context,
error);
if (content == NULL)
return FALSE;
sha256 = g_compute_checksum_for_string (G_CHECKSUM_SHA256,
msg->response_body->data,
msg->response_body->length);
g_bytes_get_data (content, NULL),
g_bytes_get_size (content));
if (strcmp (sha256, self->sha256) != 0)
{
@ -301,8 +315,8 @@ builder_source_archive_download (BuilderSource *source,
g_mkdir_with_parents (dir_path, 0755);
if (!g_file_replace_contents (file,
msg->response_body->data,
msg->response_body->length,
g_bytes_get_data (content, NULL),
g_bytes_get_size (content),
NULL, FALSE, G_FILE_CREATE_NONE, NULL,
NULL, error))
return FALSE;
@ -406,8 +420,6 @@ strip_components_into (GFile *dest,
{
g_autoptr(GFile) child = NULL;
g_autoptr(GFile) dest_child = NULL;
g_autoptr(GFileEnumerator) dir_enum2 = NULL;
g_autoptr(GFileInfo) child_info2 = NULL;
child = g_file_get_child (src, g_file_info_get_name (child_info));