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)
434434switch (whence )
435435{
436436case SEEK_SET :
437- if (fileno < 0 || fileno >=file -> numFiles ||
438- offset < 0 )
437+ if (fileno < 0 )
439438return EOF ;
440439newFile = fileno ;
441440newOffset = offset ;
442441break ;
443442case 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 */
448447newFile = file -> curFile ;
449448newOffset = (file -> curOffset + file -> pos )+ offset ;
@@ -463,15 +462,6 @@ BufFileSeek(BufFile *file, int fileno, long offset, int whence)
463462return EOF ;
464463newOffset += 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- }
475465if (newFile == file -> curFile &&
476466newOffset >=file -> curOffset &&
477467newOffset <=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 */
489479if (BufFileFlush (file )!= 0 )
490480return 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! */
491504file -> curFile = newFile ;
492505file -> curOffset = newOffset ;
493506file -> pos = 0 ;