v9-0001-minor-refactor-pg_dumpall.no-cfbot

application/octet-stream

Filename: v9-0001-minor-refactor-pg_dumpall.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: Non-text mode for pg_dumpall
diff --git a/src/bin/pg_dump/meson.build b/src/bin/pg_dump/meson.build
index 603ba6cfbf..99ae774b32 100644
--- a/src/bin/pg_dump/meson.build
+++ b/src/bin/pg_dump/meson.build
@@ -16,6 +16,7 @@ pg_dump_common_sources = files(
   'pg_backup_null.c',
   'pg_backup_tar.c',
   'pg_backup_utils.c',
+  'common_dumpall_restore.c',
 )
 
 pg_dump_common = static_library('libpgdump_common',
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 4bb0c8030e..8409359b9b 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -478,6 +478,33 @@ main(int argc, char *argv[])
 	if (on_conflict_do_nothing)
 		appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing");
 
+	/*
+	 * Open the output file if required, otherwise use stdout.  If required,
+	 * then create new directory and global.dat file.
+	 */
+	if (archDumpFormat != archNull)
+	{
+		char	toc_path[MAXPGPATH];
+
+		/* Create new directory or accept the empty existing directory. */
+		create_or_open_dir(filename);
+
+		snprintf(toc_path, MAXPGPATH, "%s/global.dat", filename);
+
+		OPF = fopen(toc_path, "w");
+		if (!OPF)
+			pg_fatal("could not open global.dat file: %s", strerror(errno));
+	}
+	else if (filename)
+	{
+		OPF = fopen(filename, PG_BINARY_W);
+		if (!OPF)
+			pg_fatal("could not open output file \"%s\": %m",
+					 filename);
+	}
+	else
+		OPF = stdout;
+
 	/*
 	 * If there was a database specified on the command line, use that,
 	 * otherwise try to connect to database "postgres", and failing that
@@ -517,33 +544,6 @@ main(int argc, char *argv[])
 	expand_dbname_patterns(conn, &database_exclude_patterns,
 						   &database_exclude_names);
 
-	/*
-	 * Open the output file if required, otherwise use stdout.  If required,
-	 * then create new directory and global.dat file.
-	 */
-	if (archDumpFormat != archNull)
-	{
-		char	toc_path[MAXPGPATH];
-
-		/* Create new directory and accept the empty existing directory. */
-		create_or_open_dir(filename);
-
-		snprintf(toc_path, MAXPGPATH, "%s/global.dat", filename);
-
-		OPF = fopen(toc_path, "w");
-		if (!OPF)
-			pg_fatal("could not open global.dat file: %s", strerror(errno));
-	}
-	else if (filename)
-	{
-		OPF = fopen(filename, PG_BINARY_W);
-		if (!OPF)
-			pg_fatal("could not open output file \"%s\": %m",
-					 filename);
-	}
-	else
-		OPF = stdout;
-
 	/*
 	 * Set the client encoding if requested.
 	 */
@@ -1887,9 +1887,17 @@ create_or_open_dir(const char *dirname)
 				pg_fatal("could not close directory \"%s\": %m",
 						dirname);
 		}
-	}
 
-	if (!is_empty && mkdir(dirname, 0700) < 0)
-		pg_fatal("could not create directory \"%s\": %m",
-				dirname);
+		if(!is_empty)
+		{
+			pg_log_error("directory \"%s\" exists but is not empty", dirname);
+			pg_log_error_hint("If you want to dump data on this directory, either remove or empty "
+							  "this directory \"%s\" or run %s "
+							  "with an argument other than \"%s\".",
+							  dirname, progname, dirname);
+			exit_nicely(1);
+		}
+	}
+	else if (mkdir(dirname, 0700) < 0)
+		pg_fatal("could not create directory \"%s\": %m", dirname);
 }