From: Tom Lane Date: Sat, 23 Feb 2002 20:55:46 +0000 (+0000) Subject: Guard against createdb --location=PGDATA foo; without this, the code X-Git-Tag: REL7_3~2057 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9a279da73d412ea41e02292207f998376b460756;p=postgresql Guard against createdb --location=PGDATA foo; without this, the code tries to create a symlink pointing at itself. Per trouble report from Kenneth McDowell. --- diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 92baacb72d..142fefb60f 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.81 2001/10/25 05:49:24 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.82 2002/02/23 20:55:46 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -164,10 +164,21 @@ createdb(const char *dbname, const char *dbpath, * Compute nominal location (where we will try to access the * database), and resolve alternate physical location if one is * specified. + * + * If an alternate location is specified but is the same as the + * normal path, just drop the alternate-location spec (this seems + * friendlier than erroring out). We must test this case to avoid + * creating a circular symlink below. */ nominal_loc = GetDatabasePath(dboid); alt_loc = resolve_alt_dbpath(dbpath, dboid); + if (alt_loc && strcmp(alt_loc, nominal_loc) == 0) + { + alt_loc = NULL; + dbpath = NULL; + } + if (strchr(nominal_loc, '\'')) elog(ERROR, "database path may not contain single quotes"); if (alt_loc && strchr(alt_loc, '\'')) @@ -198,7 +209,9 @@ createdb(const char *dbname, const char *dbpath, if (mkdir(target_dir, S_IRWXU) != 0) elog(ERROR, "CREATE DATABASE: unable to create database directory '%s': %m", target_dir); - rmdir(target_dir); + if (rmdir(target_dir) != 0) + elog(ERROR, "CREATE DATABASE: unable to remove temp directory '%s': %m", + target_dir); /* Make the symlink, if needed */ if (alt_loc) @@ -548,6 +561,9 @@ resolve_alt_dbpath(const char *dbpath, Oid dboid) } len = strlen(prefix) + 6 + sizeof(Oid) * 8 + 1; + if (len >= MAXPGPATH - 100) + elog(ERROR, "Alternate path is too long"); + ret = palloc(len); snprintf(ret, len, "%s/base/%u", prefix, dboid);