From 3b64171edb2a18e8d14b80172f6648e246a08709 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon, 20 Oct 2003 21:05:12 +0000
Subject: [PATCH] Complain if pg_restore is given both -d and -f options; this
 suggests the user is confused about whether -f is input or output file.

---
 src/bin/pg_dump/pg_backup_archiver.c | 35 ++++++++++++----------------
 src/bin/pg_dump/pg_restore.c         | 30 ++++++++++++++++--------
 2 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 7096df3b956..cd86b62d98f 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *		$Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.78 2003/10/03 20:10:59 tgl Exp $
+ *		$Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.79 2003/10/20 21:05:11 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -381,7 +381,7 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
 	/*
 	 * Clean up & we're done.
 	 */
-	if (ropt->filename)
+	if (ropt->filename || ropt->compression)
 		ResetOutput(AH, sav);
 
 	if (ropt->useDB)
@@ -596,7 +596,7 @@ PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
 	char	   *fmtName;
 
 	if (ropt->filename)
-		sav = SetOutput(AH, ropt->filename, ropt->compression);
+		sav = SetOutput(AH, ropt->filename, 0 /* no compression */);
 
 	ahprintf(AH, ";\n; Archive created at %s", ctime(&AH->createDate));
 	ahprintf(AH, ";     dbname: %s\n;     TOC Entries: %d\n;     Compression: %d\n",
@@ -1039,23 +1039,19 @@ OutputContext
 SetOutput(ArchiveHandle *AH, char *filename, int compression)
 {
 	OutputContext sav;
-
-#ifdef HAVE_LIBZ
-	char		fmode[10];
-#endif
-	int			fn = 0;
+	int			fn;
 
 	/* Replace the AH output file handle */
 	sav.OF = AH->OF;
 	sav.gzOut = AH->gzOut;
 
 	if (filename)
-		fn = 0;
+		fn = -1;
 	else if (AH->FH)
 		fn = fileno(AH->FH);
 	else if (AH->fSpec)
 	{
-		fn = 0;
+		fn = -1;
 		filename = AH->fSpec;
 	}
 	else
@@ -1065,27 +1061,25 @@ SetOutput(ArchiveHandle *AH, char *filename, int compression)
 #ifdef HAVE_LIBZ
 	if (compression != 0)
 	{
+		char		fmode[10];
+
+		/* Don't use PG_BINARY_x since this is zlib */
 		sprintf(fmode, "wb%d", compression);
-		if (fn)
-		{
-			AH->OF = gzdopen(dup(fn), fmode);	/* Don't use PG_BINARY_x
-												 * since this is zlib */
-		}
+		if (fn >= 0)
+			AH->OF = gzdopen(dup(fn), fmode);
 		else
 			AH->OF = gzopen(filename, fmode);
 		AH->gzOut = 1;
 	}
 	else
-	{							/* Use fopen */
 #endif
-		if (fn)
+	{							/* Use fopen */
+		if (fn >= 0)
 			AH->OF = fdopen(dup(fn), PG_BINARY_W);
 		else
 			AH->OF = fopen(filename, PG_BINARY_W);
 		AH->gzOut = 0;
-#ifdef HAVE_LIBZ
 	}
-#endif
 
 	if (!AH->OF)
 		die_horribly(AH, modulename, "could not open output file: %s\n", strerror(errno));
@@ -1104,7 +1098,8 @@ ResetOutput(ArchiveHandle *AH, OutputContext sav)
 		res = fclose(AH->OF);
 
 	if (res != 0)
-		die_horribly(AH, modulename, "could not close output file: %s\n", strerror(errno));
+		die_horribly(AH, modulename, "could not close output file: %s\n",
+					 strerror(errno));
 
 	AH->gzOut = sav.gzOut;
 	AH->OF = sav.OF;
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 2414de8f5c9..33aafcd6c45 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -34,7 +34,7 @@
  *
  *
  * IDENTIFICATION
- *		$Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.52 2003/09/23 22:48:53 tgl Exp $
+ *		$Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.53 2003/10/20 21:05:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -78,7 +78,7 @@ main(int argc, char **argv)
 	RestoreOptions *opts;
 	int			c;
 	Archive    *AH;
-	char	   *fileSpec = NULL;
+	char	   *inputFileSpec;
 	extern int	optind;
 	extern char *optarg;
 	static int	use_setsessauth = 0;
@@ -163,11 +163,7 @@ main(int argc, char **argv)
 				opts->create = 1;
 				break;
 			case 'd':
-				if (strlen(optarg) != 0)
-				{
-					opts->dbname = strdup(optarg);
-					opts->useDB = 1;
-				}
+				opts->dbname = strdup(optarg);
 				break;
 			case 'f':			/* output file name */
 				opts->filename = strdup(optarg);
@@ -286,9 +282,23 @@ main(int argc, char **argv)
 	}
 
 	if (optind < argc)
-		fileSpec = argv[optind];
+		inputFileSpec = argv[optind];
 	else
-		fileSpec = NULL;
+		inputFileSpec = NULL;
+
+	/* Should get at most one of -d and -f, else user is confused */
+	if (opts->dbname)
+	{
+		if (opts->filename)
+		{
+			fprintf(stderr, _("%s: cannot specify both -d and -f output\n"),
+					progname);
+			fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
+					progname);
+			exit(1);
+		}
+		opts->useDB = 1;
+	}
 
 	opts->disable_triggers = disable_triggers;
 
@@ -320,7 +330,7 @@ main(int argc, char **argv)
 		}
 	}
 
-	AH = OpenArchive(fileSpec, opts->format);
+	AH = OpenArchive(inputFileSpec, opts->format);
 
 	/* Let the archiver know how noisy to be */
 	AH->verbose = opts->verbose;
-- 
GitLab