pg_backup_archiver.c 53.6 KB
Newer Older
B
Bruce Momjian 已提交
1 2 3 4 5 6 7 8 9 10
/*-------------------------------------------------------------------------
 *
 * pg_backup_archiver.c
 *
 *	Private implementation of the archiver routines.
 *
 *	See the headers to pg_restore for more details.
 *
 * Copyright (c) 2000, Philip Warner
 *	Rights are granted to use this software in any way so long
B
Bruce Momjian 已提交
11
 *	as this notice is not removed.
B
Bruce Momjian 已提交
12 13
 *
 *	The author is not responsible for loss or damages that may
14
 *	result from its use.
B
Bruce Momjian 已提交
15 16 17
 *
 *
 * IDENTIFICATION
18
 *		$Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.56 2002/08/27 18:57:26 petere Exp $
19
 *
B
Bruce Momjian 已提交
20 21 22 23
 *-------------------------------------------------------------------------
 */

#include "pg_backup.h"
24
#include "pg_dump.h"
B
Bruce Momjian 已提交
25
#include "pg_backup_archiver.h"
26
#include "pg_backup_db.h"
27
#include "dumputils.h"
28

29
#include <ctype.h>
30
#include <errno.h>
31
#include <unistd.h>
B
Bruce Momjian 已提交
32

33 34
#include "pqexpbuffer.h"
#include "libpq/libpq-fs.h"
35

36

37 38
typedef enum _teReqs_
{
39
	REQ_SCHEMA = 1,
40 41
	REQ_DATA = 2,
	REQ_ALL = REQ_SCHEMA + REQ_DATA
42 43
} teReqs;

B
Bruce Momjian 已提交
44 45 46 47 48
static void _SortToc(ArchiveHandle *AH, TocSortCompareFn fn);
static int	_tocSortCompareByOIDNum(const void *p1, const void *p2);
static int	_tocSortCompareByIDNum(const void *p1, const void *p2);
static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt,
		 const int compression, ArchiveMode mode);
49
static int	_printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData);
50

51
static void _doSetSessionAuth(ArchiveHandle *AH, const char *user);
B
Bruce Momjian 已提交
52
static void _reconnectAsOwner(ArchiveHandle *AH, const char *dbname, TocEntry *te);
53
static void _reconnectAsUser(ArchiveHandle *AH, const char *dbname, const char *user);
54
static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName);
55

56
static teReqs _tocEntryRequired(TocEntry *te, RestoreOptions *ropt);
B
Bruce Momjian 已提交
57 58 59 60 61 62
static void _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
static void _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
static TocEntry *_getTocEntry(ArchiveHandle *AH, int id);
static void _moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te);
static void _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te);
static int	_discoverArchiveFormat(ArchiveHandle *AH);
63
static void _fixupOidInfo(TocEntry *te);
64
static Oid	_findMaxOID(const char *((*deps)[]));
65

66
const char *progname;
67
static char *modulename = gettext_noop("archiver");
B
Bruce Momjian 已提交
68

69 70
static void _write_msg(const char *modulename, const char *fmt, va_list ap);
static void _die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap);
71

B
Bruce Momjian 已提交
72 73
static int	_canRestoreBlobs(ArchiveHandle *AH);
static int	_restoringToDB(ArchiveHandle *AH);
74

B
Bruce Momjian 已提交
75
/*
B
Bruce Momjian 已提交
76 77 78 79
 *	Wrapper functions.
 *
 *	The objective it to make writing new formats and dumpers as simple
 *	as possible, if necessary at the expense of extra function calls etc.
B
Bruce Momjian 已提交
80 81 82 83 84 85
 *
 */


/* Create a new archive */
/* Public */
86
Archive *
B
Bruce Momjian 已提交
87 88
CreateArchive(const char *FileSpec, const ArchiveFormat fmt,
			  const int compression)
89

B
Bruce Momjian 已提交
90
{
B
Bruce Momjian 已提交
91 92 93
	ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, archModeWrite);

	return (Archive *) AH;
B
Bruce Momjian 已提交
94 95 96 97
}

/* Open an existing archive */
/* Public */
98
Archive *
B
Bruce Momjian 已提交
99
OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
B
Bruce Momjian 已提交
100
{
B
Bruce Momjian 已提交
101 102 103
	ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, archModeRead);

	return (Archive *) AH;
B
Bruce Momjian 已提交
104 105 106
}

/* Public */
B
Bruce Momjian 已提交
107 108
void
CloseArchive(Archive *AHX)
B
Bruce Momjian 已提交
109
{
B
Bruce Momjian 已提交
110 111 112 113
	int			res = 0;
	ArchiveHandle *AH = (ArchiveHandle *) AHX;

	(*AH->ClosePtr) (AH);
B
Bruce Momjian 已提交
114

B
Bruce Momjian 已提交
115 116
	/* Close the output */
	if (AH->gzOut)
117
		res = GZCLOSE(AH->OF);
B
Bruce Momjian 已提交
118
	else if (AH->OF != stdout)
119 120 121
		res = fclose(AH->OF);

	if (res != 0)
122
		die_horribly(AH, modulename, "could not close the output file in CloseArchive\n");
B
Bruce Momjian 已提交
123 124 125
}

/* Public */
B
Bruce Momjian 已提交
126 127
void
RestoreArchive(Archive *AHX, RestoreOptions *ropt)
B
Bruce Momjian 已提交
128
{
B
Bruce Momjian 已提交
129 130
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
	TocEntry   *te = AH->toc->next;
131
	teReqs		reqs;
B
Bruce Momjian 已提交
132 133
	OutputContext sav;
	int			impliedDataOnly;
134
	bool		defnDumped;
B
Bruce Momjian 已提交
135

136 137
	AH->ropt = ropt;

138 139 140 141 142
	/*
	 * Check for nonsensical option combinations.
	 *
	 * NB: create+dropSchema is useless because if you're creating the DB,
	 * there's no need to drop individual items in it.  Moreover, if we
143 144 145
	 * tried to do that then we'd issue the drops in the database
	 * initially connected to, not the one we will create, which is very
	 * bad...
146
	 */
147

148
	if (ropt->create && ropt->noReconnect)
149
		die_horribly(AH, modulename, "-C and -R are incompatible options\n");
150

151 152 153
	if (ropt->create && ropt->dropSchema)
		die_horribly(AH, modulename, "-C and -c are incompatible options\n");

154 155 156 157 158
	/*
	 * If we're using a DB connection, then connect it.
	 */
	if (ropt->useDB)
	{
159
		ahlog(AH, 1, "connecting to database for restore\n");
160
		if (AH->version < K_VERS_1_3)
161
			die_horribly(AH, modulename, "direct database connections are not supported in pre-1.3 archives\n");
162

163 164 165 166
		/* XXX Should get this from the archive */
		AHX->minRemoteVersion = 070100;
		AHX->maxRemoteVersion = 999999;

167 168
		ConnectDatabase(AHX, ropt->dbname,
						ropt->pghost, ropt->pgport, ropt->username,
B
Bruce Momjian 已提交
169
						ropt->requirePassword, ropt->ignoreVersion);
170 171
	}

172
	/*
173
	 * Work out if we have an implied data-only restore. This can happen if
B
Bruce Momjian 已提交
174 175 176
	 * the dump was data only or if the user has used a toc list to
	 * exclude all of the schema data. All we do is look for schema
	 * entries - if none are found then we set the dataOnly flag.
177
	 *
B
Bruce Momjian 已提交
178
	 * We could scan for wanted TABLE entries, but that is not the same as
179
	 * dataOnly. At this stage, it seems unnecessary (6-Mar-2001).
B
Bruce Momjian 已提交
180 181 182
	 */
	if (!ropt->dataOnly)
	{
183 184
		te = AH->toc->next;
		impliedDataOnly = 1;
B
Bruce Momjian 已提交
185 186
		while (te != AH->toc)
		{
187
			reqs = _tocEntryRequired(te, ropt);
188
			if ((reqs & REQ_SCHEMA) != 0)
B
Bruce Momjian 已提交
189
			{					/* It's schema, and it's wanted */
190 191 192 193 194 195 196 197
				impliedDataOnly = 0;
				break;
			}
			te = te->next;
		}
		if (impliedDataOnly)
		{
			ropt->dataOnly = impliedDataOnly;
198
			ahlog(AH, 1, "implied data-only restore\n");
199
		}
B
Bruce Momjian 已提交
200
	}
201

202
	/*
B
Bruce Momjian 已提交
203
	 * Setup the output file if necessary.
204
	 */
B
Bruce Momjian 已提交
205
	if (ropt->filename || ropt->compression)
206
		sav = SetOutput(AH, ropt->filename, ropt->compression);
B
Bruce Momjian 已提交
207

208
	ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
B
Bruce Momjian 已提交
209

B
Bruce Momjian 已提交
210 211
	/*
	 * Drop the items at the start, in reverse order
212
	 */
B
Bruce Momjian 已提交
213 214
	if (ropt->dropSchema)
	{
215
		te = AH->toc->prev;
B
Bruce Momjian 已提交
216 217
		while (te != AH->toc)
		{
218
			reqs = _tocEntryRequired(te, ropt);
219
			if (((reqs & REQ_SCHEMA) != 0) && te->dropStmt)
220 221
			{
				/* We want the schema */
222
				ahlog(AH, 1, "dropping %s %s\n", te->desc, te->tag);
223
				/* Select owner and schema as necessary */
224
				_reconnectAsOwner(AH, NULL, te);
225
				_selectOutputSchema(AH, te->namespace);
226
				/* Drop it */
227 228 229 230
				ahprintf(AH, "%s", te->dropStmt);
			}
			te = te->prev;
		}
B
Bruce Momjian 已提交
231
	}
B
Bruce Momjian 已提交
232

233 234 235
	/*
	 * Now process each TOC entry
	 */
B
Bruce Momjian 已提交
236 237 238
	te = AH->toc->next;
	while (te != AH->toc)
	{
239 240 241
		/* Work out what, if anything, we want from this entry */
		reqs = _tocEntryRequired(te, ropt);

242 243 244
		/* Dump any relevant dump warnings to stderr */
		if (!ropt->suppressDumpWarnings && strcmp(te->desc, "WARNING") == 0)
		{
245
			if (!ropt->dataOnly && te->defn != NULL && strlen(te->defn) != 0)
246 247 248
				write_msg(modulename, "warning from original dump file: %s\n", te->defn);
			else if (te->copyStmt != NULL && strlen(te->copyStmt) != 0)
				write_msg(modulename, "warning from original dump file: %s\n", te->copyStmt);
249
		}
250

251 252
		defnDumped = false;

253
		if ((reqs & REQ_SCHEMA) != 0)	/* We want the schema */
254
		{
255
			ahlog(AH, 1, "creating %s %s\n", te->desc, te->tag);
256
			_printTocEntry(AH, te, ropt, false);
257
			defnDumped = true;
258 259

			/* If we created a DB, connect to it... */
B
Bruce Momjian 已提交
260
			if (strcmp(te->desc, "DATABASE") == 0)
261
			{
262 263
				ahlog(AH, 1, "connecting to new database %s as user %s\n", te->tag, te->owner);
				_reconnectAsUser(AH, te->tag, te->owner);
264
			}
265
		}
B
Bruce Momjian 已提交
266

B
Bruce Momjian 已提交
267
		/*
268
		 * If we have a data component, then process it
269
		 */
270
		if ((reqs & REQ_DATA) != 0)
B
Bruce Momjian 已提交
271
		{
272 273 274 275 276
			/*
			 * hadDumper will be set if there is genuine data component
			 * for this node. Otherwise, we need to check the defn field
			 * for statements that need to be executed in data-only
			 * restores.
277
			 */
278
			if (te->hadDumper)
279 280
			{
				/*
281
				 * If we can output the data, then restore it.
282
				 */
283
				if (AH->PrintTocDataPtr !=NULL && (reqs & REQ_DATA) != 0)
284 285 286
				{
#ifndef HAVE_LIBZ
					if (AH->compression != 0)
287
						die_horribly(AH, modulename, "unable to restore from compressed archive (not configured for compression support)\n");
288
#endif
289

290 291 292
					_printTocEntry(AH, te, ropt, true);

					/*
293 294
					 * Maybe we can't do BLOBS, so check if this node is
					 * for BLOBS
295 296 297 298 299 300
					 */
					if ((strcmp(te->desc, "BLOBS") == 0) && !_canRestoreBlobs(AH))
					{
						ahprintf(AH, "--\n-- SKIPPED \n--\n\n");

						/*
301 302 303
						 * This is a bit nasty - we assume, for the
						 * moment, that if a custom output is used, then
						 * we don't want warnings.
304 305
						 */
						if (!AH->CustomOutPtr)
306
							write_msg(modulename, "WARNING: skipping large object restoration\n");
307 308 309 310 311 312 313 314

					}
					else
					{

						_disableTriggersIfNecessary(AH, te, ropt);

						/*
315 316
						 * Reconnect if necessary (_disableTriggers may
						 * have reconnected)
317
						 */
318
						_reconnectAsOwner(AH, NULL, te);
319
						_selectOutputSchema(AH, te->namespace);
320

321
						ahlog(AH, 1, "restoring data for table %s\n", te->tag);
322 323

						/*
324 325 326 327 328
						 * If we have a copy statement, use it. As of
						 * V1.3, these are separate to allow easy import
						 * from withing a database connection. Pre 1.3
						 * archives can not use DB connections and are
						 * sent to output only.
329
						 *
330 331 332
						 * For V1.3+, the table data MUST have a copy
						 * statement so that we can go into appropriate
						 * mode with libpq.
333 334 335 336 337 338 339 340 341
						 */
						if (te->copyStmt && strlen(te->copyStmt) > 0)
							ahprintf(AH, te->copyStmt);

						(*AH->PrintTocDataPtr) (AH, te, ropt);

						_enableTriggersIfNecessary(AH, te, ropt);
					}
				}
342 343 344 345
			}
			else if (!defnDumped)
			{
				/* If we haven't already dumped the defn part, do so now */
346
				ahlog(AH, 1, "executing %s %s\n", te->desc, te->tag);
347
				_printTocEntry(AH, te, ropt, false);
348 349 350
			}
		}
		te = te->next;
B
Bruce Momjian 已提交
351
	}
B
Bruce Momjian 已提交
352

353
	/*
B
Bruce Momjian 已提交
354 355
	 * Now use blobs_xref (if used) to fixup any refs for tables that we
	 * loaded
356 357 358
	 */
	if (_canRestoreBlobs(AH) && AH->createdBlobXref)
	{
359
		/* NULL parameter means disable ALL user triggers */
360
		_disableTriggersIfNecessary(AH, NULL, ropt);
361

362
		te = AH->toc->next;
B
Bruce Momjian 已提交
363 364
		while (te != AH->toc)
		{
365 366

			/* Is it table data? */
B
Bruce Momjian 已提交
367 368
			if (strcmp(te->desc, "TABLE DATA") == 0)
			{
369

370
				ahlog(AH, 2, "checking whether we loaded %s\n", te->tag);
371 372 373

				reqs = _tocEntryRequired(te, ropt);

374
				if ((reqs & REQ_DATA) != 0)		/* We loaded the data */
375
				{
376
					ahlog(AH, 1, "fixing up large object cross-reference for %s\n", te->tag);
377
					FixupBlobRefs(AH, te);
378 379 380
				}
			}
			else
381
				ahlog(AH, 2, "ignoring large object cross-references for %s %s\n", te->desc, te->tag);
382 383 384

			te = te->next;
		}
385 386

		/* NULL parameter means enable ALL user triggers */
387
		_enableTriggersIfNecessary(AH, NULL, ropt);
388 389 390 391 392
	}

	/*
	 * Clean up & we're done.
	 */
B
Bruce Momjian 已提交
393
	if (ropt->filename)
394
		ResetOutput(AH, sav);
B
Bruce Momjian 已提交
395

396 397 398 399
	if (ropt->useDB)
	{
		PQfinish(AH->connection);
		AH->connection = NULL;
400 401 402 403 404 405

		if (AH->blobConnection)
		{
			PQfinish(AH->blobConnection);
			AH->blobConnection = NULL;
		}
406
	}
B
Bruce Momjian 已提交
407 408
}

