]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_db: redirect printfs when metadumping to stdout
authorDarrick J. Wong <darrick.wong@oracle.com>
Thu, 20 Jul 2017 15:51:37 +0000 (10:51 -0500)
committerEric Sandeen <sandeen@redhat.com>
Thu, 20 Jul 2017 15:51:37 +0000 (10:51 -0500)
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 <dshaw@jabberwocky.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
db/metadump.c

index 4515f682673fe5cc266f66935c7383187898ad87..96641e05a53419932f3e46b5c39f53697e2918aa 100644 (file)
@@ -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 */