77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.9 1997/09/08 21:46:07 momjian Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.10 1997/11/07 06:37:55 thomas Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
1414#include <stdio.h>
1515#include <string.h>
1616#include <signal.h>
17+ #include <sys/stat.h>
1718
1819#include "postgres.h"
1920#include "miscadmin.h" /* for DataDir */
3637
3738/* non-export function prototypes */
3839static void
39- check_permissions (char * command ,char * dbname ,
40+ check_permissions (char * command ,char * dbpath , char * dbname ,
4041Oid * dbIdP ,Oid * userIdP );
4142static HeapTuple get_pg_dbtup (char * command ,char * dbname ,Relation dbrel );
42- static void stop_vacuum (char * dbname );
43+ static void stop_vacuum (char * dbpath , char * dbname );
4344
4445void
45- createdb (char * dbname )
46+ createdb (char * dbname , char * dbpath )
4647{
4748Oid db_id ,
4849user_id ;
4950char buf [512 ];
51+ char * lp ,
52+ loc [512 ];
5053
5154/*
5255 * If this call returns, the database does not exist and we're allowed
5356 * to create databases.
5457 */
55- check_permissions ("createdb" ,dbname ,& db_id ,& user_id );
58+ check_permissions ("createdb" ,dbpath , dbname ,& db_id ,& user_id );
5659
5760/* close virtual file descriptors so we can do system() calls */
5861closeAllVfds ();
5962
60- sprintf (buf ,"mkdir %s%cbase%c%s" ,DataDir ,SEP_CHAR ,SEP_CHAR ,dbname );
61- system (buf );
62- sprintf (buf ,"%s %s%cbase%ctemplate1%c* %s%cbase%c%s" ,
63- COPY_CMD ,DataDir ,SEP_CHAR ,SEP_CHAR ,SEP_CHAR ,DataDir ,
64- SEP_CHAR ,SEP_CHAR ,dbname );
63+ /* Now create directory for this new database */
64+ if ((dbpath != NULL )&& (strcmp (dbpath ,dbname )!= 0 ))
65+ {
66+ if (* (dbpath + strlen (dbpath )- 1 )== SEP_CHAR )
67+ * (dbpath + strlen (dbpath )- 1 )= '\0' ;
68+ sprintf (loc ,"%s%c%s" ,dbpath ,SEP_CHAR ,dbname );
69+ }
70+ else
71+ {
72+ strcpy (loc ,dbname );
73+ }
74+
75+ lp = ExpandDatabasePath (loc );
76+
77+ if (mkdir (lp ,S_IRWXU )!= 0 )
78+ elog (WARN ,"Unable to create database directory %s" ,lp );
79+
80+ sprintf (buf ,"%s %s%cbase%ctemplate1%c* %s" ,
81+ COPY_CMD ,DataDir ,SEP_CHAR ,SEP_CHAR ,SEP_CHAR ,lp );
6582system (buf );
6683
67- /* sprintf(buf, "insert into pg_database (datname, datdba, datpath) \
68- values (\'%s\'::char16, \'%d\'::oid, \'%s\'::text);",
69- dbname, user_id, dbname);
70- */
84+ #if FALSE
7185sprintf (buf ,"insert into pg_database (datname, datdba, datpath) \
72- values (\'%s\', \'%d\', \'%s\');" ,
86+ values (\'%s\'::char16 , \'%d\'::oid , \'%s\'::text );" ,
7387dbname ,user_id ,dbname );
88+ #endif
89+
90+ sprintf (buf ,"insert into pg_database (datname, datdba, datpath)"
91+ " values (\'%s\', \'%d\', \'%s\');" ,dbname ,user_id ,loc );
7492
7593pg_eval (buf , (char * * )NULL , (Oid * )NULL ,0 );
7694}
@@ -80,21 +98,57 @@ destroydb(char *dbname)
8098{
8199Oid user_id ,
82100db_id ;
101+ char * path ;
102+ char dbpath [MAXPGPATH + 1 ];
83103char buf [512 ];
104+ char loc [512 ];
105+ text * dbtext ;
106+
107+ Relation dbrel ;
108+ HeapTuple dbtup ;
84109
85110/*
86111 * If this call returns, the database exists and we're allowed to
87112 * remove it.
88113 */
89- check_permissions ("destroydb" ,dbname ,& db_id ,& user_id );
114+ check_permissions ("destroydb" ,dbpath , dbname ,& db_id ,& user_id );
90115
91116if (!OidIsValid (db_id ))
92117{
93118elog (FATAL ,"impossible: pg_database instance with invalid OID." );
94119}
95120
96121/* stop the vacuum daemon */
97- stop_vacuum (dbname );
122+ stop_vacuum (dbpath ,dbname );
123+
124+ #if FALSE
125+ dbrel = heap_openr (DatabaseRelationName );
126+ if (!RelationIsValid (dbrel ))
127+ elog (FATAL ,"%s: cannot open relation \"%-.*s\"" ,
128+ "destroydb" ,DatabaseRelationName );
129+
130+ dbtup = get_pg_dbtup ("destroydb" ,dbname ,dbrel );
131+
132+ if (!HeapTupleIsValid (dbtup ))
133+ elog (NOTICE ,"destroydb: pg_database entry not found %s" ,dbname );
134+
135+ dbtext = (text * )heap_getattr (dbtup ,InvalidBuffer ,
136+ Anum_pg_database_datpath ,
137+ RelationGetTupleDescriptor (dbrel ),
138+ (char * )NULL );
139+ memcpy (loc ,VARDATA (dbtext ), (VARSIZE (dbtext )- VARHDRSZ ));
140+ * (loc + (VARSIZE (dbtext )- VARHDRSZ ))= '\0' ;
141+
142+ #if FALSE
143+ if (* loc != SEP_CHAR )
144+ {
145+ sprintf (buf ,"%s/base/%s" ,DataDir ,loc );
146+ strcpy (loc ,buf );
147+ }
148+ #endif
149+
150+ heap_close (dbrel );
151+ #endif
98152
99153/*
100154 * remove the pg_database tuple FIRST, this may fail due to
@@ -108,7 +162,9 @@ destroydb(char *dbname)
108162 * remove the data directory. If the DELETE above failed, this will
109163 * not be reached
110164 */
111- sprintf (buf ,"rm -r %s/base/%s" ,DataDir ,dbname );
165+ path = ExpandDatabasePath (dbpath );
166+
167+ sprintf (buf ,"rm -r %s" ,path );
112168system (buf );
113169
114170/* drop pages for this database that are in the shared buffer cache */
@@ -160,6 +216,7 @@ get_pg_dbtup(char *command, char *dbname, Relation dbrel)
160216
161217static void
162218check_permissions (char * command ,
219+ char * dbpath ,
163220char * dbname ,
164221Oid * dbIdP ,
165222Oid * userIdP )
@@ -172,6 +229,8 @@ check_permissions(char *command,
172229bool dbfound ;
173230bool use_super ;
174231char * userName ;
232+ text * dbtext ;
233+ char path [MAXPGPATH + 1 ];
175234
176235userName = GetPgUserName ();
177236utup = SearchSysCacheTuple (USENAME ,PointerGetDatum (userName ),
@@ -228,6 +287,13 @@ check_permissions(char *command,
228287RelationGetTupleDescriptor (dbrel ),
229288 (char * )NULL );
230289* dbIdP = dbtup -> t_oid ;
290+ dbtext = (text * )heap_getattr (dbtup ,InvalidBuffer ,
291+ Anum_pg_database_datpath ,
292+ RelationGetTupleDescriptor (dbrel ),
293+ (char * )NULL );
294+
295+ strncpy (path ,VARDATA (dbtext ), (VARSIZE (dbtext )- VARHDRSZ ));
296+ * (path + VARSIZE (dbtext )- VARHDRSZ )= '\0' ;
231297}
232298else
233299{
@@ -259,21 +325,31 @@ check_permissions(char *command,
259325elog (WARN ,"%s: database %s is not owned by you." ,command ,dbname );
260326
261327}
262- }
328+
329+ if (dbfound && !strcmp (command ,"destroydb" ))
330+ strcpy (dbpath ,path );
331+ }/* check_permissions() */
263332
264333/*
265- *stop_vacuum() -- stop the vacuum daemon on the database, if one is
266- * running.
334+ *stop_vacuum() -- stop the vacuum daemon on the database, if one is running.
267335 */
268336static void
269- stop_vacuum (char * dbname )
337+ stop_vacuum (char * dbpath , char * dbname )
270338{
271339char filename [256 ];
272340FILE * fp ;
273341int pid ;
274342
275- sprintf (filename ,"%s%cbase%c%s%c%s.vacuum" ,DataDir ,SEP_CHAR ,SEP_CHAR ,
343+ if (strchr (dbpath ,SEP_CHAR )!= 0 )
344+ {
345+ sprintf (filename ,"%s%cbase%c%s%c%s.vacuum" ,DataDir ,SEP_CHAR ,SEP_CHAR ,
276346dbname ,SEP_CHAR ,dbname );
347+ }
348+ else
349+ {
350+ sprintf (filename ,"%s%c%s.vacuum" ,dbpath ,SEP_CHAR ,dbname );
351+ }
352+
277353if ((fp = AllocateFile (filename ,"r" ))!= NULL )
278354{
279355fscanf (fp ,"%d" ,& pid );