409 410 411 412
/*
 * Allocate a new RestoreOptions block.
 * This is mainly so we can initialize it, but also for future expansion,
 */
B
Bruce Momjian 已提交
413 414
RestoreOptions *
NewRestoreOptions(void)
B
Bruce Momjian 已提交
415
{
B
Bruce Momjian 已提交
416
	RestoreOptions *opts;
B
Bruce Momjian 已提交
417

B
Bruce Momjian 已提交
418
	opts = (RestoreOptions *) calloc(1, sizeof(RestoreOptions));
B
Bruce Momjian 已提交
419 420

	opts->format = archUnknown;
421
	opts->suppressDumpWarnings = false;
B
Bruce Momjian 已提交
422 423 424 425

	return opts;
}

426 427 428 429
/*
 * Returns true if we're restoring directly to the database (and
 * aren't just making a psql script that can do the restoration).
 */
B
Bruce Momjian 已提交
430 431
static int
_restoringToDB(ArchiveHandle *AH)
432 433 434 435
{
	return (AH->ropt->useDB && AH->connection);
}

B
Bruce Momjian 已提交
436 437
static int
_canRestoreBlobs(ArchiveHandle *AH)
438 439 440 441
{
	return _restoringToDB(AH);
}

B
Bruce Momjian 已提交
442 443
static void
_disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
B
Bruce Momjian 已提交
444
{
445 446
	char	   *oldUser;
	char	   *oldSchema;
447

448 449
	/* This hack is only needed in a data-only restore */
	if (!ropt->dataOnly || !ropt->disable_triggers)
450 451
		return;

452 453 454
	oldUser = strdup(AH->currUser);
	oldSchema = strdup(AH->currSchema);

455
	/*
456 457 458 459
	 * Become superuser if possible, since they are the only ones
	 * who can update pg_class.  If -S was not given, but we are allowed
	 * to use SET SESSION AUTHORIZATION, assume the initial user identity
	 * is a superuser.  Otherwise we just have to bull ahead anyway.
460 461 462
	 */
	if (ropt->superuser)
	{
463 464 465 466 467 468
		_reconnectAsUser(AH, NULL, ropt->superuser);
		/* be careful to preserve schema setting */
		_selectOutputSchema(AH, oldSchema);
	}
	else if (AH->ropt->use_setsessauth)
	{
469
		_doSetSessionAuth(AH, NULL);
470 471
	}

472
	ahlog(AH, 1, "disabling triggers\n");
473 474

	/*
B
Bruce Momjian 已提交
475 476
	 * Disable them. This is a hack. Needs to be done via an appropriate
	 * 'SET' command when one is available.
477
	 */
B
Bruce Momjian 已提交
478
	ahprintf(AH, "-- Disable triggers\n");
479 480

	/*
481 482
	 * Just update the AFFECTED table, if known.  Otherwise update all
	 * non-system tables.
483
	 */
484
	if (te && te->tag && strlen(te->tag) > 0)
485 486
		ahprintf(AH, "UPDATE pg_catalog.pg_class SET reltriggers = 0 "
				 "WHERE oid = '%s'::pg_catalog.regclass;\n\n",
487
				 fmtId(te->tag));
488
	else
489
		ahprintf(AH, "UPDATE pg_catalog.pg_class SET reltriggers = 0 FROM pg_catalog.pg_namespace "
490
				 "WHERE relnamespace = pg_namespace.oid AND nspname !~ '^pg_';\n\n");
491 492

	/*
493
	 * Restore original user and schema state.
494
	 */
495
	if (ropt->superuser)
496
	{
497
		_reconnectAsUser(AH, NULL, oldUser);
498 499
		/* be careful to preserve schema setting */
		_selectOutputSchema(AH, oldSchema);
500
	}
501 502
	else if (AH->ropt->use_setsessauth)
	{
503
		_doSetSessionAuth(AH, oldUser);
504 505 506
	}
	free(oldUser);
	free(oldSchema);
B
Bruce Momjian 已提交
507 508
}

B
Bruce Momjian 已提交
509 510
static void
_enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
B
Bruce Momjian 已提交
511
{
512 513
	char	   *oldUser;
	char	   *oldSchema;
514

515 516
	/* This hack is only needed in a data-only restore */
	if (!ropt->dataOnly || !ropt->disable_triggers)
517 518
		return;

519 520 521
	oldUser = strdup(AH->currUser);
	oldSchema = strdup(AH->currSchema);

522
	/*
523 524 525 526
	 * Become superuser if possible, since they are the only ones
	 * who can update pg_class.  If -S was not given, but we are allowed
	 * to use SET SESSION AUTHORIZATION, assume the initial user identity
	 * is a superuser.  Otherwise we just have to bull ahead anyway.
527 528 529
	 */
	if (ropt->superuser)
	{
530 531 532 533 534 535
		_reconnectAsUser(AH, NULL, ropt->superuser);
		/* be careful to preserve schema setting */
		_selectOutputSchema(AH, oldSchema);
	}
	else if (AH->ropt->use_setsessauth)
	{
536
		_doSetSessionAuth(AH, NULL);
537 538
	}

539
	ahlog(AH, 1, "enabling triggers\n");
540 541

	/*
B
Bruce Momjian 已提交
542 543
	 * Enable them. This is a hack. Needs to be done via an appropriate
	 * 'SET' command when one is available.
544
	 */
B
Bruce Momjian 已提交
545
	ahprintf(AH, "-- Enable triggers\n");
546 547

	/*
548 549
	 * Just update the AFFECTED table, if known.  Otherwise update all
	 * non-system tables.
550
	 */
551
	if (te && te->tag && strlen(te->tag) > 0)
552
		ahprintf(AH, "UPDATE pg_catalog.pg_class SET reltriggers = "
553
				 "(SELECT pg_catalog.count(*) FROM pg_catalog.pg_trigger where pg_class.oid = tgrelid) "
554
				 "WHERE oid = '%s'::pg_catalog.regclass;\n\n",
555
				 fmtId(te->tag));
B
Bruce Momjian 已提交
556
	else
557
		ahprintf(AH, "UPDATE pg_catalog.pg_class SET reltriggers = "
558
				 "(SELECT pg_catalog.count(*) FROM pg_catalog.pg_trigger where pg_class.oid = tgrelid) "
559
				 "FROM pg_catalog.pg_namespace "
560
				 "WHERE relnamespace = pg_namespace.oid AND nspname !~ '^pg_';\n\n");
B
Bruce Momjian 已提交
561

562
	/*
563
	 * Restore original user and schema state.
564
	 */
565
	if (ropt->superuser)
566
	{
567
		_reconnectAsUser(AH, NULL, oldUser);
568 569
		/* be careful to preserve schema setting */
		_selectOutputSchema(AH, oldSchema);
570
	}
571 572
	else if (AH->ropt->use_setsessauth)
	{
573
		_doSetSessionAuth(AH, oldUser);
574 575 576
	}
	free(oldUser);
	free(oldSchema);
577
}
B
Bruce Momjian 已提交
578 579

/*
580
 * This is a routine that is part of the dumper interface, hence the 'Archive*' parameter.
B
Bruce Momjian 已提交
581 582 583
 */

/* Public */
P
Peter Eisentraut 已提交
584 585
size_t
WriteData(Archive *AHX, const void *data, size_t dLen)
B
Bruce Momjian 已提交
586
{
B
Bruce Momjian 已提交
587
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
B
Bruce Momjian 已提交
588

589
	if (!AH->currToc)
590
		die_horribly(AH, modulename, "WriteData cannot be called outside the context of a DataDumper routine\n");
591

B
Bruce Momjian 已提交
592
	return (*AH->WriteDataPtr) (AH, data, dLen);
B
Bruce Momjian 已提交
593 594 595
}

/*
B
Bruce Momjian 已提交
596
 * Create a new TOC entry. The TOC was designed as a TOC, but is now the
B
Bruce Momjian 已提交
597 598 599 600
 * repository for all metadata. But the name has stuck.
 */

/* Public */
B
Bruce Momjian 已提交
601
void
602
ArchiveEntry(Archive *AHX, const char *oid, const char *tag,
603 604 605 606
			 const char *namespace, const char *owner,
			 const char *desc, const char *((*deps)[]),
			 const char *defn, const char *dropStmt,
			 const char *copyStmt,
B
Bruce Momjian 已提交
607
			 DataDumperPtr dumpFn, void *dumpArg)
