pg_backup_archiver.c 26.5 KB
Newer Older
B
Bruce Momjian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/*-------------------------------------------------------------------------
 *
 * 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
 * 	as this notice is not removed.
 *
 *	The author is not responsible for loss or damages that may
 *	result from it's use.
 *
 *
 * IDENTIFICATION
 *
 * Modifications - 28-Jun-2000 - pjw@rhyme.com.au
 *
 *	Initial version. 
 *
 *-------------------------------------------------------------------------
 */

#include "pg_backup.h"
#include "pg_backup_archiver.h"
#include <string.h>
#include <unistd.h> /* for dup */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

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, ArchiveFormat fmt, 
				int compression, ArchiveMode mode);
static int 		_printTocEntry(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt);
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);
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);
static char	*progname = "Archiver";

/*
 *  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.
 *
 */


/* Create a new archive */
/* Public */
Archive* CreateArchive(const char* FileSpec, ArchiveFormat fmt, int compression)
{
    ArchiveHandle*	AH = _allocAH(FileSpec, fmt, compression, archModeWrite);
    return (Archive*)AH;
}

/* Open an existing archive */
/* Public */
Archive* OpenArchive(const char* FileSpec, ArchiveFormat fmt) 
{
    ArchiveHandle*      AH = _allocAH(FileSpec, fmt, 0, archModeRead);
    return (Archive*)AH;
}

/* Public */
void	CloseArchive(Archive* AHX)
{
    ArchiveHandle*      AH = (ArchiveHandle*)AHX;
    (*AH->ClosePtr)(AH);

    /* Close the output */
    if (AH->gzOut)
	GZCLOSE(AH->OF);
    else if (AH->OF != stdout)
	fclose(AH->OF);
}

/* Public */
void RestoreArchive(Archive* AHX, RestoreOptions *ropt)
{
    ArchiveHandle*	AH = (ArchiveHandle*) AHX;
    TocEntry		*te = AH->toc->next;
    int			reqs;
    OutputContext	sav;

    if (ropt->filename || ropt->compression)
	sav = SetOutput(AH, ropt->filename, ropt->compression);

    ahprintf(AH, "--\n-- Selected TOC Entries:\n--\n");

    /* Drop the items at the start, in reverse order */
    if (ropt->dropSchema) {
	te = AH->toc->prev;
	while (te != AH->toc) {
	    reqs = _tocEntryRequired(te, ropt);
	    if ( (reqs & 1) && te->dropStmt) {  /* We want the schema */
		ahprintf(AH, "%s", te->dropStmt);
	    }
	    te = te->prev;
	}
    }

    te = AH->toc->next;
    while (te != AH->toc) {
	reqs = _tocEntryRequired(te, ropt);

	if (reqs & 1) /* We want the schema */
	    _printTocEntry(AH, te, ropt);

	if (AH->PrintTocDataPtr != NULL && (reqs & 2) != 0) {
121
#ifndef HAVE_LIBZ
B
Bruce Momjian 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	    if (AH->compression != 0)
		die_horribly("%s: Unable to restore data from a compressed archive\n", progname);
#endif
	    _disableTriggers(AH, te, ropt);
	    (*AH->PrintTocDataPtr)(AH, te, ropt);
	    _enableTriggers(AH, te, ropt);
	}
	te = te->next;
    }

    if (ropt->filename)
	ResetOutput(AH, sav);

}

RestoreOptions*		NewRestoreOptions(void)
{
	RestoreOptions* opts;

	opts = (RestoreOptions*)calloc(1, sizeof(RestoreOptions));

	opts->format = archUnknown;

	return opts;
}

