import: Draw progress bars

Currently every progress update results in a new progress message
which is extremely verbose. Instead, let's use the progress bar infra
to draw a proper progress bar similar to what we do in systemd-repart
now.
This commit is contained in:
Daan De Meyer
2024-10-29 21:11:26 +01:00
committed by Lennart Poettering
parent 1322af50e5
commit 3dd0389ba0
4 changed files with 69 additions and 4 deletions
+18 -1
View File
@@ -9,9 +9,11 @@
#include "copy.h"
#include "export-raw.h"
#include "fd-util.h"
#include "format-util.h"
#include "fs-util.h"
#include "import-common.h"
#include "missing_fcntl.h"
#include "pretty-print.h"
#include "ratelimit.h"
#include "stat-util.h"
#include "string-util.h"
@@ -121,7 +123,19 @@ static void raw_export_report_progress(RawExport *e) {
return;
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
log_info("Exported %u%%.", percent);
if (isatty_safe(STDERR_FILENO)) {
_cleanup_free_ char *s = NULL;
if (asprintf(&s, "%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(e->written_uncompressed),
FORMAT_BYTES(e->st.st_size)) < 0)
return;
draw_progress_bar(s, percent);
} else
log_info("Exported %u%%.", percent);
e->last_percent = percent;
}
@@ -215,6 +229,9 @@ static int raw_export_process(RawExport *e) {
finish:
if (r >= 0) {
if (isatty_safe(STDERR_FILENO))
clear_progress_bar(/* prefix= */ NULL);
(void) copy_times(e->input_fd, e->output_fd, COPY_CRTIME);
(void) copy_xattr(e->input_fd, NULL, e->output_fd, NULL, 0);
}
+17 -1
View File
@@ -7,6 +7,7 @@
#include "export-tar.h"
#include "fd-util.h"
#include "import-common.h"
#include "pretty-print.h"
#include "process-util.h"
#include "ratelimit.h"
#include "string-util.h"
@@ -132,7 +133,19 @@ static void tar_export_report_progress(TarExport *e) {
return;
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
log_info("Exported %u%%.", percent);
if (isatty_safe(STDERR_FILENO)) {
_cleanup_free_ char *s = NULL;
if (asprintf(&s, "%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(e->written_uncompressed),
FORMAT_BYTES(e->quota_referenced)) < 0)
return;
draw_progress_bar(s, percent);
} else
log_info("Exported %u%%.", percent);
e->last_percent = percent;
}
@@ -229,6 +242,9 @@ static int tar_export_process(TarExport *e) {
return 0;
finish:
if (r >= 0 && isatty_safe(STDERR_FILENO))
clear_progress_bar(/* prefix= */ NULL);
if (e->on_finished)
e->on_finished(e, r, e->userdata);
else
+17 -1
View File
@@ -20,6 +20,7 @@
#include "machine-pool.h"
#include "mkdir-label.h"
#include "path-util.h"
#include "pretty-print.h"
#include "qcow2-util.h"
#include "ratelimit.h"
#include "rm-rf.h"
@@ -149,7 +150,19 @@ static void raw_import_report_progress(RawImport *i) {
return;
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
log_info("Imported %u%%.", percent);
if (isatty_safe(STDERR_FILENO)) {
_cleanup_free_ char *s = NULL;
if (asprintf(&s, "%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(i->written_compressed),
FORMAT_BYTES(i->input_stat.st_size)) < 0)
return;
draw_progress_bar(s, percent);
} else
log_info("Imported %u%%.", percent);
i->last_percent = percent;
}
@@ -459,6 +472,9 @@ static int raw_import_process(RawImport *i) {
return 0;
complete:
if (isatty_safe(STDERR_FILENO))
clear_progress_bar(/* prefix= */ NULL);
r = raw_import_finish(i);
finish:
+17 -1
View File
@@ -20,6 +20,7 @@
#include "machine-pool.h"
#include "mkdir-label.h"
#include "path-util.h"
#include "pretty-print.h"
#include "process-util.h"
#include "qcow2-util.h"
#include "ratelimit.h"
@@ -150,7 +151,19 @@ static void tar_import_report_progress(TarImport *i) {
return;
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
log_info("Imported %u%%.", percent);
if (isatty_safe(STDERR_FILENO)) {
_cleanup_free_ char *s = NULL;
if (asprintf(&s, "%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(i->written_compressed),
FORMAT_BYTES(i->input_stat.st_size)) < 0)
return;
draw_progress_bar(s, percent);
} else
log_info("Imported %u%%.", percent);
i->last_percent = percent;
}
@@ -322,6 +335,9 @@ static int tar_import_process(TarImport *i) {
return 0;
finish:
if (r >= 0 && isatty_safe(STDERR_FILENO))
clear_progress_bar(/* prefix= */ NULL);
if (i->on_finished)
i->on_finished(i, r, i->userdata);
else