B
Bruce Momjian 已提交
608
{
B
Bruce Momjian 已提交
609 610 611 612 613 614 615 616
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
	TocEntry   *newToc;

	AH->lastID++;
	AH->tocCount++;

	newToc = (TocEntry *) calloc(1, sizeof(TocEntry));
	if (!newToc)
617
		die_horribly(AH, modulename, "out of memory\n");
B
Bruce Momjian 已提交
618 619 620 621 622 623 624

	newToc->prev = AH->toc->prev;
	newToc->next = AH->toc;
	AH->toc->prev->next = newToc;
	AH->toc->prev = newToc;

	newToc->id = AH->lastID;
625

626
	newToc->tag = strdup(tag);
627 628
	newToc->namespace = namespace ? strdup(namespace) : NULL;
	newToc->owner = strdup(owner);
B
Bruce Momjian 已提交
629
	newToc->desc = strdup(desc);
630 631 632
	newToc->defn = strdup(defn);
	newToc->dropStmt = strdup(dropStmt);
	newToc->copyStmt = copyStmt ? strdup(copyStmt) : NULL;
633 634

	newToc->oid = strdup(oid);
635
	newToc->depOid = deps;		/* NB: not copied */
636 637
	_fixupOidInfo(newToc);

B
Bruce Momjian 已提交
638 639
	newToc->printed = 0;
	newToc->formatData = NULL;
640 641
	newToc->dataDumper = dumpFn;
	newToc->dataDumperArg = dumpArg;
B
Bruce Momjian 已提交
642

B
Bruce Momjian 已提交
643
	newToc->hadDumper = dumpFn ? 1 : 0;
B
Bruce Momjian 已提交
644

B
Bruce Momjian 已提交
645 646
	if (AH->ArchiveEntryPtr !=NULL)
		(*AH->ArchiveEntryPtr) (AH, newToc);
B
Bruce Momjian 已提交
647

B
Bruce Momjian 已提交
648
	/*
P
Peter Eisentraut 已提交
649
	 * printf("New toc owned by '%s', oid %u\n", newToc->owner,
B
Bruce Momjian 已提交
650 651
	 * newToc->oidVal);
	 */
B
Bruce Momjian 已提交
652 653 654
}

/* Public */
B
Bruce Momjian 已提交
655 656
void
PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
B
Bruce Momjian 已提交
657
{
B
Bruce Momjian 已提交
658 659 660 661
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
	TocEntry   *te = AH->toc->next;
	OutputContext sav;
	char	   *fmtName;
B
Bruce Momjian 已提交
662

B
Bruce Momjian 已提交
663 664
	if (ropt->filename)
		sav = SetOutput(AH, ropt->filename, ropt->compression);
B
Bruce Momjian 已提交
665

666 667
	ahprintf(AH, ";\n; Archive created at %s", ctime(&AH->createDate));
	ahprintf(AH, ";     dbname: %s\n;     TOC Entries: %d\n;     Compression: %d\n",
B
Bruce Momjian 已提交
668
			 AH->archdbname, AH->tocCount, AH->compression);
669

B
Bruce Momjian 已提交
670 671
	switch (AH->format)
	{
672 673 674 675 676 677 678 679 680 681 682 683
		case archFiles:
			fmtName = "FILES";
			break;
		case archCustom:
			fmtName = "CUSTOM";
			break;
		case archTar:
			fmtName = "TAR";
			break;
		default:
			fmtName = "UNKNOWN";
	}
684 685

	ahprintf(AH, ";     Dump Version: %d.%d-%d\n", AH->vmaj, AH->vmin, AH->vrev);
686 687
	ahprintf(AH, ";     Format: %s\n;\n", fmtName);

B
Bruce Momjian 已提交
688
	ahprintf(AH, ";\n; Selected TOC Entries:\n;\n");
B
Bruce Momjian 已提交
689

B
Bruce Momjian 已提交
690 691 692
	while (te != AH->toc)
	{
		if (_tocEntryRequired(te, ropt) != 0)
693
			ahprintf(AH, "%d; %d %s %s %s\n", te->id, te->oidVal, te->desc, te->tag, te->owner);
B
Bruce Momjian 已提交
694
		te = te->next;
B
Bruce Momjian 已提交
695
	}
B
Bruce Momjian 已提交
696

B
Bruce Momjian 已提交
697 698
	if (ropt->filename)
		ResetOutput(AH, sav);
B
Bruce Momjian 已提交
699 700
}

701 702 703 704 705
/***********
 * BLOB Archival
 ***********/

/* Called by a dumper to signal start of a BLOB */
B
Bruce Momjian 已提交
706
int
707
StartBlob(Archive *AHX, Oid oid)
708
{
B
Bruce Momjian 已提交
709
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
710

B
Bruce Momjian 已提交
711
	if (!AH->StartBlobPtr)
712
		die_horribly(AH, modulename, "large object output not supported in chosen format\n");
713

B
Bruce Momjian 已提交
714
	(*AH->StartBlobPtr) (AH, AH->currToc, oid);
715

B
Bruce Momjian 已提交
716
	return 1;
717 718 719
}

/* Called by a dumper to signal end of a BLOB */
B
Bruce Momjian 已提交
720
int
721
EndBlob(Archive *AHX, Oid oid)
722
{
B
Bruce Momjian 已提交
723
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
724

B
Bruce Momjian 已提交
725 726
	if (AH->EndBlobPtr)
		(*AH->EndBlobPtr) (AH, AH->currToc, oid);
727

B
Bruce Momjian 已提交
728
	return 1;
729 730 731 732 733 734
}

/**********
 * BLOB Restoration
 **********/

735
/*
B
Bruce Momjian 已提交
736
 * Called by a format handler before any blobs are restored
737
 */
B
Bruce Momjian 已提交
738 739
void
StartRestoreBlobs(ArchiveHandle *AH)
740 741 742 743 744
{
	AH->blobCount = 0;
}

/*
B
Bruce Momjian 已提交
745
 * Called by a format handler after all blobs are restored
746
 */
B
Bruce Momjian 已提交
747 748
void
EndRestoreBlobs(ArchiveHandle *AH)
749 750 751
{
	if (AH->txActive)
	{
752
		ahlog(AH, 2, "committing large object transactions\n");
753 754 755 756 757 758
		CommitTransaction(AH);
	}

	if (AH->blobTxActive)
		CommitTransactionXref(AH);

759
	ahlog(AH, 1, "restored %d large objects\n", AH->blobCount);
760 761 762
}


763 764 765
/*
 * Called by a format handler to initiate restoration of a blob
 */
B
Bruce Momjian 已提交
766
void
767
StartRestoreBlob(ArchiveHandle *AH, Oid oid)
768
{
769
	Oid			loOid;
770

771 772
	AH->blobCount++;

773 774 775
	if (!AH->createdBlobXref)
	{
		if (!AH->connection)
776
			die_horribly(AH, modulename, "cannot restore large objects without a database connection\n");
777 778 779 780 781

		CreateBlobXrefTable(AH);
		AH->createdBlobXref = 1;
	}

782 783 784
	/* Initialize the LO Buffer */
	AH->lo_buf_used = 0;

785 786 787 788 789
	/*
	 * Start long-running TXs if necessary
	 */
	if (!AH->txActive)
	{
790
		ahlog(AH, 2, "starting large object transactions\n");
791 792 793 794
		StartTransaction(AH);
	}
	if (!AH->blobTxActive)
		StartTransactionXref(AH);
795

796 797
	loOid = lo_creat(AH->connection, INV_READ | INV_WRITE);
	if (loOid == 0)
798
		die_horribly(AH, modulename, "could not create large object\n");
799

800
	ahlog(AH, 2, "restoring large object with oid %u as %u\n", oid, loOid);
801 802 803 804 805

	InsertBlobXref(AH, oid, loOid);

	AH->loFd = lo_open(AH->connection, loOid, INV_WRITE);
	if (AH->loFd == -1)
806
		die_horribly(AH, modulename, "could not open large object\n");
807

B
Bruce Momjian 已提交
808
	AH->writingBlob = 1;
809 810
}

B
Bruce Momjian 已提交
811
void
812
EndRestoreBlob(ArchiveHandle *AH, Oid oid)
813
{
814 815 816
	if (AH->lo_buf_used > 0)
	{
		/* Write remaining bytes from the LO buffer */
P
Peter Eisentraut 已提交
817
		size_t res;
818 819 820

		res = lo_write(AH->connection, AH->loFd, (void *) AH->lo_buf, AH->lo_buf_used);

P
Peter Eisentraut 已提交
821 822
		ahlog(AH, 5, "wrote remaining %lu bytes of large object data (result = %lu)\n",
			  (unsigned long) AH->lo_buf_used, (unsigned long) res);
823
		if (res != AH->lo_buf_used)
P
Peter Eisentraut 已提交
824 825
			die_horribly(AH, modulename, "could not write to large object (result: %lu, expected: %lu)\n",
						 (unsigned long) res, (unsigned long) AH->lo_buf_used);
826 827
		AH->lo_buf_used = 0;
	}
828

B
Bruce Momjian 已提交
829 830
	lo_close(AH->connection, AH->loFd);
	AH->writingBlob = 0;
831

832 833 834
	/*
	 * Commit every BLOB_BATCH_SIZE blobs...
	 */
B
Bruce Momjian 已提交
835
	if (((AH->blobCount / BLOB_BATCH_SIZE) * BLOB_BATCH_SIZE) == AH->blobCount)
836
	{
837
		ahlog(AH, 2, "committing large object transactions\n");
838 839 840
		CommitTransaction(AH);
		CommitTransactionXref(AH);
	}
841 842
}

B
Bruce Momjian 已提交
843 844 845 846 847 848 849
/***********
 * Sorting and Reordering
 ***********/

/*
 * Move TOC entries of the specified type to the START of the TOC.
 */
850

B
Bruce Momjian 已提交
851
/* Public */
B
Bruce Momjian 已提交
852 853
void
MoveToStart(Archive *AHX, char *oType)
B
Bruce Momjian 已提交
854
{
B
Bruce Momjian 已提交
855 856 857
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
	TocEntry   *te = AH->toc->next;
	TocEntry   *newTe;
B
Bruce Momjian 已提交
858

B
Bruce Momjian 已提交
859 860
	while (te != AH->toc)
	{
B
Bruce Momjian 已提交
861 862 863
		te->_moved = 0;
		te = te->next;
	}
B
Bruce Momjian 已提交
864 865 866 867 868 869 870

	te = AH->toc->prev;
	while (te != AH->toc && !te->_moved)
	{
		newTe = te->prev;
		if (strcmp(te->desc, oType) == 0)
			_moveAfter(AH, AH->toc, te);
B
Bruce Momjian 已提交
871
		te = newTe;
B
Bruce Momjian 已提交
872
	}
B
Bruce Momjian 已提交
873 874 875 876 877 878 879
}


/*
 * Move TOC entries of the specified type to the end of the TOC.
 */
/* Public */
B
Bruce Momjian 已提交
880 881
void
MoveToEnd(Archive *AHX, char *oType)
B
Bruce Momjian 已提交
882
{
B
Bruce Momjian 已提交
883 884 885 886 887 888 889 890
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
	TocEntry   *te = AH->toc->next;
	TocEntry   *newTe;

	while (te != AH->toc)
	{
		te->_moved = 0;
		te = te->next;
B
Bruce Momjian 已提交
891
	}
B
Bruce Momjian 已提交
892 893 894 895 896 897 898

	te = AH->toc->next;
	while (te != AH->toc && !te->_moved)
	{
		newTe = te->next;
		if (strcmp(te->desc, oType) == 0)
			_moveBefore(AH, AH->toc, te);
B
Bruce Momjian 已提交
899
		te = newTe;
B
Bruce Momjian 已提交
900
	}
B
Bruce Momjian 已提交
901 902
}

B
Bruce Momjian 已提交
903
/*
B
Bruce Momjian 已提交
904 905 906
 * Sort TOC by OID
 */
/* Public */
B
Bruce Momjian 已提交
907 908
void
SortTocByOID(Archive *AHX)
B
Bruce Momjian 已提交
909
{
B
Bruce Momjian 已提交
910 911 912
	ArchiveHandle *AH = (ArchiveHandle *) AHX;

	_SortToc(AH, _tocSortCompareByOIDNum);
B
Bruce Momjian 已提交
913 914 915 916 917 918
}

/*
 * Sort TOC by ID
 */
/* Public */
B
Bruce Momjian 已提交
919 920
void
SortTocByID(Archive *AHX)
B
Bruce Momjian 已提交
921
{
B
Bruce Momjian 已提交
922 923 924
	ArchiveHandle *AH = (ArchiveHandle *) AHX;

	_SortToc(AH, _tocSortCompareByIDNum);
B
Bruce Momjian 已提交
925 926
}