static void _disableTriggers(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
{
    ahprintf(AH, "-- Disable triggers\n");
    ahprintf(AH, "UPDATE \"pg_class\" SET \"reltriggers\" = 0 WHERE \"relname\" !~ '^pg_';\n\n");
}

static void _enableTriggers(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
{
    ahprintf(AH, "-- Enable triggers\n");
    ahprintf(AH, "BEGIN TRANSACTION;\n");
    ahprintf(AH, "CREATE TEMP TABLE \"tr\" (\"tmp_relname\" name, \"tmp_reltriggers\" smallint);\n");
    ahprintf(AH, "INSERT INTO \"tr\" SELECT C.\"relname\", count(T.\"oid\") FROM \"pg_class\" C,"
	    " \"pg_trigger\" T WHERE C.\"oid\" = T.\"tgrelid\" AND C.\"relname\" !~ '^pg_' "
	    " GROUP BY 1;\n");
    ahprintf(AH, "UPDATE \"pg_class\" SET \"reltriggers\" = TMP.\"tmp_reltriggers\" "
	    "FROM \"tr\" TMP WHERE "
	    "\"pg_class\".\"relname\" = TMP.\"tmp_relname\";\n");
    ahprintf(AH, "DROP TABLE \"tr\";\n");
    ahprintf(AH, "COMMIT TRANSACTION;\n\n");
}


/*
 * This is a routine that is available to pg_dump, hence the 'Archive*' parameter.
 */

/* Public */
int	WriteData(Archive* AHX, const void* data, int dLen)
{
    ArchiveHandle*      AH = (ArchiveHandle*)AHX;

    return (*AH->WriteDataPtr)(AH, data, dLen);
}

/*
 * Create a new TOC entry. The TOC was designed as a TOC, but is now the 
 * repository for all metadata. But the name has stuck.
 */

/* Public */
void	ArchiveEntry(Archive* AHX, const char* oid, const char* name,
			const char* desc, const char* (deps[]), const char* defn,
                        const char* dropStmt, const char* owner,
			DataDumperPtr dumpFn, void* dumpArg)
{
    ArchiveHandle*	AH = (ArchiveHandle*)AHX;
    TocEntry*		newToc;

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

    newToc = (TocEntry*)malloc(sizeof(TocEntry));
    if (!newToc)
	die_horribly("Archiver: unable to allocate memory for TOC entry\n");

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

    newToc->id = AH->lastID;
    newToc->oid = strdup(oid);
    newToc->oidVal = atoi(oid);
    newToc->name = strdup(name);
    newToc->desc = strdup(desc);
    newToc->defn = strdup(defn);
    newToc->dropStmt = strdup(dropStmt);
    newToc->owner = strdup(owner);
    newToc->printed = 0;
    newToc->formatData = NULL;
    newToc->dataDumper = dumpFn,
    newToc->dataDumperArg = dumpArg;

    newToc->hadDumper = dumpFn ? 1 : 0;

    if (AH->ArchiveEntryPtr != NULL) {
		(*AH->ArchiveEntryPtr)(AH, newToc);
    }

    /* printf("New toc owned by '%s', oid %d\n", newToc->owner, newToc->oidVal); */
}

/* Public */
void PrintTOCSummary(Archive* AHX, RestoreOptions *ropt)
{
    ArchiveHandle*	AH = (ArchiveHandle*) AHX;
    TocEntry		*te = AH->toc->next;
    OutputContext	sav;

    if (ropt->filename)
	sav = SetOutput(AH, ropt->filename, ropt->compression);

    ahprintf(AH, ";\n; Selected TOC Entries:\n;\n");

    while (te != AH->toc) {
	if (_tocEntryRequired(te, ropt) != 0)
		ahprintf(AH, "%d; %d %s %s %s\n", te->id, te->oidVal, te->desc, te->name, te->owner);
		te = te->next;
    }

    if (ropt->filename)
	ResetOutput(AH, sav);
}

/***********
 * Sorting and Reordering
 ***********/

/*
 * Move TOC entries of the specified type to the START of the TOC.
 */
/* Public */
void MoveToStart(Archive* AHX, char *oType) 
{
    ArchiveHandle* AH = (ArchiveHandle*)AHX;
    TocEntry	*te = AH->toc->next;
    TocEntry	*newTe;

    while (te != AH->toc) {
		te->_moved = 0;
		te = te->next;
    }

    te = AH->toc->prev;
    while (te != AH->toc && !te->_moved) {
	newTe = te->prev;
	if (strcmp(te->desc, oType) == 0) {
	    _moveAfter(AH, AH->toc, te);
	}
		te = newTe;
    }
}


/*
 * Move TOC entries of the specified type to the end of the TOC.
 */
/* Public */
void MoveToEnd(Archive* AHX, char *oType) 
{
    ArchiveHandle* AH = (ArchiveHandle*)AHX;
    TocEntry	*te = AH->toc->next;
    TocEntry	*newTe;

    while (te != AH->toc) {
	te->_moved = 0;
	te = te->next;
    }

    te = AH->toc->next;
    while (te != AH->toc && !te->_moved) {
	newTe = te->next;
	if (strcmp(te->desc, oType) == 0) {
	    _moveBefore(AH, AH->toc, te);
	}
		te = newTe;
    }
}

/* 
 * Sort TOC by OID
 */
/* Public */
void SortTocByOID(Archive* AHX)
{
    ArchiveHandle* AH = (ArchiveHandle*)AHX;
    _SortToc(AH, _tocSortCompareByOIDNum);
}

/*
 * Sort TOC by ID
 */
/* Public */
void SortTocByID(Archive* AHX)
{
    ArchiveHandle* AH = (ArchiveHandle*)AHX;
    _SortToc(AH, _tocSortCompareByIDNum);
}

void SortTocFromFile(Archive* AHX, RestoreOptions *ropt)
{
    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;
    }

    /* Set prev entry as head of list */
    tePrev = AH->toc;

    /* Setup the file */
    fh = fopen(ropt->tocFile, PG_BINARY_R);
    if (!fh)
	die_horribly("%s: could not open TOC file\n", progname);

    while (fgets(buf, 1024, fh) != NULL)
    {
	/* 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)
	{
	    fprintf(stderr, "%s: warning - line ignored: %s\n", progname, buf);
	    continue;
	}

	/* Find TOC entry */
	te = _getTocEntry(AH, id);
	if (!te) 
	    die_horribly("%s: could not find entry for id %d\n",progname, id);

	ropt->idWanted[id-1] = 1;

	_moveAfter(AH, tePrev, te);
	tePrev = te;
    }

    fclose(fh);
}

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

/* Public */
int archputs(const char *s, Archive* AH) {
    return WriteData(AH, s, strlen(s));
}

/* Public */
int archputc(const char c, Archive* AH) {
    return WriteData(AH, &c, 1);
}

/* Public */
int archprintf(Archive* AH, const char *fmt, ...)
{
    char 	*p = NULL;
    va_list 	ap;
418
    int		bSize = strlen(fmt) + 256;
B
Bruce Momjian 已提交
419 420 421
    int		cnt = -1;

    va_start(ap, fmt);
422 423 424 425

    /* 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 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	if (p != NULL) free(p);
	bSize *= 2;
	if ((p = malloc(bSize)) == NULL)
	{
	    va_end(ap);
	    die_horribly("%s: could not allocate buffer for archprintf\n", progname);
	}
	cnt = vsnprintf(p, bSize, fmt, ap);
    }
    va_end(ap);
    WriteData(AH, p, cnt);
    free(p);
    return cnt;
}


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

OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression)
{
    OutputContext	sav;
449
#ifdef HAVE_LIBZ
B
Bruce Momjian 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    char		fmode[10];
#endif
    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 */
470
#ifdef HAVE_LIBZ
B
Bruce Momjian 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    if (compression != 0)
    {
	sprintf(fmode, "wb%d", compression);
	if (fn) {
	    AH->OF = gzdopen(dup(fn), fmode); /* Don't use PG_BINARY_x since this is zlib */
	} else {
	    AH->OF = gzopen(filename, fmode);
	}
	AH->gzOut = 1;
    } else { /* Use fopen */
#endif
	if (fn) {
	    AH->OF = fdopen(dup(fn), PG_BINARY_W);
	} else {
	    AH->OF = fopen(filename, PG_BINARY_W);
	}
	AH->gzOut = 0;
488
#ifdef HAVE_LIBZ
B
Bruce Momjian 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    }
#endif

    return sav;
}

void ResetOutput(ArchiveHandle* AH, OutputContext sav)
{
    if (AH->gzOut)
	GZCLOSE(AH->OF);
    else
	fclose(AH->OF);

    AH->gzOut = sav.gzOut;
    AH->OF = sav.OF;
}



/*
 *  Print formatted text to the output file (usually stdout).
 */
int ahprintf(ArchiveHandle* AH, const char *fmt, ...)
{
    char 	*p = NULL;
    va_list 	ap;
515
    int		bSize = strlen(fmt) + 256; /* Should be enough */
B
Bruce Momjian 已提交
516 517 518
    int		cnt = -1;

    va_start(ap, fmt);
519 520 521
    /* 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 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	if (p != NULL) free(p);
	bSize *= 2;
	p = (char*)malloc(bSize);
	if (p == NULL)
	{
	    va_end(ap);
	    die_horribly("%s: could not allocate buffer for ahprintf\n", progname);
	}
	cnt = vsnprintf(p, bSize, fmt, ap);
    }
    va_end(ap);
    ahwrite(p, 1, cnt, AH);
    free(p);
    return cnt;
}

/*
 *  Write buffer to the output file (usually stdout).
 */
int ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle* AH)
{
    if (AH->gzOut)
	return GZWRITE((void*)ptr, size, nmemb, AH->OF);
    else
	return fwrite((void*)ptr, size, nmemb, AH->OF);
}


void die_horribly(const char *fmt, ...)
{
    va_list 	ap;

    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    exit(1);
}

static void _moveAfter(ArchiveHandle* AH, TocEntry* pos, TocEntry* te)
{
    te->prev->next = te->next;
    te->next->prev = te->prev;

    te->prev = pos;
    te->next = pos->next;

    pos->next->prev = te;
    pos->next = te;

    te->_moved = 1;
}

static void _moveBefore(ArchiveHandle* AH, TocEntry* pos, TocEntry* te)
{
    te->prev->next = te->next;
    te->next->prev = te->prev;

    te->prev = pos->prev;
    te->next = pos;
    pos->prev->next = te;
    pos->prev = te;

    te->_moved = 1;
}

static TocEntry* _getTocEntry(ArchiveHandle* AH, int id)
{
    TocEntry    *te;

    te = AH->toc->next;
    while (te != AH->toc) {
	if (te->id == id)
	    return te;
	te = te->next;
    }
    return NULL;
}

int	TocIDRequired(ArchiveHandle* AH, int id, RestoreOptions *ropt)
{
    TocEntry	*te = _getTocEntry(AH, id);

    if (!te)
	return 0;

    return _tocEntryRequired(te, ropt);
}

int	WriteInt(ArchiveHandle* AH, int i)
{
    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);
	i = -i;
    } else {
	(*AH->WriteBytePtr)(AH, 0);
    }
    
    for(b = 0 ; b < AH->intSize ; b++) {
        (*AH->WriteBytePtr)(AH, i & 0xFF);
        i = i / 256;
    }

    return AH->intSize + 1;
}

