- no longer put ACLs at end of dump
authorPhilip Warner
Mon, 24 Jul 2000 06:24:26 +0000 (06:24 +0000)
committerPhilip Warner
Mon, 24 Jul 2000 06:24:26 +0000 (06:24 +0000)
- connect as appropriate user in pg_restore with db connection
- dump owner of rule in pg_dump

src/bin/pg_dump/Makefile
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_backup_archiver.h
src/bin/pg_dump/pg_backup_db.c
src/bin/pg_dump/pg_backup_plain_text.c
src/bin/pg_dump/pg_dump.c

index 6b104028615e806a7ea613e8b3aec0bed5131c6b..6bbef564c5f523a6e2e6af0444be170bb998b41a 100644 (file)
@@ -4,7 +4,7 @@
 #
 # Copyright (c) 1994, Regents of the University of California
 #
-# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.20 2000/07/21 11:40:08 pjw Exp $
+# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.21 2000/07/24 06:24:26 pjw Exp $
 #
 #-------------------------------------------------------------------------
 
index e210780196be7a7f27daccfe073e42617a5f203e..55fb0f742f17999dbf98d71622128353360cde16 100644 (file)
@@ -43,6 +43,8 @@ static int        _tocSortCompareByIDNum(const void *p1, const void *p2);
 static ArchiveHandle*  _allocAH(const char* FileSpec, const ArchiveFormat fmt, 
                int compression, ArchiveMode mode);
 static int         _printTocEntry(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt);
+static void        _reconnectAsOwner(ArchiveHandle* AH, TocEntry* te);
+
 static int     _tocEntryRequired(TocEntry* te, RestoreOptions *ropt);
 static void        _disableTriggers(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
 static void        _enableTriggers(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
@@ -153,6 +155,12 @@ void RestoreArchive(Archive* AHX, RestoreOptions *ropt)
        /* Work out what, if anything, we want from this entry */
        reqs = _tocEntryRequired(te, ropt);
 
+       /* Reconnect if necessary */
+       if (reqs != 0)
+       {
+           _reconnectAsOwner(AH, te); 
+       }
+
        if ( (reqs & 1) != 0) /* We want the schema */
        {
            ahlog(AH, 1, "Creating %s %s\n", te->desc, te->name);
@@ -772,6 +780,14 @@ void ahlog(ArchiveHandle* AH, int level, const char *fmt, ...)
    va_end(ap);
 }
 
+/*
+ * Single place for logic which says 'We are restoring to a direct DB connection'.
+ */
+int RestoringToDB(ArchiveHandle* AH)
+{
+   return (AH->ropt && AH->ropt->useDB && AH->connection);
+}
+
 /*
  *  Write buffer to the output file (usually stdout). This is user for
  *  outputting 'restore' scripts etc. It is even possible for an archive
@@ -798,7 +814,7 @@ int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle* AH)
         * If we're doing a restore, and it's direct to DB, and we're connected
         * then send it to the DB.
         */ 
-       if (AH->ropt && AH->ropt->useDB && AH->connection)
+       if (RestoringToDB(AH))
            return ExecuteSqlCommandBuf(AH, (void*)ptr, size*nmemb);
        else
            return fwrite((void*)ptr, size, nmemb, AH->OF);
@@ -1335,22 +1351,32 @@ static int _tocEntryRequired(TocEntry* te, RestoreOptions *ropt)
     return res;
 }
 
+static void _reconnectAsOwner(ArchiveHandle* AH, TocEntry* te) 
+{
+    if (te->owner && strlen(te->owner) != 0 && strcmp(AH->currUser, te->owner) != 0) {
+       if (RestoringToDB(AH))
+       {
+           ReconnectDatabase(AH, te->owner);
+           //todo pjw - ???? fix for db connection...
+       }
+       else
+       {
+           ahprintf(AH, "\\connect - %s\n", te->owner);
+       }
+       AH->currUser = te->owner;
+    }
+}
+
 static int _printTocEntry(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt) 
 {
     ahprintf(AH, "--\n-- TOC Entry ID %d (OID %s)\n--\n-- Name: %s Type: %s Owner: %s\n",
        te->id, te->oid, te->name, te->desc, te->owner);
     if (AH->PrintExtraTocPtr != NULL) {
-   (*AH->PrintExtraTocPtr)(AH, te);
+       (*AH->PrintExtraTocPtr)(AH, te);
     }
     ahprintf(AH, "--\n\n");
 
-    if (te->owner && strlen(te->owner) != 0 && strcmp(AH->currUser, te->owner) != 0) {
-       //todo pjw - fix for db connection...
-       //ahprintf(AH, "\\connect - %s\n", te->owner);
-       AH->currUser = te->owner;
-    }
-
-    ahprintf(AH, "%s\n", te->defn);
+   ahprintf(AH, "%s\n", te->defn);
 
     return 1;
 }
index f3b0ba538589c7458d8ead57971d2b597c0e3598..326cb4df7f10367c0ce4e7295efe92bc436d7221 100644 (file)
@@ -264,6 +264,8 @@ extern int              isValidTarHeader(char *header);
 
 extern OutputContext   SetOutput(ArchiveHandle* AH, char *filename, int compression);
 extern void            ResetOutput(ArchiveHandle* AH, OutputContext savedContext);
+extern int                 RestoringToDB(ArchiveHandle* AH);
+extern int             ReconnectDatabase(ArchiveHandle *AH, char *newUser);
 
 int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle* AH);
 int ahprintf(ArchiveHandle* AH, const char *fmt, ...);
