From a6f56efc3518b1edbbe466277da7a604b4ffeb56 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Wed, 6 Jan 2010 23:23:51 +0000
Subject: [PATCH] PG_MAJORVERSION:

For simplicity, use PG_MAJORVERSION rather than PG_VERSION for creation
of the PG_VERSION file.
---
 src/backend/commands/tablespace.c | 42 ++++---------------
 src/bin/initdb/initdb.c           | 68 ++++++-------------------------
 2 files changed, 20 insertions(+), 90 deletions(-)

diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index ad7d6662ec9..5c6767d6138 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.67 2010/01/06 01:48:09 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.68 2010/01/06 23:23:51 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -82,7 +82,7 @@ char	   *temp_tablespaces = NULL;
 
 
 static bool remove_tablespace_directories(Oid tablespaceoid, bool redo);
-static void set_short_version(const char *path);
+static void write_version_file(const char *path);
 
 
 /*
@@ -332,7 +332,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
 	 * (the emptiness check above will fail), and to label tablespace
 	 * directories by PG version.
 	 */
-	set_short_version(location);
+	write_version_file(location);
 
 	/*
 	 * All seems well, create the symlink
@@ -673,46 +673,21 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo)
  * write out the PG_VERSION file in the specified directory
  */
 static void
-set_short_version(const char *path)
+write_version_file(const char *path)
 {
-	char	   *short_version;
-	bool		gotdot = false;
-	int			end;
 	char	   *fullname;
 	FILE	   *version_file;
 
-	/* Construct short version string (should match initdb.c) */
-	short_version = pstrdup(PG_VERSION);
-
-	for (end = 0; short_version[end] != '\0'; end++)
-	{
-		if (short_version[end] == '.')
-		{
-			Assert(end != 0);
-			if (gotdot)
-				break;
-			else
-				gotdot = true;
-		}
-		else if (short_version[end] < '0' || short_version[end] > '9')
-		{
-			/* gone past digits and dots */
-			break;
-		}
-	}
-	Assert(end > 0 && short_version[end - 1] != '.' && gotdot);
-	short_version[end] = '\0';
-
 	/* Now write the file */
 	fullname = palloc(strlen(path) + 11 + 1);
 	sprintf(fullname, "%s/PG_VERSION", path);
-	version_file = AllocateFile(fullname, PG_BINARY_W);
-	if (version_file == NULL)
+
+	if ((version_file = AllocateFile(fullname, PG_BINARY_W)) == NULL)
 		ereport(ERROR,
 				(errcode_for_file_access(),
 				 errmsg("could not write to file \"%s\": %m",
 						fullname)));
-	fprintf(version_file, "%s\n", short_version);
+	fprintf(version_file, "%s\n", PG_MAJORVERSION);
 	if (FreeFile(version_file))
 		ereport(ERROR,
 				(errcode_for_file_access(),
@@ -720,7 +695,6 @@ set_short_version(const char *path)
 						fullname)));
 
 	pfree(fullname);
-	pfree(short_version);
 }
 
 /*
@@ -1370,7 +1344,7 @@ tblspc_redo(XLogRecPtr lsn, XLogRecord *record)
 						 location)));
 
 		/* Create or re-create the PG_VERSION file in the target directory */
-		set_short_version(location);
+		write_version_file(location);
 
 		/* Create the symlink if not already present */
 		linkloc = (char *) palloc(OIDCHARS + OIDCHARS + 1);
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 45ad7ba4106..36cf34eab61 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -42,7 +42,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  * Portions taken from FreeBSD.
  *
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.181 2010/01/02 16:57:58 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.182 2010/01/06 23:23:51 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -156,16 +156,15 @@ static int	mkdir_p(char *path, mode_t omode);
 static void exit_nicely(void);
 static char *get_id(void);
 static char *get_encoding_id(char *encoding_name);
-static char *get_short_version(void);
 static int	check_data_dir(char *dir);
 static bool mkdatadir(const char *subdir);
 static void set_input(char **dest, char *filename);
 static void check_input(char *path);
-static void set_short_version(char *short_version, char *extrapath);
+static void write_version_file(char *extrapath);
 static void set_null_conf(void);
 static void test_config_settings(void);
 static void setup_config(void);
