Skip to content

Commit 413ccaa

Browse files
committed
pg_restore: Require "-f -" to mean stdout
The previous convention that stdout was selected by default when nothing is specified was just too error-prone. After a suggestion from Andrew Gierth. Author: Euler Taveira Reviewed-by: Yoshikazu Imai, José Arthur Benetasso Villanova Discussion: https://postgr.es/m/87sgwrmhdv.fsf@news-spur.riddles.org.uk
1 parent 9c703c1 commit 413ccaa

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

doc/src/sgml/ref/pg_restore.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@
176176
<listitem>
177177
<para>
178178
Specify output file for generated script, or for the listing
179-
when used with <option>-l</option>. Default is the standard
180-
output.
179+
when used with <option>-l</option>. Use <literal>-</literal>
180+
for <systemitem>stdout</systemitem>.
181181
</para>
182182
</listitem>
183183
</varlistentry>

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,12 @@ SetOutput(ArchiveHandle *AH, const char *filename, int compression)
15121512
int fn;
15131513

15141514
if (filename)
1515-
fn = -1;
1515+
{
1516+
if (strcmp(filename, "-") == 0)
1517+
fn = fileno(stdout);
1518+
else
1519+
fn = -1;
1520+
}
15161521
else if (AH->FH)
15171522
fn = fileno(AH->FH);
15181523
else if (AH->fSpec)

src/bin/pg_dump/pg_restore.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ main(int argc, char **argv)
306306
exit_nicely(1);
307307
}
308308

309+
/* Complain if neither -f nor -d was specified (except if dumping TOC) */
310+
if (!opts->dbname && !opts->filename && !opts->tocSummary)
311+
{
312+
pg_log_error("one of -d/--dbname and -f/--file must be specified");
313+
exit_nicely(1);
314+
}
315+
309316
/* Should get at most one of -d and -f, else user is confused */
310317
if (opts->dbname)
311318
{
@@ -461,7 +468,7 @@ usage(const char *progname)
461468

462469
printf(_("\nGeneral options:\n"));
463470
printf(_(" -d, --dbname=NAME connect to database name\n"));
464-
printf(_(" -f, --file=FILENAME output file name\n"));
471+
printf(_(" -f, --file=FILENAME output file name (- for stdout)\n"));
465472
printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
466473
printf(_(" -l, --list print summarized TOC of the archive\n"));
467474
printf(_(" -v, --verbose verbose mode\n"));

src/bin/pg_dump/t/001_basic.pl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Config;
55
use PostgresNode;
66
use TestLib;
7-
use Test::More tests => 72;
7+
use Test::More tests => 74;
88

99
my $tempdir = TestLib::tempdir;
1010
my $tempdir_short = TestLib::tempdir_short;
@@ -50,7 +50,13 @@
5050
);
5151

5252
command_fails_like(
53-
[ 'pg_restore', '-s', '-a' ],
53+
[ 'pg_restore' ],
54+
qr{\Qpg_restore: error: one of -d/--dbname and -f/--file must be specified\E},
55+
'pg_restore: error: one of -d/--dbname and -f/--file must be specified'
56+
);
57+
58+
command_fails_like(
59+
[ 'pg_restore', '-s', '-a', '-f -' ],
5460
qr/\Qpg_restore: error: options -s\/--schema-only and -a\/--data-only cannot be used together\E/,
5561
'pg_restore: options -s/--schema-only and -a/--data-only cannot be used together'
5662
);
@@ -66,7 +72,7 @@
6672
'pg_dump: options -c/--clean and -a/--data-only cannot be used together');
6773

6874
command_fails_like(
69-
[ 'pg_restore', '-c', '-a' ],
75+
[ 'pg_restore', '-c', '-a', '-f -' ],
7076
qr/\Qpg_restore: error: options -c\/--clean and -a\/--data-only cannot be used together\E/,
7177
'pg_restore: options -c/--clean and -a/--data-only cannot be used together'
7278
);
@@ -92,12 +98,12 @@
9298
'pg_dump: invalid output format');
9399

94100
command_fails_like(
95-
[ 'pg_restore', '-j', '-1' ],
101+
[ 'pg_restore', '-j', '-1', '-f -' ],
96102
qr/\Qpg_restore: error: invalid number of parallel jobs\E/,
97103
'pg_restore: invalid number of parallel jobs');
98104

99105
command_fails_like(
100-
[ 'pg_restore', '--single-transaction', '-j3' ],
106+
[ 'pg_restore', '--single-transaction', '-j3', '-f -' ],
101107
qr/\Qpg_restore: error: cannot specify both --single-transaction and multiple jobs\E/,
102108
'pg_restore: cannot specify both --single-transaction and multiple jobs');
103109

@@ -107,12 +113,12 @@
107113
'pg_dump: compression level must be in range 0..9');
108114

109115
command_fails_like(
110-
[ 'pg_restore', '--if-exists' ],
116+
[ 'pg_restore', '--if-exists', '-f -' ],
111117
qr/\Qpg_restore: error: option --if-exists requires option -c\/--clean\E/,
112118
'pg_restore: option --if-exists requires option -c/--clean');
113119

114120
command_fails_like(
115-
[ 'pg_restore', '-F', 'garbage' ],
121+
[ 'pg_restore', '-f -', '-F', 'garbage' ],
116122
qr/\Qpg_restore: error: unrecognized archive format "garbage";\E/,
117123
'pg_dump: unrecognized archive format');
118124

@@ -146,7 +152,7 @@
146152
'pg_dumpall: option --if-exists requires option -c/--clean');
147153

148154
command_fails_like(
149-
[ 'pg_restore', '-C', '-1' ],
155+
[ 'pg_restore', '-C', '-1', '-f -' ],
150156
qr/\Qpg_restore: error: options -C\/--create and -1\/--single-transaction cannot be used together\E/,
151157
'pg_restore: options -C\/--create and -1\/--single-transaction cannot be used together'
152158
);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy