From b2e29e3d33f3eda3a3eb3f7be959cf841a393fb0 Mon Sep 17 00:00:00 2001 From: Dave West Date: Fri, 5 Oct 2007 19:52:27 +0000 Subject: [PATCH] Added code to rebuild citadel.control if it gets deleted some how. --- citadel/control.c | 56 ++++++++++++++++++++++++++++++++++++++++++- citadel/server_main.c | 6 ++--- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/citadel/control.c b/citadel/control.c index 75d15143c..dbe840670 100644 --- a/citadel/control.c +++ b/citadel/control.c @@ -39,6 +39,8 @@ #include "citserver.h" #include "tools.h" #include "room_ops.h" +#include "user_ops.h" +#include "database.h" #ifndef HAVE_SNPRINTF #include "snprintf.h" @@ -47,7 +49,7 @@ struct CitControl CitControl; extern struct config config; FILE *control_fp = NULL; - +long control_highest_user = 0; /* @@ -70,6 +72,55 @@ void lock_control(void) #endif } +/* + * callback to get highest room number when rebuilding control file + */ +void control_find_highest(struct ctdlroom *qrbuf, void *data) +{ + struct ctdlroom room; + struct cdbdata *cdbfr; + long *msglist; + int num_msgs=0; + int c; + + + if (qrbuf->QRnumber > CitControl.MMnextroom) + CitControl.MMnextroom = qrbuf->QRnumber; + + getroom (&room, qrbuf->QRname); + + /* Load the message list */ + cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long)); + if (cdbfr != NULL) { + msglist = (long *) cdbfr->ptr; + num_msgs = cdbfr->len / sizeof(long); + } else { + return; /* No messages at all? No further action. */ + } + + if (num_msgs>0) + { + for (c=0; c CitControl.MMhighest) + CitControl.MMhighest = msglist[c]; + } + } + cdb_free(cdbfr); + return; +} + + +/* + * Callback to get highest user number. + */ + +void control_find_user (struct ctdluser *EachUser, void *out_data) +{ + if (EachUser->usernum > CitControl.MMnextuser) + CitControl.MMnextuser = EachUser->usernum; +} + /* * get_control - read the control record into memory. @@ -102,6 +153,9 @@ void get_control(void) lock_control(); fchown(fileno(control_fp), config.c_ctdluid, -1); memset(&CitControl, 0, sizeof(struct CitControl)); + // Find highest room number and message number. + ForEachRoom(control_find_highest, NULL); + ForEachUser(control_find_user, NULL); fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp); rewind(control_fp); diff --git a/citadel/server_main.c b/citadel/server_main.c index a2bc2b4ef..f13a77361 100644 --- a/citadel/server_main.c +++ b/citadel/server_main.c @@ -216,9 +216,6 @@ int main(int argc, char **argv) config.c_ipgm_secret = rand(); put_config(); - lprintf(CTDL_INFO, "Acquiring control record\n"); - get_control(); - #ifdef HAVE_RUN_DIR /* on some dists rundir gets purged on startup. so we need to recreate it. */ @@ -243,6 +240,9 @@ int main(int argc, char **argv) */ master_startup(); + lprintf(CTDL_INFO, "Acquiring control record\n"); + get_control(); + /* * Bind the server to a Unix-domain socket. */ -- 2.30.2