int    	ReadInt(ArchiveHandle* AH)
{
    int res = 0;
    int shft = 1;
    int bv, b;
    int	sign = 0; /* Default positive */

    if (AH->version > K_VERS_1_0)
	/* Read a sign byte */
	sign = (*AH->ReadBytePtr)(AH);

    for( b = 0 ; b < AH->intSize ; b++) {
        bv = (*AH->ReadBytePtr)(AH);
        res = res  + shft * bv;
        shft *= 256;
    }

    if (sign)
	res = - res;

    return res;
}

int	WriteStr(ArchiveHandle* AH, char* c)
{
    int l = WriteInt(AH, strlen(c));
    return (*AH->WriteBufPtr)(AH, c, strlen(c)) + l;
}

char*	ReadStr(ArchiveHandle* AH)
{
    char*	buf;
    int		l;

    l = ReadInt(AH);
    buf = (char*)malloc(l+1);
    if (!buf)
	die_horribly("Archiver: Unable to allocate sufficient memory in ReadStr\n");

    (*AH->ReadBufPtr)(AH, (void*)buf, l);
    buf[l] = '\0';
    return buf;
}

int _discoverArchiveFormat(ArchiveHandle* AH)
{
    FILE	*fh;
    char	sig[6]; /* More than enough */
    int		cnt;
    int		wantClose = 0;

689

B
Bruce Momjian 已提交
690 691 692 693 694 695 696 697 698 699 700 701
    if (AH->fSpec) {
	wantClose = 1;
	fh = fopen(AH->fSpec, PG_BINARY_R);
    } else {
	fh = stdin;
    }

    if (!fh)
	die_horribly("Archiver: could not open input file\n");

    cnt = fread(sig, 1, 5, fh);

702 703
    if (cnt != 5)
        die_horribly("%s: input file is too short, or is unreadable\n", progname);
B
Bruce Momjian 已提交
704 705

    if (strncmp(sig, "PGDMP", 5) != 0)
706
	die_horribly("%s: input file does not appear to be a valid archive\n", progname);
B
Bruce Momjian 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

    AH->vmaj = fgetc(fh);
    AH->vmin = fgetc(fh);

    /* Check header version; varies from V1.0 */
    if (AH->vmaj > 1 || ( (AH->vmaj == 1) && (AH->vmin > 0) ) ) /* Version > 1.0 */ 
	AH->vrev = fgetc(fh);
    else
	AH->vrev = 0;

    AH->intSize = fgetc(fh);
    AH->format = fgetc(fh);

    /* Make a convenient integer <maj><min><rev>00 */
    AH->version = ( (AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev ) * 256 + 0;

    /* If we can't seek, then mark the header as read */
    if (fseek(fh, 0, SEEK_SET) != 0) 
	AH->readHeader = 1;

    /* Close the file */
    if (wantClose)
	fclose(fh);

    return AH->format;

}


/*
 * Allocate an archive handle
 */
static ArchiveHandle* _allocAH(const char* FileSpec, ArchiveFormat fmt, 
				int compression, ArchiveMode mode) {
    ArchiveHandle*	AH;

743
    AH = (ArchiveHandle*)calloc(1, sizeof(ArchiveHandle));
B
Bruce Momjian 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
    if (!AH) 
	die_horribly("Archiver: Could not allocate archive handle\n");

    AH->vmaj = K_VERS_MAJOR;
    AH->vmin = K_VERS_MINOR;

    AH->intSize = sizeof(int);
    AH->lastID = 0;
    if (FileSpec) {
	AH->fSpec = strdup(FileSpec);
    } else {
	AH->fSpec = NULL;
    }
    AH->FH = NULL;
    AH->formatData = NULL;

    AH->currToc = NULL;
    AH->currUser = "";

763
    AH->toc = (TocEntry*)calloc(1, sizeof(TocEntry));
B
Bruce Momjian 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    if (!AH->toc)
	die_horribly("Archiver: Could not allocate TOC header\n");

    AH->tocCount = 0;
    AH->toc->next = AH->toc;
    AH->toc->prev = AH->toc;
    AH->toc->id  = 0;
    AH->toc->oid  = NULL;
    AH->toc->name = NULL; /* eg. MY_SPECIAL_FUNCTION */
    AH->toc->desc = NULL; /* eg. FUNCTION */
    AH->toc->defn = NULL; /* ie. sql to define it */
    AH->toc->depOid = NULL;
    
    AH->mode = mode;
    AH->format = fmt;
    AH->compression = compression;

    AH->ArchiveEntryPtr = NULL;

    AH->StartDataPtr = NULL;
    AH->WriteDataPtr = NULL;
    AH->EndDataPtr = NULL;

    AH->WriteBytePtr = NULL;
    AH->ReadBytePtr = NULL;
    AH->WriteBufPtr = NULL;
    AH->ReadBufPtr = NULL;
    AH->ClosePtr = NULL;
    AH->WriteExtraTocPtr = NULL;
    AH->ReadExtraTocPtr = NULL;
    AH->PrintExtraTocPtr = NULL;

    AH->readHeader = 0;

    /* Open stdout with no compression for AH output handle */
    AH->gzOut = 0;
    AH->OF = stdout;

    if (fmt == archUnknown)
	fmt = _discoverArchiveFormat(AH);

    switch (fmt) {

	case archCustom:
	    InitArchiveFmt_Custom(AH);
	    break;

	case archFiles:
	    InitArchiveFmt_Files(AH);
	    break;

	case archPlainText:
	    InitArchiveFmt_PlainText(AH);
	    break;

	default:
	    die_horribly("Archiver: Unrecognized file format '%d'\n", fmt);
    }

    return AH;
}


void WriteDataChunks(ArchiveHandle* AH)
{
    TocEntry		*te = AH->toc->next;

    while (te != AH->toc) {
	if (te->dataDumper != NULL) {
	    AH->currToc = te;
	    /* printf("Writing data for %d (%x)\n", te->id, te); */
	    if (AH->StartDataPtr != NULL) {
		(*AH->StartDataPtr)(AH, te);
	    }

	    /* 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 (AH->EndDataPtr != NULL) {
		(*AH->EndDataPtr)(AH, te);
	    }
	    AH->currToc = NULL;
	}
	te = te->next;
    }
}

void WriteToc(ArchiveHandle* AH)
{
    TocEntry	*te = AH->toc->next;

    /* 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);
	WriteStr(AH, te->name);
	WriteStr(AH, te->desc);
	WriteStr(AH, te->defn);
	WriteStr(AH, te->dropStmt);
	WriteStr(AH, te->owner);
	if (AH->WriteExtraTocPtr) {
	    (*AH->WriteExtraTocPtr)(AH, te);
	}
	te = te->next;
    }
}

void ReadToc(ArchiveHandle* AH)
{
    int 		i;

    TocEntry	*te = AH->toc->next;

    AH->tocCount = ReadInt(AH);

    for( i = 0 ; i < AH->tocCount ; i++) {

	te = (TocEntry*)malloc(sizeof(TocEntry));
	te->id = ReadInt(AH);

	/* Sanity check */
	if (te->id <= 0 || te->id > AH->tocCount)
	    die_horribly("Archiver: failed sanity check (bad entry id) - perhaps a corrupt TOC\n");

	te->hadDumper = ReadInt(AH);
	te->oid = ReadStr(AH);
	te->oidVal = atoi(te->oid);
	te->name = ReadStr(AH);
	te->desc = ReadStr(AH);
	te->defn = ReadStr(AH);
	te->dropStmt = ReadStr(AH);
	te->owner = ReadStr(AH);
	if (AH->ReadExtraTocPtr) {
	    (*AH->ReadExtraTocPtr)(AH, te);
	}
	te->prev = AH->toc->prev;
	AH->toc->prev->next = te;
	AH->toc->prev = te;
	te->next = AH->toc;
    }
}

static int _tocEntryRequired(TocEntry* te, RestoreOptions *ropt)
{
    int res = 3; /* Data and Schema */

    /* If it's an ACL, maybe ignore it */
    if (ropt->aclsSkip && strcmp(te->desc,"ACL") == 0)
	return 0;

    /* Check if tablename only is wanted */
    if (ropt->selTypes)
    {
	if ( (strcmp(te->desc, "TABLE") == 0) || (strcmp(te->desc, "TABLE DATA") == 0) )
	{
	    if (!ropt->selTable)
		return 0;
	    if (ropt->tableNames && strcmp(ropt->tableNames, te->name) != 0)
		return 0;
       	} else if (strcmp(te->desc, "INDEX") == 0) {
	    if (!ropt->selIndex)
		return 0;
	    if (ropt->indexNames && strcmp(ropt->indexNames, te->name) != 0)
		return 0;
	} else if (strcmp(te->desc, "FUNCTION") == 0) {
	    if (!ropt->selFunction)
		return 0;
	    if (ropt->functionNames && strcmp(ropt->functionNames, te->name) != 0)
		return 0;
	} else if (strcmp(te->desc, "TRIGGER") == 0) {
	    if (!ropt->selTrigger)
		return 0;
	    if (ropt->triggerNames && strcmp(ropt->triggerNames, te->name) != 0)
		return 0;
	} else {
	    return 0;
	}
    }

    /* Mask it if we only want schema */
    if (ropt->schemaOnly)
	res = res & 1;

    /* Mask it we only want data */
    if (ropt->dataOnly) 
       res = res & 2;

    /* Mask it if we don't have a schema contribition */
    if (!te->defn || strlen(te->defn) == 0) 
	res = res & 2;

    /* Mask it if we don't have a possible data contribition */
    if (!te->hadDumper)
	res = res & 1;

    /* Finally, if we used a list, limit based on that as well */
    if (ropt->limitToList && !ropt->idWanted[te->id - 1]) 
	return 0;

    return res;
}

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);
    }
    ahprintf(AH, "--\n\n");

    if (te->owner && strlen(te->owner) != 0 && strcmp(AH->currUser, te->owner) != 0) {
	ahprintf(AH, "\\connect - %s\n", te->owner);
	AH->currUser = te->owner;
    }

    ahprintf(AH, "%s\n", te->defn);

    return 1;
}

