| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* pg_backup_null.c |
| 4 |
* |
| 5 |
* Implementation of an archive that is never saved; it is used by |
| 6 |
* pg_dump to output a plain text SQL script instead of saving |
| 7 |
* a real archive. |
| 8 |
* |
| 9 |
* See the headers to pg_restore for more details. |
| 10 |
* |
| 11 |
* Copyright (c) 2000, Philip Warner |
| 12 |
* Rights are granted to use this software in any way so long |
| 13 |
* as this notice is not removed. |
| 14 |
* |
| 15 |
* The author is not responsible for loss or damages that may |
| 16 |
* result from its use. |
| 17 |
* |
| 18 |
* |
| 19 |
* IDENTIFICATION |
| 20 |
* src/bin/pg_dump/pg_backup_null.c |
| 21 |
* |
| 22 |
*------------------------------------------------------------------------- |
| 23 |
*/ |
| 24 |
#include "postgres_fe.h" |
| 25 |
|
| 26 |
#include "fe_utils/string_utils.h" |
| 27 |
#include "libpq/libpq-fs.h" |
| 28 |
#include "pg_backup_archiver.h" |
| 29 |
#include "pg_backup_utils.h" |
| 30 |
|
| 31 |
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen); |
| 32 |
static void _WriteLOData(ArchiveHandle *AH, const void *data, size_t dLen); |
| 33 |
static void _EndData(ArchiveHandle *AH, TocEntry *te); |
| 34 |
static int _WriteByte(ArchiveHandle *AH, const int i); |
| 35 |
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len); |
| 36 |
static void _CloseArchive(ArchiveHandle *AH); |
| 37 |
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te); |
| 38 |
static void _StartLOs(ArchiveHandle *AH, TocEntry *te); |
| 39 |
static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid); |
| 40 |
static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid); |
| 41 |
static void _EndLOs(ArchiveHandle *AH, TocEntry *te); |
| 42 |
|
| 43 |
|
| 44 |
/* |
| 45 |
* Initializer |
| 46 |
*/ |
| 47 |
void |
| 48 |
InitArchiveFmt_Null(ArchiveHandle *AH) |
| 49 |
{ |
| 50 |
/* Assuming static functions, this can be copied for each format. */ |
| 51 |
AH->WriteDataPtr = _WriteData; |
| 52 |
AH->EndDataPtr = _EndData; |
| 53 |
AH->WriteBytePtr = _WriteByte; |
| 54 |
AH->WriteBufPtr = _WriteBuf; |
| 55 |
AH->ClosePtr = _CloseArchive; |
| 56 |
AH->ReopenPtr = NULL; |
| 57 |
AH->PrintTocDataPtr = _PrintTocData; |
| 58 |
|
| 59 |
AH->StartLOsPtr = _StartLOs; |
| 60 |
AH->StartLOPtr = _StartLO; |
| 61 |
AH->EndLOPtr = _EndLO; |
| 62 |
AH->EndLOsPtr = _EndLOs; |
| 63 |
AH->ClonePtr = NULL; |
| 64 |
AH->DeClonePtr = NULL; |
| 65 |
|
| 66 |
/* |
| 67 |
* Now prevent reading... |
| 68 |
*/ |
| 69 |
if (AH->mode == archModeRead) |
| 70 |
pg_fatal("this format cannot be read"); |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* - Start a new TOC entry |
| 75 |
*/ |
| 76 |
|
| 77 |
/* |
| 78 |
* Called by dumper via archiver from within a data dump routine |
| 79 |
*/ |
| 80 |
static void |
| 81 |
_WriteData(ArchiveHandle *AH, const void *data, size_t dLen) |
| 82 |
{ |
| 83 |
/* Just send it to output, ahwrite() already errors on failure */ |
| 84 |
ahwrite(data, 1, dLen, AH); |
| 85 |
} |
| 86 |
|
| 87 |
/* |
| 88 |
* Called by dumper via archiver from within a data dump routine |
| 89 |
* We substitute this for _WriteData while emitting a LO |
| 90 |
*/ |
| 91 |
static void |
| 92 |
_WriteLOData(ArchiveHandle *AH, const void *data, size_t dLen) |
| 93 |
{ |
| 94 |
if (dLen > 0) |
| 95 |
{ |
| 96 |
PQExpBuffer buf = createPQExpBuffer(); |
| 97 |
|
| 98 |
appendByteaLiteralAHX(buf, |
| 99 |
(const unsigned char *) data, |
| 100 |
dLen, |
| 101 |
AH); |
| 102 |
|
| 103 |
ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data); |
| 104 |
|
| 105 |
destroyPQExpBuffer(buf); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
_EndData(ArchiveHandle *AH, TocEntry *te) |
| 111 |
{ |
| 112 |
ahprintf(AH, "\n\n"); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* Called by the archiver when starting to save BLOB DATA (not schema). |
| 117 |
* This routine should save whatever format-specific information is needed |
| 118 |
* to read the LOs back into memory. |
| 119 |
* |
| 120 |
* It is called just prior to the dumper's DataDumper routine. |
| 121 |
* |
| 122 |
* Optional, but strongly recommended. |
| 123 |
*/ |
| 124 |
static void |
| 125 |
_StartLOs(ArchiveHandle *AH, TocEntry *te) |
| 126 |
{ |
| 127 |
ahprintf(AH, "BEGIN;\n\n"); |
| 128 |
} |
| 129 |
|
| 130 |
/* |
| 131 |
* Called by the archiver when the dumper calls StartLO. |
| 132 |
* |
| 133 |
* Mandatory. |
| 134 |
* |
| 135 |
* Must save the passed OID for retrieval at restore-time. |
| 136 |
*/ |
| 137 |
static void |
| 138 |
_StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid) |
| 139 |
{ |
| 140 |
bool old_lo_style = (AH->version < K_VERS_1_12); |
| 141 |
|
| 142 |
if (oid == 0) |
| 143 |
pg_fatal("invalid OID for large object"); |
| 144 |
|
| 145 |
/* With an old archive we must do drop and create logic here */ |
| 146 |
if (old_lo_style && AH->public.ropt->dropSchema) |
| 147 |
DropLOIfExists(AH, oid); |
| 148 |
|
| 149 |
if (old_lo_style) |
| 150 |
ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n", |
| 151 |
oid, INV_WRITE); |
| 152 |
else |
| 153 |
ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n", |
| 154 |
oid, INV_WRITE); |
| 155 |
|
| 156 |
AH->WriteDataPtr = _WriteLOData; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Called by the archiver when the dumper calls EndLO. |
| 161 |
* |
| 162 |
* Optional. |
| 163 |
*/ |
| 164 |
static void |
| 165 |
_EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid) |
| 166 |
{ |
| 167 |
AH->WriteDataPtr = _WriteData; |
| 168 |
|
| 169 |
ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n"); |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Called by the archiver when finishing saving BLOB DATA. |
| 174 |
* |
| 175 |
* Optional. |
| 176 |
*/ |
| 177 |
static void |
| 178 |
_EndLOs(ArchiveHandle *AH, TocEntry *te) |
| 179 |
{ |
| 180 |
ahprintf(AH, "COMMIT;\n\n"); |
| 181 |
} |
| 182 |
|
| 183 |
/*------ |
| 184 |
* Called as part of a RestoreArchive call; for the NULL archive, this |
| 185 |
* just sends the data for a given TOC entry to the output. |
| 186 |
*------ |
| 187 |
*/ |
| 188 |
static void |
| 189 |
_PrintTocData(ArchiveHandle *AH, TocEntry *te) |
| 190 |
{ |
| 191 |
if (te->dataDumper) |
| 192 |
{ |
| 193 |
AH->currToc = te; |
| 194 |
|
| 195 |
if (strcmp(te->desc, "BLOBS") == 0) |
| 196 |
_StartLOs(AH, te); |
| 197 |
|
| 198 |
te->dataDumper((Archive *) AH, te->dataDumperArg); |
| 199 |
|
| 200 |
if (strcmp(te->desc, "BLOBS") == 0) |
| 201 |
_EndLOs(AH, te); |
| 202 |
|
| 203 |
AH->currToc = NULL; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
static int |
| 208 |
_WriteByte(ArchiveHandle *AH, const int i) |
| 209 |
{ |
| 210 |
/* Don't do anything */ |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
static void |
| 215 |
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len) |
| 216 |
{ |
| 217 |
/* Don't do anything */ |
| 218 |
} |
| 219 |
|
| 220 |
static void |
| 221 |
_CloseArchive(ArchiveHandle *AH) |
| 222 |
{ |
| 223 |
/* Nothing to do */ |
| 224 |
} |