index 62e4c2b0dd879468ad37c8732a84af420c89134d..17f025ab9fba23a98a878cb4064aa9258c7f480a 100644 (file)
@@ -48,20 +48,26 @@ _prompt_for_password(char *username, char *password)
                   t;
 #endif
 
-   fprintf(stderr, "Username: ");
-   fflush(stderr);
-   fgets(username, 100, stdin);
-   length = strlen(username);
-   /* skip rest of the line */
-   if (length > 0 && username[length - 1] != '\n')
+   /*
+    * Allow for forcing a specific username
+    */
+   if (strlen(username) == 0)
    {
-       do
+       fprintf(stderr, "Username: ");
+       fflush(stderr);
+       fgets(username, 100, stdin);
+       length = strlen(username);
+       /* skip rest of the line */
+       if (length > 0 && username[length - 1] != '\n')
        {
-           fgets(buf, 512, stdin);
-       } while (buf[strlen(buf) - 1] != '\n');
+           do
+           {
+               fgets(buf, 512, stdin);
+           } while (buf[strlen(buf) - 1] != '\n');
+       }
+       if (length > 0 && username[length - 1] == '\n')
+           username[length - 1] = '\0';
    }
-   if (length > 0 && username[length - 1] == '\n')
-       username[length - 1] = '\0';
 
 #ifdef HAVE_TERMIOS_H
    tcgetattr(0, &t);
@@ -125,17 +131,69 @@ _check_database_version(ArchiveHandle *AH, bool ignoreVersion)
    PQclear(res);
 }
 
