From: Darrick J. Wong Date: Thu, 20 Jul 2017 15:51:37 +0000 (-0500) Subject: xfs_db: redirect printfs when metadumping to stdout X-Git-Tag: v4.12.0~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4944defad4;p=thirdparty%2Fxfsprogs-dev.git xfs_db: redirect printfs when metadumping to stdout If we're metadumping to stdout, we don't want xfs_db's various dbprintf statements dumping to stdout because that'll corrupt the metadump. Therefore, let outf point to the existing stdout and redirect stdout to stderr for the duration of the dump operation. Reported-by: David Shaw Signed-off-by: Darrick J. Wong Reviewed-by: Carlos Maiolino Signed-off-by: Eric Sandeen --- diff --git a/db/metadump.c b/db/metadump.c index 4515f6826..96641e05a 100644 --- a/db/metadump.c +++ b/db/metadump.c @@ -2750,6 +2750,7 @@ metadump_f( xfs_agnumber_t agno; int c; int start_iocur_sp; + bool stdout_metadump = false; char *p; exitcode = 1; @@ -2864,7 +2865,15 @@ metadump_f( free(metablock); return 0; } + /* + * Redirect stdout to stderr for the duration of the + * metadump operation so that dbprintf and other messages + * are sent to the console instead of polluting the + * metadump stream. + */ outf = stdout; + stdout = stderr; + stdout_metadump = true; } else { outf = fopen(argv[optind], "wb"); if (outf == NULL) { @@ -2896,9 +2905,11 @@ metadump_f( exitcode = write_index() < 0; if (progress_since_warning) - fputc('\n', (outf == stdout) ? stderr : stdout); + fputc('\n', stdout_metadump ? stderr : stdout); - if (outf != stdout) + if (stdout_metadump) + stdout = outf; + else fclose(outf); /* cleanup iocur stack */