From 4944defad4ae2cbcf056207490d7c597bb92cd7c Mon Sep 17 00:00:00 2001 From: "Darrick J. Wong" Date: Thu, 20 Jul 2017 10:51:37 -0500 Subject: [PATCH] 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 --- db/metadump.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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 */ -- 2.47.2