PostgreSQL Source Code: src/bin/pg_dump/pg_backup_custom.c Source File (original) (raw)
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
27
31
32
33
34
35
36
51
55
61
65
67
68typedef struct
69{
72
75
76typedef struct
77{
81
82
83
84
85
86
89
92
93
94
95
96
97
98
99
100
101
102
103
104void
106{
108
109
124
129
133
134
137
138
141
142
143
144
146 {
147 if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
148 {
150 if (!AH->FH)
151 pg_fatal("could not open output file \"%s\": %m", AH->fSpec);
152 }
153 else
154 {
156 if (!AH->FH)
157 pg_fatal("could not open output file: %m");
158 }
159
161 }
162 else
163 {
164 if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
165 {
167 if (!AH->FH)
168 pg_fatal("could not open input file \"%s\": %m", AH->fSpec);
169 }
170 else
171 {
172 AH->FH = stdin;
173 if (!AH->FH)
174 pg_fatal("could not open input file: %m");
175 }
176
178
181
182
183
184
185
187 }
188}
189
190
191
192
193
194
195
196
197static void
199{
201
205 else
207
209}
210
211
212
213
214
215
216
217
218
219
220static void
222{
224
226}
227
228
229
230
231
232
233
234
235
236static void
238{
240
241 if (ctx == NULL)
242 {
245 }
246
248
249
250
251
252
255}
256
257
258
259
260
261
262
263static void
265{
267
271}
272
273
274
275
276
277
278
279
280
281
282
283static void
285{
288
290 if (tctx->dataPos >= 0)
292
295
297 NULL,
299}
300
301
302
303
304
305
306
307
308
309
310static void
312{
315
316 if (dLen > 0)
317
318 cs->writeData(AH, cs, data, dLen);
319}
320
321
322
323
324
325
326
327static void
329{
331
333 ctx->cs = NULL;
334
335
337}
338
339
340
341
342
343
344
345
346
347
348static void
350{
353
355 if (tctx->dataPos >= 0)
357
360}
361
362
363
364
365
366
367
368
369static void
371{
373
374 if (oid == 0)
375 pg_fatal("invalid OID for large object");
376
378
380 NULL,
382}
383
384
385
386
387
388
389static void
391{
393
395
397}
398
399
400
401
402
403
404static void
406{
407
409}
410
411
412
413
414static void
416{
419 int blkType;
420 int id;
421
423 return;
424
426 {
427
428
429
430
431
432
433
434
435
436
438 {
440 pg_fatal("error during file seek: %m");
441 }
442
443 for (;;)
444 {
446
448
449 if (blkType == EOF || id == te->dumpId)
450 break;
451
452
453 if (thisBlkPos >= 0)
454 {
456
458 {
460
461
462
463
464
465
466
467
468
469
471 {
472 othertctx->dataPos = thisBlkPos;
474 }
475 else if (othertctx->dataPos != thisBlkPos ||
477 {
478
479 pg_log_warning("data block %d has wrong seek position",
480 id);
481 }
482 }
483 }
484
485 switch (blkType)
486 {
489 break;
490
493 break;
494
495 default:
496 pg_fatal("unrecognized data block type (%d) while searching archive",
497 blkType);
498 break;
499 }
500 }
501 }
502 else
503 {
504
505 if (fseeko(AH->FH, tctx->dataPos, SEEK_SET) != 0)
506 pg_fatal("error during file seek: %m");
507
509 }
510
511
512
513
514
515 if (blkType == EOF)
516 {
518 pg_fatal("could not find block ID %d in archive -- "
519 "possibly due to out-of-order restore request, "
520 "which cannot be handled due to non-seekable input file",
522 else
523 pg_fatal("could not find block ID %d in archive -- "
524 "possibly corrupt archive",
526 }
527
528
529 if (id != te->dumpId)
530 pg_fatal("found unexpected block ID (%d) when reading data -- expected %d",
532
533 switch (blkType)
534 {
537 break;
538
541 break;
542
543 default:
544 pg_fatal("unrecognized data block type %d while restoring archive",
545 blkType);
546 break;
547 }
548
549
550
551
552
553
554
556 {
558
561 }
562}
563
564
565
566
567static void
569{
571
576}
577
578static void
580{
582
584
586 while (oid != 0)
587 {
592 }
593
595}
596
597
598
599
600
601
602
603static void
605{
607
609 while (oid != 0)
610 {
613 }
614}
615
616
617
618
619
620
621static void
623{
625 size_t blkLen;
626 char *buf = NULL;
627 int buflen = 0;
628
630 while (blkLen != 0)
631 {
633 {
634 if (fseeko(AH->FH, blkLen, SEEK_CUR) != 0)
635 pg_fatal("error during file seek: %m");
636 }
637 else
638 {
639 if (blkLen > buflen)
640 {
643 buflen = blkLen;
644 }
645 if (fread(buf, 1, blkLen, AH->FH) != blkLen)
646 {
647 if (feof(AH->FH))
648 pg_fatal("could not read from input file: end of file");
649 else
650 pg_fatal("could not read from input file: %m");
651 }
652 }
653
655 }
656
658}
659
660
661
662
663
664
665
666
667static int
669{
670 if (fputc(i, AH->FH) == EOF)
672
673 return 1;
674}
675
676
677
678
679
680
681
682
683
684static int
686{
687 int res;
688
689 res = getc(AH->FH);
690 if (res == EOF)
692 return res;
693}
694
695
696
697
698
699
700
701
702static void
704{
707}
708
709
710
711
712
713
714
715
716static void
718{
721}
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738static void
740{
743
745 {
747
749 if (tpos < 0 && ctx->hasSeek)
750 pg_fatal("could not determine seek position in archive file: %m");
753
754
755
756
757
758
759
760
761
763 fseeko(AH->FH, tpos, SEEK_SET) == 0)
765 }
766
767 if (fclose(AH->FH) != 0)
768 pg_fatal("could not close archive file: %m");
769
770
773
774 AH->FH = NULL;
775}
776
777
778
779
780
781
782
783
784static void
786{
789
791 pg_fatal("can only reopen input archives");
792
793
794
795
796
797 if (AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0)
798 pg_fatal("parallel restore from standard input is not supported");
800 pg_fatal("parallel restore from non-seekable file is not supported");
801
803 if (tpos < 0)
804 pg_fatal("could not determine seek position in archive file: %m");
805
806#ifndef WIN32
807 if (fclose(AH->FH) != 0)
808 pg_fatal("could not close archive file: %m");
809#endif
810
812 if (!AH->FH)
813 pg_fatal("could not open input file \"%s\": %m", AH->fSpec);
814
815 if (fseeko(AH->FH, tpos, SEEK_SET) != 0)
816 pg_fatal("could not set seek position in archive file: %m");
817}
818
819
820
821
822
823
824
825
826
827
828
829static void
831{
836
837
838
839
840
841
843 {
845
846
847
848
849
850
852 continue;
853
854
855 if (prev_te)
856 {
857 if (tctx->dataPos > prev_tctx->dataPos)
858 prev_te->dataLength = tctx->dataPos - prev_tctx->dataPos;
859 }
860
861 prev_te = te;
862 prev_tctx = tctx;
863 }
864
865
866 if (prev_te && ctx->hasSeek)
867 {
869
870 if (fseeko(AH->FH, 0, SEEK_END) != 0)
871 pg_fatal("error during file seek: %m");
873 if (endpos > prev_tctx->dataPos)
874 prev_te->dataLength = endpos - prev_tctx->dataPos;
875 }
876}
877
878
879
880
881static void
883{
885
886
887
888
892
893
895 pg_fatal("compressor active");
896
897
898
899
900
901
902
903}
904
905static void
907{
909
911}
912
913
914
915
916
917static int
919{
921}
922
923
924
925
926
927
928
929
930
931
932
933
934
935
938{
940
942 if (pos < 0)
943 {
944
946 pg_fatal("could not determine seek position in archive file: %m");
947 }
948 return pos;
949}
950
951
952
953
954
955
956static void
958{
959 int byt;
960
961
962
963
964
965
966
967
970 else
971 {
972 byt = getc(AH->FH);
974 if (byt == EOF)
975 {
976 *id = 0;
977 return;
978 }
979 }
980
982}
983
984
985
986
987
988static void
990{
991
992 if (len > 0)
993 {
996 }
997}
998
999
1000
1001
1002
1003static size_t
1005{
1006 size_t blkLen;
1007
1008
1010 if (blkLen == 0)
1011 return 0;
1012
1013
1014 if (blkLen > *buflen)
1015 {
1018 *buflen = blkLen;
1019 }
1020
1021
1023
1024 return blkLen;
1025}
CompressorState * AllocateCompressor(const pg_compress_specification compression_spec, ReadFunc readF, WriteFunc writeF)
void EndCompressor(ArchiveHandle *AH, CompressorState *cs)
void fsync_fname(const char *fname, bool isdir)
void * pg_malloc(size_t size)
void * pg_malloc0(size_t size)
if(TABLE==NULL||TABLE_index==NULL)
void WriteHead(ArchiveHandle *AH)
size_t WriteInt(ArchiveHandle *AH, int i)
size_t WriteOffset(ArchiveHandle *AH, pgoff_t o, int wasSet)
void StartRestoreLOs(ArchiveHandle *AH)
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
void WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
int ReadOffset(ArchiveHandle *AH, pgoff_t *o)
int ReadInt(ArchiveHandle *AH)
void ReadHead(ArchiveHandle *AH)
void ReadToc(ArchiveHandle *AH)
void EndRestoreLO(ArchiveHandle *AH, Oid oid)
void WriteToc(ArchiveHandle *AH)
void EndRestoreLOs(ArchiveHandle *AH)
void StartRestoreLO(ArchiveHandle *AH, Oid oid, bool drop)
int parallel_restore(ArchiveHandle *AH, TocEntry *te)
#define K_OFFSET_POS_NOT_SET
#define READ_ERROR_EXIT(fd)
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _skipData(ArchiveHandle *AH)
static void _StartData(ArchiveHandle *AH, TocEntry *te)
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
static void _CloseArchive(ArchiveHandle *AH)
static void _skipLOs(ArchiveHandle *AH)
static pgoff_t _getFilePos(ArchiveHandle *AH, lclContext *ctx)
static void _DeClone(ArchiveHandle *AH)
static void _ReopenArchive(ArchiveHandle *AH)
static void _StartLOs(ArchiveHandle *AH, TocEntry *te)
void InitArchiveFmt_Custom(ArchiveHandle *AH)
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
static size_t _CustomReadFunc(ArchiveHandle *AH, char **buf, size_t *buflen)
static void _EndLOs(ArchiveHandle *AH, TocEntry *te)
static int _WriteByte(ArchiveHandle *AH, const int i)
static void _PrepParallelRestore(ArchiveHandle *AH)
static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
static int _ReadByte(ArchiveHandle *AH)
static void _PrintData(ArchiveHandle *AH)
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
static void _EndData(ArchiveHandle *AH, TocEntry *te)
static void _CustomWriteFunc(ArchiveHandle *AH, const char *buf, size_t len)
static int _WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te)
static void _Clone(ArchiveHandle *AH)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
static void _readBlockHeader(ArchiveHandle *AH, int *type, int *id)
static void _LoadLOs(ArchiveHandle *AH, bool drop)
#define pg_log_warning(...)
void(* readData)(ArchiveHandle *AH, CompressorState *cs)
DeClonePtrType DeClonePtr
ReadExtraTocPtrType ReadExtraTocPtr
WorkerJobDumpPtrType WorkerJobDumpPtr
StartLOsPtrType StartLOsPtr
ArchiveEntryPtrType ArchiveEntryPtr
pg_compress_specification compression_spec
WriteDataPtrType WriteDataPtr
StartLOPtrType StartLOPtr
WriteBufPtrType WriteBufPtr
PrepParallelRestorePtrType PrepParallelRestorePtr
WriteExtraTocPtrType WriteExtraTocPtr
ReadBytePtrType ReadBytePtr
WorkerJobRestorePtrType WorkerJobRestorePtr
PrintTocDataPtrType PrintTocDataPtr
WriteBytePtrType WriteBytePtr
ReadBufPtrType ReadBufPtr
PrintExtraTocPtrType PrintExtraTocPtr
StartDataPtrType StartDataPtr
EndDataPtrType EndDataPtr
#define fseeko(stream, offset, origin)