erofs-utils: avoid noisy prints if stdout is not a tty
authorGao Xiang <hsiangkao@linux.alibaba.com>
Tue, 16 Jan 2024 04:26:42 +0000 (12:26 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Tue, 16 Jan 2024 15:13:12 +0000 (23:13 +0800)
As Daan reported, "mkfs.erofs is super verbose as \r doesn't go to
the beginning of the line".  Don't print messages like this if
stdout is not a tty.

Reported-by: Daan De Meyer <daan.j.demeyer@gmail.com>
Closes: https://lore.kernel.org/r/CAO8sHcmnq+foWo7AZYbkxJXHfSeZkd73Dq+1dQSZYBE6QxL8JQ@mail.gmail.com
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20240116042642.3124559-1-hsiangkao@linux.alibaba.com
lib/config.c

index 0cddc95d61b7cbc419e72d0a86abf8dda3574cfa..aa3dd1f074df5efb347e41d7fd86f9dfed48a7e6 100644 (file)
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <unistd.h>
 #include "erofs/print.h"
 #include "erofs/internal.h"
 #include "liberofs_private.h"
@@ -16,6 +17,7 @@
 
 struct erofs_configure cfg;
 struct erofs_sb_info sbi;
+bool erofs_stdout_tty;
 
 void erofs_init_configure(void)
 {
@@ -33,6 +35,8 @@ void erofs_init_configure(void)
        cfg.c_pclusterblks_max = 1;
        cfg.c_pclusterblks_def = 1;
        cfg.c_max_decompressed_extent_bytes = -1;
+
+       erofs_stdout_tty = isatty(STDOUT_FILENO);
 }
 
 void erofs_show_config(void)
@@ -107,15 +111,19 @@ char *erofs_trim_for_progressinfo(const char *str, int placeholder)
 {
        int col, len;
 
+       if (!erofs_stdout_tty) {
+               return strdup(str);
+       } else {
 #ifdef GWINSZ_IN_SYS_IOCTL
-       struct winsize winsize;
+               struct winsize winsize;
 
-       if(ioctl(1, TIOCGWINSZ, &winsize) >= 0 &&
-          winsize.ws_col > 0)
-               col = winsize.ws_col;
-       else
+               if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) >= 0 &&
+                  winsize.ws_col > 0)
+                       col = winsize.ws_col;
+               else
 #endif
-               col = 80;
+                       col = 80;
+       }
 
        if (col <= placeholder)
                return strdup("");
@@ -140,7 +148,7 @@ void erofs_msg(int dbglv, const char *fmt, ...)
        FILE *f = dbglv >= EROFS_ERR ? stderr : stdout;
 
        if (__erofs_is_progressmsg) {
-               fputc('\n', f);
+               fputc('\n', stdout);
                __erofs_is_progressmsg = false;
        }
        va_start(ap, fmt);
@@ -160,7 +168,12 @@ void erofs_update_progressinfo(const char *fmt, ...)
        vsprintf(msg, fmt, ap);
        va_end(ap);
 
-       printf("\r\033[K%s", msg);
-       __erofs_is_progressmsg = true;
-       fflush(stdout);
+       if (erofs_stdout_tty) {
+               printf("\r\033[K%s", msg);
+               __erofs_is_progressmsg = true;
+               fflush(stdout);
+               return;
+       }
+       fputs(msg, stdout);
+       fputc('\n', stdout);
 }