B
Bruce Momjian 已提交
927 928
void
SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
B
Bruce Momjian 已提交
929
{
B
Bruce Momjian 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
	ArchiveHandle *AH = (ArchiveHandle *) AHX;
	FILE	   *fh;
	char		buf[1024];
	char	   *cmnt;
	char	   *endptr;
	int			id;
	TocEntry   *te;
	TocEntry   *tePrev;
	int			i;

	/* Allocate space for the 'wanted' array, and init it */
	ropt->idWanted = (int *) malloc(sizeof(int) * AH->tocCount);
	for (i = 0; i < AH->tocCount; i++)
		ropt->idWanted[i] = 0;

	ropt->limitToList = 1;

	/* Mark all entries as 'not moved' */
	te = AH->toc->next;
	while (te != AH->toc)
	{
		te->_moved = 0;
		te = te->next;
	}
B
Bruce Momjian 已提交
954

B
Bruce Momjian 已提交
955 956
	/* Set prev entry as head of list */
	tePrev = AH->toc;
B
Bruce Momjian 已提交
957

B
Bruce Momjian 已提交
958 959 960
	/* Setup the file */
	fh = fopen(ropt->tocFile, PG_BINARY_R);
	if (!fh)
961
		die_horribly(AH, modulename, "could not open TOC file\n");
B
Bruce Momjian 已提交
962

B
Bruce Momjian 已提交
963
	while (fgets(buf, 1024, fh) != NULL)
B
Bruce Momjian 已提交
964
	{
B
Bruce Momjian 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
		/* Find a comment */
		cmnt = strchr(buf, ';');
		if (cmnt == buf)
			continue;

		/* End string at comment */
		if (cmnt != NULL)
			cmnt[0] = '\0';

		/* Skip if all spaces */
		if (strspn(buf, " \t") == strlen(buf))
			continue;

		/* Get an ID */
		id = strtol(buf, &endptr, 10);
		if (endptr == buf)
		{
982
			write_msg(modulename, "WARNING: line ignored: %s\n", buf);
B
Bruce Momjian 已提交
983 984
			continue;
		}
B
Bruce Momjian 已提交
985

B
Bruce Momjian 已提交
986 987 988
		/* Find TOC entry */
		te = _getTocEntry(AH, id);
		if (!te)
989
			die_horribly(AH, modulename, "could not find entry for id %d\n", id);
B
Bruce Momjian 已提交
990

B
Bruce Momjian 已提交
991
		ropt->idWanted[id - 1] = 1;
B
Bruce Momjian 已提交
992

B
Bruce Momjian 已提交
993 994 995
		_moveAfter(AH, tePrev, te);
		tePrev = te;
	}
B
Bruce Momjian 已提交
996

B
Bruce Momjian 已提交
997
	if (fclose(fh) != 0)
998
		die_horribly(AH, modulename, "could not close TOC file: %s\n", strerror(errno));
B
Bruce Momjian 已提交
999 1000 1001 1002 1003 1004 1005 1006
}

/**********************
 * 'Convenience functions that look like standard IO functions
 * for writing data when in dump mode.
 **********************/

/* Public */
B
Bruce Momjian 已提交
1007 1008 1009 1010
int
archputs(const char *s, Archive *AH)
{
	return WriteData(AH, s, strlen(s));
B
Bruce Momjian 已提交
1011 1012 1013
}

/* Public */
B
Bruce Momjian 已提交
1014 1015 1016 1017
int
archputc(const char c, Archive *AH)
{
	return WriteData(AH, &c, 1);
B
Bruce Momjian 已提交
1018 1019 1020
}

/* Public */
B
Bruce Momjian 已提交
1021 1022
int
archprintf(Archive *AH, const char *fmt,...)
B
Bruce Momjian 已提交
1023
{
B
Bruce Momjian 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
	char	   *p = NULL;
	va_list		ap;
	int			bSize = strlen(fmt) + 256;
	int			cnt = -1;

	/*
	 * This is paranoid: deal with the possibility that vsnprintf is
	 * willing to ignore trailing null
	 */

	/*
	 * or returns > 0 even if string does not fit. It may be the case that
	 * it returns cnt = bufsize
	 */
	while (cnt < 0 || cnt >= (bSize - 1))
B
Bruce Momjian 已提交
1039
	{
B
Bruce Momjian 已提交
1040 1041
		if (p != NULL)
			free(p);
1042
		bSize *= 2;
B
Bruce Momjian 已提交
1043
		p = (char *) malloc(bSize);
1044
		if (p == NULL)
1045
			exit_horribly(AH, modulename, "out of memory\n");
1046 1047 1048
		va_start(ap, fmt);
		cnt = vsnprintf(p, bSize, fmt, ap);
		va_end(ap);
B
Bruce Momjian 已提交
1049 1050 1051 1052
	}
	WriteData(AH, p, cnt);
	free(p);
	return cnt;
B
Bruce Momjian 已提交
1053 1054 1055 1056 1057 1058 1059
}


/*******************************
 * Stuff below here should be 'private' to the archiver routines
 *******************************/

B
Bruce Momjian 已提交
1060 1061
OutputContext
SetOutput(ArchiveHandle *AH, char *filename, int compression)
B
Bruce Momjian 已提交
1062
{
B
Bruce Momjian 已提交
1063 1064
	OutputContext sav;

1065
#ifdef HAVE_LIBZ
B
Bruce Momjian 已提交
1066
	char		fmode[10];
B
Bruce Momjian 已提交
1067
#endif
B
Bruce Momjian 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	int			fn = 0;

	/* Replace the AH output file handle */
	sav.OF = AH->OF;
	sav.gzOut = AH->gzOut;

	if (filename)
		fn = 0;
	else if (AH->FH)
		fn = fileno(AH->FH);
	else if (AH->fSpec)
	{
		fn = 0;
		filename = AH->fSpec;
	}
	else
		fn = fileno(stdout);

	/* If compression explicitly requested, use gzopen */
1087
#ifdef HAVE_LIBZ
B
Bruce Momjian 已提交
1088 1089
	if (compression != 0)
	{
1090
		sprintf(fmode, "wb%d", compression);
B
Bruce Momjian 已提交
1091 1092 1093 1094
		if (fn)
		{
			AH->OF = gzdopen(dup(fn), fmode);	/* Don't use PG_BINARY_x
												 * since this is zlib */
1095
		}
B
Bruce Momjian 已提交
1096 1097
		else
			AH->OF = gzopen(filename, fmode);
1098
		AH->gzOut = 1;
B
Bruce Momjian 已提交
1099 1100 1101
	}
	else
	{							/* Use fopen */
B
Bruce Momjian 已提交
1102
#endif
B
Bruce Momjian 已提交
1103
		if (fn)
1104
			AH->OF = fdopen(dup(fn), PG_BINARY_W);
B
Bruce Momjian 已提交
1105
		else
1106 1107
			AH->OF = fopen(filename, PG_BINARY_W);
		AH->gzOut = 0;
1108
#ifdef HAVE_LIBZ
B
Bruce Momjian 已提交
1109
	}
B
Bruce Momjian 已提交
1110 1111
#endif

1112
	if (!AH->OF)
1113
		die_horribly(AH, modulename, "could not open output file: %s\n", strerror(errno));
1114

B
Bruce Momjian 已提交
1115
	return sav;
B
Bruce Momjian 已提交
1116 1117
}

B
Bruce Momjian 已提交
1118 1119
void
ResetOutput(ArchiveHandle *AH, OutputContext sav)
B
Bruce Momjian 已提交
1120
{
B
Bruce Momjian 已提交
1121
	int			res;
1122

B
Bruce Momjian 已提交
1123
	if (AH->gzOut)
1124
		res = GZCLOSE(AH->OF);
B
Bruce Momjian 已提交
1125
	else
1126 1127 1128
		res = fclose(AH->OF);

	if (res != 0)
1129
		die_horribly(AH, modulename, "could not close output file: %s\n", strerror(errno));
B
Bruce Momjian 已提交
1130

B
Bruce Momjian 已提交
1131 1132
	AH->gzOut = sav.gzOut;
	AH->OF = sav.OF;
B
Bruce Momjian 已提交
1133 1134 1135 1136 1137
}



/*
B
Bruce Momjian 已提交
1138
 *	Print formatted text to the output file (usually stdout).
B
Bruce Momjian 已提交
1139
 */
B
Bruce Momjian 已提交
1140 1141
int
ahprintf(ArchiveHandle *AH, const char *fmt,...)
B
Bruce Momjian 已提交
1142
{
B
Bruce Momjian 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
	char	   *p = NULL;
	va_list		ap;
	int			bSize = strlen(fmt) + 256;		/* Should be enough */
	int			cnt = -1;

	/*
	 * This is paranoid: deal with the possibility that vsnprintf is
	 * willing to ignore trailing null
	 */

	/*
	 * or returns > 0 even if string does not fit. It may be the case that
	 * it returns cnt = bufsize
	 */
	while (cnt < 0 || cnt >= (bSize - 1))
1158
	{
B
Bruce Momjian 已提交
1159 1160
		if (p != NULL)
			free(p);
1161
		bSize *= 2;
B
Bruce Momjian 已提交
1162
		p = (char *) malloc(bSize);
1163
		if (p == NULL)
1164
			die_horribly(AH, modulename, "out of memory\n");
1165
		va_start(ap, fmt);
1166
		cnt = vsnprintf(p, bSize, fmt, ap);
1167
		va_end(ap);
B
Bruce Momjian 已提交
1168 1169 1170 1171
	}
	ahwrite(p, 1, cnt, AH);
	free(p);
	return cnt;
B
Bruce Momjian 已提交
1172 1173
}

B
Bruce Momjian 已提交
1174 1175
void
ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
1176 1177 1178 1179 1180 1181 1182
{
	va_list		ap;

	if (AH->debugLevel < level && (!AH->public.verbose || level > 1))
		return;

	va_start(ap, fmt);
1183
	_write_msg(NULL, fmt, ap);
1184 1185 1186
	va_end(ap);
}

1187 1188 1189
/*
 * Single place for logic which says 'We are restoring to a direct DB connection'.
 */
B
Bruce Momjian 已提交
1190 1191
int
RestoringToDB(ArchiveHandle *AH)
1192 1193 1194 1195
{
	return (AH->ropt && AH->ropt->useDB && AH->connection);
}

B
Bruce Momjian 已提交
1196
/*
B
Bruce Momjian 已提交
1197 1198 1199
 *	Write buffer to the output file (usually stdout). This is user for
 *	outputting 'restore' scripts etc. It is even possible for an archive
 *	format to create a custom output routine to 'fake' a restore if it
1200
 *	wants to generate a script (see TAR output).
B
Bruce Momjian 已提交
1201
 */
B
Bruce Momjian 已提交
1202 1203
int
ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
B
Bruce Momjian 已提交
1204
{
P
Peter Eisentraut 已提交
1205
	size_t		res;
1206

B
Bruce Momjian 已提交
1207
	if (AH->writingBlob)
1208
	{
P
Peter Eisentraut 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
		if (AH->lo_buf_used + size * nmemb > AH->lo_buf_size)
		{
			/* Split LO buffer */
			size_t remaining = AH->lo_buf_size - AH->lo_buf_used;
			size_t slack = nmemb * size - remaining;

			memcpy((char *)AH->lo_buf + AH->lo_buf_used, ptr, remaining);
			res = lo_write(AH->connection, AH->loFd, AH->lo_buf, AH->lo_buf_size);
			ahlog(AH, 5, "wrote %lu bytes of large object data (result = %lu)\n",
				  (unsigned long) AH->lo_buf_size, (unsigned long) res);
			if (res != AH->lo_buf_size)
				die_horribly(AH, modulename,
							 "could not write to large object (result: %lu, expected: %lu)\n",
							 (unsigned long) res, (unsigned long) AH->lo_buf_size);
			memcpy(AH->lo_buf, (char *)ptr + remaining, slack);
			AH->lo_buf_used = slack;
		}
		else
		{
			/* LO Buffer is still large enough, buffer it */
			memcpy((char *)AH->lo_buf + AH->lo_buf_used, ptr, size * nmemb);
			AH->lo_buf_used += size * nmemb;
		}

		return size * nmemb;
1234
	}
B
Bruce Momjian 已提交
1235
	else if (AH->gzOut)
1236
	{
B
Bruce Momjian 已提交
1237
		res = GZWRITE((void *) ptr, size, nmemb, AH->OF);
1238
		if (res != (nmemb * size))
1239
			die_horribly(AH, modulename, "could not write to compressed archive\n");
1240 1241
		return res;
	}
B
Bruce Momjian 已提交
1242
	else if (AH->CustomOutPtr)
1243
	{
B
Bruce Momjian 已提交
1244 1245
		res = AH->CustomOutPtr (AH, ptr, size * nmemb);

1246
		if (res != (nmemb * size))
1247
			die_horribly(AH, modulename, "could not write to custom output routine\n");
1248 1249
		return res;
	}
1250 1251 1252
	else
	{
		/*
B
Bruce Momjian 已提交
1253 1254 1255
		 * If we're doing a restore, and it's direct to DB, and we're
		 * connected then send it to the DB.
		 */
1256
		if (RestoringToDB(AH))
B
Bruce Momjian 已提交
1257
			return ExecuteSqlCommandBuf(AH, (void *) ptr, size * nmemb);		/* Always 1, currently */
1258
		else
1259
		{
B
Bruce Momjian 已提交
1260
			res = fwrite((void *) ptr, size, nmemb, AH->OF);
1261
			if (res != nmemb)
P
Peter Eisentraut 已提交
1262 1263
				die_horribly(AH, modulename, "could not write to output file (%lu != %lu)\n",
							 (unsigned long) res, (unsigned long) nmemb);
1264 1265
			return res;
		}
1266
	}
B
Bruce Momjian 已提交
1267
}
1268 1269