void WriteHead(ArchiveHandle* AH) 
{
    (*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);

1000
#ifndef HAVE_LIBZ
B
Bruce Momjian 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
    if (AH->compression != 0)
	fprintf(stderr, "%s: WARNING - requested compression not available in this installation - "
		    "archive will be uncompressed \n", progname);

    AH->compression = 0;
    (*AH->WriteBytePtr)(AH, 0);

#else

    (*AH->WriteBytePtr)(AH, AH->compression);

#endif    
}

void ReadHead(ArchiveHandle* AH)
{
    char	tmpMag[7];
    int		fmt;

1020
    if (!AH->readHeader) {
B
Bruce Momjian 已提交
1021

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

1024 1025
	if (strncmp(tmpMag,"PGDMP", 5) != 0)
	    die_horribly("Archiver: Did not fing magic PGDMP in file header\n");
B
Bruce Momjian 已提交
1026

1027 1028
	AH->vmaj = (*AH->ReadBytePtr)(AH);
	AH->vmin = (*AH->ReadBytePtr)(AH);
B
Bruce Momjian 已提交
1029

1030 1031 1032 1033 1034 1035
	if (AH->vmaj > 1 || ( (AH->vmaj == 1) && (AH->vmin > 0) ) ) /* Version > 1.0 */
	{
	    AH->vrev = (*AH->ReadBytePtr)(AH);
	} else {
	    AH->vrev = 0;
	}
B
Bruce Momjian 已提交
1036

1037
	AH->version = ( (AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev ) * 256 + 0;
B
Bruce Momjian 已提交
1038 1039


1040 1041
	if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)
	    die_horribly("Archiver: unsupported version (%d.%d) in file header\n", AH->vmaj, AH->vmin);
B
Bruce Momjian 已提交
1042

1043 1044 1045
	AH->intSize = (*AH->ReadBytePtr)(AH);
	if (AH->intSize > 32)
	    die_horribly("Archiver: sanity check on integer size (%d) failes\n", AH->intSize);
B
Bruce Momjian 已提交
1046

1047 1048 1049
	if (AH->intSize > sizeof(int))
	    fprintf(stderr, "\nWARNING: Backup file was made on a machine with larger integers, "
			    "some operations may fail\n");
B
Bruce Momjian 已提交
1050

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

1053 1054 1055 1056
	if (AH->format != fmt)
	    die_horribly("Archiver: expected format (%d) differs from format found in file (%d)\n", 
			    AH->format, fmt);
    }
B
Bruce Momjian 已提交
1057 1058 1059 1060 1061 1062 1063 1064

    if (AH->version >= K_VERS_1_2)
    {
	AH->compression = (*AH->ReadBytePtr)(AH);
    } else {
	AH->compression = Z_DEFAULT_COMPRESSION;
    }

1065 1066 1067
#ifndef HAVE_LIBZ
    if (AH->compression != 0)
	fprintf(stderr, "%s: WARNING - archive is compressed - any data will not be available\n", progname);
B
Bruce Momjian 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
#endif

}


