@@ -109,7 +109,7 @@ static WALKeyCacheRec *tde_wal_key_cache = NULL;
109
109
static WALKeyCacheRec * tde_wal_key_last_rec = NULL ;
110
110
111
111
static InternalKey * pg_tde_get_key_from_file (const RelFileLocator * rlocator , uint32 key_type );
112
- static TDEMapEntry * pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path );
112
+ static bool pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path , TDEMapEntry * map_entry );
113
113
static InternalKey * tde_decrypt_rel_key (TDEPrincipalKey * principal_key , TDEMapEntry * map_entry );
114
114
static int pg_tde_open_file_basic (const char * tde_filename , int fileFlags , bool ignore_missing );
115
115
static void pg_tde_file_header_read (const char * tde_filename , int fd , TDEFileHeader * fheader , off_t * bytes_read );
@@ -846,7 +846,7 @@ pg_tde_open_file_write(const char *tde_filename, const TDESignedPrincipalKeyInfo
846
846
static InternalKey *
847
847
pg_tde_get_key_from_file (const RelFileLocator * rlocator , uint32 key_type )
848
848
{
849
- TDEMapEntry * map_entry ;
849
+ TDEMapEntry map_entry ;
850
850
TDEPrincipalKey * principal_key ;
851
851
LWLock * lock_pk = tde_lwlock_enc_keys ();
852
852
char db_map_path [MAXPGPATH ] = {0 };
@@ -862,10 +862,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
862
862
863
863
LWLockAcquire (lock_pk , LW_SHARED );
864
864
865
- /* Read the map entry and get the index of the relation key */
866
- map_entry = pg_tde_find_map_entry (rlocator , key_type , db_map_path );
867
-
868
- if (map_entry == NULL )
865
+ if (!pg_tde_find_map_entry (rlocator , key_type , db_map_path , & map_entry ))
869
866
{
870
867
LWLockRelease (lock_pk );
871
868
return NULL ;
@@ -889,44 +886,41 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
889
886
errmsg ("principal key not configured" ),
890
887
errhint ("create one using pg_tde_set_key before using encrypted tables" ));
891
888
892
- rel_key = tde_decrypt_rel_key (principal_key , map_entry );
889
+ rel_key = tde_decrypt_rel_key (principal_key , & map_entry );
893
890
894
891
LWLockRelease (lock_pk );
895
892
896
893
return rel_key ;
897
894
}
898
895
899
896
/*
900
- * Returns the entry of the map if we find a valid match; e.g. flags is not
901
- * set to MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one
902
- * provided in rlocator.
897
+ * Returns true if we find a valid match; e.g. flags is not set to
898
+ * MAP_ENTRY_EMPTY and the relNumber and spcOid matches the one provided in
899
+ * rlocator.
903
900
*/
904
- static TDEMapEntry *
905
- pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path )
901
+ static bool
902
+ pg_tde_find_map_entry (const RelFileLocator * rlocator , uint32 key_type , char * db_map_path , TDEMapEntry * map_entry )
906
903
{
907
904
File map_fd ;
908
- TDEMapEntry * map_entry = palloc_object (TDEMapEntry );
909
905
off_t curr_pos = 0 ;
906
+ bool found = false;
910
907
911
908
Assert (rlocator != NULL );
912
909
913
910
map_fd = pg_tde_open_file_read (db_map_path , & curr_pos );
914
911
915
- while (1 )
912
+ while (pg_tde_read_one_map_entry ( map_fd , map_entry , & curr_pos ) )
916
913
{
917
- if (!pg_tde_read_one_map_entry (map_fd , map_entry , & curr_pos ))
918
- {
919
- close (map_fd );
920
- pfree (map_entry );
921
- return NULL ;
922
- }
923
-
924
914
if ((map_entry -> flags & key_type ) && map_entry -> spcOid == rlocator -> spcOid && map_entry -> relNumber == rlocator -> relNumber )
925
915
{
926
- close ( map_fd ) ;
927
- return map_entry ;
916
+ found = true ;
917
+ break ;
928
918
}
929
919
}
920
+
921
+ close (map_fd );
922
+
923
+ return found ;
930
924
}
931
925
932
926
bool
0 commit comments