Skip to content

Commit 15ea835

Browse files
committed
Do not switch between using prev_pos and curr_pos
Be consistent about always passing curr_pos when reading or writing the map file. The code is easier to understand if only one variable is used for positioning in the file.
1 parent 0b2dbd2 commit 15ea835

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

contrib/pg_tde/src/access/pg_tde_tdemap.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, InternalKey *rel_key_
422422
char db_map_path[MAXPGPATH];
423423
int map_fd;
424424
off_t curr_pos = 0;
425-
off_t prev_pos = 0;
426425
TDEMapEntry write_map_entry;
427426
TDESignedPrincipalKeyInfo signed_key_Info;
428427

@@ -434,7 +433,6 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, InternalKey *rel_key_
434433

435434
/* Open and validate file for basic correctness. */
436435
map_fd = pg_tde_open_file_write(db_map_path, &signed_key_Info, false, &curr_pos);
437-
prev_pos = curr_pos;
438436

439437
/*
440438
* Read until we find an empty slot. Otherwise, read until end. This seems
@@ -444,13 +442,19 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, InternalKey *rel_key_
444442
while (1)
445443
{
446444
TDEMapEntry read_map_entry;
445+
off_t prev_pos = curr_pos;
447446

448-
prev_pos = curr_pos;
449447
if (!pg_tde_read_one_map_entry(map_fd, &read_map_entry, &curr_pos))
448+
{
449+
curr_pos = prev_pos;
450450
break;
451+
}
451452

452453
if (read_map_entry.flags == MAP_ENTRY_EMPTY)
454+
{
455+
curr_pos = prev_pos;
453456
break;
457+
}
454458
}
455459

456460
/* Initialize map entry and encrypt key */
@@ -468,12 +472,8 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, InternalKey *rel_key_
468472
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_ADD_RELATION_KEY);
469473
}
470474

471-
/*
472-
* Write the given entry at the location pointed by prev_pos; i.e. the
473-
* free entry
474-
*/
475-
curr_pos = prev_pos;
476-
pg_tde_write_one_map_entry(map_fd, &write_map_entry, &prev_pos, db_map_path);
475+
/* Write the given entry at curr_pos; i.e. the free entry. */
476+
pg_tde_write_one_map_entry(map_fd, &write_map_entry, &curr_pos, db_map_path);
477477

478478
close(map_fd);
479479
}
@@ -490,15 +490,13 @@ pg_tde_write_key_map_entry_redo(const TDEMapEntry *write_map_entry, TDESignedPri
490490
char db_map_path[MAXPGPATH];
491491
int map_fd;
492492
off_t curr_pos = 0;
493-
off_t prev_pos = 0;
494493

495494
pg_tde_set_db_file_path(signed_key_info->data.databaseId, db_map_path);
496495

497496
LWLockAcquire(tde_lwlock_enc_keys(), LW_EXCLUSIVE);
498497

499498
/* Open and validate file for basic correctness. */
500499
map_fd = pg_tde_open_file_write(db_map_path, signed_key_info, false, &curr_pos);
501-
prev_pos = curr_pos;
502500

503501
/*
504502
* Read until we find an empty slot. Otherwise, read until end. This seems
@@ -508,21 +506,23 @@ pg_tde_write_key_map_entry_redo(const TDEMapEntry *write_map_entry, TDESignedPri
508506
while (1)
509507
{
510508
TDEMapEntry read_map_entry;
509+
off_t prev_pos = curr_pos;
511510

512-
prev_pos = curr_pos;
513511
if (!pg_tde_read_one_map_entry(map_fd, &read_map_entry, &curr_pos))
512+
{
513+
curr_pos = prev_pos;
514514
break;
515+
}
515516

516517
if (read_map_entry.flags == MAP_ENTRY_EMPTY)
518+
{
519+
curr_pos = prev_pos;
517520
break;
521+
}
518522
}
519523

520-
/*
521-
* Write the given entry at the location pointed by prev_pos; i.e. the
522-
* free entry
523-
*/
524-
curr_pos = prev_pos;
525-
pg_tde_write_one_map_entry(map_fd, write_map_entry, &prev_pos, db_map_path);
524+
/* Write the given entry at curr_pos; i.e. the free entry. */
525+
pg_tde_write_one_map_entry(map_fd, write_map_entry, &curr_pos, db_map_path);
526526

527527
close(map_fd);
528528

0 commit comments

Comments
 (0)