Skip to content

Commit

Permalink
Move database GUID to the core header part
Browse files Browse the repository at this point in the history
  • Loading branch information
dyemanov committed Jan 19, 2025
1 parent 963d71a commit 30448b5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 39 deletions.
22 changes: 12 additions & 10 deletions src/jrd/ods.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,16 @@ struct header_page
UCHAR hdr_cc; // Compiler of engine on which database was created
UCHAR hdr_compat; // Cross-platform database transfer compatibility flags
} hdr_db_impl;
UCHAR hdr_guid[16]; // Database GUID
SLONG hdr_creation_date[2]; // Date/time of creation
SLONG hdr_shadow_count; // Event count for shadow synchronization
USHORT hdr_sequence; // sequence number of file
USHORT hdr_end; // offset of HDR_end in page
ULONG hdr_crypt_page; // Page at which processing is in progress
TEXT hdr_crypt_plugin[32]; // Name of plugin used to crypt this DB
UCHAR hdr_data[1]; // Misc data
};

static_assert(sizeof(struct header_page) == 144, "struct header_page size mismatch");
static_assert(sizeof(struct header_page) == 160, "struct header_page size mismatch");
static_assert(offsetof(struct header_page, hdr_header) == 0, "hdr_header offset mismatch");
static_assert(offsetof(struct header_page, hdr_page_size) == 16, "hdr_page_size offset mismatch");
static_assert(offsetof(struct header_page, hdr_ods_version) == 18, "hdr_ods_version offset mismatch");
Expand All @@ -512,13 +512,15 @@ static_assert(offsetof(struct header_page, hdr_oldest_active) == 56, "hdr_oldest
static_assert(offsetof(struct header_page, hdr_oldest_snapshot) == 64, "hdr_oldest_snapshot offset mismatch");
static_assert(offsetof(struct header_page, hdr_attachment_id) == 72, "hdr_attachment_id offset mismatch");
static_assert(offsetof(struct header_page, hdr_db_impl) == 80, "hdr_shadow_count offset mismatch");
static_assert(offsetof(struct header_page, hdr_creation_date) == 84, "hdr_creation_date offset mismatch");
static_assert(offsetof(struct header_page, hdr_shadow_count) == 92, "hdr_shadow_count offset mismatch");
static_assert(offsetof(struct header_page, hdr_sequence) == 96, "hdr_sequence offset mismatch");
static_assert(offsetof(struct header_page, hdr_end) == 98, "hdr_end offset mismatch");
static_assert(offsetof(struct header_page, hdr_crypt_page) == 100, "hdr_crypt_page offset mismatch");
static_assert(offsetof(struct header_page, hdr_crypt_plugin) == 104, "hdr_crypt_plugin offset mismatch");
static_assert(offsetof(struct header_page, hdr_data) == 136, "hdr_data offset mismatch");
static_assert(offsetof(struct header_page, hdr_guid) == 84, "hdr_guid offset mismatch");
static_assert(offsetof(struct header_page, hdr_creation_date) == 100, "hdr_creation_date offset mismatch");
static_assert(offsetof(struct header_page, hdr_shadow_count) == 108, "hdr_shadow_count offset mismatch");
static_assert(offsetof(struct header_page, hdr_end) == 112, "hdr_end offset mismatch");
static_assert(offsetof(struct header_page, hdr_crypt_page) == 116, "hdr_crypt_page offset mismatch");
static_assert(offsetof(struct header_page, hdr_crypt_plugin) == 120, "hdr_crypt_plugin offset mismatch");
static_assert(offsetof(struct header_page, hdr_data) == 152, "hdr_data offset mismatch");

static_assert(sizeof(header_page::hdr_guid) == Firebird::Guid::SIZE, "hdr_guid size mismatch");

#define HDR_SIZE static_cast<FB_SIZE_T>(offsetof(Ods::header_page, hdr_data[0]))

Expand All @@ -538,7 +540,7 @@ inline constexpr UCHAR HDR_difference_file = 6; // Delta file that is used duri
inline constexpr UCHAR HDR_backup_guid = 7; // GUID generated on each switch into backup mode
inline constexpr UCHAR HDR_crypt_key = 8; // Name of a key used to crypt database
inline constexpr UCHAR HDR_crypt_hash = 9; // Validator of key correctness
inline constexpr UCHAR HDR_db_guid = 10; // Database GUID
//inline constexpr UCHAR HDR_db_guid = 10; // Database GUID
inline constexpr UCHAR HDR_repl_seq = 11; // Replication changelog sequence
inline constexpr UCHAR HDR_max = 11; // Maximum HDR_clump value

Expand Down
8 changes: 3 additions & 5 deletions src/jrd/os/posix/unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ static bool raw_devices_validate_database(int desc, const PathName& file_name)
}

// Validate database header. Code lifted from PAG_header.
if (hp->hdr_header.pag_type != pag_header /*|| hp->hdr_sequence*/)
if (hp->hdr_header.pag_type != pag_header)
goto quit;

if (!Ods::isSupported(hp))
Expand All @@ -1240,10 +1240,8 @@ static bool raw_devices_validate_database(int desc, const PathName& file_name)

quit:
#ifdef DEV_BUILD
gds__log ("raw_devices_validate_database: %s -> %s%s\n",
file_name.c_str(),
retval ? "true" : "false",
retval && hp->hdr_sequence != 0 ? " (continuation file)" : "");
gds__log ("raw_devices_validate_database: %s -> %s\n",
file_name.c_str(), retval ? "true" : "false");
#endif
return retval;
}
Expand Down
20 changes: 13 additions & 7 deletions src/jrd/pag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ void PAG_header_init(thread_db* tdbb)
PIO_header(tdbb, temp_page, headerSize);
const header_page* header = (header_page*) temp_page;

