]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
pg_basebackup: Fix cross-platform tablespace relocation.
authorRobert Haas <rhaas@postgresql.org>
Fri, 21 Oct 2022 12:21:55 +0000 (08:21 -0400)
committerRobert Haas <rhaas@postgresql.org>
Fri, 21 Oct 2022 12:59:26 +0000 (08:59 -0400)
Specifically, when pg_basebackup is invoked with -Tx=y, don't error
out if x could plausibly be an absolute path either on Windows or on
non-Windows systems. We don't know whether the remote system is
running the same OS as the local system, so it's not appropriate to
assume that our local rule about absolute pathnames is the same as
the rule on the remote system.

Patch by me, reviewed by Tom Lane, Andrew Dunstan, and
Davinder Singh.

Discussion: http://postgr.es/m/CA+TgmoY+jC3YiskomvYKDPK3FbrmsDU7_8+wMHt02HOdJeRb0g@mail.gmail.com

src/bin/pg_basebackup/pg_basebackup.c
src/include/port.h

index 095953b1c9644daa9a8af750c645ce3295117577..792c81e788cd0980b55d152071dd61bff84dafbb 100644 (file)
@@ -271,12 +271,22 @@ tablespace_list_append(const char *arg)
        }
 
        /*
-        * This check isn't absolutely necessary.  But all tablespaces are created
-        * with absolute directories, so specifying a non-absolute path here would
-        * just never match, possibly confusing users.  It's also good to be
-        * consistent with the new_dir check.
+        * All tablespaces are created with absolute directories, so specifying a
+        * non-absolute path here would just never match, possibly confusing users.
+        * Since we don't know whether the remote side is Windows or not, and it
+        * might be different than the local side, permit any path that could be
+        * absolute under either set of rules.
+        *
+        * (There is little practical risk of confusion here, because someone
+        * running entirely on Linux isn't likely to have a relative path that
+        * begins with a backslash or something that looks like a drive
+        * specification. If they do, and they also incorrectly believe that
+        * a relative path is acceptable here, we'll silently fail to warn them
+        * of their mistake, and the -T option will just not get applied, same
+        * as if they'd specified -T for a nonexistent tablespace.)
         */
-       if (!is_absolute_path(cell->old_dir))
+       if (!is_nonwindows_absolute_path(cell->old_dir) &&
+               !is_windows_absolute_path(cell->old_dir))
        {
                pg_log_error("old directory is not an absolute path in tablespace mapping: %s",
                                         cell->old_dir);
index 8a5c6389e6fa5e3bc62e05cd4d638aea6e24cb0c..ecf52bcc9096cbba76d3008651d40bb24246649f 100644 (file)
@@ -75,28 +75,32 @@ extern void get_parent_directory(char *path);
 extern char **pgfnames(const char *path);
 extern void pgfnames_cleanup(char **filenames);
 
-/*
- *     is_absolute_path
- *
- *     By making this a macro we avoid needing to include path.c in libpq.
- */
-#ifndef WIN32
-#define IS_DIR_SEP(ch) ((ch) == '/')
-
-#define is_absolute_path(filename) \
+#define IS_NONWINDOWS_DIR_SEP(ch)      ((ch) == '/')
+#define is_nonwindows_absolute_path(filename) \
 ( \
-       IS_DIR_SEP((filename)[0]) \
+       IS_NONWINDOWS_DIR_SEP((filename)[0]) \
 )
-#else
-#define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
 
+#define IS_WINDOWS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
 /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
-#define is_absolute_path(filename) \
+#define is_windows_absolute_path(filename) \
 ( \
-       IS_DIR_SEP((filename)[0]) || \
+       IS_WINDOWS_DIR_SEP((filename)[0]) || \
        (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
-        IS_DIR_SEP((filename)[2])) \
+        IS_WINDOWS_DIR_SEP((filename)[2])) \
 )
+
+/*
+ *     is_absolute_path and IS_DIR_SEP
+ *
+ *     By using macros here we avoid needing to include path.c in libpq.
+ */
+#ifndef WIN32
+#define IS_DIR_SEP(ch) IS_NONWINDOWS_DIR_SEP(ch)
+#define is_absolute_path(filename) is_nonwindows_absolute_path(filename)
+#else
+#define IS_DIR_SEP(ch) IS_WINDOWS_DIR_SEP(ch)
+#define is_absolute_path(filename) is_windows_absolute_path(filename)
 #endif
 
 /* Portable locale initialization (in exec.c) */