@@ -110,7 +110,7 @@ static WALKeyCacheRec *tde_wal_key_cache = NULL;
110
110
static WALKeyCacheRec * tde_wal_key_last_rec = NULL ;
111
111
112
112
static InternalKey * pg_tde_get_key_from_file (const RelFileLocator * rlocator , uint32 key_type );
113
- static TDEMapEntry * pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path );
113
+ static bool pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path , TDEMapEntry * map_entry );
114
114
static InternalKey * tde_decrypt_rel_key (TDEPrincipalKey * principal_key , TDEMapEntry * map_entry );
115
115
static int pg_tde_open_file_basic (const char * tde_filename , int fileFlags , bool ignore_missing );
116
116
static void pg_tde_file_header_read (const char * tde_filename , int fd , TDEFileHeader * fheader , off_t * bytes_read );
@@ -906,7 +906,7 @@ pg_tde_open_file_write(const char *tde_filename, const TDESignedPrincipalKeyInfo
906
906
static InternalKey *
907
907
pg_tde_get_key_from_file (const RelFileLocator * rlocator , uint32 key_type )
908
908
{
909
- TDEMapEntry * map_entry ;
909
+ TDEMapEntry map_entry ;
910
910
TDEPrincipalKey * principal_key ;
911
911
LWLock * lock_pk = tde_lwlock_enc_keys ();
912
912
char db_map_path [MAXPGPATH ] = {0 };
@@ -922,10 +922,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
922
922
923
923
LWLockAcquire (lock_pk , LW_SHARED );
924
924
925
- /* Read the map entry and get the index of the relation key */
926
- map_entry = pg_tde_find_map_entry (rlocator , key_type , db_map_path );
927
-
928
- if (map_entry == NULL )
925
+ if (!pg_tde_find_map_entry (rlocator , key_type , db_map_path , & map_entry ))
929
926
{
930
927
LWLockRelease (lock_pk );
931
928
return NULL ;
@@ -949,44 +946,41 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
949
946
errmsg ("principal key not configured" ),
950
947
errhint ("create one using pg_tde_set_key before using encrypted tables" ));
951
948
952
- rel_key = tde_decrypt_rel_key (principal_key , map_entry );
949
+ rel_key = tde_decrypt_rel_key (principal_key , & map_entry );
953
950
954
951
LWLockRelease (lock_pk );
955
952
956
953
return rel_key ;
957
954
}
958
955
959
956
/*
960
- * Returns the entry of the map if we find a valid match; e.g. flags is not
961
- * set to MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one
962
- * provided in rlocator.
957
+ * Returns true if we find a valid match; e.g. flags is not set to
958
+ * MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one provided in
959
+ * rlocator.
963
960
*/
964
- static TDEMapEntry *
965
- pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path )
961
+ static bool
962
+ pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path , TDEMapEntry * map_entry )
966
963
{
967
964
File map_fd ;
968
- TDEMapEntry * map_entry = palloc_object (TDEMapEntry );
969
965
off_t curr_pos = 0 ;
966
+ bool found = false;
970
967
971
968
Assert (rlocator != NULL );
972
969
973
970
map_fd = pg_tde_open_file_read (db_map_path , & curr_pos );
974
971
975
- while (1 )
972
+ while (pg_tde_read_one_map_entry ( map_fd , map_entry , & curr_pos ) )
976
973
{
977
- if (!pg_tde_read_one_map_entry (map_fd , map_entry , & curr_pos ))
978
- {
979
- close (map_fd );
980
- pfree (map_entry );
981
- return NULL ;
982
- }
983
-
984
974
if ((map_entry -> flags & key_type ) && map_entry -> spcOid == rlocator -> spcOid && map_entry -> relNumber == rlocator -> relNumber )
985
975
{
986
- close ( map_fd ) ;
987
- return map_entry ;
976
+ found = true ;
977
+ break ;
988
978
}
989
979
}
980
+
981
+ close (map_fd );
982
+
983
+ return found ;
990
984
}
991
985
992
986
bool
0 commit comments