aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_dump/pg_backup_custom.c
blob: 3edbb751f9a0a80ada721fdc9e9012610083fd64 (plain)
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
121
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
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
515
516
517
518
519
520
521
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
/*-------------------------------------------------------------------------
 *
 * pg_backup_custom.c
 *
 *	Implements the custom output format.
 *
 *	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 <stdlib.h>
#include "pg_backup.h"
#include "pg_backup_archiver.h"

extern int	errno;

static void     _ArchiveEntry(ArchiveHandle* AH, TocEntry* te);
static void	_StartData(ArchiveHandle* AH, TocEntry* te);
static int	_WriteData(ArchiveHandle* AH, const void* data, int dLen);
static void     _EndData(ArchiveHandle* AH, TocEntry* te);
static int      _WriteByte(ArchiveHandle* AH, const int i);
static int      _ReadByte(ArchiveHandle* );
static int      _WriteBuf(ArchiveHandle* AH, const void* buf, int len);
static int    	_ReadBuf(ArchiveHandle* AH, void* buf, int len);
static void     _CloseArchive(ArchiveHandle* AH);
static void	_PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt);
static void	_WriteExtraToc(ArchiveHandle* AH, TocEntry* te);
static void	_ReadExtraToc(ArchiveHandle* AH, TocEntry* te);
static void	_PrintExtraToc(ArchiveHandle* AH, TocEntry* te);

static void	_PrintData(ArchiveHandle* AH);
static void     _skipData(ArchiveHandle* AH);

#define zlibOutSize	4096
#define zlibInSize	4096

typedef struct {
    z_streamp	zp;
    char*	zlibOut;
    char*	zlibIn;
    int		inSize;
    int		hasSeek;
    int		filePos;
    int		dataStart;
} lclContext;

typedef struct {
    int		dataPos;
    int		dataLen;
} lclTocEntry;

static int	_getFilePos(ArchiveHandle* AH, lclContext* ctx);

static char* progname = "Archiver(custom)";

/*
 *  Handler functions. 
 */
void InitArchiveFmt_Custom(ArchiveHandle* AH) 
{
    lclContext*		ctx;

    /* Assuming static functions, this can be copied for each format. */
    AH->ArchiveEntryPtr = _ArchiveEntry;
    AH->StartDataPtr = _StartData;
    AH->WriteDataPtr = _WriteData;
    AH->EndDataPtr = _EndData;
    AH->WriteBytePtr = _WriteByte;
    AH->ReadBytePtr = _ReadByte;
    AH->WriteBufPtr = _WriteBuf;
    AH->ReadBufPtr = _ReadBuf;
    AH->ClosePtr = _CloseArchive;
    AH->PrintTocDataPtr = _PrintTocData;
    AH->ReadExtraTocPtr = _ReadExtraToc;
    AH->WriteExtraTocPtr = _WriteExtraToc;
    AH->PrintExtraTocPtr = _PrintExtraToc;

    /*
     *	Set up some special context used in compressing data.
    */
    ctx = (lclContext*)malloc(sizeof(lclContext));
    if (ctx == NULL)
	die_horribly("%s: Unable to allocate archive context",progname);
    AH->formatData = (void*)ctx;

    ctx->zp = (z_streamp)malloc(sizeof(z_stream));
    if (ctx->zp == NULL)
	die_horribly("%s: unable to allocate zlib stream archive context",progname);

    ctx->zlibOut = (char*)malloc(zlibOutSize);
    ctx->zlibIn = (char*)malloc(zlibInSize);
    ctx->inSize = zlibInSize;
    ctx->filePos = 0;

    if (ctx->zlibOut == NULL || ctx->zlibIn == NULL)
	die_horribly("%s: unable to allocate buffers in archive context",progname);

    /*
     * Now open the file
    */
    if (AH->mode == archModeWrite) {
	if (AH->fSpec && strcmp(AH->fSpec,"") != 0) {
	    AH->FH = fopen(AH->fSpec, PG_BINARY_W);
	} else {
	    AH->FH = stdout;
	}

	if (!AH)
	    die_horribly("%s: unable to open archive file %s",progname, AH->fSpec);

	ctx->hasSeek = (fseek(AH->FH, 0, SEEK_CUR) == 0);

    } else {
	if (AH->fSpec && strcmp(AH->fSpec,"") != 0) {
	    AH->FH = fopen(AH->fSpec, PG_BINARY_R);
	} else {
	    AH->FH = stdin;
	}
	if (!AH)
	    die_horribly("%s: unable to open archive file %s",progname, AH->fSpec);

	ctx->hasSeek = (fseek(AH->FH, 0, SEEK_CUR) == 0);

	ReadHead(AH);
	ReadToc(AH);
	ctx->dataStart = _getFilePos(AH, ctx);
    }

}

