dfu: Don't print out progress if no terminal 39/231139/1
authorDongwoo Lee <dwoo08.lee@samsung.com>
Fri, 17 Apr 2020 10:22:18 +0000 (19:22 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Fri, 17 Apr 2020 10:50:49 +0000 (19:50 +0900)
Since commit 76316d20e333 ("dfu: Improve progress display"), broken
progress bar can flood massively if the output is redirected to
non-terminal device such as a log file. To prevent this, prints out
the simple result instead of progress bar if terminal doesn't exist.

Change-Id: If5edb6408b7614951180fb4a73df53ad06572cc7
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
src/dfu.c

index 8a477de..c7d1039 100644 (file)
--- a/src/dfu.c
+++ b/src/dfu.c
@@ -329,6 +329,25 @@ static void print_progress(struct dfu_entry *e, uint64_t progress)
        fflush(stdout);
 }
 
+static void print_progress_notty(struct dfu_entry *e, uint64_t progress)
+{
+       struct dfu_context *ctx = e->ctx;
+       static uint64_t completed;
+       uint64_t total_progress;
+
+       total_progress = completed + progress;
+
+       fputc('#', stdout);
+
+       if (progress == e->file_size) {
+               completed += e->file_size;
+               fprintf(stdout, "...Done");
+       }
+
+       if (total_progress == ctx->total_size)
+               fprintf(stdout, "\nDownload Finished\n");
+}
+
 static void *dfu_thread_main(void *ptr)
 {
        struct dfu_entry *e = ptr;
@@ -336,8 +355,14 @@ static void *dfu_thread_main(void *ptr)
        struct dfu_frame *frame;
        int state = DFU_THREAD_STATE_IDLE;
        uint64_t progress = 0;
+       void (*do_progress)(struct dfu_entry *e, uint64_t progress);
        int ret;
 
+       if (isatty(fileno(stdout)))
+               do_progress = print_progress;
+       else
+               do_progress = print_progress_notty;
+
        pthread_cleanup_push(dfu_thread_cleanup, ptr);
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
@@ -372,7 +397,7 @@ static void *dfu_thread_main(void *ptr)
                ctx->ioq_len--;
                pthread_mutex_unlock(&ctx->mutex);
 
-               print_progress(e, progress);
+               do_progress(e, progress);
 
                free(frame->buf);
                free(frame);
@@ -468,7 +493,8 @@ void dfu_init_download(struct dfu_context *ctx, uint64_t size)
        fprintf(stdout, "Start Download (Total: %.2lfMB)",
                        (double)size / (1024.0f * 1024.0f));
 
-       print_bar_label("Total");
+       if (isatty(fileno(stdout)))
+               print_bar_label("Total");
 }
 
 struct dfu_entry *dfu_start(struct dfu_context *ctx,
@@ -496,7 +522,10 @@ struct dfu_entry *dfu_start(struct dfu_context *ctx,
                return NULL;
        }
 
-       print_bar_label(filename);
+       if (isatty(fileno(stdout)))
+               print_bar_label(filename);
+       else
+               fprintf(stdout, "\n%s", filename);
 
        e->ctx = ctx;
        pthread_create(&ctx->thread, NULL, dfu_thread_main, e);