Skip to content

Commit a8d0732

Browse files
committed
Remove manual tracking of file position in pg_dump/pg_backup_custom.c.
We do not really need to track the file position by hand. We were already relying on ftello() whenever the archive file is seekable, while if it's not seekable we don't need the file position info anyway because we're not going to be able to re-write the TOC. Moreover, that tracking was buggy since it failed to account for the effects of fseeko(). Somewhat remarkably, that seems not to have made for any live bugs up to now. We could fix the oversights, but it seems better to just get rid of the whole error-prone mess. In itself this is merely code cleanup. However, it's necessary infrastructure for an upcoming bug-fix patch (because that code *does* need valid file position after fseeko). The bug fix needs to go back as far as v12; hence, back-patch that far. Discussion: https://postgr.es/m/CALBH9DDuJ+scZc4MEvw5uO-=vRyR2=QF9+Yh=3hPEnKHWfS81A@mail.gmail.com
1 parent 5da8bf8 commit a8d0732

File tree

1 file changed

+14
-39
lines changed

1 file changed

+14
-39
lines changed

src/bin/pg_dump/pg_backup_custom.c

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,12 @@ typedef struct
7070
{
7171
CompressorState *cs;
7272
int hasSeek;
73-
pgoff_t filePos;
74-
pgoff_t dataStart;
7573
} lclContext;
7674

7775
typedef struct
7876
{
7977
int dataState;
80-
pgoff_t dataPos;
78+
pgoff_t dataPos; /* valid only if dataState=K_OFFSET_POS_SET */
8179
} lclTocEntry;
8280

8381

@@ -144,8 +142,6 @@ InitArchiveFmt_Custom(ArchiveHandle *AH)
144142
AH->lo_buf_size = LOBBUFSIZE;
145143
AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
146144

147-
ctx->filePos = 0;
148-
149145
/*
150146
* Now open the file
151147
*/
@@ -185,7 +181,6 @@ InitArchiveFmt_Custom(ArchiveHandle *AH)
185181

186182
ReadHead(AH);
187183
ReadToc(AH);
188-
ctx->dataStart = _getFilePos(AH, ctx);
189184
}
190185

191186
}
@@ -290,7 +285,8 @@ _StartData(ArchiveHandle *AH, TocEntry *te)
290285
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
291286

292287
tctx->dataPos = _getFilePos(AH, ctx);
293-
tctx->dataState = K_OFFSET_POS_SET;
288+
if (tctx->dataPos >= 0)
289+
tctx->dataState = K_OFFSET_POS_SET;
294290

295291
_WriteByte(AH, BLK_DATA); /* Block type */
296292
WriteInt(AH, te->dumpId); /* For sanity check */
@@ -350,7 +346,8 @@ _StartBlobs(ArchiveHandle *AH, TocEntry *te)
350346
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
351347

352348
tctx->dataPos = _getFilePos(AH, ctx);
353-
tctx->dataState = K_OFFSET_POS_SET;
349+
if (tctx->dataPos >= 0)
350+
tctx->dataState = K_OFFSET_POS_SET;
354351

355352
_WriteByte(AH, BLK_BLOBS); /* Block type */
356353
WriteInt(AH, te->dumpId); /* For sanity check */
@@ -551,7 +548,6 @@ _skipBlobs(ArchiveHandle *AH)
551548
static void
552549
_skipData(ArchiveHandle *AH)
553550
{
554-
lclContext *ctx = (lclContext *) AH->formatData;
555551
size_t blkLen;
556552
char *buf = NULL;
557553
int buflen = 0;
@@ -575,8 +571,6 @@ _skipData(ArchiveHandle *AH)
575571
fatal("could not read from input file: %m");
576572
}
577573

578-
ctx->filePos += blkLen;
579-
580574
blkLen = ReadInt(AH);
581575
}
582576

