Skip to content

Commit bb08040

Browse files
committed
- no longer put ACLs at end of dump
- connect as appropriate user in pg_restore with db connection - dump owner of rule in pg_dump
1 parent cd9f0ca commit bb08040

File tree

6 files changed

+122
-147
lines changed

6 files changed

+122
-147
lines changed

src/bin/pg_dump/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Copyright (c) 1994, Regents of the University of California
66
#
7-
# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.20 2000/07/21 11:40:08 pjw Exp $
7+
# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.21 2000/07/24 06:24:26 pjw Exp $
88
#
99
#-------------------------------------------------------------------------
1010

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ static int _tocSortCompareByIDNum(const void *p1, const void *p2);
4343
static ArchiveHandle* _allocAH(const char* FileSpec, const ArchiveFormat fmt,
4444
int compression, ArchiveMode mode);
4545
static int _printTocEntry(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt);
46+
static void _reconnectAsOwner(ArchiveHandle* AH, TocEntry* te);
47+
4648
static int _tocEntryRequired(TocEntry* te, RestoreOptions *ropt);
4749
static void _disableTriggers(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
4850
static void _enableTriggers(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
@@ -153,6 +155,12 @@ void RestoreArchive(Archive* AHX, RestoreOptions *ropt)
153155
/* Work out what, if anything, we want from this entry */
154156
reqs = _tocEntryRequired(te, ropt);
155157

158+
/* Reconnect if necessary */
159+
if (reqs != 0)
160+
{
161+
_reconnectAsOwner(AH, te);
162+
}
163+
156164
if ( (reqs & 1) != 0) /* We want the schema */
157165
{
158166
ahlog(AH, 1, "Creating %s %s\n", te->desc, te->name);
@@ -772,6 +780,14 @@ void ahlog(ArchiveHandle* AH, int level, const char *fmt, ...)
772780
va_end(ap);
773781
}
774782

783+
/*
784+
* Single place for logic which says 'We are restoring to a direct DB connection'.
785+
*/
786+
int RestoringToDB(ArchiveHandle* AH)
787+
{
788+
return (AH->ropt && AH->ropt->useDB && AH->connection);
789+
}
790+
775791
/*
776792
* Write buffer to the output file (usually stdout). This is user for
777793
* 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)
798814
* If we're doing a restore, and it's direct to DB, and we're connected
799815
* then send it to the DB.
800816
*/
801-
if (AH->ropt && AH->ropt->useDB && AH->connection)
817+
if (RestoringToDB(AH))
802818
return ExecuteSqlCommandBuf(AH, (void*)ptr, size*nmemb);
803819
else
804820
return fwrite((void*)ptr, size, nmemb, AH->OF);
@@ -1335,22 +1351,32 @@ static int _tocEntryRequired(TocEntry* te, RestoreOptions *ropt)
13351351
return res;
13361352
}
13371353

1354+
static void _reconnectAsOwner(ArchiveHandle* AH, TocEntry* te)
1355+
{
1356+
if (te->owner && strlen(te->owner) != 0 && strcmp(AH->currUser, te->owner) != 0) {
1357+
if (RestoringToDB(AH))
1358+
{
1359+
ReconnectDatabase(AH, te->owner);
1360+
//todo pjw - ???? fix for db connection...
1361+
}
1362+
else
1363+
{
1364+
ahprintf(AH, "\\connect - %s\n", te->owner);
1365+
}
1366+
AH->currUser = te->owner;
1367+
}
1368+
}
1369+
13381370
static int _printTocEntry(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt)
13391371
{
13401372
ahprintf(AH, "--\n-- TOC Entry ID %d (OID %s)\n--\n-- Name: %s Type: %s Owner: %s\n",
13411373
te->id, te->oid, te->name, te->desc, te->owner);
13421374
if (AH->PrintExtraTocPtr != NULL) {
1343-
(*AH->PrintExtraTocPtr)(AH, te);
1375+
(*AH->PrintExtraTocPtr)(AH, te);
13441376
}
13451377
ahprintf(AH, "--\n\n");
13461378

1347-
if (te->owner && strlen(te->owner) != 0 && strcmp(AH->currUser, te->owner) != 0) {
1348-
//todo pjw - fix for db connection...
1349-
//ahprintf(AH, "\\connect - %s\n", te->owner);
1350-
AH->currUser = te->owner;
1351-
}
1352-
1353-
ahprintf(AH, "%s\n", te->defn);
1379+
ahprintf(AH, "%s\n", te->defn);
13541380

13551381
return 1;
13561382
}

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ extern int isValidTarHeader(char *header);
264264

265265
extern OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression);
266266
extern void ResetOutput(ArchiveHandle* AH, OutputContext savedContext);
267+
extern int RestoringToDB(ArchiveHandle* AH);
268+
extern int ReconnectDatabase(ArchiveHandle *AH, char *newUser);
267269