/*
 * - Start a new TOC entry
*/
static void	_ArchiveEntry(ArchiveHandle* AH, TocEntry* te) 
{
    lclTocEntry*	ctx;

    ctx = (lclTocEntry*)malloc(sizeof(lclTocEntry));
    if (te->dataDumper) {
	ctx->dataPos = -1;
    } else {
	ctx->dataPos = 0;
    }
    ctx->dataLen = 0;
    te->formatData = (void*)ctx;

}

static void	_WriteExtraToc(ArchiveHandle* AH, TocEntry* te)
{
    lclTocEntry*	ctx = (lclTocEntry*)te->formatData;

    WriteInt(AH, ctx->dataPos);
    WriteInt(AH, ctx->dataLen);
}

static void	_ReadExtraToc(ArchiveHandle* AH, TocEntry* te)
{
    lclTocEntry*	ctx = (lclTocEntry*)te->formatData;

    if (ctx == NULL) {
	ctx = (lclTocEntry*)malloc(sizeof(lclTocEntry));
	te->formatData = (void*)ctx;
    }

    ctx->dataPos = ReadInt( AH );
    ctx->dataLen = ReadInt( AH );
    
}

static void	_PrintExtraToc(ArchiveHandle* AH, TocEntry* te)
{
    lclTocEntry*	ctx = (lclTocEntry*)te->formatData;

    ahprintf(AH, "-- Data Pos: %d (Length %d)\n", ctx->dataPos, ctx->dataLen);
}

static void	_StartData(ArchiveHandle* AH, TocEntry* te)
{
    lclContext*		ctx = (lclContext*)AH->formatData;
    z_streamp   	zp = ctx->zp;
    lclTocEntry*	tctx = (lclTocEntry*)te->formatData;

    tctx->dataPos = _getFilePos(AH, ctx);

    WriteInt(AH, te->id); /* For sanity check */

#ifdef HAVE_LIBZ

    if (AH->compression < 0 || AH->compression > 9) {
	AH->compression = Z_DEFAULT_COMPRESSION;
    }

    if (AH->compression != 0) {
	zp->zalloc = Z_NULL;
	zp->zfree = Z_NULL;
	zp->opaque = Z_NULL;

	if (deflateInit(zp, AH->compression) != Z_OK)
	    die_horribly("%s: could not initialize compression library - %s\n",progname, zp->msg);
    }

#else

    AH->compression = 0;

#endif

    /* Just be paranoid - maye End is called after Start, with no Write */
    zp->next_out = ctx->zlibOut;
    zp->avail_out = zlibOutSize;
}

static int	_DoDeflate(ArchiveHandle* AH, lclContext* ctx, int flush) 
{
    z_streamp   zp = ctx->zp;

#ifdef HAVE_LIBZ
    char*	out = ctx->zlibOut;
    int		res = Z_OK;

    if (AH->compression != 0) 
    {
	res = deflate(zp, flush);
	if (res == Z_STREAM_ERROR)
	    die_horribly("%s: could not compress data - %s\n",progname, zp->msg);

	if 	(      ( (flush == Z_FINISH) && (zp->avail_out < zlibOutSize) )
		|| (zp->avail_out == 0) 
		|| (zp->avail_in != 0)
	    ) 
	{
	    /*
	     * Extra paranoia: avoid zero-length chunks since a zero 
	     * length chunk is the EOF marker. This should never happen
	     * but...
	    */
	    if (zp->avail_out < zlibOutSize) {
		/* printf("Wrote %d byte deflated chunk\n", zlibOutSize - zp->avail_out); */
		WriteInt(AH, zlibOutSize - zp->avail_out);
		fwrite(out, 1, zlibOutSize - zp->avail_out, AH->FH);
		ctx->filePos += zlibOutSize - zp->avail_out;
	    }
	    zp->next_out = out;
	    zp->avail_out = zlibOutSize;
	}
    } else {
#endif
	if (zp->avail_in > 0)
	{
	    WriteInt(AH, zp->avail_in);
	    fwrite(zp->next_in, 1, zp->avail_in, AH->FH);
	    ctx->filePos += zp->avail_in;
	    zp->avail_in = 0;
	} else {
#ifdef HAVE_LIBZ
	    if (flush == Z_FINISH)
		res = Z_STREAM_END;
#endif
	}


#ifdef HAVE_LIBZ
    }

    return res;
#else
    return 1;
#endif

}

