X-Git-Url: https://code.citadel.org/?p=citadel.git;a=blobdiff_plain;f=citadel%2Fserver%2Fbackends%2Fcommon%2Fdatabase.c;h=0b8276b0713ee9fca754bc97e3c91e36eca35929;hp=ac1ebbad61595a249ec413e20fe1728c68e4c9f6;hb=HEAD;hpb=7d327eaa0500ed510350362e6e1eb74f3500a911 diff --git a/citadel/server/backends/common/database.c b/citadel/server/backends/common/database.c index ac1ebbad6..0b8276b07 100644 --- a/citadel/server/backends/common/database.c +++ b/citadel/server/backends/common/database.c @@ -1,6 +1,5 @@ -// Copyright (c) 1987-2023 by the citadel.org team -// This program is open source software. Use, duplication, or disclosure -// are subject to the terms of the GNU General Public License, version 3. +// Copyright (c) 1987-2024 by the citadel.org team +// This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License version 3. // The functions in this file handle the selection and activation of a storage backend for Citadel Server. // Right now, it simply activates Berkeley DB because that's the only one we have. @@ -17,6 +16,8 @@ #include "../../citserver.h" #include "../../config.h" +void cdb_chmod_data(void); + // Header files for all available backends must be included here. #include "../berkeley_db/berkeley_db.h" @@ -37,10 +38,42 @@ void (*check_handles)(void *) = NULL; void (*cdb_compact)(void) = NULL; void (*cdb_checkpoint)(void) = NULL; void (*cdb_rewind)(int) = NULL; +void (*cdb_tick)(void) = NULL; // This function is responsible for choosing and initializing a back end. void cdb_init_backends(void) { - bdb_init_backend(); // for now, this is the only one, so we select it always. + + char *chosen_backend = NULL; + + cdb_chmod_data(); // Set file level permissions so we can actually access the data files + +#ifdef THIS_BUILD_CONTAINS_BDB + // Test for Berkeley DB (this does nothing yet -- we're preparing to test for multiple back ends in the future) + int fd = open(ctdl_db_dir"/cdb.00", O_RDONLY); + if (fd) { + char junk[12]; + uint32_t magic; + read(fd, junk, 12); // throw the first 12 bytes away + read(fd, &magic, sizeof(magic)); + if (magic == 0x00053162) { + syslog(LOG_DEBUG, "db: found existing Citadel database in Berkeley DB format"); + if (!chosen_backend) { + chosen_backend = "bdb"; + } + } + close(fd); + } +#endif + + // If no existing database has been detected, go with Berkeley DB. + if (!chosen_backend) { + chosen_backend = "bdb"; + } + + // Initialize the chosen backend. + if (!strcmp(chosen_backend, "bdb")) { + bdb_init_backend(); + } } @@ -52,28 +85,33 @@ void cdb_chmod_data(void) { // Silently try to create the database subdirectory. If it's already there, no problem. if ((mkdir(ctdl_db_dir, 0700) != 0) && (errno != EEXIST)) { - syslog(LOG_ERR, "bdb: database directory [%s] does not exist and could not be created: %m", ctdl_db_dir); + syslog(LOG_ERR, "db: database directory [%s] does not exist and could not be created: %m", ctdl_db_dir); exit(CTDLEXIT_DB); } if (chmod(ctdl_db_dir, 0700) != 0) { - syslog(LOG_ERR, "bdb: unable to set database directory permissions [%s]: %m", ctdl_db_dir); + syslog(LOG_ERR, "db: unable to set database directory permissions [%s]: %m", ctdl_db_dir); exit(CTDLEXIT_DB); } if (chown(ctdl_db_dir, ctdluid, (-1)) != 0) { - syslog(LOG_ERR, "bdb: unable to set the owner for [%s]: %m", ctdl_db_dir); + syslog(LOG_ERR, "db: unable to set the owner for [%s]: %m", ctdl_db_dir); exit(CTDLEXIT_DB); } dp = opendir(ctdl_db_dir); if (dp != NULL) { while (d = readdir(dp), d != NULL) { if (d->d_name[0] != '.') { + int ret; snprintf(filename, sizeof filename, "%s/%s", ctdl_db_dir, d->d_name); - syslog(LOG_DEBUG, "bdb: chmod(%s, 0600) returned %d", filename, chmod(filename, 0600)); - syslog(LOG_DEBUG, "bdb: chown(%s, ctdluid, -1) returned %d", filename, chown(filename, ctdluid, (-1))); + ret = chmod(filename, 0600); + if (ret != 0) { + syslog(LOG_DEBUG, "db: chmod(%s, 0600) returned %d", filename, ret); + } + ret = chown(filename, ctdluid, (-1)); + if (ret != 0) { + syslog(LOG_DEBUG, "db: chown(%s, ctdluid, -1) returned %d", filename, ret); + } } } closedir(dp); } } - -