Skip to content

autoupdater: update download progress display less frequently #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions admin/autoupdater/src/autoupdater.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct recv_manifest_ctx {
struct recv_image_ctx {
int fd;
ecdsa_sha256_context_t hash_ctx;
ssize_t downloaded;
};


Expand Down Expand Up @@ -248,6 +249,32 @@ static void recv_manifest_cb(struct uclient *cl) {
}
}

/** Updates the "XX.X / XX.X MiB" progress display */
static void update_progress(struct recv_image_ctx *ctx, ssize_t downloaded, ssize_t length)
{
/* Numbers in 0.1 MiB units */
ssize_t downloaded_01_mib = ((downloaded / 1024) * 10) / 1024;
ssize_t length_01_mib = ((length / 1024) * 10) / 1024;

if (downloaded_01_mib == ctx->downloaded)
return;

ctx->downloaded = downloaded_01_mib;

/* Integer and 1 digit fractional part in 1 MiB units */
ssize_t downloaded_mib = downloaded_01_mib / 10;
int downloaded_mib_frac = downloaded_01_mib % 10;
ssize_t length_mib = length_01_mib / 10;
int length_mib_frac = length_01_mib % 10;

printf(
"\rDownloading image: % 3zi.%i / %zi.%i MiB",
downloaded_mib, downloaded_mib_frac,
length_mib, length_mib_frac
);
fflush(stdout);
}


/** Receives data from uclient and writes it to file */
static void recv_image_cb(struct uclient *cl) {
Expand All @@ -260,12 +287,7 @@ static void recv_image_cb(struct uclient *cl) {
if (len <= 0)
return;

printf(
"\rDownloading image: % 5zi / %zi KiB",
uclient_data(cl)->downloaded / 1024,
uclient_data(cl)->length / 1024
);
fflush(stdout);
update_progress(ctx, uclient_data(cl)->downloaded, uclient_data(cl)->length);

if (write(ctx->fd, buf, len) < len) {
fputs("autoupdater: error: downloading firmware image failed: ", stderr);
Expand Down Expand Up @@ -349,7 +371,7 @@ static bool autoupdate(const char *mirror, struct settings *s, int lock_fd) {
/* Begin download of the image */
run_dir(download_d_dir);

struct recv_image_ctx image_ctx = { };
struct recv_image_ctx image_ctx = { .downloaded = -1 };
image_ctx.fd = open(firmware_path, O_WRONLY|O_CREAT, 0600);
if (image_ctx.fd < 0) {
fprintf(stderr, "autoupdater: error: failed opening firmware file %s\n", firmware_path);
Expand Down