/* Common exit code */
B
Bruce Momjian 已提交
1270
static void
1271
_write_msg(const char *modulename, const char *fmt, va_list ap)
1272
{
1273
	if (modulename)
1274
		fprintf(stderr, "%s: [%s] ", progname, gettext(modulename));
1275 1276 1277 1278 1279 1280
	else
		fprintf(stderr, "%s: ", progname);
	vfprintf(stderr, gettext(fmt), ap);
}

void
1281
write_msg(const char *modulename, const char *fmt,...)
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
{
	va_list		ap;

	va_start(ap, fmt);
	_write_msg(modulename, fmt, ap);
	va_end(ap);
}


static void
_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt, va_list ap)
{
	_write_msg(modulename, fmt, ap);
1295 1296
	if (AH->public.verbose)
		write_msg(NULL, "*** aborted because of error\n");
1297

B
Bruce Momjian 已提交
1298 1299 1300
	if (AH)
		if (AH->connection)
			PQfinish(AH->connection);
1301 1302
	if (AH->blobConnection)
		PQfinish(AH->blobConnection);
1303

B
Bruce Momjian 已提交
1304
	exit(1);
B
Bruce Momjian 已提交
1305 1306
}

1307
/* External use */
B
Bruce Momjian 已提交
1308
void
1309
exit_horribly(Archive *AH, const char *modulename, const char *fmt,...)
1310
{
B
Bruce Momjian 已提交
1311
	va_list		ap;
1312

B
Bruce Momjian 已提交
1313
	va_start(ap, fmt);
1314
	_die_horribly((ArchiveHandle *) AH, modulename, fmt, ap);
1315
}
B
Bruce Momjian 已提交
1316

1317
/* Archiver use (just different arg declaration) */
B
Bruce Momjian 已提交
1318
void
1319
die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
B
Bruce Momjian 已提交
1320
{
B
Bruce Momjian 已提交
1321
	va_list		ap;
B
Bruce Momjian 已提交
1322

B
Bruce Momjian 已提交
1323
	va_start(ap, fmt);
1324
	_die_horribly(AH, modulename, fmt, ap);
B
Bruce Momjian 已提交
1325 1326
}

1327

B
Bruce Momjian 已提交
1328 1329
static void
_moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
B
Bruce Momjian 已提交
1330
{
B
Bruce Momjian 已提交
1331 1332
	te->prev->next = te->next;
	te->next->prev = te->prev;
B
Bruce Momjian 已提交
1333

B
Bruce Momjian 已提交
1334 1335
	te->prev = pos;
	te->next = pos->next;
B
Bruce Momjian 已提交
1336

B
Bruce Momjian 已提交
1337 1338
	pos->next->prev = te;
	pos->next = te;
B
Bruce Momjian 已提交
1339

B
Bruce Momjian 已提交
1340
	te->_moved = 1;
B
Bruce Momjian 已提交
1341 1342
}

B
Bruce Momjian 已提交
1343 1344
static void
_moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
B
Bruce Momjian 已提交
1345
{
B
Bruce Momjian 已提交
1346 1347
	te->prev->next = te->next;
	te->next->prev = te->prev;
B
Bruce Momjian 已提交
1348

B
Bruce Momjian 已提交
1349 1350 1351 1352
	te->prev = pos->prev;
	te->next = pos;
	pos->prev->next = te;
	pos->prev = te;
B
Bruce Momjian 已提交
1353

B
Bruce Momjian 已提交
1354
	te->_moved = 1;
B
Bruce Momjian 已提交
1355 1356
}

B
Bruce Momjian 已提交
1357 1358
static TocEntry *
_getTocEntry(ArchiveHandle *AH, int id)
B
Bruce Momjian 已提交
1359
{
B
Bruce Momjian 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
	TocEntry   *te;

	te = AH->toc->next;
	while (te != AH->toc)
	{
		if (te->id == id)
			return te;
		te = te->next;
	}
	return NULL;
B
Bruce Momjian 已提交
1370 1371
}

B
Bruce Momjian 已提交
1372 1373
int
TocIDRequired(ArchiveHandle *AH, int id, RestoreOptions *ropt)
B
Bruce Momjian 已提交
1374
{
B
Bruce Momjian 已提交
1375
	TocEntry   *te = _getTocEntry(AH, id);
B
Bruce Momjian 已提交
1376

B
Bruce Momjian 已提交
1377 1378
	if (!te)
		return 0;
B
Bruce Momjian 已提交
1379

B
Bruce Momjian 已提交
1380
	return _tocEntryRequired(te, ropt);
B
Bruce Momjian 已提交
1381 1382
}

P
Peter Eisentraut 已提交
1383
size_t
B
Bruce Momjian 已提交
1384
WriteInt(ArchiveHandle *AH, int i)
B
Bruce Momjian 已提交
1385
{
B
Bruce Momjian 已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
	int			b;

	/*
	 * This is a bit yucky, but I don't want to make the binary format
	 * very dependant on representation, and not knowing much about it, I
	 * write out a sign byte. If you change this, don't forget to change
	 * the file version #, and modify readInt to read the new format AS
	 * WELL AS the old formats.
	 */

	/* SIGN byte */
	if (i < 0)
	{
		(*AH->WriteBytePtr) (AH, 1);
1400
		i = -i;
B
Bruce Momjian 已提交
1401 1402 1403 1404 1405 1406 1407
	}
	else
		(*AH->WriteBytePtr) (AH, 0);

	for (b = 0; b < AH->intSize; b++)
	{
		(*AH->WriteBytePtr) (AH, i & 0xFF);
1408
		i >>= 8;
B
Bruce Momjian 已提交
1409 1410 1411
	}

	return AH->intSize + 1;
B
Bruce Momjian 已提交
1412 1413
}

B
Bruce Momjian 已提交
1414 1415
int
ReadInt(ArchiveHandle *AH)
B
Bruce Momjian 已提交
1416
{
B
Bruce Momjian 已提交
1417 1418 1419 1420 1421
	int			res = 0;
	int			bv,
				b;
	int			sign = 0;		/* Default positive */
	int			bitShift = 0;
B
Bruce Momjian 已提交
1422

B
Bruce Momjian 已提交
1423
	if (AH->version > K_VERS_1_0)
1424
		/* Read a sign byte */
B
Bruce Momjian 已提交
1425
		sign = (*AH->ReadBytePtr) (AH);
B
Bruce Momjian 已提交
1426

B
Bruce Momjian 已提交
1427 1428 1429
	for (b = 0; b < AH->intSize; b++)
	{
		bv = (*AH->ReadBytePtr) (AH) & 0xFF;
1430 1431 1432
		if (bv != 0)
			res = res + (bv << bitShift);
		bitShift += 8;
B
Bruce Momjian 已提交
1433
	}
B
Bruce Momjian 已提交
1434

B
Bruce Momjian 已提交
1435 1436
	if (sign)
		res = -res;
B
Bruce Momjian 已提交
1437

B
Bruce Momjian 已提交
1438
	return res;
B
Bruce Momjian 已提交
1439 1440
}

P
Peter Eisentraut 已提交
1441
size_t
1442
WriteStr(ArchiveHandle *AH, const char *c)
B
Bruce Momjian 已提交
1443
{
P
Peter Eisentraut 已提交
1444
	size_t		res;
1445 1446 1447 1448

	if (c)
	{
		res = WriteInt(AH, strlen(c));
B
Bruce Momjian 已提交
1449
		res += (*AH->WriteBufPtr) (AH, c, strlen(c));
1450 1451 1452 1453
	}
	else
		res = WriteInt(AH, -1);

B
Bruce Momjian 已提交
1454
	return res;
B
Bruce Momjian 已提交
1455 1456
}

B
Bruce Momjian 已提交
1457 1458
char *
ReadStr(ArchiveHandle *AH)
B
Bruce Momjian 已提交
1459
{
B
Bruce Momjian 已提交
1460 1461
	char	   *buf;
	int			l;
B
Bruce Momjian 已提交
1462

B
Bruce Momjian 已提交
1463
	l = ReadInt(AH);
1464 1465 1466 1467
	if (l == -1)
		buf = NULL;
	else
	{
B
Bruce Momjian 已提交
1468
		buf = (char *) malloc(l + 1);
1469
		if (!buf)
1470
			die_horribly(AH, modulename, "out of memory\n");
1471

B
Bruce Momjian 已提交
1472
		(*AH->ReadBufPtr) (AH, (void *) buf, l);
1473 1474
		buf[l] = '\0';
	}
B
Bruce Momjian 已提交
1475

B
Bruce Momjian 已提交
1476
	return buf;
B
Bruce Momjian 已提交
1477 1478
}

T
Tom Lane 已提交
1479
static int
B
Bruce Momjian 已提交
1480
_discoverArchiveFormat(ArchiveHandle *AH)
B
Bruce Momjian 已提交
1481
{
B
Bruce Momjian 已提交
1482 1483
	FILE	   *fh;
	char		sig[6];			/* More than enough */
P
Peter Eisentraut 已提交
1484
	size_t		cnt;
B
Bruce Momjian 已提交
1485
	int			wantClose = 0;
B
Bruce Momjian 已提交
1486

1487
#if 0
1488
	write_msg(modulename, "attempting to ascertain archive format\n");
1489
#endif
1490 1491 1492 1493 1494 1495 1496 1497

	if (AH->lookahead)
		free(AH->lookahead);

	AH->lookaheadSize = 512;
	AH->lookahead = calloc(1, 512);
	AH->lookaheadLen = 0;
	AH->lookaheadPos = 0;
1498

B
Bruce Momjian 已提交
1499 1500
	if (AH->fSpec)
	{
1501 1502
		wantClose = 1;
		fh = fopen(AH->fSpec, PG_BINARY_R);
B
Bruce Momjian 已提交
1503 1504
	}
	else
1505
		fh = stdin;
B
Bruce Momjian 已提交
1506

B
Bruce Momjian 已提交
1507
	if (!fh)
1508
		die_horribly(AH, modulename, "could not open input file: %s\n", strerror(errno));
B
Bruce Momjian 已提交
1509

B
Bruce Momjian 已提交
1510
	cnt = fread(sig, 1, 5, fh);
B
Bruce Momjian 已提交
1511

B
Bruce Momjian 已提交
1512
	if (cnt != 5)
1513 1514 1515 1516
	{
		if (ferror(fh))
			die_horribly(AH, modulename, "could not read input file: %s\n", strerror(errno));
		else
P
Peter Eisentraut 已提交
1517 1518
			die_horribly(AH, modulename, "input file is too short (read %lu, expected 5)\n",
						 (unsigned long) cnt);
1519
	}
B
Bruce Momjian 已提交
1520

B
Bruce Momjian 已提交
1521
	/* Save it, just in case we need it later */
1522 1523
	strncpy(&AH->lookahead[0], sig, 5);
	AH->lookaheadLen = 5;
B
Bruce Momjian 已提交
1524

B
Bruce Momjian 已提交
1525
	if (strncmp(sig, "PGDMP", 5) == 0)
1526 1527 1528 1529 1530 1531 1532 1533 1534
	{
		AH->vmaj = fgetc(fh);
		AH->vmin = fgetc(fh);

		/* Save these too... */
		AH->lookahead[AH->lookaheadLen++] = AH->vmaj;
		AH->lookahead[AH->lookaheadLen++] = AH->vmin;

		/* Check header version; varies from V1.0 */
B
Bruce Momjian 已提交
1535 1536
		if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0)))		/* Version > 1.0 */
		{
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
			AH->vrev = fgetc(fh);
			AH->lookahead[AH->lookaheadLen++] = AH->vrev;
		}
		else
			AH->vrev = 0;

		AH->intSize = fgetc(fh);
		AH->lookahead[AH->lookaheadLen++] = AH->intSize;

		AH->format = fgetc(fh);
		AH->lookahead[AH->lookaheadLen++] = AH->format;

		/* Make a convenient integer <maj><min><rev>00 */
