Mailing list header changes (fuck you Google)
[citadel.git] / citadel / server / backends / common / database.c
index 9ac8817eb732a08c8dcf8d68be7aa82307f2493c..0b8276b0713ee9fca754bc97e3c91e36eca35929 100644 (file)
@@ -1,6 +1,5 @@
-// Copyright (c) 1987-2023 by the citadel.org team
-// This program is open source software.  Use, duplication, or disclosure
-// is 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.
@@ -43,8 +42,38 @@ void                 (*cdb_tick)(void)                                       = NULL;
 
 // This function is responsible for choosing and initializing a back end.
 void cdb_init_backends(void) {
-       cdb_chmod_data();
-       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();
+       }
 }
 
 
@@ -86,5 +115,3 @@ void cdb_chmod_data(void) {
                closedir(dp);
        }
 }
-
-