static int	_WriteData(ArchiveHandle* AH, const void* data, int dLen)
{
    lclContext*	ctx = (lclContext*)AH->formatData;
    z_streamp	zp = ctx->zp;

    zp->next_in = (void*)data;
    zp->avail_in = dLen;

    while (zp->avail_in != 0) {
	/* printf("Deflating %d bytes\n", dLen); */
	_DoDeflate(AH, ctx, 0);
    }
    return dLen;
}

static void	_EndData(ArchiveHandle* AH, TocEntry* te)
{
    lclContext*		ctx = (lclContext*)AH->formatData;
    lclTocEntry*	tctx = (lclTocEntry*) te->formatData;

#ifdef HAVE_LIBZ
    z_streamp		zp = ctx->zp;
    int			res;

    if (AH->compression != 0)
    {
	zp->next_in = NULL;
	zp->avail_in = 0;

	do { 	
	    /* printf("Ending data output\n"); */
	    res = _DoDeflate(AH, ctx, Z_FINISH);
	} while (res != Z_STREAM_END);

	if (deflateEnd(zp) != Z_OK)
	    die_horribly("%s: error closing compression stream - %s\n", progname, zp->msg);
    }
#endif

    /* Send the end marker */
    WriteInt(AH, 0);

    tctx->dataLen = _getFilePos(AH, ctx) - tctx->dataPos;

}

/*
 * Print data for a gievn TOC entry
*/
static void	_PrintTocData(ArchiveHandle* AH, TocEntry* te, RestoreOptions *ropt)
{
    lclContext* 	ctx = (lclContext*)AH->formatData;
    int			id;
    lclTocEntry*	tctx = (lclTocEntry*) te->formatData;

    if (tctx->dataPos == 0) 
	return;

    if (!ctx->hasSeek || tctx->dataPos < 0) {
	id = ReadInt(AH);

	while (id != te->id) {
	    if (TocIDRequired(AH, id, ropt) & 2)
		die_horribly("%s: Dumping a specific TOC data block out of order is not supported"
			       " without on this input stream (fseek required)\n", progname);
	    _skipData(AH);
	    id = ReadInt(AH);
	}
    } else {

	if (fseek(AH->FH, tctx->dataPos, SEEK_SET) != 0)
	    die_horribly("%s: error %d in file seek\n",progname, errno);

	id = ReadInt(AH);

    }

    if (id != te->id)
	die_horribly("%s: Found unexpected block ID (%d) when reading data - expected %d\n",
			progname, id, te->id);

    ahprintf(AH, "--\n-- Data for TOC Entry ID %d (OID %s) %s %s\n--\n\n",
		te->id, te->oid, te->desc, te->name);

    _PrintData(AH);

    ahprintf(AH, "\n\n");
}

/*
 * Print data from current file position.
*/
static void	_PrintData(ArchiveHandle* AH)
{
    lclContext*	ctx = (lclContext*)AH->formatData;
    z_streamp	zp = ctx->zp;
    int		blkLen;
    char*	in = ctx->zlibIn;
    int		cnt;

#ifdef HAVE_LIBZ

    int		res;
    char*	out = ctx->zlibOut;

    res = Z_OK;

    if (AH->compression != 0) {
	zp->zalloc = Z_NULL;
	zp->zfree = Z_NULL;
	zp->opaque = Z_NULL;

	if (inflateInit(zp) != Z_OK)
	    die_horribly("%s: could not initialize compression library - %s\n", progname, zp->msg);
    }

#endif

    blkLen = ReadInt(AH);
    while (blkLen != 0) {
	if (blkLen > ctx->inSize) {
	    free(ctx->zlibIn);
	    ctx->zlibIn = NULL;
	    ctx->zlibIn = (char*)malloc(blkLen);
	    if (!ctx->zlibIn)
		die_horribly("%s: failed to allocate decompression buffer\n", progname);

	    ctx->inSize = blkLen;
	    in = ctx->zlibIn;
	}
	cnt = fread(in, 1, blkLen, AH->FH);
	if (cnt != blkLen) 
	    die_horribly("%s: could not read data block - expected %d, got %d\n", progname, blkLen, cnt);

	ctx->filePos += blkLen;

	zp->next_in = in;
	zp->avail_in = blkLen;

#ifdef HAVE_LIBZ

	if (AH->compression != 0) {

	    while (zp->avail_in != 0) {
		zp->next_out = out;
		zp->avail_out = zlibOutSize;
		res = inflate(zp, 0);
		if (res != Z_OK && res != Z_STREAM_END)
		    die_horribly("%s: unable to uncompress data - %s\n", progname, zp->msg);

		out[zlibOutSize - zp->avail_out] = '\0';
		ahwrite(out, 1, zlibOutSize - zp->avail_out, AH);
	    }
	} else {
#endif
	    ahwrite(in, 1, zp->avail_in, AH);
	    zp->avail_in = 0;

#ifdef HAVE_LIBZ
	}
#endif

	blkLen = ReadInt(AH);
    }

#ifdef HAVE_LIBZ
    if (AH->compression != 0) 
    {
	zp->next_in = NULL;
	zp->avail_in = 0;
	while (res != Z_STREAM_END) {
	    zp->next_out = out;
	    zp->avail_out = zlibOutSize;
	    res = inflate(zp, 0);
	    if (res != Z_OK && res != Z_STREAM_END)
		die_horribly("%s: unable to uncompress data - %s\n", progname, zp->msg);

	    out[zlibOutSize - zp->avail_out] = '\0';
	    ahwrite(out, 1, zlibOutSize - zp->avail_out, AH);
	}
    }
#endif

}