B
Bruce Momjian 已提交
1550 1551 1552 1553
		AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
	}
	else
	{
1554
		/*
B
Bruce Momjian 已提交
1555 1556
		 * *Maybe* we have a tar archive format file... So, read first 512
		 * byte header...
1557 1558 1559
		 */
		cnt = fread(&AH->lookahead[AH->lookaheadLen], 1, 512 - AH->lookaheadLen, fh);
		AH->lookaheadLen += cnt;
B
Bruce Momjian 已提交
1560

1561
		if (AH->lookaheadLen != 512)
1562
			die_horribly(AH, modulename, "input file does not appear to be a valid archive (too short?)\n");
B
Bruce Momjian 已提交
1563

1564
		if (!isValidTarHeader(AH->lookahead))
1565
			die_horribly(AH, modulename, "input file does not appear to be a valid archive\n");
B
Bruce Momjian 已提交
1566

1567 1568
		AH->format = archTar;
	}
B
Bruce Momjian 已提交
1569

B
Bruce Momjian 已提交
1570
	/* If we can't seek, then mark the header as read */
P
Peter Eisentraut 已提交
1571
	if (fseeko(fh, 0, SEEK_SET) != 0)
1572 1573
	{
		/*
B
Bruce Momjian 已提交
1574 1575
		 * NOTE: Formats that use the looahead buffer can unset this in
		 * their Init routine.
1576 1577 1578 1579
		 */
		AH->readHeader = 1;
	}
	else
B
Bruce Momjian 已提交
1580
		AH->lookaheadLen = 0;	/* Don't bother since we've reset the file */
1581

1582
#if 0
P
Peter Eisentraut 已提交
1583 1584
	write_msg(modulename, "read %lu bytes into lookahead buffer\n",
			  (unsigned long) AH->lookaheadLen);
1585
#endif
B
Bruce Momjian 已提交
1586

B
Bruce Momjian 已提交
1587 1588
	/* Close the file */
	if (wantClose)
1589
		if (fclose(fh) != 0)
1590 1591
			die_horribly(AH, modulename, "could not close the input file after reading header: %s\n",
						 strerror(errno));
B
Bruce Momjian 已提交
1592

B
Bruce Momjian 已提交
1593
	return AH->format;
B
Bruce Momjian 已提交
1594 1595 1596 1597 1598 1599
}


/*
 * Allocate an archive handle
 */
B
Bruce Momjian 已提交
1600 1601 1602
static ArchiveHandle *
_allocAH(const char *FileSpec, const ArchiveFormat fmt,
		 const int compression, ArchiveMode mode)
1603
{
B
Bruce Momjian 已提交
1604
	ArchiveHandle *AH;
B
Bruce Momjian 已提交
1605

1606
#if 0
1607
	write_msg(modulename, "allocating AH for %s, format %d\n", FileSpec, fmt);
1608
#endif
1609

B
Bruce Momjian 已提交
1610 1611
	AH = (ArchiveHandle *) calloc(1, sizeof(ArchiveHandle));
	if (!AH)
1612
		die_horribly(AH, modulename, "out of memory\n");
B
Bruce Momjian 已提交
1613

B
Bruce Momjian 已提交
1614 1615
	AH->vmaj = K_VERS_MAJOR;
	AH->vmin = K_VERS_MINOR;
1616
	AH->vrev = K_VERS_REV;
B
Bruce Momjian 已提交
1617

1618 1619
	AH->createDate = time(NULL);

B
Bruce Momjian 已提交
1620 1621 1622 1623
	AH->intSize = sizeof(int);
	AH->lastID = 0;
	if (FileSpec)
	{
1624
		AH->fSpec = strdup(FileSpec);
B
Bruce Momjian 已提交
1625

1626 1627 1628
		/*
		 * Not used; maybe later....
		 *
B
Bruce Momjian 已提交
1629 1630
		 * AH->workDir = strdup(FileSpec); for(i=strlen(FileSpec) ; i > 0 ;
		 * i--) if (AH->workDir[i-1] == '/')
1631
		 */
B
Bruce Momjian 已提交
1632 1633
	}
	else
1634
		AH->fSpec = NULL;
B
Bruce Momjian 已提交
1635

B
Bruce Momjian 已提交
1636 1637
	AH->currUser = strdup("");	/* So it's valid, but we can free() it
								 * later if necessary */
1638
	AH->currSchema = strdup(""); /* ditto */
B
Bruce Momjian 已提交
1639

B
Bruce Momjian 已提交
1640 1641
	AH->toc = (TocEntry *) calloc(1, sizeof(TocEntry));
	if (!AH->toc)
1642
		die_horribly(AH, modulename, "out of memory\n");
B
Bruce Momjian 已提交
1643

B
Bruce Momjian 已提交
1644 1645 1646 1647 1648
	AH->toc->next = AH->toc;
	AH->toc->prev = AH->toc;

	AH->mode = mode;
	AH->compression = compression;
B
Bruce Momjian 已提交
1649

1650 1651
	AH->pgCopyBuf = createPQExpBuffer();
	AH->sqlBuf = createPQExpBuffer();
B
Bruce Momjian 已提交
1652

B
Bruce Momjian 已提交
1653 1654 1655
	/* Open stdout with no compression for AH output handle */
	AH->gzOut = 0;
	AH->OF = stdout;
B
Bruce Momjian 已提交
1656

1657
#if 0
1658
	write_msg(modulename, "archive format is %d\n", fmt);
1659
#endif
1660

B
Bruce Momjian 已提交
1661
	if (fmt == archUnknown)
1662 1663 1664
		AH->format = _discoverArchiveFormat(AH);
	else
		AH->format = fmt;
B
Bruce Momjian 已提交
1665

B
Bruce Momjian 已提交
1666 1667
	switch (AH->format)
	{
B
Bruce Momjian 已提交
1668

1669 1670 1671
		case archCustom:
			InitArchiveFmt_Custom(AH);
			break;
B
Bruce Momjian 已提交
1672

1673 1674 1675
		case archFiles:
			InitArchiveFmt_Files(AH);
			break;
B
Bruce Momjian 已提交
1676

1677 1678 1679
		case archNull:
			InitArchiveFmt_Null(AH);
			break;
B
Bruce Momjian 已提交
1680

1681 1682 1683 1684 1685
		case archTar:
			InitArchiveFmt_Tar(AH);
			break;

		default:
1686
			die_horribly(AH, modulename, "unrecognized file format '%d'\n", fmt);
B
Bruce Momjian 已提交
1687
	}
B
Bruce Momjian 已提交
1688

B
Bruce Momjian 已提交
1689
	return AH;
B
Bruce Momjian 已提交
1690 1691 1692
}


B
Bruce Momjian 已提交
1693 1694
void
WriteDataChunks(ArchiveHandle *AH)
B
Bruce Momjian 已提交
1695
{
B
Bruce Momjian 已提交
1696 1697 1698
	TocEntry   *te = AH->toc->next;
	StartDataPtr startPtr;
	EndDataPtr	endPtr;
B
Bruce Momjian 已提交
1699

B
Bruce Momjian 已提交
1700 1701 1702
	while (te != AH->toc)
	{
		if (te->dataDumper != NULL)
1703
		{
B
Bruce Momjian 已提交
1704 1705
			AH->currToc = te;
			/* printf("Writing data for %d (%x)\n", te->id, te); */
1706

B
Bruce Momjian 已提交
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
			if (strcmp(te->desc, "BLOBS") == 0)
			{
				startPtr = AH->StartBlobsPtr;
				endPtr = AH->EndBlobsPtr;
			}
			else
			{
				startPtr = AH->StartDataPtr;
				endPtr = AH->EndDataPtr;
			}
B
Bruce Momjian 已提交
1717

B
Bruce Momjian 已提交
1718 1719
			if (startPtr != NULL)
				(*startPtr) (AH, te);
B
Bruce Momjian 已提交
1720

B
Bruce Momjian 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
			/*
			 * printf("Dumper arg for %d is %x\n", te->id,
			 * te->dataDumperArg);
			 */

			/*
			 * The user-provided DataDumper routine needs to call
			 * AH->WriteData
			 */
			(*te->dataDumper) ((Archive *) AH, te->oid, te->dataDumperArg);

			if (endPtr != NULL)
				(*endPtr) (AH, te);
			AH->currToc = NULL;
		}
1736
		te = te->next;
B
Bruce Momjian 已提交
1737
	}
B
Bruce Momjian 已提交
1738 1739
}

B
Bruce Momjian 已提交
1740 1741
void
WriteToc(ArchiveHandle *AH)
B
Bruce Momjian 已提交
1742
{
B
Bruce Momjian 已提交
1743
	TocEntry   *te = AH->toc->next;
1744 1745
	const char *dep;
	int			i;
B
Bruce Momjian 已提交
1746 1747 1748 1749 1750 1751 1752 1753 1754

	/* printf("%d TOC Entries to save\n", AH->tocCount); */

	WriteInt(AH, AH->tocCount);
	while (te != AH->toc)
	{
		WriteInt(AH, te->id);
		WriteInt(AH, te->dataDumper ? 1 : 0);
		WriteStr(AH, te->oid);
1755

1756
		WriteStr(AH, te->tag);
B
Bruce Momjian 已提交
1757 1758 1759 1760
		WriteStr(AH, te->desc);
		WriteStr(AH, te->defn);
		WriteStr(AH, te->dropStmt);
		WriteStr(AH, te->copyStmt);
1761
		WriteStr(AH, te->namespace);
B
Bruce Momjian 已提交
1762
		WriteStr(AH, te->owner);
1763 1764 1765 1766 1767

		/* Dump list of dependencies */
		if (te->depOid != NULL)
		{
			i = 0;
1768
			while ((dep = (*te->depOid)[i++]) != NULL)
1769 1770
				WriteStr(AH, dep);
		}
1771
		WriteStr(AH, NULL);		/* Terminate List */
1772

B
Bruce Momjian 已提交
1773 1774
		if (AH->WriteExtraTocPtr)
			(*AH->WriteExtraTocPtr) (AH, te);
1775
		te = te->next;
B
Bruce Momjian 已提交
1776
	}
B
Bruce Momjian 已提交
1777 1778
}

B
Bruce Momjian 已提交
1779 1780
void
ReadToc(ArchiveHandle *AH)
B
Bruce Momjian 已提交
1781
{
B
Bruce Momjian 已提交
1782
	int			i;
1783
	char	   *((*deps)[]);
1784 1785
	int			depIdx;
	int			depSize;
B
Bruce Momjian 已提交
1786

B
Bruce Momjian 已提交
1787
	TocEntry   *te = AH->toc->next;
B
Bruce Momjian 已提交
1788

B
Bruce Momjian 已提交
1789
	AH->tocCount = ReadInt(AH);
B
Bruce Momjian 已提交
1790

B
Bruce Momjian 已提交
1791 1792
	for (i = 0; i < AH->tocCount; i++)
	{
B
Bruce Momjian 已提交
1793

B
Bruce Momjian 已提交
1794
		te = (TocEntry *) calloc(1, sizeof(TocEntry));
1795 1796 1797 1798
		te->id = ReadInt(AH);

		/* Sanity check */
		if (te->id <= 0 || te->id > AH->tocCount)
1799
			die_horribly(AH, modulename, "entry id out of range - perhaps a corrupt TOC\n");
1800 1801 1802

		te->hadDumper = ReadInt(AH);
		te->oid = ReadStr(AH);
1803 1804
		te->oidVal = atooid(te->oid);

1805
		te->tag = ReadStr(AH);
1806 1807 1808 1809 1810 1811 1812
		te->desc = ReadStr(AH);
		te->defn = ReadStr(AH);
		te->dropStmt = ReadStr(AH);

		if (AH->version >= K_VERS_1_3)
			te->copyStmt = ReadStr(AH);

1813 1814 1815
		if (AH->version >= K_VERS_1_6)
			te->namespace = ReadStr(AH);

1816 1817
		te->owner = ReadStr(AH);

1818 1819 1820 1821
		/* Read TOC entry dependencies */
		if (AH->version >= K_VERS_1_5)
		{
			depSize = 100;
1822
			deps = malloc(sizeof(char *) * depSize);
1823 1824 1825 1826 1827 1828
			depIdx = 0;
			do
			{
				if (depIdx > depSize)
				{
					depSize *= 2;
1829
					deps = realloc(deps, sizeof(char *) * depSize);
1830 1831
				}
				(*deps)[depIdx] = ReadStr(AH);
1832 1833 1834
#if 0
				if ((*deps)[depIdx])
					write_msg(modulename, "read dependency for %s -> %s\n",
1835
							  te->tag, (*deps)[depIdx]);
1836
#endif
1837
			} while ((*deps)[depIdx++] != NULL);
1838

1839 1840
			if (depIdx > 1)		/* We have a non-null entry */
				te->depOid = realloc(deps, sizeof(char *) * depIdx);	/* trim it */
1841
			else
1842
				te->depOid = NULL;		/* no deps */
1843
		}
1844 1845
		else
			te->depOid = NULL;
1846 1847 1848 1849

		/* Set maxOidVal etc for use in sorting */
		_fixupOidInfo(te);

B
Bruce Momjian 已提交
1850 1851
		if (AH->ReadExtraTocPtr)
			(*AH->ReadExtraTocPtr) (AH, te);
1852

1853
		ahlog(AH, 3, "read TOC entry %d (id %d) for %s %s\n", i, te->id, te->desc, te->tag);
1854 1855 1856 1857 1858

		te->prev = AH->toc->prev;
		AH->toc->prev->next = te;
		AH->toc->prev = te;
		te->next = AH->toc;
B
Bruce Momjian 已提交
1859
	}
B
Bruce Momjian 已提交
1860 1861
}

