⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pg_backup_archiver.c

📁 PostgreSQL7.4.6 for Linux
💻 C
📖 第 1 页 / 共 4 页
字号:
	/* 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->tag) != 0)				return 0;		}		else if (strcmp(te->desc, "INDEX") == 0)		{			if (!ropt->selIndex)				return 0;			if (ropt->indexNames && strcmp(ropt->indexNames, te->tag) != 0)				return 0;		}		else if (strcmp(te->desc, "FUNCTION") == 0)		{			if (!ropt->selFunction)				return 0;			if (ropt->functionNames && strcmp(ropt->functionNames, te->tag) != 0)				return 0;		}		else if (strcmp(te->desc, "TRIGGER") == 0)		{			if (!ropt->selTrigger)				return 0;			if (ropt->triggerNames && strcmp(ropt->triggerNames, te->tag) != 0)				return 0;		}		else			return 0;	}	/*	 * 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			res = res & ~REQ_DATA;	}	/*	 * Special case: <Init> type with <Max OID> tag; this is part of a	 * DATA restore even though it has SQL.	 */	if ((strcmp(te->desc, "<Init>") == 0) && (strcmp(te->tag, "Max OID") == 0))		res = REQ_DATA;	/* Mask it if we only want schema */	if (ropt->schemaOnly)		res = res & REQ_SCHEMA;	/* Mask it we only want data */	if (ropt->dataOnly)		res = res & REQ_DATA;	/* Mask it if we don't have a schema contribution */	if (!te->defn || strlen(te->defn) == 0)		res = res & ~REQ_SCHEMA;	/* Finally, if we used a list, limit based on that as well */	if (ropt->limitToList && !ropt->idWanted[te->id - 1])		return 0;	return res;}/* * Issue SET commands for parameters that we want to have set the same way * at all times during execution of a restore script. */static void_doSetFixedOutputState(ArchiveHandle *AH){	TocEntry   *te;	/* If we have an encoding setting, emit that */	te = AH->toc->next;	while (te != AH->toc)	{		if (strcmp(te->desc, "ENCODING") == 0)		{			ahprintf(AH, "%s", te->defn);			break;		}		te = te->next;	}	/* Make sure function checking is disabled */	ahprintf(AH, "SET check_function_bodies = false;\n");	ahprintf(AH, "\n");}/* * Issue a SET SESSION AUTHORIZATION command.  Caller is responsible * for updating state if appropriate.  If user is NULL or an empty string, * the specification DEFAULT will be used. */static void_doSetSessionAuth(ArchiveHandle *AH, const char *user){	PQExpBuffer cmd = createPQExpBuffer();	appendPQExpBuffer(cmd, "SET SESSION AUTHORIZATION ");	/*	 * SQL requires a string literal here.	Might as well be correct.	 */	if (user && *user)		appendStringLiteral(cmd, user, false);	else		appendPQExpBuffer(cmd, "DEFAULT");	appendPQExpBuffer(cmd, ";");	if (RestoringToDB(AH))	{		PGresult   *res;		res = PQexec(AH->connection, cmd->data);		if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)			die_horribly(AH, modulename, "could not set session user to \"%s\": %s",						 user, PQerrorMessage(AH->connection));		PQclear(res);	}	else		ahprintf(AH, "%s\n\n", cmd->data);	destroyPQExpBuffer(cmd);}/* * Issue the commands to connect to the specified database * as the specified user. * * If we're currently restoring right into a database, this will * actually establish a connection. Otherwise it puts a \connect into * the script output. */static void_reconnectToDB(ArchiveHandle *AH, const char *dbname, const char *user){	if (RestoringToDB(AH))		ReconnectToServer(AH, dbname, user);	else	{		PQExpBuffer qry = createPQExpBuffer();		appendPQExpBuffer(qry, "\\connect %s",						  dbname ? fmtId(dbname) : "-");		appendPQExpBuffer(qry, " %s\n\n",						  fmtId(user));		ahprintf(AH, qry->data);		destroyPQExpBuffer(qry);	}	/*	 * NOTE: currUser keeps track of what the imaginary session user in	 * our script is	 */	if (AH->currUser)		free(AH->currUser);	AH->currUser = strdup(user);	/* don't assume we still know the output schema */	if (AH->currSchema)		free(AH->currSchema);	AH->currSchema = strdup("");	/* re-establish fixed state */	_doSetFixedOutputState(AH);}/* * Become the specified user, and update state to avoid redundant commands * * NULL or empty argument is taken to mean restoring the session default */static void_becomeUser(ArchiveHandle *AH, const char *user){	if (!user)		user = "";				/* avoid null pointers */	if (AH->currUser && strcmp(AH->currUser, user) == 0)		return;					/* no need to do anything */	_doSetSessionAuth(AH, user);	/*	 * NOTE: currUser keeps track of what the imaginary session user in	 * our script is	 */	if (AH->currUser)		free(AH->currUser);	AH->currUser = strdup(user);}/* * Become the owner of the the given TOC entry object.  If * changes in ownership are not allowed, this doesn't do anything. */static void_becomeOwner(ArchiveHandle *AH, TocEntry *te){	if (AH->ropt && AH->ropt->noOwner)		return;	_becomeUser(AH, te->owner);}/* * Issue the commands to select the specified schema as the current schema * in the target database. */static void_selectOutputSchema(ArchiveHandle *AH, const char *schemaName){	PQExpBuffer qry;	if (!schemaName || *schemaName == '\0' ||		strcmp(AH->currSchema, schemaName) == 0)		return;					/* no need to do anything */	qry = createPQExpBuffer();	appendPQExpBuffer(qry, "SET search_path = %s",					  fmtId(schemaName));	if (strcmp(schemaName, "pg_catalog") != 0)		appendPQExpBuffer(qry, ", pg_catalog");	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		ahprintf(AH, "%s;\n\n", qry->data);	if (AH->currSchema)		free(AH->currSchema);	AH->currSchema = strdup(schemaName);	destroyPQExpBuffer(qry);}static int_printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isData){	char	   *pfx;	/* Select owner and schema as necessary */	_becomeOwner(AH, te);	_selectOutputSchema(AH, te->namespace);	if (isData)		pfx = "Data for ";	else		pfx = "";	ahprintf(AH, "--\n-- %sTOC entry %d (OID %s)\n-- Name: %s; Type: %s; Schema: %s; Owner: %s\n",			 pfx, te->id, te->oid, te->tag, te->desc,			 te->namespace ? te->namespace : "-",			 te->owner);	if (AH->PrintExtraTocPtr !=NULL)		(*AH->PrintExtraTocPtr) (AH, te);	ahprintf(AH, "--\n\n");	/*	 * Really crude hack for suppressing AUTHORIZATION clause of CREATE SCHEMA	 * when --no-owner mode is selected.  This is ugly, but I see no other	 * good way ...	 */	if (AH->ropt && AH->ropt->noOwner && strcmp(te->desc, "SCHEMA") == 0)	{		ahprintf(AH, "CREATE SCHEMA %s;\n\n\n", te->tag);	}	else	{		/* normal case */		if (strlen(te->defn) > 0)			ahprintf(AH, "%s\n\n", te->defn);	}	/*	 * If it's an ACL entry, it might contain SET SESSION AUTHORIZATION	 * commands, so we can no longer assume we know the current auth setting.	 */	if (strncmp(te->desc, "ACL", 3) == 0)	{		if (AH->currUser)			free(AH->currUser);		AH->currUser = NULL;	}	return 1;}voidWriteHead(ArchiveHandle *AH){	struct tm	crtm;	(*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->offSize);	(*AH->WriteBytePtr) (AH, AH->format);#ifndef HAVE_LIBZ	if (AH->compression != 0)		write_msg(modulename, "WARNING: requested compression not available in this "				  "installation -- archive will be uncompressed\n");	AH->compression = 0;#endif	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);	WriteStr(AH, PQdb(AH->connection));}voidReadHead(ArchiveHandle *AH){	char		tmpMag[7];	int			fmt;	struct tm	crtm;	/* If we haven't already read the header... */	if (!AH->readHeader)	{		(*AH->ReadBufPtr) (AH, tmpMag, 5);		if (strncmp(tmpMag, "PGDMP", 5) != 0)			die_horribly(AH, modulename, "did not find magic string in file header\n");		AH->vmaj = (*AH->ReadBytePtr) (AH);		AH->vmin = (*AH->ReadBytePtr) (AH);		if (AH->vmaj > 1 || ((AH->vmaj == 1) && (AH->vmin > 0)))		/* Version > 1.0 */			AH->vrev = (*AH->ReadBytePtr) (AH);		else			AH->vrev = 0;		AH->version = ((AH->vmaj * 256 + AH->vmin) * 256 + AH->vrev) * 256 + 0;		if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX)			die_horribly(AH, modulename, "unsupported version (%d.%d) in file header\n",						 AH->vmaj, AH->vmin);		AH->intSize = (*AH->ReadBytePtr) (AH);		if (AH->intSize > 32)			die_horribly(AH, modulename, "sanity check on integer size (%lu) failed\n",						 (unsigned long) AH->intSize);		if (AH->intSize > sizeof(int))			write_msg(modulename, "WARNING: archive was made on a machine with larger integers, some operations may fail\n");		if (AH->version >= K_VERS_1_7)			AH->offSize = (*AH->ReadBytePtr) (AH);		else			AH->offSize = AH->intSize;		fmt = (*AH->ReadBytePtr) (AH);		if (AH->format != fmt)			die_horribly(AH, modulename, "expected format (%d) differs from format found in file (%d)\n",						 AH->format, fmt);	}	if (AH->version >= K_VERS_1_2)	{		if (AH->version < K_VERS_1_4)			AH->compression = (*AH->ReadBytePtr) (AH);		else			AH->compression = ReadInt(AH);	}	else		AH->compression = Z_DEFAULT_COMPRESSION;#ifndef HAVE_LIBZ	if (AH->compression != 0)		write_msg(modulename, "WARNING: archive is compressed, but this installation does not support compression -- no data will be available\n");#endif	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);		if (AH->createDate == (time_t) -1)			write_msg(modulename, "WARNING: invalid creation date in header\n");	}}/* * checkSeek *	  check to see if fseek can be performed. */boolcheckSeek(FILE *fp){	if (fseeko(fp, 0, SEEK_CUR) != 0)		return false;	else if (sizeof(off_t) > sizeof(long))		/*		 * At this point, off_t is too large for long, so we return based		 * on whether an off_t version of fseek is available.		 */#ifdef HAVE_FSEEKO		return true;#else		return false;#endif	else		return true;}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) - %u\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 because 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) - %u\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;	Oid			id1 = te1->maxOidVal;	Oid			id2 = te2->maxOidVal;	int			cmpval;	/* printf("Comparing %u to %u\n", id1, id2); */	cmpval = oidcmp(id1, id2);	/* If we have a deterministic answer, return it. */	if (cmpval != 0)		return cmpval;	/* More comparisons required */	if (oideq(id1, te1->maxDepOidVal))	/* maxOid1 came from deps */	{		if (oideq(id2, te2->maxDepOidVal))		/* maxOid2 also came from												 * deps */		{			cmpval = oidcmp(te1->oidVal, te2->oidVal);	/* Just compare base														 * OIDs */		}		else/* MaxOid2 was entry OID */		{			return 1;			/* entry1 > entry2 */		};	}	else/* must have oideq(id1, te1->oidVal) => maxOid1 = Oid1 */	{		if (oideq(id2, te2->maxDepOidVal))		/* maxOid2 came from deps */		{			return -1;			/* entry1 < entry2 */		}		else/* MaxOid2 was entry OID - deps don't matter */		{			cmpval = 0;		};	};	/*	 * If we get here, then we've done another comparison Once again, a 0	 * result means we require even more	 */	if (cmpval != 0)		return cmpval;	/*	 * Entire OID details match, so use ID number (ie. original pg_dump	 * order)	 */	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;}/* * Assuming Oid and depOid are set, work out the various * Oid values used in sorting. */static void_fixupOidInfo(TocEntry *te){	te->oidVal = atooid(te->oid);	te->maxDepOidVal = _findMaxOID(te->depOid);	/* For the purpose of sorting, find the max OID. */	if (oidcmp(te->oidVal, te->maxDepOidVal) >= 0)		te->maxOidVal = te->oidVal;	else		te->maxOidVal = te->maxDepOidVal;}/* * Find the max OID value for a given list of string Oid values */static Oid_findMaxOID(const char *((*deps)[])){	const char *dep;	int			i;	Oid			maxOid = (Oid) 0;	Oid			currOid;	if (!deps)		return maxOid;	i = 0;	while ((dep = (*deps)[i++]) != NULL)	{		currOid = atooid(dep);		if (oidcmp(maxOid, currOid) < 0)			maxOid = currOid;	}	return maxOid;}/* * 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; */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -