metalink: Return requested file as a GBytes

This may not be the best idea for general usage, but the only use case
for metalinks currently is fetching a summary file and those are pretty
small.  Far more convenient to return the file content in a GBytes.
This commit is contained in:
Matthew Barnes 2015-06-10 15:40:42 -04:00 committed by Giuseppe Scrivano
parent ebef43eabe
commit 87851c7bbe
3 changed files with 44 additions and 60 deletions

View File

@ -72,7 +72,7 @@ typedef struct
char *verification_sha256; char *verification_sha256;
char *verification_sha512; char *verification_sha512;
char *result; GBytes *result;
char *last_metalink_error; char *last_metalink_error;
guint current_url_index; guint current_url_index;
@ -431,48 +431,51 @@ on_fetched_url (GObject *src,
gpointer user_data) gpointer user_data)
{ {
GTask *task = user_data; GTask *task = user_data;
GCancellable *cancellable;
OstreeMetalinkRequest *self = g_task_get_task_data (task); OstreeMetalinkRequest *self = g_task_get_task_data (task);
GError *local_error = NULL; GError *local_error = NULL;
struct stat stbuf;
int parent_dfd = _ostree_fetcher_get_dfd (self->metalink->fetcher); int parent_dfd = _ostree_fetcher_get_dfd (self->metalink->fetcher);
g_autoptr(GInputStream) instream = NULL; g_autoptr(GInputStream) instream = NULL;
g_autofree char *result = NULL; g_autoptr(GOutputStream) outstream = NULL;
GChecksum *checksum = NULL; g_autoptr(GBytes) bytes = NULL;
g_autofree char *path = NULL;
gssize n_bytes;
result = _ostree_fetcher_request_uri_with_partial_finish ((OstreeFetcher*)src, res, &local_error); path = _ostree_fetcher_request_uri_with_partial_finish ((OstreeFetcher*)src, res, &local_error);
if (!result) if (!path)
goto out; goto out;
if (!ot_openat_read_stream (parent_dfd, result, FALSE, cancellable = g_task_get_cancellable (task);
&instream, NULL, &local_error))
goto out;
if (fstat (g_file_descriptor_based_get_fd ((GFileDescriptorBased*)instream), &stbuf) != 0)
{
gs_set_error_from_errno (&local_error, errno);
goto out;
}
if (stbuf.st_size != self->size) if (!ot_openat_read_stream (parent_dfd, path, FALSE, &instream,
cancellable, &local_error))
goto out;
outstream = g_memory_output_stream_new_resizable ();
n_bytes = g_output_stream_splice (outstream, instream,
G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
cancellable, &local_error);
if (n_bytes < 0)
goto out;
bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (outstream));
if (n_bytes != self->size)
{ {
g_set_error (&local_error, G_IO_ERROR, G_IO_ERROR_FAILED, g_set_error (&local_error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Expected size is %" G_GUINT64_FORMAT " bytes but content is %" G_GUINT64_FORMAT " bytes", "Expected size is %" G_GUINT64_FORMAT " bytes but content is %" G_GSSIZE_FORMAT " bytes",
self->size, stbuf.st_size); self->size, n_bytes);
goto out; goto out;
} }
if (self->verification_sha512) if (self->verification_sha512)
{ {
const char *actual; g_autofree char *actual = NULL;
checksum = g_checksum_new (G_CHECKSUM_SHA512); actual = g_compute_checksum_for_bytes (G_CHECKSUM_SHA512, bytes);
if (!ot_gio_splice_update_checksum (NULL, instream, checksum,
g_task_get_cancellable (task),
&local_error))
goto out;
actual = g_checksum_get_string (checksum);
if (strcmp (self->verification_sha512, actual) != 0) if (strcmp (self->verification_sha512, actual) != 0)
{ {
@ -484,16 +487,9 @@ on_fetched_url (GObject *src,
} }
else if (self->verification_sha256) else if (self->verification_sha256)
{ {
const char *actual; g_autofree char *actual = NULL;
checksum = g_checksum_new (G_CHECKSUM_SHA256); actual = g_compute_checksum_for_bytes (G_CHECKSUM_SHA256, bytes);
if (!ot_gio_splice_update_checksum (NULL, instream, checksum,
g_task_get_cancellable (task),
&local_error))
goto out;
actual = g_checksum_get_string (checksum);
if (strcmp (self->verification_sha256, actual) != 0) if (strcmp (self->verification_sha256, actual) != 0)
{ {
@ -505,8 +501,6 @@ on_fetched_url (GObject *src,
} }
out: out:
if (checksum)
g_checksum_free (checksum);
if (local_error) if (local_error)
{ {
g_free (self->last_metalink_error); g_free (self->last_metalink_error);
@ -519,8 +513,7 @@ on_fetched_url (GObject *src,
} }
else else
{ {
self->result = result; self->result = g_bytes_ref (bytes);
result = NULL; /* Transfer ownership */
g_task_return_boolean (self->task, TRUE); g_task_return_boolean (self->task, TRUE);
} }
} }
@ -609,7 +602,7 @@ ostree_metalink_request_unref (gpointer data)
{ {
OstreeMetalinkRequest *request = data; OstreeMetalinkRequest *request = data;
g_object_unref (request->metalink); g_object_unref (request->metalink);
g_free (request->result); g_clear_pointer (&request->result, g_bytes_unref);
g_free (request->last_metalink_error); g_free (request->last_metalink_error);
g_ptr_array_unref (request->urls); g_ptr_array_unref (request->urls);
g_free (request); g_free (request);
@ -626,7 +619,7 @@ static const GMarkupParser metalink_parser = {
typedef struct typedef struct
{ {
SoupURI **out_target_uri; SoupURI **out_target_uri;
char **out_data; GBytes **out_data;
gboolean success; gboolean success;
GError **error; GError **error;
GMainLoop *loop; GMainLoop *loop;
@ -636,7 +629,7 @@ static gboolean
ostree_metalink_request_finish (OstreeMetalink *self, ostree_metalink_request_finish (OstreeMetalink *self,
GAsyncResult *result, GAsyncResult *result,
SoupURI **out_target_uri, SoupURI **out_target_uri,
char **out_data, GBytes **out_data,
GError **error) GError **error)
{ {
OstreeMetalinkRequest *request; OstreeMetalinkRequest *request;
@ -651,7 +644,7 @@ ostree_metalink_request_finish (OstreeMetalink *self,
if (out_target_uri != NULL) if (out_target_uri != NULL)
*out_target_uri = request->urls->pdata[request->current_url_index]; *out_target_uri = request->urls->pdata[request->current_url_index];
if (out_data != NULL) if (out_data != NULL)
*out_data = g_strdup (request->result); *out_data = g_bytes_ref (request->result);
return TRUE; return TRUE;
} }
else else
@ -695,7 +688,7 @@ gboolean
_ostree_metalink_request_sync (OstreeMetalink *self, _ostree_metalink_request_sync (OstreeMetalink *self,
GMainLoop *loop, GMainLoop *loop,
SoupURI **out_target_uri, SoupURI **out_target_uri,
char **out_data, GBytes **out_data,
SoupURI **fetching_sync_uri, SoupURI **fetching_sync_uri,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)

View File

@ -53,7 +53,7 @@ SoupURI *_ostree_metalink_get_uri (OstreeMetalink *self);
gboolean _ostree_metalink_request_sync (OstreeMetalink *self, gboolean _ostree_metalink_request_sync (OstreeMetalink *self,
GMainLoop *loop, GMainLoop *loop,
SoupURI **out_target_uri, SoupURI **out_target_uri,
char **out_data, GBytes **out_data,
SoupURI **fetching_sync_uri, SoupURI **fetching_sync_uri,
GCancellable *cancellable, GCancellable *cancellable,
GError **error); GError **error);

View File

@ -1709,10 +1709,9 @@ ostree_repo_pull_with_options (OstreeRepo *self,
} }
else else
{ {
g_autofree char *metalink_data = NULL; g_autoptr(GBytes) summary_bytes = NULL;
SoupURI *metalink_uri = soup_uri_new (metalink_url_str); SoupURI *metalink_uri = soup_uri_new (metalink_url_str);
SoupURI *target_uri = NULL; SoupURI *target_uri = NULL;
gs_fd_close int fd = -1;
if (!metalink_uri) if (!metalink_uri)
{ {
@ -1728,7 +1727,7 @@ ostree_repo_pull_with_options (OstreeRepo *self,
if (! _ostree_metalink_request_sync (metalink, if (! _ostree_metalink_request_sync (metalink,
pull_data->loop, pull_data->loop,
&target_uri, &target_uri,
&metalink_data, &summary_bytes,
&pull_data->fetching_sync_uri, &pull_data->fetching_sync_uri,
cancellable, cancellable,
error)) error))
@ -1740,16 +1739,8 @@ ostree_repo_pull_with_options (OstreeRepo *self,
soup_uri_set_path (pull_data->base_uri, repo_base); soup_uri_set_path (pull_data->base_uri, repo_base);
} }
fd = openat (pull_data->tmpdir_dfd, metalink_data, O_RDONLY | O_CLOEXEC); pull_data->summary = g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT,
if (fd == -1) summary_bytes, FALSE);
{
gs_set_error_from_errno (error, errno);
goto out;
}
if (!ot_util_variant_map_fd (fd, 0, OSTREE_SUMMARY_GVARIANT_FORMAT, FALSE,
&pull_data->summary, error))
goto out;
} }
if (!_ostree_repo_get_remote_list_option (self, if (!_ostree_repo_get_remote_list_option (self,