Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42b991f

Browse files
committed
BufFileSeek's behavior at segment boundaries wasn't what
logfile.c wanted ... seems easier to fix BufFileSeek.
1 parent d357c96 commit 42b991f

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/backend/storage/file/buffile.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 1994, Regents of the University of California
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/storage/file/buffile.c,v 1.2 1999/10/16 19:49:26 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/storage/file/buffile.c,v 1.3 1999/10/19 02:34:45 tgl Exp $
1010
*
1111
* NOTES:
1212
*
@@ -434,16 +434,15 @@ BufFileSeek(BufFile *file, int fileno, long offset, int whence)
434434
switch (whence)
435435
{
436436
case SEEK_SET:
437-
if (fileno < 0 || fileno >= file->numFiles ||
438-
offset < 0)
437+
if (fileno < 0)
439438
return EOF;
440439
newFile = fileno;
441440
newOffset = offset;
442441
break;
443442
case SEEK_CUR:
444443
/*
445444
* Relative seek considers only the signed offset, ignoring fileno.
446-
* Note that large offsets (> 1 gig) risk overflow.
445+
* Note that large offsets (> 1 gig) risk overflow in this add...
447446
*/
448447
newFile = file->curFile;
449448
newOffset = (file->curOffset + file->pos) + offset;
@@ -463,15 +462,6 @@ BufFileSeek(BufFile *file, int fileno, long offset, int whence)
463462
return EOF;
464463
newOffset += MAX_PHYSICAL_FILESIZE;
465464
}
466-
if (file->isTemp)
467-
{
468-
while (newOffset > MAX_PHYSICAL_FILESIZE)
469-
{
470-
if (++newFile >= file->numFiles)
471-
return EOF;
472-
newOffset -= MAX_PHYSICAL_FILESIZE;
473-
}
474-
}
475465
if (newFile == file->curFile &&
476466
newOffset >= file->curOffset &&
477467
newOffset <= file->curOffset + file->nbytes)
@@ -488,6 +478,29 @@ BufFileSeek(BufFile *file, int fileno, long offset, int whence)
488478
/* Otherwise, must reposition buffer, so flush any dirty data */
489479
if (BufFileFlush(file) != 0)
490480
return EOF;
481+
/*
482+
* At this point and no sooner, check for seek past last segment.
483+
* The above flush could have created a new segment, so
484+
* checking sooner would not work (at least not with this code).
485+
*/
486+
if (file->isTemp)
487+
{
488+
/* convert seek to "start of next seg" to "end of last seg" */
489+
if (newFile == file->numFiles && newOffset == 0)
490+
{
491+
newFile--;
492+
newOffset = MAX_PHYSICAL_FILESIZE;
493+
}
494+
while (newOffset > MAX_PHYSICAL_FILESIZE)
495+
{
496+
if (++newFile >= file->numFiles)
497+
return EOF;
498+
newOffset -= MAX_PHYSICAL_FILESIZE;
499+
}
500+
}
501+
if (newFile >= file->numFiles)
502+
return EOF;
503+
/* Seek is OK! */
491504
file->curFile = newFile;
492505
file->curOffset = newOffset;
493506
file->pos = 0;

0 commit comments

Comments
 (0)