Fix pg_restore to complain if any arguments remain after parsing the switches
authorTom Lane
Fri, 13 Aug 2010 14:38:12 +0000 (14:38 +0000)
committerTom Lane
Fri, 13 Aug 2010 14:38:12 +0000 (14:38 +0000)
and input file name, per bug #5617 from Leo Shklovskii.  Rearrange the
corresponding code in pg_dump and pg_dumpall so that all three programs
handle this in a consistent, straightforward fashion.

Back-patch to 9.0, but no further.  Although this is certainly a bug, it's
possible that people have scripts that will be broken by the added error
check, so it seems better not to change the behavior in stable branches.

src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dumpall.c
src/bin/pg_dump/pg_restore.c

index 228c0614809cbc50eab0beb702e46f912111b86c..7a06f9b20a808011e2d2e1892845b41e80611ef7 100644 (file)
@@ -25,7 +25,7 @@
  * http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.581.2.1 2010/07/14 21:21:16 tgl Exp $
+ *   $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.581.2.2 2010/08/13 14:38:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -479,19 +479,20 @@ main(int argc, char **argv)
        }
    }
 
-   if (optind < (argc - 1))
+   /* Get database name from command line */
+   if (optind < argc)
+       dbname = argv[optind++];
+
+   /* Complain if any arguments remain */
+   if (optind < argc)
    {
        fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
-               progname, argv[optind + 1]);
+               progname, argv[optind]);
        fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                progname);
        exit(1);
    }
 
-   /* Get database name from command line */
-   if (optind < argc)
-       dbname = argv[optind];
-
    /* --column-inserts implies --inserts */
    if (column_inserts)
        dump_inserts = 1;
index 275a22ab88d2a19726f81caa5930eea9a65d8b66..85032a94132dcb8084a680f78c7a1f6f899359f4 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.134 2010/02/26 02:01:17 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.134.4.1 2010/08/13 14:38:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -315,22 +315,7 @@ main(int argc, char *argv[])
        }
    }
 
-   /* Add long options to the pg_dump argument list */
-   if (binary_upgrade)
-       appendPQExpBuffer(pgdumpopts, " --binary-upgrade");
-   if (column_inserts)
-       appendPQExpBuffer(pgdumpopts, " --column-inserts");
-   if (disable_dollar_quoting)
-       appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
-   if (disable_triggers)
-       appendPQExpBuffer(pgdumpopts, " --disable-triggers");
-   if (inserts)
-       appendPQExpBuffer(pgdumpopts, " --inserts");
-   if (no_tablespaces)
-       appendPQExpBuffer(pgdumpopts, " --no-tablespaces");
-   if (use_setsessauth)
-       appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization");
-
+   /* Complain if any arguments remain */
    if (optind < argc)
    {
        fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
@@ -368,6 +353,22 @@ main(int argc, char *argv[])
        exit(1);
    }
 
+   /* Add long options to the pg_dump argument list */
+   if (binary_upgrade)
+       appendPQExpBuffer(pgdumpopts, " --binary-upgrade");
+   if (column_inserts)
+       appendPQExpBuffer(pgdumpopts, " --column-inserts");
+   if (disable_dollar_quoting)
+       appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
+   if (disable_triggers)
+       appendPQExpBuffer(pgdumpopts, " --disable-triggers");
+   if (inserts)
+       appendPQExpBuffer(pgdumpopts, " --inserts");
+   if (no_tablespaces)
+       appendPQExpBuffer(pgdumpopts, " --no-tablespaces");
+   if (use_setsessauth)
+       appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization");
+
    /*
     * If there was a database specified on the command line, use that,
     * otherwise try to connect to database "postgres", and failing that
index 4927471dce44393a26e216767813d60db3135625..dbdf7acd4449be66b894b2dd020e5224859f4c8f 100644 (file)
@@ -34,7 +34,7 @@
  *
  *
  * IDENTIFICATION
- *     $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.102 2010/05/15 21:41:16 tgl Exp $
+ *     $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.102.2.1 2010/08/13 14:38:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -295,11 +295,22 @@ main(int argc, char **argv)
        }
    }
 
+   /* Get file name from command line */
    if (optind < argc)
-       inputFileSpec = argv[optind];
+       inputFileSpec = argv[optind++];
    else
        inputFileSpec = NULL;
 
+   /* Complain if any arguments remain */
+   if (optind < argc)
+   {
+       fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
+               progname, argv[optind]);
+       fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
+               progname);
+       exit(1);
+   }
+
    /* Should get at most one of -d and -f, else user is confused */
    if (opts->dbname)
    {