@@ -106,7 +106,7 @@ using namespace Firebird;
106106#define TEXT SCHAR
107107
108108static bool maybeCloseFile (HANDLE&);
109- static jrd_file* seek_file (jrd_file*, BufferDesc*, OVERLAPPED*);
109+ static bool seek_file (jrd_file*, BufferDesc*, OVERLAPPED*);
110110static jrd_file* setup_file (Database*, const Firebird::PathName&, HANDLE, USHORT);
111111static bool nt_error (const TEXT*, const jrd_file*, ISC_STATUS, FbStatusVector* const );
112112
@@ -216,7 +216,7 @@ bool PIO_expand(const TEXT* file_name, USHORT file_length, TEXT* expanded_name,
216216}
217217
218218
219- void PIO_extend (thread_db* tdbb, jrd_file* main_file , const ULONG extPages, const USHORT pageSize)
219+ void PIO_extend (thread_db* tdbb, jrd_file* file , const ULONG extPages, const USHORT pageSize)
220220{
221221/* *************************************
222222 *
@@ -238,11 +238,11 @@ void PIO_extend(thread_db* tdbb, jrd_file* main_file, const ULONG extPages, cons
238238 // and read\write activity performed simultaneously)
239239
240240 // if file have no extend lock it is better to not extend file than corrupt it
241- if (!main_file ->fil_ext_lock )
241+ if (!file ->fil_ext_lock )
242242 return ;
243243
244244 EngineCheckout cout (tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
245- FileExtendLockGuard extLock (main_file ->fil_ext_lock , true );
245+ FileExtendLockGuard extLock (file ->fil_ext_lock , true );
246246
247247 const ULONG filePages = PIO_get_number_of_pages (file, pageSize);
248248 const ULONG extendBy = MIN (MAX_ULONG - filePages, extPages);
@@ -372,7 +372,7 @@ void PIO_header(thread_db* tdbb, UCHAR* address, int length)
372372static Firebird::InitInstance<ZeroBuffer> zeros;
373373
374374
375- USHORT PIO_init_data (thread_db* tdbb, jrd_file* main_file , FbStatusVector* status_vector,
375+ USHORT PIO_init_data (thread_db* tdbb, jrd_file* file , FbStatusVector* status_vector,
376376 ULONG startPage, USHORT initPages)
377377{
378378/* *************************************
@@ -391,17 +391,15 @@ USHORT PIO_init_data(thread_db* tdbb, jrd_file* main_file, FbStatusVector* statu
391391 Database* const dbb = tdbb->getDatabase ();
392392
393393 EngineCheckout cout (tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
394- FileExtendLockGuard extLock (main_file ->fil_ext_lock , false );
394+ FileExtendLockGuard extLock (file ->fil_ext_lock , false );
395395
396396 // Fake buffer, used in seek_file. Page space ID doesn't matter there
397397 // as we already know file to work with
398398 BufferDesc bdb (dbb->dbb_bcb );
399399 bdb.bdb_page = PageNumber (0 , startPage);
400400
401401 OVERLAPPED overlapped;
402- jrd_file* file = seek_file (main_file, &bdb, &overlapped);
403-
404- if (!file)
402+ if (!seek_file (file, &bdb, &overlapped))
405403 return 0 ;
406404
407405 if (startPage < 8 )
@@ -419,8 +417,8 @@ USHORT PIO_init_data(thread_db* tdbb, jrd_file* main_file, FbStatusVector* statu
419417 if (write_pages > leftPages)
420418 write_pages = leftPages;
421419
422- jrd_file* file1 = seek_file (main_file , &bdb, &overlapped);
423- fb_assert (file1 == file) ;
420+ if (! seek_file (file , &bdb, &overlapped))
421+ return 0 ;
424422
425423 const DWORD to_write = (DWORD) write_pages * dbb->dbb_page_size ;
426424 DWORD written;
@@ -545,7 +543,7 @@ bool PIO_read(thread_db* tdbb, jrd_file* file, BufferDesc* bdb, Ods::pag* page,
545543 FileExtendLockGuard extLock (file->fil_ext_lock , false );
546544
547545 OVERLAPPED overlapped;
548- if (!(file = seek_file (file, bdb, &overlapped) ))
546+ if (!seek_file (file, bdb, &overlapped))
549547 return false ;
550548
551549 HANDLE desc = file->fil_desc ;
@@ -580,14 +578,12 @@ bool PIO_read_ahead(thread_db* tdbb,
580578 **************************************
581579 *
582580 * Functional description
583- * Read a contiguous set of pages. The only
584- * tricky part is to segment the I/O when crossing
585- * file boundaries.
581+ * Read a contiguous set of pages.
586582 *
587583 **************************************/
588584 OVERLAPPED overlapped, *overlapped_ptr;
589585
590- Database* const dbb = tdbb->getDatabase ();
586+ const auto dbb = tdbb->getDatabase ();
591587
592588 EngineCheckout cout (tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
593589
@@ -602,61 +598,46 @@ bool PIO_read_ahead(thread_db* tdbb,
602598 piob->piob_flags = 0 ;
603599 }
604600
601+ // Setup up a dummy buffer descriptor block for seeking file
605602 BufferDesc bdb;
606- while (pages)
607- {
608- // Setup up a dummy buffer descriptor block for seeking file.
609-
610- bdb.bdb_dbb = dbb;
611- bdb.bdb_page = start_page;
612-
613- jrd_file* file = seek_file (dbb->dbb_file , &bdb, status_vector, overlapped_ptr, &overlapped_ptr);
614- if (!file)
615- return false ;
603+ bdb.bdb_dbb = dbb;
604+ bdb.bdb_page = start_page;
616605
617- // Check that every page within the set resides in the same database
618- // file. If not read what you can and loop back for the rest.
606+ const jrd_file* const file = dbb->dbb_file ;
619607
620- DWORD segmented_length = 0 ;
621- while (pages && start_page >= file->fil_min_page && start_page <= file->fil_max_page )
622- {
623- segmented_length += dbb->dbb_page_size ;
624- ++start_page;
625- --pages;
626- }
627-
628- HANDLE desc = file->fil_desc ;
629-
630- DWORD actual_length;
631- if (ReadFile (desc, buffer, segmented_length, &actual_length, overlapped_ptr) &&
632- actual_length == segmented_length)
633- {
634- if (piob && !pages)
635- piob->piob_flags = PIOB_success;
636- }
637- else if (piob && !pages)
638- {
639- piob->piob_flags = PIOB_pending;
640- piob->piob_desc = reinterpret_cast <SLONG>(desc);
641- piob->piob_file = file;
642- piob->piob_io_length = segmented_length;
643- }
644- else if (!GetOverlappedResult (desc, overlapped_ptr, &actual_length, TRUE ) ||
645- actual_length != segmented_length)
646- {
647- if (piob)
648- piob->piob_flags = PIOB_error;
608+ if (!seek_file (file, &bdb, status_vector, overlapped_ptr, &overlapped_ptr))
609+ return false ;
649610
650- release_io_event (file, overlapped_ptr);
651- return nt_error (" GetOverlappedResult" , file, isc_io_read_err, status_vector);
652- }
611+ const HANDLE desc = file->fil_desc ;
612+ const DWORD length = pages * dbb->dbb_page_size ;
653613
654- if (!piob || (piob->piob_flags & (PIOB_success | PIOB_error)))
655- release_io_event (file, overlapped_ptr);
614+ DWORD actual_length;
615+ if (ReadFile (desc, buffer, length, &actual_length, overlapped_ptr) &&
616+ actual_length == length)
617+ {
618+ if (piob)
619+ piob->piob_flags = PIOB_success;
620+ }
621+ else if (piob)
622+ {
623+ piob->piob_flags = PIOB_pending;
624+ piob->piob_desc = reinterpret_cast <SLONG>(desc);
625+ piob->piob_file = file;
626+ piob->piob_io_length = segmented_length;
627+ }
628+ else if (!GetOverlappedResult (desc, overlapped_ptr, &actual_length, TRUE ) ||
629+ actual_length != length)
630+ {
631+ if (piob)
632+ piob->piob_flags = PIOB_error;
656633
657- buffer += segmented_length;
634+ release_io_event (file, overlapped_ptr);
635+ return nt_error (" GetOverlappedResult" , file, isc_io_read_err, status_vector);
658636 }
659637
638+ if (!piob || (piob->piob_flags & (PIOB_success | PIOB_error)))
639+ release_io_event (file, overlapped_ptr);
640+
660641 return true ;
661642}
662643#endif
@@ -722,8 +703,7 @@ bool PIO_write(thread_db* tdbb, jrd_file* file, BufferDesc* bdb, Ods::pag* page,
722703 FileExtendLockGuard extLock (file->fil_ext_lock , false );
723704
724705 OVERLAPPED overlapped;
725- file = seek_file (file, bdb, &overlapped);
726- if (!file)
706+ if (!seek_file (file, bdb, &overlapped))
727707 return false ;
728708
729709 HANDLE desc = file->fil_desc ;
@@ -768,9 +748,7 @@ ULONG PIO_get_number_of_pages(const jrd_file* file, const USHORT pagesize)
768748}
769749
770750
771- static jrd_file* seek_file (jrd_file* file,
772- BufferDesc* bdb,
773- OVERLAPPED* overlapped)
751+ static bool seek_file (jrd_file* file, BufferDesc* bdb, OVERLAPPED* overlapped)
774752{
775753/* *************************************
776754 *
@@ -779,8 +757,7 @@ static jrd_file* seek_file(jrd_file* file,
779757 **************************************
780758 *
781759 * Functional description
782- * Given a buffer descriptor block, find the appropriate
783- * file block and seek to the proper page in that file.
760+ * Given a buffer descriptor block, seek to the proper page in that file.
784761 *
785762 **************************************/
786763 BufferControl* const bcb = bdb->bdb_bcb ;
@@ -797,7 +774,7 @@ static jrd_file* seek_file(jrd_file* file,
797774 ThreadSync* thd = ThreadSync::getThread (FB_FUNCTION);
798775 overlapped->hEvent = thd->getIOEvent ();
799776
800- return file ;
777+ return true ;
801778}
802779
803780
@@ -819,7 +796,6 @@ static jrd_file* setup_file(Database* dbb, const Firebird::PathName& file_name,
819796 {
820797 file = FB_NEW_RPT (*dbb->dbb_permanent , file_name.length () + 1 ) jrd_file ();
821798 file->fil_desc = desc;
822- file->fil_max_page = MAX_ULONG;
823799 file->fil_flags = flags;
824800 strcpy (file->fil_string , file_name.c_str ());
825801
0 commit comments