From e8f069271f79aebed13b3d6c28197a87f40cd7cb Mon Sep 17 00:00:00 2001 From: Mark Reynolds Date: Sep 02 2020 13:12:05 +0000 Subject: Issue 51121 - Remove hardcoded changelog file name Bug Description: There are several places in the code where the new changelog name is just a hardcoded string. So if the name changes we will have many places that need to be updated. Fix Description: Use a "define" for the file name in the bdb code, and in the changelog get get the filename from backend ldbminfo. Also extended the ldbminfo flags to include the backend implementation. This will be used as we start to need a way t odetect what database backend is in use. relates: https://pagure.io/389-ds-base/issue/51121 Reviewed by: firstyear(Thanks!) --- diff --git a/ldap/servers/plugins/replication/cl5_init.c b/ldap/servers/plugins/replication/cl5_init.c index 318677e..77e1793 100644 --- a/ldap/servers/plugins/replication/cl5_init.c +++ b/ldap/servers/plugins/replication/cl5_init.c @@ -99,6 +99,7 @@ changelog5_cleanup() /* cleanup config */ changelog5_config_cleanup(); } + static int _cl5_upgrade_replica_config(Replica *replica, changelog5Config *config) { @@ -119,13 +120,11 @@ _cl5_upgrade_replica_config(Replica *replica, changelog5Config *config) slapi_entry_add_string(config_entry, CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE, config->maxAge); } if (config->trimInterval != CHANGELOGDB_TRIM_INTERVAL) { - /* char *interval = slapi_ch_smprintf("%ld", config->trimInterval); */ slapi_entry_add_string(config_entry, CONFIG_CHANGELOG_TRIM_ATTRIBUTE, gen_duration(config->trimInterval)); } /* if changelog encryption is enabled then in the upgrade mode all backends will have * an encrypted changelog, store the encryption attrs */ - if (config->encryptionAlgorithm) { slapi_entry_add_string(config_entry, CONFIG_CHANGELOG_ENCRYPTION_ALGORITHM, config->encryptionAlgorithm); slapi_entry_add_string(config_entry, CONFIG_CHANGELOG_SYMMETRIC_KEY, config->symmetricKey); @@ -134,36 +133,43 @@ _cl5_upgrade_replica_config(Replica *replica, changelog5Config *config) return rc; } + static int _cl5_upgrade_replica(Replica *replica, void *arg) { - int rc = 0; changelog5Config *config = (changelog5Config *)arg; + const char *replName = replica_get_name(replica); + char *replGen = replica_get_generation(replica); + char *oldFile = slapi_ch_smprintf("%s/%s_%s.db", config->dir, replName, replGen); + char *newFile = NULL; + char *instancedir = NULL; + int rc = 0; - /* Move existing database file to backend */ - char *replGen = replica_get_generation (replica); - const char *replName = replica_get_name (replica); - char *oldFile = slapi_ch_smprintf("%s/%s_%s.db", - config->dir, replName, replGen); - slapi_ch_free_string(&replGen); if (PR_Access(oldFile, PR_ACCESS_EXISTS) == PR_SUCCESS) { Slapi_Backend *be = slapi_be_select(replica_get_root(replica)); - char *instancedir; - slapi_back_get_info(be, BACK_INFO_INSTANCE_DIR, (void **)&instancedir); - char *newFile = slapi_ch_smprintf("%s/changelog.db", instancedir); + char *cl_filename; + slapi_back_get_info(be, BACK_INFO_INSTANCE_DIR, (void **)&instancedir); + slapi_back_get_info(be, BACK_INFO_CLDB_FILENAME, (void **)&cl_filename); + newFile = slapi_ch_smprintf("%s/%s", instancedir, cl_filename); rc = slapi_back_ctrl_info(be, BACK_INFO_DBENV_CLDB_UPGRADE, oldFile); slapi_log_err(SLAPI_LOG_INFO, repl_plugin_name_cl, - "_cl5_upgrade_replica: moving file (%s) to (%s) %s\n", + "_cl5_upgrade_replica: moving changelog file (%s) to (%s) %s\n", oldFile, newFile, rc?"failed":"succeeded"); - slapi_ch_free_string(&instancedir); } /* Move changelog config to backend config */ rc = _cl5_upgrade_replica_config(replica, config); + /* Cleanup */ + slapi_ch_free_string(&instancedir); + slapi_ch_free_string(&oldFile); + slapi_ch_free_string(&newFile); + slapi_ch_free_string(&replGen); + return rc; } + static int _cl5_upgrade_removedir(char *path) { @@ -198,6 +204,7 @@ _cl5_upgrade_removedir(char *path) rval += PR_RmDir(path); return rval; } + static int _cl5_upgrade_removeconfig(void) { diff --git a/ldap/servers/slapd/back-ldbm/back-ldbm.h b/ldap/servers/slapd/back-ldbm/back-ldbm.h index 94ab66e..b8091ff 100644 --- a/ldap/servers/slapd/back-ldbm/back-ldbm.h +++ b/ldap/servers/slapd/back-ldbm/back-ldbm.h @@ -118,6 +118,10 @@ typedef unsigned short u_int16_t; #define BDB_RDNFORMAT_VERSION "3" /* rdn-format version (by default, 0) */ #define BDB_DNFORMAT "dn-4514" /* DN format RFC 4514 compliant */ #define BDB_DNFORMAT_VERSION "1" /* DN format version */ +#define BDB_CL_FILENAME "changelog.db" + +#define LMDB_IMPL "lmdb" +#define LMDB_CL_FILENAME "changelog.mdb" #define DBVERSION_NEWIDL 0x1 #define DBVERSION_RDNFORMAT 0x2 @@ -645,6 +649,10 @@ struct ldbminfo */ /* allow conf w/o CONFIG_FLAG_ALLOW_RUNNING_CHANGE to be updated */ #define LI_FORCE_MOD_CONFIG 0x10 +#define LI_BDB_IMPL 0x20 +#define LI_LMDB_IMPL 0x40 + +#define LI_DEFAULT_IMPL_FLAG LI_BDB_IMPL /* the default is BDB for now */ /* Structure used to hold stuff for the lifetime of an LDAP transaction */ /* If we do clever stuff like LDAP transactions, we'll need a stack of TXN ID's */ diff --git a/ldap/servers/slapd/back-ldbm/db-bdb/bdb_layer.c b/ldap/servers/slapd/back-ldbm/db-bdb/bdb_layer.c index 44e2467..2948b28 100644 --- a/ldap/servers/slapd/back-ldbm/db-bdb/bdb_layer.c +++ b/ldap/servers/slapd/back-ldbm/db-bdb/bdb_layer.c @@ -4264,7 +4264,7 @@ _dblayer_delete_instance_dir(ldbm_instance *inst, int startdb) if (pEnv && /* PL_strcmp takes NULL arg */ (PL_strcmp(LDBM_FILENAME_SUFFIX, strrchr(direntry->name, '.')) == 0)) { - if (strcmp(direntry->name, "changelog.db") == 0) { + if (strcmp(direntry->name, BDB_CL_FILENAME) == 0) { /* do not delete the changelog, if it no longer * matches the database it will be recreated later */ @@ -6002,6 +6002,11 @@ bdb_get_info(Slapi_Backend *be, int cmd, void **info) } break; } + case BACK_INFO_CLDB_FILENAME: { + *(char **)info = BDB_CL_FILENAME; + rc = 0; + break; + } default: break; } @@ -6074,7 +6079,7 @@ bdb_back_ctrl(Slapi_Backend *be, int cmd, void *info) if (priv && priv->dblayer_env) { char *instancedir; slapi_back_get_info(be, BACK_INFO_INSTANCE_DIR, (void **)&instancedir); - char *path = slapi_ch_smprintf("%s/changelog.db", instancedir); + char *path = slapi_ch_smprintf("%s/%s", instancedir, BDB_CL_FILENAME); db->close(db, 0); rc = bdb_db_remove_ex((bdb_db_env *)priv->dblayer_env, path, NULL, PR_TRUE); inst->inst_changelog = NULL; @@ -6086,16 +6091,20 @@ bdb_back_ctrl(Slapi_Backend *be, int cmd, void *info) case BACK_INFO_DBENV_CLDB_UPGRADE: { struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private; char *oldFile = (char *)info; + if (li) { dblayer_private *priv = (dblayer_private *)li->li_dblayer_private; if (priv && priv->dblayer_env) { DB_ENV *pEnv = ((bdb_db_env *)priv->dblayer_env)->bdb_DB_ENV; if (pEnv) { char *instancedir; + char *newFile; + slapi_back_get_info(be, BACK_INFO_INSTANCE_DIR, (void **)&instancedir); - char *newFile = slapi_ch_smprintf("%s/changelog.db", instancedir); + newFile = slapi_ch_smprintf("%s/%s", instancedir, BDB_CL_FILENAME); rc = pEnv->dbrename(pEnv, 0, oldFile, 0, newFile, 0); slapi_ch_free_string(&instancedir); + slapi_ch_free_string(&newFile); bdb_force_logrenewal(li); } } diff --git a/ldap/servers/slapd/back-ldbm/ldbm_config.c b/ldap/servers/slapd/back-ldbm/ldbm_config.c index 3fe86d5..84fdc3c 100644 --- a/ldap/servers/slapd/back-ldbm/ldbm_config.c +++ b/ldap/servers/slapd/back-ldbm/ldbm_config.c @@ -283,6 +283,15 @@ ldbm_config_backend_implement_set(void *arg, void *value, char *errorbuf __attri if (apply) { slapi_ch_free((void **)&(li->li_backend_implement)); li->li_backend_implement = slapi_ch_strdup((char *)value); + /* Set a flag that we can efficiently check which backend + * implementation we are using */ + if (strcasecmp(li->li_backend_implement, BDB_IMPL) == 0) { + li->li_flags |= LI_BDB_IMPL; + } else if (strcasecmp(li->li_backend_implement, LMDB_IMPL) == 0) { + li->li_flags |= LI_LMDB_IMPL; + } else { + li->li_flags |= LI_DEFAULT_IMPL_FLAG; + } } return retval; diff --git a/ldap/servers/slapd/slapi-plugin.h b/ldap/servers/slapd/slapi-plugin.h index fa79a49..c97f8ea 100644 --- a/ldap/servers/slapd/slapi-plugin.h +++ b/ldap/servers/slapd/slapi-plugin.h @@ -7721,7 +7721,7 @@ int slapi_check_account_lock(Slapi_PBlock *pb, Slapi_Entry *bind_target_entry, i /** * Get backend info based upon cmd * - * \param be Backend from which the infomation will be retrieved + * \param be Backend from which the information will be retrieved * \param cmd macro to specify the information type * \param info pointer to store the information * \return \c 0 if the operation was successful @@ -7787,7 +7787,8 @@ enum BACK_INFO_INDEX_KEY, /* Get the status of a key in an index */ BACK_INFO_DB_DIRECTORY, /* Get the db directory */ BACK_INFO_DBHOME_DIRECTORY, /* Get the dbhome directory */ - BACK_INFO_IS_ENTRYRDN /* Get the flag for entryrdn */ + BACK_INFO_IS_ENTRYRDN, /* Get the flag for entryrdn */ + BACK_INFO_CLDB_FILENAME /* Get the backend replication changelog name */ }; struct _back_info_index_key