if (header->hdr_header.pag_type != pag_header || header->hdr_sequence)
if (header->hdr_header.pag_type != pag_header)
ERR_post(Arg::Gds(isc_bad_db_format) << Arg::Str(attachment->att_filename));

const USHORT ods_version = header->hdr_ods_version & ~ODS_FIREBIRD_FLAG;
Expand Down Expand Up @@ -1227,11 +1227,6 @@ void PAG_init2(thread_db* tdbb)
memcpy(&dbb->dbb_sweep_interval, p + 2, sizeof(SLONG));
break;

case HDR_db_guid:
fb_assert(p[1] == Guid::SIZE);
dbb->dbb_guid = Guid(p + 2);
break;

case HDR_repl_seq:
fb_assert(p[1] == sizeof(FB_UINT64));
memcpy(&dbb->dbb_repl_sequence, p + 2, sizeof(FB_UINT64));
Expand Down Expand Up @@ -1390,7 +1385,18 @@ void PAG_set_db_guid(thread_db* tdbb, const Guid& guid)
*
**************************************/
SET_TDBB(tdbb);
storeClump(tdbb, HDR_db_guid, Guid::SIZE, guid.getData());
ensureDbWritable(tdbb);

WIN window(HEADER_PAGE_NUMBER);
header_page* header = (header_page*) CCH_FETCH(tdbb, &window, LCK_write, pag_header);
CCH_MARK_MUST_WRITE(tdbb, &window);

const auto dbb = tdbb->getDatabase();

guid.copyTo(header->hdr_guid);
dbb->dbb_guid = guid;

CCH_RELEASE(tdbb, &window);
}


Expand Down
12 changes: 3 additions & 9 deletions src/utilities/gstat/ppg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void PPG_print_header(const header_page* header, bool nocreation, Firebird::Util
uSvc->printf(false, "\tOldest active\t\t%" SQUADFORMAT"\n", header->hdr_oldest_active);
uSvc->printf(false, "\tOldest snapshot\t\t%" SQUADFORMAT"\n", header->hdr_oldest_snapshot);
uSvc->printf(false, "\tNext transaction\t%" SQUADFORMAT"\n", header->hdr_next_transaction);
uSvc->printf(false, "\tSequence number\t\t%d\n", header->hdr_sequence);
uSvc->printf(false, "\tNext attachment ID\t%" SQUADFORMAT"\n", header->hdr_attachment_id);

Firebird::DbImplementation imp(header);
Expand All @@ -89,6 +88,9 @@ void PPG_print_header(const header_page* header, bool nocreation, Firebird::Util

if (!nocreation)
{
const Guid guid(header->hdr_guid);
uSvc->printf(false, "\tDatabase GUID:\t%s\n", guid.toString().c_str());

struct tm time;
isc_decode_timestamp(reinterpret_cast<const ISC_TIMESTAMP*>(header->hdr_creation_date),
&time);
Expand Down Expand Up @@ -264,14 +266,6 @@ void PPG_print_header(const header_page* header, bool nocreation, Firebird::Util
uSvc->printf(false, "\tCrypt checksum:\t%*.*s\n", p[1], p[1], p + 2);
break;

case HDR_db_guid:
{
fb_assert(p[1] == Guid::SIZE);
const Guid guid(p + 2);
uSvc->printf(false, "\tDatabase GUID:\t%s\n", guid.toString().c_str());
break;
}

case HDR_repl_seq:
{
FB_UINT64 sequence;
Expand Down
12 changes: 5 additions & 7 deletions src/utilities/nbackup/nbackup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,9 @@ void NBackup::fixup_database(bool repl_seq, bool set_readonly)

if (!repl_seq)
{
// Replace existing database GUID with a regenerated one
Guid::generate().copyTo(header->hdr_guid);

size = page_size;
header = reinterpret_cast<Ods::header_page*>(header_buffer.getBuffer(size));

Expand All @@ -871,18 +874,13 @@ void NBackup::fixup_database(bool repl_seq, bool set_readonly)
const auto end = (UCHAR*) header + header->hdr_page_size;
while (p < end && *p != Ods::HDR_end)
{
if (*p == Ods::HDR_db_guid)
{
// Replace existing database GUID with a regenerated one
fb_assert(p[1] == Guid::SIZE);
Guid::generate().copyTo(p + 2);
}
else if (*p == Ods::HDR_repl_seq)
if (*p == Ods::HDR_repl_seq)
{
// Reset the sequence counter
const FB_UINT64 sequence = 0;
fb_assert(p[1] == sizeof(sequence));
memcpy(p + 2, &sequence, sizeof(sequence));
break;
}

p += p[1] + 2;
Expand Down
1 change: 0 additions & 1 deletion src/utilities/rebuild/rebuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,6 @@ static void print_db_header( FILE* file, const header_page* header)
fprintf(file, " Data pages per pointer page\t%ld\n", gdbb->tdbb_database->dbb_dp_per_pp);
fprintf(file, " Max records per page\t%ld\n", gdbb->tdbb_database->dbb_max_records);
//fprintf (" Sequence number %d\n", header->hdr_sequence);
//fprintf (" Creation date \n", header->hdr_creation_date);
fprintf(file, " Next attachment ID\t\t%ld\n", header->hdr_attachment_id);
Expand Down

0 comments on commit 30448b5

Please sign in to comment.