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