268270
int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle* AH);
269271
int ahprintf(ArchiveHandle* AH, const char *fmt, ...);

src/bin/pg_dump/pg_backup_db.c

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,26 @@ _prompt_for_password(char *username, char *password)
4848
t;
4949
#endif
5050

51-
fprintf(stderr, "Username: ");
52-
fflush(stderr);
53-
fgets(username, 100, stdin);
54-
length = strlen(username);
55-
/* skip rest of the line */
56-
if (length > 0 && username[length - 1] != '\n')
51+
/*
52+
* Allow for forcing a specific username
53+
*/
54+
if (strlen(username) == 0)
5755
{
58-
do
56+
fprintf(stderr, "Username: ");
57+
fflush(stderr);
58+
fgets(username, 100, stdin);
59+
length = strlen(username);
60+
/* skip rest of the line */
61+
if (length > 0 && username[length - 1] != '\n')
5962
{
60-
fgets(buf, 512, stdin);
61-
} while (buf[strlen(buf) - 1] != '\n');
63+
do
64+
{
65+
fgets(buf, 512, stdin);
66+
} while (buf[strlen(buf) - 1] != '\n');
67+
}
68+
if (length > 0 && username[length - 1] == '\n')
69+
username[length - 1] = '\0';
6270
}
63-
if (length > 0 && username[length - 1] == '\n')
64-
username[length - 1] = '\0';
6571

6672
#ifdef HAVE_TERMIOS_H
6773
tcgetattr(0, &t);
@@ -125,17 +131,69 @@ _check_database_version(ArchiveHandle *AH, bool ignoreVersion)
125131
PQclear(res);
126132
}
127133

