mirror of
https://github.com/flatpak/flatpak.git
synced 2026-05-18 21:55:22 -04:00
builder: Add progress reporing while downloading
This commit is contained in:
@@ -1600,6 +1600,32 @@ builder_maybe_host_spawnv (GFile *dir,
|
||||
return flatpak_spawnv (dir, output, error, argv);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int stage;
|
||||
gboolean printed_something;
|
||||
} DownloadPromptData;
|
||||
|
||||
static void
|
||||
download_progress (guint64 downloaded_bytes,
|
||||
gpointer user_data)
|
||||
{
|
||||
DownloadPromptData *progress_data = user_data;
|
||||
char chrs[] = { '|', '/', '-', '\\' };
|
||||
|
||||
if (progress_data->printed_something)
|
||||
g_print ("\b");
|
||||
progress_data->printed_something = TRUE;
|
||||
g_print ("%c", chrs[progress_data->stage]);
|
||||
progress_data->stage = (progress_data->stage + 1) % G_N_ELEMENTS(chrs);
|
||||
}
|
||||
|
||||
static void
|
||||
download_progress_cleanup (DownloadPromptData *progress_data)
|
||||
{
|
||||
if (progress_data->printed_something)
|
||||
g_print ("\b");
|
||||
}
|
||||
|
||||
gboolean
|
||||
builder_download_uri (const char *url,
|
||||
GFile *dest,
|
||||
@@ -1613,7 +1639,7 @@ builder_download_uri (const char *url,
|
||||
g_autoptr(GFile) tmp = NULL;
|
||||
g_autoptr(GFile) dir = NULL;
|
||||
g_autoptr(GChecksum) checksum = g_checksum_new (G_CHECKSUM_SHA256);
|
||||
|
||||
DownloadPromptData progress_data = {0};
|
||||
g_autofree char *basename = g_file_get_basename (dest);
|
||||
g_autofree char *template = g_strconcat (".", basename, "XXXXXX", NULL);
|
||||
|
||||
@@ -1637,7 +1663,7 @@ builder_download_uri (const char *url,
|
||||
if (input == NULL)
|
||||
return FALSE;
|
||||
|
||||
if (!flatpak_splice_update_checksum (G_OUTPUT_STREAM (out), input, checksum, NULL, error))
|
||||
if (!flatpak_splice_update_checksum (G_OUTPUT_STREAM (out), input, checksum, download_progress, &progress_data, NULL, error))
|
||||
{
|
||||
unlink (flatpak_file_get_path_cached (tmp));
|
||||
return FALSE;
|
||||
@@ -1650,6 +1676,8 @@ builder_download_uri (const char *url,
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
download_progress_cleanup (&progress_data);
|
||||
|
||||
if (sha256 != NULL && strcmp (g_checksum_get_string (checksum), sha256) != 0)
|
||||
{
|
||||
unlink (flatpak_file_get_path_cached (tmp));
|
||||
|
||||
@@ -123,12 +123,17 @@ gboolean
|
||||
flatpak_splice_update_checksum (GOutputStream *out,
|
||||
GInputStream *in,
|
||||
GChecksum *checksum,
|
||||
FlatpakLoadUriProgress progress,
|
||||
gpointer progress_data,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
gsize bytes_read, bytes_written;
|
||||
char buf[32*1024];
|
||||
guint64 downloaded_bytes = 0;
|
||||
gint64 progress_start;
|
||||
|
||||
progress_start = g_get_monotonic_time ();
|
||||
do
|
||||
{
|
||||
if (!g_input_stream_read_all (in, buf, sizeof buf, &bytes_read, cancellable, error))
|
||||
@@ -137,9 +142,21 @@ flatpak_splice_update_checksum (GOutputStream *out,
|
||||
if (!flatpak_write_update_checksum (out, buf, bytes_read, &bytes_written, checksum,
|
||||
cancellable, error))
|
||||
return FALSE;
|
||||
|
||||
downloaded_bytes += bytes_read;
|
||||
|
||||
if (progress &&
|
||||
g_get_monotonic_time () - progress_start > 5 * 1000000)
|
||||
{
|
||||
progress (downloaded_bytes, progress_data);
|
||||
progress_start = g_get_monotonic_time ();
|
||||
}
|
||||
}
|
||||
while (bytes_read > 0);
|
||||
|
||||
if (progress)
|
||||
progress (downloaded_bytes, progress_data);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@ typedef enum {
|
||||
FLATPAK_HOST_COMMAND_FLAGS_CLEAR_ENV = 1 << 0,
|
||||
} FlatpakHostCommandFlags;
|
||||
|
||||
typedef void (*FlatpakLoadUriProgress) (guint64 downloaded_bytes,
|
||||
gpointer user_data);
|
||||
|
||||
/* https://bugzilla.gnome.org/show_bug.cgi?id=766370 */
|
||||
#if !GLIB_CHECK_VERSION(2, 49, 3)
|
||||
@@ -91,9 +93,12 @@ gboolean flatpak_write_update_checksum (GOutputStream *out,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
|
||||
gboolean flatpak_splice_update_checksum (GOutputStream *out,
|
||||
GInputStream *in,
|
||||
GChecksum *checksum,
|
||||
FlatpakLoadUriProgress progress,
|
||||
gpointer progress_data,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
@@ -610,9 +615,6 @@ gboolean flatpak_allocate_tmpdir (int tmpdir_dfd,
|
||||
gboolean flatpak_yes_no_prompt (const char *prompt, ...) G_GNUC_PRINTF(1, 2);
|
||||
long flatpak_number_prompt (int min, int max, const char *prompt, ...) G_GNUC_PRINTF(3, 4);
|
||||
|
||||
typedef void (*FlatpakLoadUriProgress) (guint64 downloaded_bytes,
|
||||
gpointer user_data);
|
||||
|
||||
SoupSession * flatpak_create_soup_session (const char *user_agent);
|
||||
GBytes * flatpak_load_http_uri (SoupSession *soup_session,
|
||||
const char *uri,
|
||||
|
||||
Reference in New Issue
Block a user