Skip to contentSkip to conversation

xfs_db: redirect printfs when metadumping to stdout

PATCH In review Darrick J. Wong 2 participants 2 messages Jul 16, 2017 – Jul 17, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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.

---
v2: Better comments.

---
 db/metadump.c |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
db/metadump.c +13 -2
index e046f60..b58acef 100644
@@ -2874,6 +2874,7 @@ metadump_f(
 	xfs_agnumber_t	agno;
 	int		c;
 	int		start_iocur_sp;
+	bool		stdout_metadump = false;
 	char		*p;
 
 	exitcode = 1;
@@ -2988,7 +2989,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) {
@@ -3020,9 +3029,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 */
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Darrick J. Wong

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.

---
v2: Better comments.
---

Quoted 49 lines
 db/metadump.c |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
db/metadump.c +13 -3
index e046f60..b58acef 100644
@@ -2874,6 +2874,7 @@ metadump_f(
 	xfs_agnumber_t	agno;
 	int		c;
 	int		start_iocur_sp;
+	bool		stdout_metadump = false;
 	char		*p;
 
 	exitcode = 1;
@@ -2988,7 +2989,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) {
@@ -3020,9 +3029,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 */
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
Carlos