Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9a279da

Browse files
committed
Guard against createdb --location=PGDATA foo; without this, the code
tries to create a symlink pointing at itself. Per trouble report from Kenneth McDowell.
1 parent 9b3a6b5 commit 9a279da

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/backend/commands/dbcommands.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.81 2001/10/25 05:49:24 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.82 2002/02/23 20:55:46 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -164,10 +164,21 @@ createdb(const char *dbname, const char *dbpath,
164164
* Compute nominal location (where we will try to access the
165165
* database), and resolve alternate physical location if one is
166166
* specified.
167+
*
168+
* If an alternate location is specified but is the same as the
169+
* normal path, just drop the alternate-location spec (this seems
170+
* friendlier than erroring out). We must test this case to avoid
171+
* creating a circular symlink below.
167172
*/
168173
nominal_loc = GetDatabasePath(dboid);
169174
alt_loc = resolve_alt_dbpath(dbpath, dboid);
170175

176+
if (alt_loc && strcmp(alt_loc, nominal_loc) == 0)
177+
{
178+
alt_loc = NULL;
179+
dbpath = NULL;
180+
}
181+
171182
if (strchr(nominal_loc, '\''))
172183
elog(ERROR, "database path may not contain single quotes");
173184
if (alt_loc && strchr(alt_loc, '\''))
@@ -198,7 +209,9 @@ createdb(const char *dbname, const char *dbpath,
198209
if (mkdir(target_dir, S_IRWXU) != 0)
199210
elog(ERROR, "CREATE DATABASE: unable to create database directory '%s': %m",
200211
target_dir);
201-
rmdir(target_dir);
212+
if (rmdir(target_dir) != 0)
213+
elog(ERROR, "CREATE DATABASE: unable to remove temp directory '%s': %m",
214+
target_dir);
202215

203216
/* Make the symlink, if needed */
204217
if (alt_loc)
@@ -548,6 +561,9 @@ resolve_alt_dbpath(const char *dbpath, Oid dboid)
548561
}
549562

550563
len = strlen(prefix) + 6 + sizeof(Oid) * 8 + 1;
564+
if (len >= MAXPGPATH - 100)
565+
elog(ERROR, "Alternate path is too long");
566+
551567
ret = palloc(len);
552568
snprintf(ret, len, "%s/base/%u", prefix, dboid);
553569

0 commit comments

Comments
 (0)