Skip to content

Commit 39a068c

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 a40733d commit 39a068c

File tree

1 file changed

+14
-43
lines changed

1 file changed

+14
-43
lines changed

src/bin/pg_dump/pg_backup_custom.c

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ typedef struct
7171
{
7272
CompressorState *cs;
7373
int hasSeek;
74-
pgoff_t filePos;
75-
pgoff_t dataStart;
7674
} lclContext;
7775

7876
typedef struct
7977
{
8078
int dataState;
81-
pgoff_t dataPos;
79+
pgoff_t dataPos; /* valid only if dataState=K_OFFSET_POS_SET */
8280
} lclTocEntry;
8381

8482

@@ -145,8 +143,6 @@ InitArchiveFmt_Custom(ArchiveHandle *AH)
145143
AH->lo_buf_size = LOBBUFSIZE;
146144
AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
147145

148-
ctx->filePos = 0;
149-
150146
/*
151147
* Now open the file
152148
*/
@@ -186,7 +182,6 @@ InitArchiveFmt_Custom(ArchiveHandle *AH)
186182

187183
ReadHead(AH);
188184
ReadToc(AH);
189-
ctx->dataStart = _getFilePos(AH, ctx);
190185
}
191186

192187
}
@@ -292,7 +287,8 @@ _StartData(ArchiveHandle *AH, TocEntry *te)
292287
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
293288

294289
tctx->dataPos = _getFilePos(AH, ctx);
295-
tctx->dataState = K_OFFSET_POS_SET;
290+
if (tctx->dataPos >= 0)
291+
tctx->dataState = K_OFFSET_POS_SET;
296292

297293
_WriteByte(AH, BLK_DATA); /* Block type */
298294
WriteInt(AH, te->dumpId); /* For sanity check */
@@ -355,7 +351,8 @@ _StartBlobs(ArchiveHandle *AH, TocEntry *te)
355351
lclTocEntry *tctx = (lclTocEntry *) te->formatData;
356352

357353
tctx->dataPos = _getFilePos(AH, ctx);
358-
tctx->dataState = K_OFFSET_POS_SET;
354+
if (tctx->dataPos >= 0)
355+
tctx->dataState = K_OFFSET_POS_SET;
359356

360357
_WriteByte(AH, BLK_BLOBS); /* Block type */
361358
WriteInt(AH, te->dumpId); /* For sanity check */
@@ -556,7 +553,6 @@ _skipBlobs(ArchiveHandle *AH)
556553
static void
557554
_skipData(ArchiveHandle *AH)
558555
{
559-
lclContext *ctx = (lclContext *) AH->formatData;
560556
size_t blkLen;
561557
char *buf = NULL;
562558
int buflen = 0;
@@ -580,8 +576,6 @@ _skipData(ArchiveHandle *AH)
580576
fatal("could not read from input file: %m");
581577
}
582578

583-
ctx->filePos += blkLen;
584-
585579
blkLen = ReadInt(AH);
586580
}
587581

@@ -599,12 +593,10 @@ _skipData(ArchiveHandle *AH)
599593
static int
600594
_WriteByte(ArchiveHandle *AH, const int i)
601595
{
602-
lclContext *ctx = (lclContext *) AH->formatData;
603596
int res;
604597

605598
if ((res = fputc(i, AH->FH)) == EOF)
606599
WRITE_ERROR_EXIT;
607-
ctx->filePos += 1;
608600

609601
return 1;
610602
}
@@ -620,13 +612,11 @@ _WriteByte(ArchiveHandle *AH, const int i)
620612
static int
621613
_ReadByte(ArchiveHandle *AH)
622614
{
623-
lclContext *ctx = (lclContext *) AH->formatData;
624615
int res;
625616

626617
res = getc(AH->FH);
627618
if (res == EOF)
628619
READ_ERROR_EXIT(AH->FH);
629-
ctx->filePos += 1;
630620
return res;
631621
}
632622

@@ -640,13 +630,8 @@ _ReadByte(ArchiveHandle *AH)
640630
static void
641631
_WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
642632
{
643-
lclContext *ctx = (lclContext *) AH->formatData;
644-
645633
if (fwrite(buf, 1, len, AH->FH) != len)
646634
WRITE_ERROR_EXIT;
647-
ctx->filePos += len;
648-
649-
return;
650635
}
651636

652637
/*
@@ -659,13 +644,8 @@ _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
659644
static void
660645
_ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
661646
{
662-
lclContext *ctx = (lclContext *) AH->formatData;
663-
664647
if (fread(buf, 1, len, AH->FH) != len)
665648
READ_ERROR_EXIT(AH->FH);
666-
ctx->filePos += len;
667-
668-
return;
669649
}
670650

671651
/*
@@ -697,7 +677,6 @@ _CloseArchive(ArchiveHandle *AH)
697677
if (tpos < 0 && ctx->hasSeek)
698678
fatal("could not determine seek position in archive file: %m");
699679
WriteToc(AH);
700-
ctx->dataStart = _getFilePos(AH, ctx);
701680
WriteDataChunks(AH, NULL);
702681

703682
/*
@@ -871,30 +850,24 @@ _WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te)
871850

872851
/*
873852
* Get the current position in the archive file.
853+
*
854+
* With a non-seekable archive file, we may not be able to obtain the
855+
* file position. If so, just return -1. It's not too important in
856+
* that case because we won't be able to rewrite the TOC to fill in
857+
* data block offsets anyway.
874858
*/
875859
static pgoff_t
876860
_getFilePos(ArchiveHandle *AH, lclContext *ctx)
877861
{
878862
pgoff_t pos;
879863

880-
if (ctx->hasSeek)
864+
pos = ftello(AH->FH);
865+
if (pos < 0)
881866
{
882-
/*
883-
* Prior to 1.7 (pg7.3) we relied on the internally maintained
884-
* pointer. Now we rely on ftello() always, unless the file has been
885-
* found to not support it. For debugging purposes, print a warning
886-
* if the internal pointer disagrees, so that we're more likely to
887-
* notice if something's broken about the internal position tracking.
888-
*/
889-
pos = ftello(AH->FH);
890-
if (pos < 0)
867+
/* Not expected if we found we can seek. */
868+
if (ctx->hasSeek)
891869
fatal("could not determine seek position in archive file: %m");
892-
893-
if (pos != ctx->filePos)
894-
pg_log_warning("ftell mismatch with expected position -- ftell used");
895870
}
896-
else
897-
pos = ctx->filePos;
898871
return pos;
899872
}
900873

@@ -906,7 +879,6 @@ _getFilePos(ArchiveHandle *AH, lclContext *ctx)
906879
static void
907880
_readBlockHeader(ArchiveHandle *AH, int *type, int *id)
908881
{
909-
lclContext *ctx = (lclContext *) AH->formatData;
910882
int byt;
911883

912884
/*
@@ -927,7 +899,6 @@ _readBlockHeader(ArchiveHandle *AH, int *type, int *id)
927899
*id = 0; /* don't return an uninitialized value */
928900
return;
929901
}
930-
ctx->filePos += 1;
931902
}
932903

933904
*id = ReadInt(AH);

0 commit comments

Comments
 (0)