-static void bootstrap_template1(char *short_version);
+static void bootstrap_template1(void);
 static void setup_auth(void);
 static void get_set_pwd(void);
 static void setup_depend(void);
@@ -802,42 +801,6 @@ find_matching_ts_config(const char *lc_type)
 }
 
 
-/*
- * get short version of VERSION
- */
-static char *
-get_short_version(void)
-{
-	bool		gotdot = false;
-	int			end;
-	char	   *vr;
-
-	vr = xstrdup(PG_VERSION);
-
-	for (end = 0; vr[end] != '\0'; end++)
-	{
-		if (vr[end] == '.')
-		{
-			if (end == 0)
-				return NULL;
-			else if (gotdot)
-				break;
-			else
-				gotdot = true;
-		}
-		else if (vr[end] < '0' || vr[end] > '9')
-		{
-			/* gone past digits and dots */
-			break;
-		}
-	}
-	if (end == 0 || vr[end - 1] == '.' || !gotdot)
-		return NULL;
-
-	vr[end] = '\0';
-	return vr;
-}
-
 /*
  * make sure the directory either doesn't exist or is empty
  *
@@ -972,7 +935,7 @@ check_input(char *path)
  * if extrapath is not NULL
  */
 static void
-set_short_version(char *short_version, char *extrapath)
+write_version_file(char *extrapath)
 {
 	FILE	   *version_file;
 	char	   *path;
@@ -987,14 +950,14 @@ set_short_version(char *short_version, char *extrapath)
 		path = pg_malloc(strlen(pg_data) + strlen(extrapath) + 13);
 		sprintf(path, "%s/%s/PG_VERSION", pg_data, extrapath);
 	}
-	version_file = fopen(path, PG_BINARY_W);
-	if (version_file == NULL)
+
+	if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
 	{
 		fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
 				progname, path, strerror(errno));
 		exit_nicely();
 	}
-	if (fprintf(version_file, "%s\n", short_version) < 0 ||
+	if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
 		fclose(version_file))
 	{
 		fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
@@ -1297,7 +1260,7 @@ setup_config(void)
  * run the BKI script in bootstrap mode to create template1
  */
 static void
-bootstrap_template1(char *short_version)
+bootstrap_template1(void)
 {
 	PG_CMD_DECL;
 	char	  **line;
@@ -1317,7 +1280,7 @@ bootstrap_template1(char *short_version)
 	/* Check that bki file appears to be of the right version */
 
 	snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
-			 short_version);
+			 PG_MAJORVERSION);
 
 	if (strcmp(headerline, *bki_lines) != 0)
 	{
@@ -2480,7 +2443,6 @@ main(int argc, char *argv[])
 				i,
 				ret;
 	int			option_index;
-	char	   *short_version;
 	char	   *effective_user;
 	char	   *pgdenv;			/* PGDATA value gotten from and sent to
 								 * environment */
@@ -2788,12 +2750,6 @@ main(int argc, char *argv[])
 
 	canonicalize_path(share_path);
 
-	if ((short_version = get_short_version()) == NULL)
-	{
-		fprintf(stderr, _("%s: could not determine valid short version string\n"), progname);
-		exit(1);
-	}
-
 	effective_user = get_id();
 	if (strlen(username) == 0)
 		username = effective_user;
@@ -3123,7 +3079,7 @@ main(int argc, char *argv[])
 	check_ok();
 
 	/* Top level PG_VERSION is checked by bootstrapper, so make it first */
-	set_short_version(short_version, NULL);
+	write_version_file(NULL);
 
 	/* Select suitable configuration settings */
 	set_null_conf();
@@ -3133,12 +3089,12 @@ main(int argc, char *argv[])
 	setup_config();
 
 	/* Bootstrap template1 */
-	bootstrap_template1(short_version);
+	bootstrap_template1();
 
 	/*
 	 * Make the per-database PG_VERSION for template1 only after init'ing it
 	 */
-	set_short_version(short_version, "base/1");
+	write_version_file("base/1");
 
 	/* Create the stuff we don't need to use bootstrap mode for */
 
-- 
GitLab