From 7b0f060d549524b39e97b4dcacd775f948ff55c6 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Sun, 11 Jul 2004 21:34:04 +0000
Subject: [PATCH] Use canonicalize_path for -D, GUC paths, and paths coming in
 from environment variables.

---
 src/backend/postmaster/postmaster.c |  5 ++-
 src/backend/utils/misc/guc.c        | 35 +++++++++++++-----
 src/bin/psql/command.c              |  9 +++--
 src/bin/psql/copy.c                 |  4 +-
 src/port/path.c                     | 57 +++++++++++++++++------------
 5 files changed, 71 insertions(+), 39 deletions(-)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index d68279b7f6a..28b1c83fa31 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.407 2004/07/11 00:18:43 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.408 2004/07/11 21:33:59 momjian Exp $
  *
  * NOTES
  *
@@ -372,7 +372,8 @@ PostmasterMain(int argc, char *argv[])
 	InitializeGUCOptions();
 
 	userPGDATA = getenv("PGDATA");		/* default value */
-
+	canonicalize_path(userPGDATA);
+	
 	opterr = 1;
 
 	while ((opt = getopt(argc, argv, "A:a:B:b:c:D:d:Fh:ik:lm:MN:no:p:Ss-:")) != -1)
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 9c8f8b5cc6d..e70d6836aa3 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -10,7 +10,7 @@
  * Written by Peter Eisentraut <peter_e@gmx.net>.
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.214 2004/07/11 00:18:44 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.215 2004/07/11 21:34:00 momjian Exp $
  *
  *--------------------------------------------------------------------
  */
@@ -113,6 +113,7 @@ static const char *assign_custom_variable_classes(const char *newval, bool doit,
 static bool assign_stage_log_stats(bool newval, bool doit, GucSource source);
 static bool assign_log_stats(bool newval, bool doit, GucSource source);
 static bool assign_transaction_read_only(bool newval, bool doit, GucSource source);
+static const char *assign_canonical_path(const char *newval, bool doit, GucSource source);
 
 static void ReadConfigFile(char *filename, GucContext context);
 
@@ -1470,7 +1471,7 @@ static struct config_string ConfigureNamesString[] =
 						 "the specified file.")
 		},
 		&Dynamic_library_path,
-		"$libdir", NULL, NULL
+		"$libdir", assign_canonical_path, NULL
 	},
 
 	{
@@ -1556,7 +1557,7 @@ static struct config_string ConfigureNamesString[] =
 			GUC_LIST_INPUT | GUC_LIST_QUOTE
 		},
 		&preload_libraries_string,
-		"", NULL, NULL
+		"", assign_canonical_path, NULL
 	},
 
 	{
@@ -1678,7 +1679,7 @@ static struct config_string ConfigureNamesString[] =
 			NULL
 		},
 		&UnixSocketDir,
-		"", NULL, NULL
+		"", assign_canonical_path, NULL
 	},
 
 	{
@@ -1712,25 +1713,25 @@ static struct config_string ConfigureNamesString[] =
 	{
 		{"pgdata", PGC_POSTMASTER, 0, gettext_noop("Sets the location of the data directory"), NULL}, 
 		&guc_pgdata,
-		NULL, NULL, NULL
+		NULL, assign_canonical_path, NULL
 	},
 
 	{
 		{"hba_conf", PGC_SIGHUP, 0, gettext_noop("Sets the location of the \"hba\" configuration file"), NULL}, 
 		&guc_hbafile,
-		NULL, NULL, NULL
+		NULL, assign_canonical_path, NULL
 	},
 
 	{
 		{"ident_conf", PGC_SIGHUP, 0, gettext_noop("Sets the location of the \"ident\" configuration file"), NULL}, 
 		&guc_identfile,
-		NULL, NULL, NULL
+		NULL, assign_canonical_path, NULL
 	},
 
 	{
 		{"external_pidfile", PGC_POSTMASTER, 0, gettext_noop("Writes the postmaster PID to the specified file"), NULL},
 		&external_pidfile,
-		NULL, NULL, NULL
+		NULL, assign_canonical_path, NULL
 	},
 
 	/* End-of-list marker */
@@ -5160,8 +5161,7 @@ assign_log_min_messages(const char *newval,
 }
 
 static const char *
-assign_client_min_messages(const char *newval,
-						   bool doit, GucSource source)
+assign_client_min_messages(const char *newval, bool doit, GucSource source)
 {
 	return (assign_msglvl(&client_min_messages, newval, doit, source));
 }
@@ -5430,4 +5430,19 @@ assign_transaction_read_only(bool newval, bool doit, GucSource source)
 	return true;
 }
 
