Use g_subprocess_communicate() to fix IPFS downloads

Since switching to a custom GMainContext we broke reading the stream of
the GSubprocess.
This commit is contained in:
Richard Hughes 2021-11-08 21:16:39 +00:00
parent 09b946cb40
commit 4d63d4ad7c

View File

@ -4511,33 +4511,16 @@ fwupd_client_download_write_callback_cb(char *ptr, size_t size, size_t nmemb, vo
return realsize; return realsize;
} }
static GBytes *
fwupd_client_stream_read_bytes(GInputStream *stream, GError **error)
{
guint8 tmp[0x8000] = {0x0};
g_autoptr(GByteArray) buf = g_byte_array_new();
/* read from stream in 32kB chunks */
while (TRUE) {
gssize sz;
sz = g_input_stream_read(stream, tmp, sizeof(tmp), NULL, error);
if (sz == 0)
break;
if (sz < 0)
return NULL;
g_byte_array_append(buf, tmp, sz);
}
return g_byte_array_free_to_bytes(g_steal_pointer(&buf));
}
static GBytes * static GBytes *
fwupd_client_download_ipfs(FwupdClient *self, fwupd_client_download_ipfs(FwupdClient *self,
const gchar *url, const gchar *url,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
GInputStream *stream = NULL; GSubprocessFlags flags = G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_PIPE;
g_autofree gchar *path = NULL; g_autofree gchar *path = NULL;
g_autoptr(GBytes) bstdout = NULL;
g_autoptr(GBytes) bstderr = NULL;
g_autoptr(GSubprocess) subprocess = NULL; g_autoptr(GSubprocess) subprocess = NULL;
/* we get no detailed progress details */ /* we get no detailed progress details */
@ -4554,16 +4537,22 @@ fwupd_client_download_ipfs(FwupdClient *self,
} }
/* run sync */ /* run sync */
subprocess = subprocess = g_subprocess_new(flags, error, "ipfs", "cat", path, NULL);
g_subprocess_new(G_SUBPROCESS_FLAGS_STDOUT_PIPE, error, "ipfs", "cat", path, NULL);
if (subprocess == NULL) if (subprocess == NULL)
return NULL; return NULL;
if (!g_subprocess_wait_check(subprocess, cancellable, error)) if (!g_subprocess_communicate(subprocess, NULL, cancellable, &bstdout, &bstderr, error))
return NULL; return NULL;
fwupd_client_set_status(self, FWUPD_STATUS_IDLE);
/* get raw stdout */ if (g_subprocess_get_exit_status(subprocess) != 0) {
stream = g_subprocess_get_stdout_pipe(subprocess); const gchar *msg = g_bytes_get_data(bstderr, NULL);
return fwupd_client_stream_read_bytes(stream, error); g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"failed to download file: %s",
msg);
return NULL;
}
return g_steal_pointer(&bstdout);
} }
static GBytes * static GBytes *