6
6
* Copyright (c) 1994, Regents of the University of California
7
7
*
8
8
* 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 $
10
10
*
11
11
* NOTES:
12
12
*
@@ -434,16 +434,15 @@ BufFileSeek(BufFile *file, int fileno, long offset, int whence)
434
434
switch (whence )
435
435
{
436
436
case SEEK_SET :
437
- if (fileno < 0 || fileno >= file -> numFiles ||
438
- offset < 0 )
437
+ if (fileno < 0 )
439
438
return EOF ;
440
439
newFile = fileno ;
441
440
newOffset = offset ;
442
441
break ;
443
442
case SEEK_CUR :
444
443
/*
445
444
* 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.. .
447
446
*/
448
447
newFile = file -> curFile ;
449
448
newOffset = (file -> curOffset + file -> pos ) + offset ;
@@ -463,15 +462,6 @@ BufFileSeek(BufFile *file, int fileno, long offset, int whence)
463
462
return EOF ;
464
463
newOffset += MAX_PHYSICAL_FILESIZE ;
465
464
}
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
- }
475
465
if (newFile == file -> curFile &&
476
466
newOffset >= file -> curOffset &&
477
467
newOffset <= file -> curOffset + file -> nbytes )
@@ -488,6 +478,29 @@ BufFileSeek(BufFile *file, int fileno, long offset, int whence)
488
478
/* Otherwise, must reposition buffer, so flush any dirty data */
489
479
if (BufFileFlush (file ) != 0 )
490
480
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! */
491
504
file -> curFile = newFile ;
492
505
file -> curOffset = newOffset ;
493
506
file -> pos = 0 ;
0 commit comments