Skip to content

Commit 9649388

Browse files
committed
Fix (fingers crossed) Windows build, some more cleanup and code simplification
1 parent 0b22257 commit 9649388

File tree

2 files changed

+63
-90
lines changed

2 files changed

+63
-90
lines changed

src/jrd/os/posix/unix.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ using namespace Firebird;
132132

133133
static const mode_t MASK = 0660;
134134

135-
static jrd_file* seek_file(jrd_file*, BufferDesc*, FB_UINT64*, FbStatusVector*);
135+
static bool seek_file(jrd_file*, BufferDesc*, FB_UINT64*, FbStatusVector*);
136136
static jrd_file* setup_file(Database*, const PathName&, int, USHORT);
137137
static void lockDatabaseFile(int& desc, const bool shareMode, const bool temporary,
138138
const char* fileName, ISC_STATUS operation);
@@ -565,7 +565,7 @@ void PIO_header(thread_db* tdbb, UCHAR* address, int length)
565565
static Firebird::InitInstance<ZeroBuffer> zeros;
566566

567567

568-
USHORT PIO_init_data(thread_db* tdbb, jrd_file* main_file, FbStatusVector* status_vector,
568+
USHORT PIO_init_data(thread_db* tdbb, jrd_file* file, FbStatusVector* status_vector,
569569
ULONG startPage, USHORT initPages)
570570
{
571571
/**************************************
@@ -592,9 +592,7 @@ USHORT PIO_init_data(thread_db* tdbb, jrd_file* main_file, FbStatusVector* statu
592592

593593
EngineCheckout cout(tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
594594

595-
jrd_file* file = seek_file(main_file, &bdb, &offset, status_vector);
596-
597-
if (!file)
595+
if (!seek_file(file, &bdb, &offset, status_vector))
598596
return 0;
599597

600598
if (startPage < 8)
@@ -617,15 +615,16 @@ USHORT PIO_init_data(thread_db* tdbb, jrd_file* main_file, FbStatusVector* statu
617615

618616
for (int r = 0; r < IO_RETRY; r++)
619617
{
620-
if (!(file = seek_file(file, &bdb, &offset, status_vector)))
621-
return false;
618+
if (!seek_file(file, &bdb, &offset, status_vector))
619+
return 0;
620+
622621
if ((written = os_utils::pwrite(file->fil_desc, zero_buff, to_write, LSEEK_OFFSET_CAST offset)) == to_write)
623622
break;
623+
624624
if (written < 0 && !SYSCALL_INTERRUPTED(errno))
625625
return unix_error("write", file, isc_io_write_err, status_vector);
626626
}
627627

628-
629628
leftPages -= write_pages;
630629
i += write_pages;
631630
}
@@ -762,7 +761,7 @@ bool PIO_read(thread_db* tdbb, jrd_file* file, BufferDesc* bdb, Ods::pag* page,
762761

763762
for (i = 0; i < IO_RETRY; i++)
764763
{
765-
if (!(file = seek_file(file, bdb, &offset, status_vector)))
764+
if (!seek_file(file, bdb, &offset, status_vector))
766765
return false;
767766

768767
if ((bytes = os_utils::pread(file->fil_desc, page, size, LSEEK_OFFSET_CAST offset)) == size)
@@ -814,7 +813,7 @@ bool PIO_write(thread_db* tdbb, jrd_file* file, BufferDesc* bdb, Ods::pag* page,
814813

815814
for (i = 0; i < IO_RETRY; i++)
816815
{
817-
if (!(file = seek_file(file, bdb, &offset, status_vector)))
816+
if (!seek_file(file, bdb, &offset, status_vector))
818817
return false;
819818

820819
if ((bytes = os_utils::pwrite(file->fil_desc, page, size, LSEEK_OFFSET_CAST offset)) == size)
@@ -831,8 +830,8 @@ bool PIO_write(thread_db* tdbb, jrd_file* file, BufferDesc* bdb, Ods::pag* page,
831830
}
832831

833832

834-
static jrd_file* seek_file(jrd_file* file, BufferDesc* bdb, FB_UINT64* offset,
835-
FbStatusVector* status_vector)
833+
static bool seek_file(jrd_file* file, BufferDesc* bdb, FB_UINT64* offset,
834+
FbStatusVector* status_vector)
836835
{
837836
/**************************************
838837
*
@@ -841,8 +840,7 @@ static jrd_file* seek_file(jrd_file* file, BufferDesc* bdb, FB_UINT64* offset,
841840
**************************************
842841
*
843842
* Functional description
844-
* Given a buffer descriptor block, find the appropriate
845-
* file block and seek to the proper page in that file.
843+
* Given a buffer descriptor block, seek to the proper page in that file.
846844
*
847845
**************************************/
848846
BufferControl* const bcb = bdb->bdb_bcb;
@@ -851,7 +849,7 @@ static jrd_file* seek_file(jrd_file* file, BufferDesc* bdb, FB_UINT64* offset,
851849
if (file->fil_desc == -1)
852850
{
853851
unix_error("lseek", file, isc_io_access_err, status_vector);
854-
return 0;
852+
return false;
855853
}
856854

857855
FB_UINT64 lseek_offset = page;
@@ -860,12 +858,11 @@ static jrd_file* seek_file(jrd_file* file, BufferDesc* bdb, FB_UINT64* offset,
860858
if (lseek_offset != (FB_UINT64) LSEEK_OFFSET_CAST lseek_offset)
861859
{
862860
unix_error("lseek", file, isc_io_32bit_exceeded_err, status_vector);
863-
return 0;
861+
return false;
864862
}
865863

866864
*offset = lseek_offset;
867-
868-
return file;
865+
return true;
869866
}
870867

871868

src/jrd/os/win32/winnt.cpp

Lines changed: 48 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ using namespace Firebird;
106106
#define TEXT SCHAR
107107

108108
static bool maybeCloseFile(HANDLE&);
109-
static jrd_file* seek_file(jrd_file*, BufferDesc*, OVERLAPPED*);
109+
static bool seek_file(jrd_file*, BufferDesc*, OVERLAPPED*);
110110
static jrd_file* setup_file(Database*, const Firebird::PathName&, HANDLE, USHORT);
111111
static 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)
372372
static 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

Comments
 (0)