@@ -594,12 +588,10 @@ _skipData(ArchiveHandle *AH)
594588
static int
595589
_WriteByte(ArchiveHandle *AH, const int i)
596590
{
597-
lclContext *ctx = (lclContext *) AH->formatData;
598591
int res;
599592

600593
if ((res = fputc(i, AH->FH)) == EOF)
601594
WRITE_ERROR_EXIT;
602-
ctx->filePos += 1;
603595

604596
return 1;
605597
}
@@ -615,13 +607,11 @@ _WriteByte(ArchiveHandle *AH, const int i)
615607
static int
616608
_ReadByte(ArchiveHandle *AH)
617609
{
618-
lclContext *ctx = (lclContext *) AH->formatData;
619610
int res;
620611

621612
res = getc(AH->FH);
622613
if (res == EOF)
623614
READ_ERROR_EXIT(AH->FH);
624-
ctx->filePos += 1;
625615
return res;
626616
}
627617

@@ -635,11 +625,8 @@ _ReadByte(ArchiveHandle *AH)
635625
static void
636626
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
637627
{
638-
lclContext *ctx = (lclContext *) AH->formatData;
639-
640628
if (fwrite(buf, 1, len, AH->FH) != len)
641629
WRITE_ERROR_EXIT;
642-
ctx->filePos += len;
643630
}
644631

645632
/*
@@ -652,11 +639,8 @@ _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
652639
static void
653640
_ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
654641
{
655-
lclContext *ctx = (lclContext *) AH->formatData;
656-
657642
if (fread(buf, 1, len, AH->FH) != len)
658643
READ_ERROR_EXIT(AH->FH);
659-
ctx->filePos += len;
660644
}
661645

662646
/*
@@ -688,7 +672,6 @@ _CloseArchive(ArchiveHandle *AH)
688672
if (tpos < 0 && ctx->hasSeek)
689673
fatal("could not determine seek position in archive file: %m");
690674
WriteToc(AH);
691-
ctx->dataStart = _getFilePos(AH, ctx);
692675
WriteDataChunks(AH, NULL);
693676

694677
/*
@@ -862,30 +845,24 @@ _WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te)
862845

863846
/*
864847
* Get the current position in the archive file.
848+
*
849+
* With a non-seekable archive file, we may not be able to obtain the
850+
* file position. If so, just return -1. It's not too important in
851+
* that case because we won't be able to rewrite the TOC to fill in
852+
* data block offsets anyway.
865853
*/
866854
static pgoff_t
867855
_getFilePos(ArchiveHandle *AH, lclContext *ctx)
868856
{
869857
pgoff_t pos;
870858

871-
if (ctx->hasSeek)
859+
pos = ftello(AH->FH);
860+
if (pos < 0)
872861
{
873-
/*
874-
* Prior to 1.7 (pg7.3) we relied on the internally maintained
875-
* pointer. Now we rely on ftello() always, unless the file has been
876-
* found to not support it. For debugging purposes, print a warning
877-
* if the internal pointer disagrees, so that we're more likely to
878-
* notice if something's broken about the internal position tracking.
879-
*/
880-
pos = ftello(AH->FH);
881-
if (pos < 0)
862+
/* Not expected if we found we can seek. */
863+
if (ctx->hasSeek)
882864
fatal("could not determine seek position in archive file: %m");
883-
884-
if (pos != ctx->filePos)
885-
pg_log_warning("ftell mismatch with expected position -- ftell used");
886865
}
887-
else
888-
pos = ctx->filePos;
889866
return pos;
890867
}
891868

@@ -897,7 +874,6 @@ _getFilePos(ArchiveHandle *AH, lclContext *ctx)
897874
static void
898875
_readBlockHeader(ArchiveHandle *AH, int *type, int *id)
899876
{
900-
lclContext *ctx = (lclContext *) AH->formatData;
901877
int byt;
902878

903879
/*
@@ -918,7 +894,6 @@ _readBlockHeader(ArchiveHandle *AH, int *type, int *id)
918894
*id = 0; /* don't return an uninitialized value */
919895
return;
920896
}
921-
ctx->filePos += 1;
922897
}
923898

924899
*id = ReadInt(AH);

0 commit comments

Comments
 (0)