Tighten pg_restore's recognition of its -F (format) option values.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 25 Jan 2025 16:24:16 +0000 (11:24 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 25 Jan 2025 16:24:16 +0000 (11:24 -0500)
Instead of checking just the first letter, match the whole string
using pg_strcasecmp.  Per the documentation, we allow either just
the first letter (e.g. "c") or the whole name ("custom"); but we
will no longer accept random variations such as "chump".  This
matches pg_dump's longstanding parsing code for the same option.

Also for consistency with pg_dump, recognize "p"/"plain".  We don't
support it, but we can give a more helpful error message than
"unrecognized archive format".

Author: Srinath Reddy <srinath2133@gmail.com>
Discussion: https://postgr.es/m/CAFC+b6pfK-BGcWW1kQmtxVrCh-JGjB2X02rLPQs_ZFaDGjZDsQ@mail.gmail.com

src/bin/pg_dump/pg_restore.c

index 88ae39d938ae8f8909d6b6c675ae7b11af720d6a..c602272d7dbbe0547688771aa8852d5bb76001d3 100644 (file)
@@ -383,27 +383,25 @@ main(int argc, char **argv)
 
    if (opts->formatName)
    {
-       switch (opts->formatName[0])
+       if (pg_strcasecmp(opts->formatName, "c") == 0 ||
+           pg_strcasecmp(opts->formatName, "custom") == 0)
+           opts->format = archCustom;
+       else if (pg_strcasecmp(opts->formatName, "d") == 0 ||
+                pg_strcasecmp(opts->formatName, "directory") == 0)
+           opts->format = archDirectory;
+       else if (pg_strcasecmp(opts->formatName, "t") == 0 ||
+                pg_strcasecmp(opts->formatName, "tar") == 0)
+           opts->format = archTar;
+       else if (pg_strcasecmp(opts->formatName, "p") == 0 ||
+                pg_strcasecmp(opts->formatName, "plain") == 0)
        {
-           case 'c':
-           case 'C':
-               opts->format = archCustom;
-               break;
-
-           case 'd':
-           case 'D':
-               opts->format = archDirectory;
-               break;
-
-           case 't':
-           case 'T':
-               opts->format = archTar;
-               break;
-
-           default:
-               pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
-                        opts->formatName);
+           /* recognize this for consistency with pg_dump */
+           pg_fatal("archive format \"%s\" is not supported; please use psql",
+                    opts->formatName);
        }
+       else
+           pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
+                    opts->formatName);
    }
 
    AH = OpenArchive(inputFileSpec, opts->format);