/*
 * Skip data from current file position.
*/
static void	_skipData(ArchiveHandle* AH)
{
    lclContext*	ctx = (lclContext*)AH->formatData;
    int		blkLen;
    char*	in = ctx->zlibIn;
    int		cnt;

    blkLen = ReadInt(AH);
    while (blkLen != 0) {
	if (blkLen > ctx->inSize) {
	    free(ctx->zlibIn);
	    ctx->zlibIn = (char*)malloc(blkLen);
	    ctx->inSize = blkLen;
	    in = ctx->zlibIn;
	}
	cnt = fread(in, 1, blkLen, AH->FH);
	if (cnt != blkLen) 
	    die_horribly("%s: could not read data block - expected %d, got %d\n", progname, blkLen, cnt);

	ctx->filePos += blkLen;

	blkLen = ReadInt(AH);
    }

}

static int	_WriteByte(ArchiveHandle* AH, const int i)
{
    lclContext*		ctx = (lclContext*)AH->formatData;
    int			res;

    res = fputc(i, AH->FH);
    if (res != EOF) {
	ctx->filePos += 1;
    }
    return res;
}

static int    	_ReadByte(ArchiveHandle* AH)
{
    lclContext*		ctx = (lclContext*)AH->formatData;
    int			res;

    res = fgetc(AH->FH);
    if (res != EOF) {
	ctx->filePos += 1;
    }
    return res;
}

static int	_WriteBuf(ArchiveHandle* AH, const void* buf, int len)
{
    lclContext*		ctx = (lclContext*)AH->formatData;
    int			res;
    res = fwrite(buf, 1, len, AH->FH);
    ctx->filePos += res;
    return res;
}

static int	_ReadBuf(ArchiveHandle* AH, void* buf, int len)
{
    lclContext*		ctx = (lclContext*)AH->formatData;
    int			res;
    res = fread(buf, 1, len, AH->FH);
    ctx->filePos += res;
    return res;
}

static void	_CloseArchive(ArchiveHandle* AH)
{
    lclContext*		ctx = (lclContext*)AH->formatData;
    int			tpos;

    if (AH->mode == archModeWrite) {
	WriteHead(AH);
	tpos = ftell(AH->FH);
	WriteToc(AH);
	ctx->dataStart = _getFilePos(AH, ctx);
	WriteDataChunks(AH);
	/* This is not an essential operation - it is really only
	 * needed if we expect to be doing seeks to read the data back
	 * - it may be ok to just use the existing self-consistent block
	 * formatting.
	 */
	if (ctx->hasSeek) {
	    fseek(AH->FH, tpos, SEEK_SET);
	    WriteToc(AH);
	}
    }

    fclose(AH->FH);
    AH->FH = NULL; 
}

static int	_getFilePos(ArchiveHandle* AH, lclContext* ctx) 
{
    int		pos;
    if (ctx->hasSeek) {
	pos = ftell(AH->FH);
	if (pos != ctx->filePos) {
	    fprintf(stderr, "Warning: ftell mismatch with filePos\n");
	}
    } else {
	pos = ctx->filePos;
    }
    return pos;
}