134+
int ReconnectDatabase(ArchiveHandle *AH, char *newUser)
135+
{
136+
int need_pass;
137+
PGconn *newConn;
138+
char password[100];
139+
char *pwparam = NULL;
140+
int badPwd = 0;
141+
int noPwd = 0;
142+
143+
ahlog(AH, 1, "Connecting as %s\n", newUser);
144+
145+
do
146+
{
147+
need_pass = false;
148+
newConn = PQsetdbLogin(PQhost(AH->connection), PQport(AH->connection),
149+
NULL, NULL, PQdb(AH->connection),
150+
newUser, pwparam);
151+
if (!newConn)
152+
die_horribly(AH, "%s: Failed to reconnect (PQsetdbLogin failed).\n", progname);
153+
154+
if (PQstatus(newConn) == CONNECTION_BAD)
155+
{
156+
noPwd = (strcmp(PQerrorMessage(newConn), "fe_sendauth: no password supplied\n") == 0);
157+
badPwd = (strncmp(PQerrorMessage(newConn), "Password authentication failed for user", 39)
158+
== 0);
159+
160+
if (noPwd || badPwd)
161+
{
162+
163+
if (badPwd)
164+
fprintf(stderr, "Password incorrect\n");
165+
166+
fprintf(stderr, "Connecting to %s as %s\n", PQdb(AH->connection), newUser);
167+
168+
need_pass = true;
169+
_prompt_for_password(newUser, password);
170+
pwparam = password;
171+
}
172+
else
173+
die_horribly(AH, "%s: Could not reconnect. %s\n", progname, PQerrorMessage(newConn));
174+
}
175+
176+
} while (need_pass);
177+
178+
PQfinish(AH->connection);
179+
AH->connection = newConn;
180+
strcpy(AH->username, newUser);
181+
182+
return 1;
183+
}
184+
185+
128186
PGconn* ConnectDatabase(Archive *AHX,
129187
const char* dbname,
130188
const char* pghost,
131189
const char* pgport,
132-
const int reqPwd,
133-
const int ignoreVersion)
190+
const int reqPwd,
191+
const int ignoreVersion)
134192
{
135193
ArchiveHandle *AH = (ArchiveHandle*)AHX;
136-
char connect_string[512] = "";
137-
char tmp_string[128];
138-
char password[100];
194+
char connect_string[512] = "";
195+
char tmp_string[128];
196+
char password[100];
139197

140198
if (AH->connection)
141199
die_horribly(AH, "%s: already connected to database\n", progname);
@@ -168,6 +226,7 @@ PGconn* ConnectDatabase(Archive *AHX,
168226

169227
if (reqPwd)
170228
{
229+
AH->username[0] = '\0';
171230
_prompt_for_password(AH->username, password);
172231
strcat(connect_string, "authtype=password ");
173232
sprintf(tmp_string, "user=%s ", AH->username);
@@ -188,6 +247,8 @@ PGconn* ConnectDatabase(Archive *AHX,
188247
/* check for version mismatch */
189248
_check_database_version(AH, ignoreVersion);
190249

250+
AH->currUser = PQuser(AH->connection);
251+
191252
return AH->connection;
192253
}
193254

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +0,0 @@
1-
/*-------------------------------------------------------------------------
2-
*
3-
* pg_backup_plain_text.c
4-
*
5-
* This file is copied from the 'custom' format file, but dumps data into
6-
* directly to a text file, and the TOC into the 'main' file.
7-
*
8-
* See the headers to pg_restore for more details.
9-
*
10-
* Copyright (c) 2000, Philip Warner
11-
* Rights are granted to use this software in any way so long
12-
* as this notice is not removed.
13-
*
14-
* The author is not responsible for loss or damages that may
15-
* result from it's use.
16-
*
17-
*
18-
* IDENTIFICATION
19-
*
20-
* Modifications - 01-Jul-2000 - pjw@rhyme.com.au
21-
*
22-
* Initial version.
23-
*
24-
*-------------------------------------------------------------------------
25-
*/
26-
27-
#include <stdlib.h>
28-
#include <string.h>
29-
#include <unistd.h> /* for dup */
30-
#include "pg_backup.h"
31-
#include "pg_backup_archiver.h"
32-
33-
static void _ArchiveEntry(ArchiveHandle* AH, TocEntry* te);
34-
static void _StartData(ArchiveHandle* AH, TocEntry* te);
35-
static int _WriteData(ArchiveHandle* AH, const void* data, int dLen);
36-
static void _EndData(ArchiveHandle* AH, TocEntry* te);
37-
static int _WriteByte(ArchiveHandle* AH, const int i);
38-
static int _WriteBuf(ArchiveHandle* AH, const void* buf, int len);
39-
static void _CloseArchive(ArchiveHandle* AH);
40-
static void _PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt);
41-
42-
/*
43-
* Initializer
44-
*/
45-
void InitArchiveFmt_PlainText(ArchiveHandle* AH)
46-
{
47-
/* Assuming static functions, this can be copied for each format. */
48-
AH->ArchiveEntryPtr = _ArchiveEntry;
49-
AH->StartDataPtr = _StartData;
50-
AH->WriteDataPtr = _WriteData;
51-
AH->EndDataPtr = _EndData;
52-
AH->WriteBytePtr = _WriteByte;
53-
AH->WriteBufPtr = _WriteBuf;
54-
AH->ClosePtr = _CloseArchive;
55-
AH->PrintTocDataPtr = _PrintTocData;
56-
57-
/*
58-
* Now prevent reading...
59-
*/
60-
if (AH->mode == archModeRead)
61-
die_horribly("%s: This format can not be read\n");
62-
63-
}
64-
65-
/*
66-
* - Start a new TOC entry
67-
*/
68-
static void _ArchiveEntry(ArchiveHandle* AH, TocEntry* te)
69-
{
70-
/* Don't need to do anything */
71-
}
72-
73-
static void _StartData(ArchiveHandle* AH, TocEntry* te)
74-
{
75-
ahprintf(AH, "--\n-- Data for TOC Entry ID %d (OID %s) %s %s\n--\n\n",
76-
te->id, te->oid, te->desc, te->name);
77-
}
78-
79-
static int _WriteData(ArchiveHandle* AH, const void* data, int dLen)
80-
{
81-
ahwrite(data, 1, dLen, AH);
82-
return dLen;
83-
}
84-
85-
static void _EndData(ArchiveHandle* AH, TocEntry* te)
86-
{
87-
ahprintf(AH, "\n\n");
88-
}
89-
90-
/*
91-
* Print data for a given TOC entry
92-
*/
93-
static void _PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt)
94-
{
95-
if (*te->dataDumper)
96-
(*te->dataDumper)((Archive*)AH, te->oid, te->dataDumperArg);
97-
}
98-
99-
static int _WriteByte(ArchiveHandle* AH, const int i)
100-
{
101-
/* Don't do anything */
102-
return 0;
103-
}
104-
105-
static int _WriteBuf(ArchiveHandle* AH, const void* buf, int len)
106-
{
107-
/* Don't do anything */
108-
return len;
109-
}
110-
111-
static void _CloseArchive(ArchiveHandle* AH)
112-
{
113-
/* Nothing to do */
114-
}
115-

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy