@@ -110,7 +110,7 @@ static WALKeyCacheRec *tde_wal_key_cache = NULL;
110110static WALKeyCacheRec * tde_wal_key_last_rec = NULL ;
111111
112112static 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 );
114114static InternalKey * tde_decrypt_rel_key (TDEPrincipalKey * principal_key , TDEMapEntry * map_entry );
115115static int pg_tde_open_file_basic (const char * tde_filename , int fileFlags , bool ignore_missing );
116116static 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
905905static InternalKey *
906906pg_tde_get_key_from_file (const RelFileLocator * rlocator , uint32 key_type )
907907{
908- TDEMapEntry * map_entry ;
908+ TDEMapEntry map_entry ;
909909 TDEPrincipalKey * principal_key ;
910910 LWLock * lock_pk = tde_lwlock_enc_keys ();
911911 char db_map_path [MAXPGPATH ] = {0 };
@@ -921,10 +921,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
921921
922922 LWLockAcquire (lock_pk , LW_SHARED );
923923
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 ))
928925 {
929926 LWLockRelease (lock_pk );
930927 return NULL ;
@@ -948,44 +945,41 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type)
948945 errmsg ("principal key not configured" ),
949946 errhint ("create one using pg_tde_set_key before using encrypted tables" ));
950947
951- rel_key = tde_decrypt_rel_key (principal_key , map_entry );
948+ rel_key = tde_decrypt_rel_key (principal_key , & map_entry );
952949
953950 LWLockRelease (lock_pk );
954951
955952 return rel_key ;
956953}
957954
958955/*
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.
962959 */
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 )
965962{
966963 File map_fd ;
967- TDEMapEntry * map_entry = palloc_object (TDEMapEntry );
968964 off_t curr_pos = 0 ;
965+ bool found = false;
969966
970967 Assert (rlocator != NULL );
971968
972969 map_fd = pg_tde_open_file_read (db_map_path , & curr_pos );
973970
974- while (1 )
971+ while (pg_tde_read_one_map_entry ( map_fd , map_entry , & curr_pos ) )
975972 {
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-
983973 if ((map_entry -> flags & key_type ) && map_entry -> spcOid == rlocator -> spcOid && map_entry -> relNumber == rlocator -> relNumber )
984974 {
985- close ( map_fd ) ;
986- return map_entry ;
975+ found = true ;
976+ break ;
987977 }
988978 }
979+
980+ close (map_fd );
981+
982+ return found ;
989983}
990984
991985bool
0 commit comments