+int ReconnectDatabase(ArchiveHandle *AH, char *newUser)
+{
+   int         need_pass;
+   PGconn      *newConn;
+   char        password[100];
+   char        *pwparam = NULL;
+   int         badPwd = 0;
+   int         noPwd = 0;
+
+   ahlog(AH, 1, "Connecting as %s\n", newUser);
+
+   do
+   {
+           need_pass = false;
+           newConn = PQsetdbLogin(PQhost(AH->connection), PQport(AH->connection),
+                                   NULL, NULL, PQdb(AH->connection), 
+                                   newUser, pwparam);
+           if (!newConn)
+               die_horribly(AH, "%s: Failed to reconnect (PQsetdbLogin failed).\n", progname);
+
+           if (PQstatus(newConn) == CONNECTION_BAD)
+           {
+               noPwd = (strcmp(PQerrorMessage(newConn), "fe_sendauth: no password supplied\n") == 0);
+               badPwd = (strncmp(PQerrorMessage(newConn), "Password authentication failed for user", 39)
+                           == 0);
+
+               if (noPwd || badPwd) 
+               {
+
+                   if (badPwd)
+                       fprintf(stderr, "Password incorrect\n");
+
+                   fprintf(stderr, "Connecting to %s as %s\n", PQdb(AH->connection), newUser);
+
+                   need_pass = true;
+                   _prompt_for_password(newUser, password);
+                   pwparam = password; 
+               }
+               else
+                   die_horribly(AH, "%s: Could not reconnect. %s\n", progname, PQerrorMessage(newConn));
+           }
+
+   } while (need_pass);
+
+   PQfinish(AH->connection);
+   AH->connection = newConn;
+   strcpy(AH->username, newUser);
+
+   return 1;
+}
+
+
 PGconn* ConnectDatabase(Archive *AHX, 
        const char*     dbname,
        const char*     pghost,
        const char*     pgport,
-       const int   reqPwd,
-       const int   ignoreVersion)
+       const int       reqPwd,
+       const int       ignoreVersion)
 {
    ArchiveHandle   *AH = (ArchiveHandle*)AHX;
-   char        connect_string[512] = "";
-   char        tmp_string[128];
-   char        password[100];
+   char            connect_string[512] = "";
+   char            tmp_string[128];
+   char            password[100];
 
    if (AH->connection)
        die_horribly(AH, "%s: already connected to database\n", progname);
@@ -168,6 +226,7 @@ PGconn* ConnectDatabase(Archive *AHX,
 
    if (reqPwd)
    {
+       AH->username[0] = '\0';
        _prompt_for_password(AH->username, password);
        strcat(connect_string, "authtype=password ");
        sprintf(tmp_string, "user=%s ", AH->username);
@@ -188,6 +247,8 @@ PGconn* ConnectDatabase(Archive *AHX,
    /* check for version mismatch */
    _check_database_version(AH, ignoreVersion);
 
+   AH->currUser = PQuser(AH->connection);
+
    return AH->connection;
 }
 
index 56866f103684f31bf55b3085511681e47f8c6985..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
@@ -1,115 +0,0 @@
-/*-------------------------------------------------------------------------\r
- *\r
- * pg_backup_plain_text.c\r
- *\r
- * This file is copied from the 'custom' format file, but dumps data into\r
- * directly to a text file, and the TOC into the 'main' file.\r
- *\r
- * See the headers to pg_restore for more details.\r
- *\r
- * Copyright (c) 2000, Philip Warner\r
- *      Rights are granted to use this software in any way so long\r
- *      as this notice is not removed.\r
- *\r
- * The author is not responsible for loss or damages that may\r
- * result from it's use.\r
- *\r
- *\r
- * IDENTIFICATION\r
- *\r
- * Modifications - 01-Jul-2000 - [email protected]\r
- *\r
- * Initial version. \r
- *\r
- *-------------------------------------------------------------------------\r
- */\r
-\r
-#include \r
-#include \r
-#include  /* for dup */\r
-#include "pg_backup.h"\r
-#include "pg_backup_archiver.h"\r
-\r
-static void     _ArchiveEntry(ArchiveHandle* AH, TocEntry* te);\r
-static void    _StartData(ArchiveHandle* AH, TocEntry* te);\r
-static int _WriteData(ArchiveHandle* AH, const void* data, int dLen);\r
-static void     _EndData(ArchiveHandle* AH, TocEntry* te);\r
-static int      _WriteByte(ArchiveHandle* AH, const int i);\r
-static int      _WriteBuf(ArchiveHandle* AH, const void* buf, int len);\r
-static void     _CloseArchive(ArchiveHandle* AH);\r
-static void    _PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt);\r
-\r
-/*\r
- *  Initializer\r
- */\r
-void InitArchiveFmt_PlainText(ArchiveHandle* AH) \r
-{\r
-    /* Assuming static functions, this can be copied for each format. */\r
-    AH->ArchiveEntryPtr = _ArchiveEntry;\r
-    AH->StartDataPtr = _StartData;\r
-    AH->WriteDataPtr = _WriteData;\r
-    AH->EndDataPtr = _EndData;\r
-    AH->WriteBytePtr = _WriteByte;\r
-    AH->WriteBufPtr = _WriteBuf;\r
-    AH->ClosePtr = _CloseArchive;\r
-    AH->PrintTocDataPtr = _PrintTocData;\r
-\r
-    /*\r
-     * Now prevent reading...\r
-     */\r
-    if (AH->mode == archModeRead)\r
-   die_horribly("%s: This format can not be read\n");\r
-\r
-}\r
-\r
-/*\r
- * - Start a new TOC entry\r
- */\r
-static void    _ArchiveEntry(ArchiveHandle* AH, TocEntry* te) \r
-{\r
-    /* Don't need to do anything */\r
-}\r
-\r
-static void    _StartData(ArchiveHandle* AH, TocEntry* te)\r
-{\r
-    ahprintf(AH, "--\n-- Data for TOC Entry ID %d (OID %s) %s %s\n--\n\n",\r
-       te->id, te->oid, te->desc, te->name);\r
-}\r
-\r
-static int _WriteData(ArchiveHandle* AH, const void* data, int dLen)\r
-{\r
-    ahwrite(data, 1, dLen, AH);\r
-    return dLen;\r
-}\r
-\r
-static void    _EndData(ArchiveHandle* AH, TocEntry* te)\r
-{\r
-    ahprintf(AH, "\n\n");\r
-}\r
-\r
-/*\r
- * Print data for a given TOC entry\r
-*/\r
-static void    _PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt)\r
-{\r
-    if (*te->dataDumper)\r
-   (*te->dataDumper)((Archive*)AH, te->oid, te->dataDumperArg);\r
-}\r
-\r
-static int _WriteByte(ArchiveHandle* AH, const int i)\r
-{\r
-    /* Don't do anything */\r
-    return 0;\r
-}\r
-\r
-static int _WriteBuf(ArchiveHandle* AH, const void* buf, int len)\r
-{\r
-    /* Don't do anything */\r
-    return len;\r
-}\r
-\r
-static void    _CloseArchive(ArchiveHandle* AH)\r
-{\r
-    /* Nothing to do */\r
-}\r
-\r
index e4a47b9ef10d0d26ad098f04ebe6e53aa47fe969..7c89573e2910ec9dbb17896e695630d2a55dd38d 100644 (file)
@@ -22,7 +22,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.160 2000/07/21 11:40:08 pjw Exp $
+ *   $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.161 2000/07/24 06:24:26 pjw Exp $
  *
  * Modifications - 6/10/96 - [email protected] - version 1.13.dhb
  *
@@ -883,7 +883,6 @@ main(int argc, char **argv)
    MoveToEnd(g_fout, "INDEX");
    MoveToEnd(g_fout, "TRIGGER");
    MoveToEnd(g_fout, "RULE");
-   MoveToEnd(g_fout, "ACL");
 
    if (plainText) 
    {
@@ -3819,6 +3818,7 @@ dumpRules(Archive *fout, const char *tablename,
 
    int         i_definition;
    int         i_oid;
+   int         i_owner;
    int         i_rulename;
 
    if (g_verbose)
@@ -3837,8 +3837,9 @@ dumpRules(Archive *fout, const char *tablename,
         * Get all rules defined for this table
         */
        resetPQExpBuffer(query);
-       appendPQExpBuffer(query, "SELECT pg_get_ruledef(pg_rewrite.rulename) "
-                         "AS definition, pg_rewrite.oid, pg_rewrite.rulename FROM pg_rewrite, pg_class "
+       appendPQExpBuffer(query, "SELECT pg_get_ruledef(pg_rewrite.rulename) AS definition,"
+                         "pg_get_userbyid(pg_class.relowner) AS viewowner, "
+                         "pg_rewrite.oid, pg_rewrite.rulename FROM pg_rewrite, pg_class "
                          "WHERE pg_class.relname = '%s' "
                          "AND pg_rewrite.ev_class = pg_class.oid "
                          "ORDER BY pg_rewrite.oid",
@@ -3854,6 +3855,7 @@ dumpRules(Archive *fout, const char *tablename,
 
        nrules = PQntuples(res);
        i_definition = PQfnumber(res, "definition");
+       i_owner = PQfnumber(res, "viewowner");
        i_oid = PQfnumber(res, "oid");
        i_rulename = PQfnumber(res, "rulename");
 
@@ -3863,10 +3865,9 @@ dumpRules(Archive *fout, const char *tablename,
 
        for (i = 0; i < nrules; i++)
        {
-
            ArchiveEntry(fout, PQgetvalue(res, i, i_oid), PQgetvalue(res, i, i_rulename),
                            "RULE", NULL, PQgetvalue(res, i, i_definition),
-                           "", "", "", NULL, NULL);
+                           "", "", PQgetvalue(res, i, i_owner), NULL, NULL);
 
            /* Dump rule comments */