Moved cdb_chmod_data() out of the backend API
authorArt Cancro <ajc@citadel.org>
Fri, 11 Aug 2023 15:46:34 +0000 (06:46 -0900)
committerArt Cancro <ajc@citadel.org>
Fri, 11 Aug 2023 15:46:34 +0000 (06:46 -0900)
This function only does file and directory level operations on
the data/ directory and files in it.  These operations will be
common across all backends.

citadel/server/backends/berkeley_db/berkeley_db.c
citadel/server/database.c
citadel/server/database.h

index 40720cf3fea9d9355f489c140c1a9f5e0ce260ff..6502792966c2f09c22e088ea8c227720a58e6ef6 100644 (file)
@@ -14,7 +14,6 @@
 #include "../../sysdep.h"
 #include <sys/stat.h>
 #include <stdio.h>
-#include <dirent.h>
 #include <zlib.h>
 #include <db.h>
 #include <libcitadel.h>
@@ -210,19 +209,6 @@ void bdb_open_databases(void) {
                exit(CTDLEXIT_DB);
        }
 
-       // 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);
-               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);
-               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);
-               exit(CTDLEXIT_DB);
-       }
        syslog(LOG_DEBUG, "bdb: Setting up DB environment");
        ret = db_env_create(&dbenv, 0);
        if (ret) {
@@ -299,26 +285,6 @@ void bdb_open_databases(void) {
 }
 
 
-// Make sure we own all the files, because in a few milliseconds we're going to drop root privs.
-void bdb_chmod_data(void) {
-       DIR *dp;
-       struct dirent *d;
-       char filename[PATH_MAX];
-
-       dp = opendir(ctdl_db_dir);
-       if (dp != NULL) {
-               while (d = readdir(dp), d != NULL) {
-                       if (d->d_name[0] != '.') {
-                               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)));
-                       }
-               }
-               closedir(dp);
-       }
-}
-
-
 // Close all of the db database files we've opened.  This can be done in a loop, since it's just a bunch of closes.
 void bdb_close_databases(void) {
        int i;
@@ -785,7 +751,6 @@ void bdb_init_backend(void) {
        cdb_end_transaction = bdb_end_transaction;
        cdb_check_handles = bdb_check_handles;
        cdb_trunc = bdb_trunc;
-       cdb_chmod_data = bdb_chmod_data;
 
        if (pthread_key_create(&bdb_thread_key, NULL) != 0) {                   // TSD key for this module
                syslog(LOG_ERR, "pthread_key_create() : %m");
index 5aa7d0e27ebd496efc9ebc8a719e830d01d76ceb..b3701b4b6a78a7754264bd49178ad4c86ee0bdff 100644 (file)
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <stdio.h>
+#include <dirent.h>
 #include <libcitadel.h>
 #include "ctdl_module.h"
 #include "control.h"
@@ -32,7 +33,6 @@ void                  (*cdb_begin_transaction)(void)                          = NULL;
 void                   (*cdb_end_transaction)(void)                            = NULL;
 void                   (*cdb_check_handles)(void)                              = NULL;
 void                   (*cdb_trunc)(int)                                       = NULL;
-void                   (*cdb_chmod_data)(void)                                 = NULL;
 void                   (*check_handles)(void *)                                = NULL;
 void                   (*cdb_compact)(void)                                    = NULL;
 void                   (*cdb_checkpoint)(void)                                 = NULL;
@@ -43,3 +43,38 @@ struct cdbdata *     (*cdb_fetch)(int, const void *, int)                    = NULL;
 void cdb_init_backends(void) {
        bdb_init_backend();             // for now, this is the only one, so we select it always.
 }
+
+
+// Make sure we own all the files, because in a few milliseconds we're going to drop root privs.
+void cdb_chmod_data(void) {
+       DIR *dp;
+       struct dirent *d;
+       char filename[PATH_MAX];
+
+       // 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);
+               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);
+               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);
+               exit(CTDLEXIT_DB);
+       }
+       dp = opendir(ctdl_db_dir);
+       if (dp != NULL) {
+               while (d = readdir(dp), d != NULL) {
+                       if (d->d_name[0] != '.') {
+                               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)));
+                       }
+               }
+               closedir(dp);
+       }
+}
+
+
index 418a2f5de8aa58bcb10d61a620c6d5b8fde920e6..9b38d844b5cba265f4224566cbe30e5d53fc2047 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef DATABASE_H
 #define DATABASE_H
 
+extern void            cdb_chmod_data(void);
+
 extern void            (*cdb_open_databases)(void);
 extern void            (*cdb_close_databases)(void);
 extern int             (*cdb_store)(int, const void *, int, void *, int);
@@ -18,7 +20,6 @@ extern void           (*cdb_begin_transaction)(void);
 extern void            (*cdb_end_transaction)(void);
 extern void            (*cdb_check_handles)(void);
 extern void            (*cdb_trunc)(int);
-extern void            (*cdb_chmod_data)(void);
 extern void            (*check_handles)(void *);
 extern struct cdbdata *        (*cdb_fetch)(int, const void *, int);
 extern void            (*cdb_checkpoint)(void);