1862
static teReqs
B
Bruce Momjian 已提交
1863
_tocEntryRequired(TocEntry *te, RestoreOptions *ropt)
B
Bruce Momjian 已提交
1864
{
1865
	teReqs		res = 3;		/* Schema = 1, Data = 2, Both = 3 */
B
Bruce Momjian 已提交
1866 1867 1868

	/* If it's an ACL, maybe ignore it */
	if (ropt->aclsSkip && strcmp(te->desc, "ACL") == 0)
1869
		return 0;
B
Bruce Momjian 已提交
1870

B
Bruce Momjian 已提交
1871
	if (!ropt->create && strcmp(te->desc, "DATABASE") == 0)
1872 1873
		return 0;

B
Bruce Momjian 已提交
1874 1875 1876 1877
	/* Check if tablename only is wanted */
	if (ropt->selTypes)
	{
		if ((strcmp(te->desc, "TABLE") == 0) || (strcmp(te->desc, "TABLE DATA") == 0))
1878 1879 1880
		{
			if (!ropt->selTable)
				return 0;
1881
			if (ropt->tableNames && strcmp(ropt->tableNames, te->tag) != 0)
1882
				return 0;
B
Bruce Momjian 已提交
1883 1884 1885
		}
		else if (strcmp(te->desc, "INDEX") == 0)
		{
1886 1887
			if (!ropt->selIndex)
				return 0;
1888
			if (ropt->indexNames && strcmp(ropt->indexNames, te->tag) != 0)
1889
				return 0;
B
Bruce Momjian 已提交
1890 1891 1892
		}
		else if (strcmp(te->desc, "FUNCTION") == 0)
		{
1893 1894
			if (!ropt->selFunction)
				return 0;
1895
			if (ropt->functionNames && strcmp(ropt->functionNames, te->tag) != 0)
1896
				return 0;
B
Bruce Momjian 已提交
1897 1898 1899
		}
		else if (strcmp(te->desc, "TRIGGER") == 0)
		{
1900 1901
			if (!ropt->selTrigger)
				return 0;
1902
			if (ropt->triggerNames && strcmp(ropt->triggerNames, te->tag) != 0)
1903 1904
				return 0;
		}
B
Bruce Momjian 已提交
1905 1906
		else
			return 0;
B
Bruce Momjian 已提交
1907 1908
	}

1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
	/*
	 * Check if we had a dataDumper. Indicates if the entry is schema or
	 * data
	 */
	if (!te->hadDumper)
	{
		/*
		 * Special Case: If 'SEQUENCE SET' then it is considered a data
		 * entry
		 */
		if (strcmp(te->desc, "SEQUENCE SET") == 0)
			res = res & REQ_DATA;
		else
1922 1923
			res = res & ~REQ_DATA;
	}
1924

1925
	/*
1926
	 * Special case: <Init> type with <Max OID> tag; this is part of a
1927 1928
	 * DATA restore even though it has SQL.
	 */
1929
	if ((strcmp(te->desc, "<Init>") == 0) && (strcmp(te->tag, "Max OID") == 0))
1930 1931
		res = REQ_DATA;

B
Bruce Momjian 已提交
1932 1933
	/* Mask it if we only want schema */
	if (ropt->schemaOnly)
1934
		res = res & REQ_SCHEMA;
B
Bruce Momjian 已提交
1935

B
Bruce Momjian 已提交
1936
	/* Mask it we only want data */
1937
	if (ropt->dataOnly)
1938
		res = res & REQ_DATA;
B
Bruce Momjian 已提交
1939

B
Bruce Momjian 已提交
1940 1941
	/* Mask it if we don't have a schema contribition */
	if (!te->defn || strlen(te->defn) == 0)
1942
		res = res & ~REQ_SCHEMA;
B
Bruce Momjian 已提交
1943

B
Bruce Momjian 已提交
1944 1945
	/* Finally, if we used a list, limit based on that as well */
	if (ropt->limitToList && !ropt->idWanted[te->id - 1])
1946
		return 0;
B
Bruce Momjian 已提交
1947

B
Bruce Momjian 已提交
1948
	return res;
B
Bruce Momjian 已提交
1949 1950
}

1951 1952
/*
 * Issue a SET SESSION AUTHORIZATION command.  Caller is responsible
1953 1954
 * for updating state if appropriate.  If user is NULL, the
 * specification DEFAULT will be used.
1955 1956
 */
static void
1957
_doSetSessionAuth(ArchiveHandle *AH, const char *user)
1958
{
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
	PQExpBuffer cmd = createPQExpBuffer();
	appendPQExpBuffer(cmd, "SET SESSION AUTHORIZATION ");
	if (user)
		/* SQL requires a string literal here.  Might as well be
		 * correct. */
		appendStringLiteral(cmd, user, false);
	else
		appendPQExpBuffer(cmd, "DEFAULT");
	appendPQExpBuffer(cmd, ";");

1969 1970 1971 1972
	if (RestoringToDB(AH))
	{
		PGresult   *res;

1973
		res = PQexec(AH->connection, cmd->data);
1974 1975 1976

		if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
			die_horribly(AH, modulename, "could not set session user to %s: %s",
1977
						 user, PQerrorMessage(AH->connection));
1978 1979 1980 1981

		PQclear(res);
	}
	else
1982 1983 1984
		ahprintf(AH, "%s\n\n", cmd->data);

	destroyPQExpBuffer(cmd);
1985 1986
}

1987 1988 1989 1990 1991 1992 1993 1994

/*
 * Issue the commands to connect to the database as the specified user
 * to the specified database.  The database name may be NULL, then the
 * current database is kept.  If reconnects were disallowed by the
 * user, this won't do anything.
 *
 * If we're currently restoring right into a database, this will
1995
 * actually establish a connection.	Otherwise it puts a \connect into
1996 1997
 * the script output.
 */
B
Bruce Momjian 已提交
1998
static void
1999
_reconnectAsUser(ArchiveHandle *AH, const char *dbname, const char *user)
2000
{
2001 2002 2003
	if (!user || strlen(user) == 0
		|| (strcmp(AH->currUser, user) == 0 && !dbname))
		return;					/* no need to do anything */
2004

2005 2006 2007 2008
	/*
	 * Use SET SESSION AUTHORIZATION if allowed and no database change
	 * needed
	 */
2009
	if (!dbname && AH->ropt->use_setsessauth)
2010
	{
2011
		_doSetSessionAuth(AH, user);
B
Bruce Momjian 已提交
2012
	}
2013
	else if (AH->ropt && AH->ropt->noReconnect)
2014 2015
	{
		/* When -R was given, don't do anything. */
2016
		return;
2017
	}
2018 2019 2020
	else if (RestoringToDB(AH))
		ReconnectToServer(AH, dbname, user);
	else
2021 2022 2023 2024
	{
		PQExpBuffer qry = createPQExpBuffer();

		appendPQExpBuffer(qry, "\\connect %s",
2025
						  dbname ? fmtId(dbname) : "-");
2026
		appendPQExpBuffer(qry, " %s\n\n",
2027
						  fmtId(user));
2028 2029 2030 2031

		ahprintf(AH, qry->data);

		destroyPQExpBuffer(qry);
2032 2033 2034 2035 2036

		/* don't assume we still know the output schema */
		if (AH->currSchema)
			free(AH->currSchema);
		AH->currSchema = strdup("");
2037
	}
2038

2039 2040 2041 2042
	/*
	 * NOTE: currUser keeps track of what the imaginary session user in
	 * our script is
	 */
2043 2044 2045 2046
	if (AH->currUser)
		free(AH->currUser);

	AH->currUser = strdup(user);
2047 2048
}

2049 2050 2051 2052 2053 2054

/*
 * Issues the commands to connect to the database (or the current one,
 * if NULL) as the owner of the the given TOC entry object.  If
 * changes in ownership are not allowed, this doesn't do anything.
 */
B
Bruce Momjian 已提交
2055 2056
static void
_reconnectAsOwner(ArchiveHandle *AH, const char *dbname, TocEntry *te)
2057 2058 2059 2060 2061
{
	if (AH->ropt && AH->ropt->noOwner)
		return;

	_reconnectAsUser(AH, dbname, te->owner);
2062 2063
}

2064

2065 2066 2067 2068 2069 2070 2071
/*
 * Issue the commands to select the specified schema as the current schema
 * in the target database.
 */
static void
_selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
{
2072 2073
	PQExpBuffer qry;

2074 2075 2076 2077
	if (!schemaName || *schemaName == '\0' ||
		strcmp(AH->currSchema, schemaName) == 0)
		return;					/* no need to do anything */

2078 2079 2080
	qry = createPQExpBuffer();

	appendPQExpBuffer(qry, "SET search_path = %s",
2081
					  fmtId(schemaName));
2082 2083 2084
	if (strcmp(schemaName, "pg_catalog") != 0)
		appendPQExpBuffer(qry, ", pg_catalog");

2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
	if (RestoringToDB(AH))
	{
		PGresult   *res;

		res = PQexec(AH->connection, qry->data);

		if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
			die_horribly(AH, modulename, "could not set search_path to %s: %s",
						 schemaName, PQerrorMessage(AH->connection));

		PQclear(res);
	}
	else
2098
		ahprintf(AH, "%s;\n\n", qry->data);
2099 2100 2101 2102

	if (AH->currSchema)
		free(AH->currSchema);
	AH->currSchema = strdup(schemaName);
2103 2104

	destroyPQExpBuffer(qry);
2105 2106 2107
}


2108

B
Bruce Momjian 已提交
2109
static int
2110
_printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData)
B
Bruce Momjian 已提交
2111
{
2112
	char	   *pfx;
2113

2114
	/* Select owner and schema as necessary */
2115
	_reconnectAsOwner(AH, NULL, te);
2116
	_selectOutputSchema(AH, te->namespace);
2117

2118 2119
	if (isData)
		pfx = "Data for ";
2120
	else
2121 2122
		pfx = "";

2123
	ahprintf(AH, "--\n-- %sTOC entry %d (OID %s)\n-- Name: %s; Type: %s; Schema: %s; Owner: %s\n",
2124
			 pfx, te->id, te->oid, te->tag, te->desc,
2125 2126
			 te->namespace ? te->namespace : "-",
			 te->owner);
B
Bruce Momjian 已提交
2127 2128 2129
	if (AH->PrintExtraTocPtr !=NULL)
		(*AH->PrintExtraTocPtr) (AH, te);
	ahprintf(AH, "--\n\n");
B
Bruce Momjian 已提交
2130

2131 2132
	if (strlen(te->defn) > 0)
		ahprintf(AH, "%s\n\n", te->defn);
B
Bruce Momjian 已提交
2133

B
Bruce Momjian 已提交
2134
	return 1;
B
Bruce Momjian 已提交
2135 2136
}

