]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
import: Draw progress bars
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 25 Oct 2024 15:03:37 +0000 (17:03 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 29 Oct 2024 20:11:26 +0000 (21:11 +0100)
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.

src/import/export-raw.c
src/import/export-tar.c
src/import/import-raw.c
src/import/import-tar.c

index f4253962615c0327bee5571f67bbef335552e4b0..337d74706a5b65b4af8a40e68c24f235f90d6c08 100644 (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);
         }
index 9e92badfef37ac1374fda55782009332c1bc9034..42f3adee96348458cde7ec67f1d15513f39f9c99 100644 (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
index 78775b96d6798f154b2c65680a141c3d0e8866c5..190a08a005ac49b7fbe9cb9c65c0340f3a838837 100644 (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:
index 976c9182461f4caf5f66552127baac7f454acc06..6e6c720782a974fdab66ec29f540caf454044afa 100644 (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