static void _SortToc(ArchiveHandle* AH, TocSortCompareFn fn)
{
    TocEntry**	tea;
    TocEntry*	te;
    int		i;

    /* 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++) {
	/* printf("%d: %x (%x, %x) - %d\n", i, te, te->prev, te->next, te->oidVal); */
	tea[i] = te;
	te = te->next;
    }

    /* Sort it, but ignore the header entries */
    qsort(&(tea[1]), AH->tocCount, sizeof(TocEntry*), fn);

    /* Rebuild list: this works becuase we have headers at each end */
    for( i = 1 ; i <= AH->tocCount ; i++) {
	tea[i]->next = tea[i+1];
	tea[i]->prev = tea[i-1];
    }


    te = AH->toc;
    for( i = 0 ; i <= AH->tocCount+1 ; i++) {
	/* printf("%d: %x (%x, %x) - %d\n", i, te, te->prev, te->next, te->oidVal); */
	te = te->next;
    }


    AH->toc->next = tea[1];
    AH->toc->prev = tea[AH->tocCount];
}

static int	_tocSortCompareByOIDNum(const void* p1, const void* p2)
{
    TocEntry*	te1 = *(TocEntry**)p1;
    TocEntry*	te2 = *(TocEntry**)p2;
    int		id1 = te1->oidVal;
    int 	id2 = te2->oidVal;

    /* printf("Comparing %d to %d\n", id1, id2); */

    if (id1 < id2) {
	return -1;
    } else if (id1 > id2) { 
	return 1;
    } else {
	return _tocSortCompareByIDNum(te1, te2);
    }
}

static int	_tocSortCompareByIDNum(const void* p1, const void* p2)
{
    TocEntry*	te1 = *(TocEntry**)p1;
    TocEntry*	te2 = *(TocEntry**)p2;
    int		id1 = te1->id;
    int 	id2 = te2->id;

    /* printf("Comparing %d to %d\n", id1, id2); */

    if (id1 < id2) {
	return -1;
    } else if (id1 > id2) { 
	return 1;
    } else {
	return 0;
    }
}