+static const char *
+assign_canonical_path(const char *newval, bool doit, GucSource source)
+{
+	
+	if (doit)
+	{
+		/* We have to create a new pointer to force the change */
+		char *canon_val = guc_strdup(FATAL, newval);
+		canonicalize_path(canon_val);
+		return canon_val;
+	}
+	else
+		return newval;
+}
+
 #include "guc-file.c"
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 9b45b96bbf4..ba8d623731c 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.119 2004/07/11 13:29:15 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.120 2004/07/11 21:34:03 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -375,6 +375,7 @@ exec_command(const char *cmd,
 			fname = psql_scan_slash_option(scan_state,
 										   OT_NORMAL, NULL, true);
 			expand_tilde(&fname);
+			canonicalize_path(fname);
 			status = do_edit(fname, query_buf) ? CMD_NEWEDIT : CMD_ERROR;
 			free(fname);
 		}
@@ -777,8 +778,10 @@ exec_command(const char *cmd,
 					fd = popen(&fname[1], "w");
 				}
 				else
+				{
+					canonicalize_path(fname);
 					fd = fopen(fname, "w");
-
+				}
 				if (!fd)
 				{
 					psql_error("%s: %s\n", fname, strerror(errno));
@@ -1122,7 +1125,6 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
 
 	if (filename_arg)
 		fname = filename_arg;
-
 	else
 	{
 		/* make a temp file to edit */
@@ -1262,6 +1264,7 @@ process_file(char *filename)
 	if (!filename)
 		return false;
 
+	canonicalize_path(filename);
 	fd = fopen(filename, PG_BINARY_R);
 
 	if (!fd)
diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c
index 54bc7aea684..86c0dde748f 100644
--- a/src/bin/psql/copy.c
+++ b/src/bin/psql/copy.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.49 2004/07/11 13:29:15 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.50 2004/07/11 21:34:03 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "copy.h"
@@ -513,6 +513,8 @@ do_copy(const char *args)
 		appendPQExpBuffer(&query, " FORCE NOT NULL %s", options->force_notnull_list);
 	}
 
+	canonicalize_path(options->file);
+
 	if (options->from)
 	{
 		if (options->file)
diff --git a/src/port/path.c b/src/port/path.c
index 8646eacc064..9a796af4dad 100644
--- a/src/port/path.c
+++ b/src/port/path.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/path.c,v 1.22 2004/07/11 02:59:42 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/port/path.c,v 1.23 2004/07/11 21:34:04 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -33,6 +33,7 @@
 #endif
 
 const static char *relative_path(const char *bin_path, const char *other_path);
+static void make_relative(const char *my_exec_path, const char *p, char *ret_path);
 static void trim_directory(char *path);
 static void trim_trailing_separator(char *path);
 
@@ -43,15 +44,6 @@ static void trim_trailing_separator(char *path);
 		(p)++; \
 }
 
-/* Macro creates a relative path */
-#define MAKE_RELATIVE \
-do { \
-		StrNCpy(path, my_exec_path, MAXPGPATH); \
-		trim_directory(path); \
-		trim_directory(path); \
-		snprintf(ret_path, MAXPGPATH, "%s/%s", path, p); \
-} while (0)
-
 /*
  *	first_dir_separator
  */
@@ -140,13 +132,13 @@ get_progname(const char *argv0)
 void
 get_share_path(const char *my_exec_path, char *ret_path)
 {
-	char path[MAXPGPATH];
 	const char *p;
 	
 	if ((p = relative_path(PGBINDIR, PGSHAREDIR)))
-		MAKE_RELATIVE;
+		make_relative(my_exec_path, p, ret_path);
 	else
 		StrNCpy(ret_path, PGSHAREDIR, MAXPGPATH);
+	canonicalize_path(ret_path);
 }
 
 
@@ -157,13 +149,13 @@ get_share_path(const char *my_exec_path, char *ret_path)
 void
 get_etc_path(const char *my_exec_path, char *ret_path)
 {
-	char path[MAXPGPATH];
 	const char *p;
 	
 	if ((p = relative_path(PGBINDIR, SYSCONFDIR)))
-		MAKE_RELATIVE;
+		make_relative(my_exec_path, p, ret_path);
 	else
 		StrNCpy(ret_path, SYSCONFDIR, MAXPGPATH);
+	canonicalize_path(ret_path);
 }
 
 
@@ -174,13 +166,13 @@ get_etc_path(const char *my_exec_path, char *ret_path)
 void
 get_include_path(const char *my_exec_path, char *ret_path)
 {
-	char path[MAXPGPATH];
 	const char *p;
 	
 	if ((p = relative_path(PGBINDIR, INCLUDEDIR)))
-		MAKE_RELATIVE;
+		make_relative(my_exec_path, p, ret_path);
 	else
 		StrNCpy(ret_path, INCLUDEDIR, MAXPGPATH);
+	canonicalize_path(ret_path);
 }
 
 
@@ -191,13 +183,13 @@ get_include_path(const char *my_exec_path, char *ret_path)
 void
 get_pkginclude_path(const char *my_exec_path, char *ret_path)
 {
-	char path[MAXPGPATH];
 	const char *p;
 	
 	if ((p = relative_path(PGBINDIR, PKGINCLUDEDIR)))
-		MAKE_RELATIVE;
+		make_relative(my_exec_path, p, ret_path);
 	else
 		StrNCpy(ret_path, PKGINCLUDEDIR, MAXPGPATH);
+	canonicalize_path(ret_path);
 }
 
 
@@ -210,13 +202,13 @@ get_pkginclude_path(const char *my_exec_path, char *ret_path)
 void
 get_pkglib_path(const char *my_exec_path, char *ret_path)
 {
-	char path[MAXPGPATH];
 	const char *p;
 	
 	if ((p = relative_path(PGBINDIR, PKGLIBDIR)))
-		MAKE_RELATIVE;
+		make_relative(my_exec_path, p, ret_path);
 	else
 		StrNCpy(ret_path, PKGLIBDIR, MAXPGPATH);
+	canonicalize_path(ret_path);
 }
 
 
@@ -229,13 +221,13 @@ get_pkglib_path(const char *my_exec_path, char *ret_path)
 void
 get_locale_path(const char *my_exec_path, char *ret_path)
 {
-	char path[MAXPGPATH];
 	const char *p;
 	
 	if ((p = relative_path(PGBINDIR, LOCALEDIR)))
-		MAKE_RELATIVE;
+		make_relative(my_exec_path, p, ret_path);
 	else
 		StrNCpy(ret_path, LOCALEDIR, MAXPGPATH);
+	canonicalize_path(ret_path);
 }
 
 
@@ -270,6 +262,7 @@ set_pglocale_pgservice(const char *argv0, const char *app)
 	{
 		/* set for libpq to use */
 		snprintf(env_path, sizeof(env_path), "PGLOCALEDIR=%s", path);
+		canonicalize_path(env_path);
 		putenv(strdup(env_path));
 	}
 #endif
@@ -280,11 +273,26 @@ set_pglocale_pgservice(const char *argv0, const char *app)
 	
 		/* set for libpq to use */
 		snprintf(env_path, sizeof(env_path), "PGSYSCONFDIR=%s", path);
+		canonicalize_path(env_path);
 		putenv(strdup(env_path));
 	}
 }
 
 
+/*
+ *	make_relative - adjust path to be relative to bin/
+ */
+static void
+make_relative(const char *my_exec_path, const char *p, char *ret_path)
+{
+	char path[MAXPGPATH];
+
+	StrNCpy(path, my_exec_path, MAXPGPATH);
+	trim_directory(path);
+	trim_directory(path);
+	snprintf(ret_path, MAXPGPATH, "%s/%s", path, p);
+}
+
 
 /*
  *	relative_path
@@ -391,7 +399,10 @@ trim_trailing_separator(char *path)
 	char *p = path + strlen(path);
 
 #ifdef WIN32
-    /* Skip over network and drive specifiers for win32 */
+	/*
+	 *	Skip over network and drive specifiers for win32.
+	 *	Set 'path' to point to the last character to keep.
+	 */
     if (strlen(path) >= 2)
     {
         if (IS_DIR_SEP(path[0]) && IS_DIR_SEP(path[1]))
-- 
GitLab