B
Bruce Momjian 已提交
2137 2138
void
WriteHead(ArchiveHandle *AH)
B
Bruce Momjian 已提交
2139
{
B
Bruce Momjian 已提交
2140
	struct tm	crtm;
2141

B
Bruce Momjian 已提交
2142 2143 2144 2145 2146 2147
	(*AH->WriteBufPtr) (AH, "PGDMP", 5);		/* Magic code */
	(*AH->WriteBytePtr) (AH, AH->vmaj);
	(*AH->WriteBytePtr) (AH, AH->vmin);
	(*AH->WriteBytePtr) (AH, AH->vrev);
	(*AH->WriteBytePtr) (AH, AH->intSize);
	(*AH->WriteBytePtr) (AH, AH->format);
B
Bruce Momjian 已提交
2148

2149
#ifndef HAVE_LIBZ
B
Bruce Momjian 已提交
2150
	if (AH->compression != 0)
2151 2152
		write_msg(modulename, "WARNING: requested compression not available in this "
				  "installation - archive will be uncompressed\n");
B
Bruce Momjian 已提交
2153

B
Bruce Momjian 已提交
2154
	AH->compression = 0;
2155
#endif
B
Bruce Momjian 已提交
2156

2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
	WriteInt(AH, AH->compression);

	crtm = *localtime(&AH->createDate);
	WriteInt(AH, crtm.tm_sec);
	WriteInt(AH, crtm.tm_min);
	WriteInt(AH, crtm.tm_hour);
	WriteInt(AH, crtm.tm_mday);
	WriteInt(AH, crtm.tm_mon);
	WriteInt(AH, crtm.tm_year);
	WriteInt(AH, crtm.tm_isdst);
2167
	WriteStr(AH, PQdb(AH->connection));
B
Bruce Momjian 已提交
2168 2169
}

B
Bruce Momjian 已提交
2170 2171
void
ReadHead(ArchiveHandle *AH)
B
Bruce Momjian 已提交
2172
{
B
Bruce Momjian 已提交
2173 2174
	char		tmpMag[7];
	int			fmt;
2175
	struct tm	crtm;
B
Bruce Momjian 已提交
2176

2177
	/* If we haven't already read the header... */
B
Bruce Momjian 已提交
2178 2179
	if (!AH->readHeader)
	{
B
Bruce Momjian 已提交
2180

B
Bruce Momjian 已提交
2181
		(*AH->ReadBufPtr) (AH, tmpMag, 5);
B
Bruce Momjian 已提交
2182

B
Bruce Momjian 已提交
2183
		if (strncmp(tmpMag, "PGDMP", 5) != 0)
2184
			die_horribly(AH, modulename, "did not find magic string in file header\n");
B
Bruce Momjian 已提交
2185

B
Bruce Momjian 已提交
2186 2187
		AH->vmaj = (*AH->ReadBytePtr) (AH);
		AH->vmin = (*AH->ReadBytePtr) (AH);
B
Bruce Momjian 已提交
2188

B
Bruce Momjian 已提交
2189 2190 2191
		if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0)))		/* Version > 1.0 */
			AH->vrev = (*AH->ReadBytePtr) (AH);
		else
2192
			AH->vrev = 0;
B
Bruce Momjian 已提交
2193

B
Bruce Momjian 已提交
2194
		AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;
B
Bruce Momjian 已提交
2195 2196


2197
		if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
2198 2199
			die_horribly(AH, modulename, "unsupported version (%d.%d) in file header\n",
						 AH->vmaj, AH->vmin);
B
Bruce Momjian 已提交
2200

B
Bruce Momjian 已提交
2201
		AH->intSize = (*AH->ReadBytePtr) (AH);
2202
		if (AH->intSize > 32)
P
Peter Eisentraut 已提交
2203 2204
			die_horribly(AH, modulename, "sanity check on integer size (%lu) failed\n",
						 (unsigned long) AH->intSize);
B
Bruce Momjian 已提交
2205

2206
		if (AH->intSize > sizeof(int))
2207
			write_msg(modulename, "WARNING: archive was made on a machine with larger integers, some operations may fail\n");
B
Bruce Momjian 已提交
2208

B
Bruce Momjian 已提交
2209
		fmt = (*AH->ReadBytePtr) (AH);
B
Bruce Momjian 已提交
2210

2211
		if (AH->format != fmt)
2212 2213
			die_horribly(AH, modulename, "expected format (%d) differs from format found in file (%d)\n",
						 AH->format, fmt);
B
Bruce Momjian 已提交
2214
	}
B
Bruce Momjian 已提交
2215

B
Bruce Momjian 已提交
2216 2217
	if (AH->version >= K_VERS_1_2)
	{
2218
		if (AH->version < K_VERS_1_4)
B
Bruce Momjian 已提交
2219
			AH->compression = (*AH->ReadBytePtr) (AH);
2220 2221
		else
			AH->compression = ReadInt(AH);
B
Bruce Momjian 已提交
2222 2223
	}
	else
2224
		AH->compression = Z_DEFAULT_COMPRESSION;
B
Bruce Momjian 已提交
2225

2226
#ifndef HAVE_LIBZ
B
Bruce Momjian 已提交
2227
	if (AH->compression != 0)
2228
		write_msg(modulename, "WARNING: archive is compressed, but this installation does not support compression - no data will be available\n");
B
Bruce Momjian 已提交
2229 2230
#endif

2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
	if (AH->version >= K_VERS_1_4)
	{
		crtm.tm_sec = ReadInt(AH);
		crtm.tm_min = ReadInt(AH);
		crtm.tm_hour = ReadInt(AH);
		crtm.tm_mday = ReadInt(AH);
		crtm.tm_mon = ReadInt(AH);
		crtm.tm_year = ReadInt(AH);
		crtm.tm_isdst = ReadInt(AH);

		AH->archdbname = ReadStr(AH);

		AH->createDate = mktime(&crtm);

B
Bruce Momjian 已提交
2245
		if (AH->createDate == (time_t) -1)
2246
			write_msg(modulename, "WARNING: invalid creation date in header\n");
2247 2248
	}

B
Bruce Momjian 已提交
2249 2250 2251
}


B
Bruce Momjian 已提交
2252 2253
static void
_SortToc(ArchiveHandle *AH, TocSortCompareFn fn)
B
Bruce Momjian 已提交
2254
{
B
Bruce Momjian 已提交
2255 2256 2257
	TocEntry  **tea;
	TocEntry   *te;
	int			i;
B
Bruce Momjian 已提交
2258

B
Bruce Momjian 已提交
2259 2260 2261 2262 2263 2264 2265 2266
	/* Allocate an array for quicksort (TOC size + head & foot) */
	tea = (TocEntry **) malloc(sizeof(TocEntry *) * (AH->tocCount + 2));

	/* Build array of toc entries, including header at start and end */
	te = AH->toc;
	for (i = 0; i <= AH->tocCount + 1; i++)
	{
		/*
P
Peter Eisentraut 已提交
2267
		 * printf("%d: %x (%x, %x) - %u\n", i, te, te->prev, te->next,
B
Bruce Momjian 已提交
2268 2269 2270 2271 2272
		 * te->oidVal);
		 */
		tea[i] = te;
		te = te->next;
	}
B
Bruce Momjian 已提交
2273

B
Bruce Momjian 已提交
2274 2275
	/* Sort it, but ignore the header entries */
	qsort(&(tea[1]), AH->tocCount, sizeof(TocEntry *), fn);
B
Bruce Momjian 已提交
2276

2277
	/* Rebuild list: this works because we have headers at each end */
B
Bruce Momjian 已提交
2278 2279 2280 2281 2282
	for (i = 1; i <= AH->tocCount; i++)
	{
		tea[i]->next = tea[i + 1];
		tea[i]->prev = tea[i - 1];
	}
B
Bruce Momjian 已提交
2283 2284


B
Bruce Momjian 已提交
2285 2286 2287 2288
	te = AH->toc;
	for (i = 0; i <= AH->tocCount + 1; i++)
	{
		/*
P
Peter Eisentraut 已提交
2289
		 * printf("%d: %x (%x, %x) - %u\n", i, te, te->prev, te->next,
B
Bruce Momjian 已提交
2290 2291 2292 2293
		 * te->oidVal);
		 */
		te = te->next;
	}
B
Bruce Momjian 已提交
2294 2295


B
Bruce Momjian 已提交
2296 2297
	AH->toc->next = tea[1];
	AH->toc->prev = tea[AH->tocCount];
B
Bruce Momjian 已提交
2298 2299
}

B
Bruce Momjian 已提交
2300 2301
static int
_tocSortCompareByOIDNum(const void *p1, const void *p2)
B
Bruce Momjian 已提交
2302
{
B
Bruce Momjian 已提交
2303 2304
	TocEntry   *te1 = *(TocEntry **) p1;
	TocEntry   *te2 = *(TocEntry **) p2;
2305 2306 2307
	Oid			id1 = te1->maxOidVal;
	Oid			id2 = te2->maxOidVal;
	int			cmpval;
B
Bruce Momjian 已提交
2308

P
Peter Eisentraut 已提交
2309
	/* printf("Comparing %u to %u\n", id1, id2); */
B
Bruce Momjian 已提交
2310

2311 2312 2313 2314
	cmpval = oidcmp(id1, id2);

	/* If we have a deterministic answer, return it. */
	if (cmpval != 0)
2315
		return cmpval;
2316 2317

	/* More comparisons required */
2318
	if (oideq(id1, te1->maxDepOidVal))	/* maxOid1 came from deps */
2319
	{
2320 2321
		if (oideq(id2, te2->maxDepOidVal))		/* maxOid2 also came from
												 * deps */
2322
		{
2323 2324
			cmpval = oidcmp(te1->oidVal, te2->oidVal);	/* Just compare base
														 * OIDs */
2325
		}
2326 2327
		else
/* MaxOid2 was entry OID */
2328
		{
2329
			return 1;			/* entry1 > entry2 */
2330
		};
2331 2332 2333
	}
	else
/* must have oideq(id1, te1->oidVal) => maxOid1 = Oid1 */
2334
	{
2335
		if (oideq(id2, te2->maxDepOidVal))		/* maxOid2 came from deps */
2336
		{
2337
			return -1;			/* entry1 < entry2 */
2338
		}
2339 2340
		else
/* MaxOid2 was entry OID - deps don't matter */
2341 2342 2343 2344 2345
		{
			cmpval = 0;
		};
	};

2346 2347 2348
	/*
	 * If we get here, then we've done another comparison Once again, a 0
	 * result means we require even more
2349 2350 2351 2352
	 */
	if (cmpval != 0)
		return cmpval;

2353 2354 2355 2356
	/*
	 * Entire OID details match, so use ID number (ie. original pg_dump
	 * order)
	 */
2357
	return _tocSortCompareByIDNum(te1, te2);
B
Bruce Momjian 已提交
2358 2359
}

B
Bruce Momjian 已提交
2360 2361
static int
_tocSortCompareByIDNum(const void *p1, const void *p2)
B
Bruce Momjian 已提交
2362
{
B
Bruce Momjian 已提交
2363 2364 2365 2366
	TocEntry   *te1 = *(TocEntry **) p1;
	TocEntry   *te2 = *(TocEntry **) p2;
	int			id1 = te1->id;
	int			id2 = te2->id;
B
Bruce Momjian 已提交
2367

B
Bruce Momjian 已提交
2368
	/* printf("Comparing %d to %d\n", id1, id2); */
B
Bruce Momjian 已提交
2369

B
Bruce Momjian 已提交
2370 2371 2372 2373 2374 2375
	if (id1 < id2)
		return -1;
	else if (id1 > id2)
		return 1;
	else
		return 0;
B
Bruce Momjian 已提交
2376 2377
}

2378 2379 2380 2381
/*
 * Assuming Oid and depOid are set, work out the various
 * Oid values used in sorting.
 */
2382
static void
2383 2384 2385 2386 2387 2388
_fixupOidInfo(TocEntry *te)
{
	te->oidVal = atooid(te->oid);
	te->maxDepOidVal = _findMaxOID(te->depOid);

	/* For the purpose of sorting, find the max OID. */
2389
	if (oidcmp(te->oidVal, te->maxDepOidVal) >= 0)
2390 2391 2392 2393 2394
		te->maxOidVal = te->oidVal;
	else
		te->maxOidVal = te->maxDepOidVal;
}

2395 2396
/*
 * Find the max OID value for a given list of string Oid values
2397 2398 2399 2400 2401 2402
 */
static Oid
_findMaxOID(const char *((*deps)[]))
{
	const char *dep;
	int			i;
2403
	Oid			maxOid = (Oid) 0;
2404 2405 2406 2407 2408 2409
	Oid			currOid;

	if (!deps)
		return maxOid;

	i = 0;
2410
	while ((dep = (*deps)[i++]) != NULL)
2411 2412 2413 2414 2415 2416 2417 2418 2419
	{
		currOid = atooid(dep);
		if (oidcmp(maxOid, currOid) < 0)
			maxOid = currOid;
	}

	return maxOid;
}

2420 2421 2422 2423 2424 2425 2426 2427
/*
 * Maybe I can use this somewhere...
 *
 *create table pgdump_blob_path(p text);
 *insert into pgdump_blob_path values('/home/pjw/work/postgresql-cvs/pgsql/src/bin/pg_dump_140');
 *
 *insert into dump_blob_xref select 12345,lo_import(p || '/q.q') from pgdump_blob_path;
 */