From: Art Cancro Date: Fri, 3 Sep 2021 03:55:37 +0000 (+0000) Subject: CtdlPutSysConfig() don't delete the old copy of the config until after the new one... X-Git-Tag: v939~18 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=d5cf6384f56c834fba45703e84381b0d7fcfb303 CtdlPutSysConfig() don't delete the old copy of the config until after the new one has been written. This eliminates data loss if the server is stopped in between. --- diff --git a/citadel/config.c b/citadel/config.c index c84d8f9e4..bd915f4c4 100644 --- a/citadel/config.c +++ b/citadel/config.c @@ -434,8 +434,7 @@ int CtdlGetConfigInt(char *key) /* * Fetch a system config value - long integer */ -long CtdlGetConfigLong(char *key) -{ +long CtdlGetConfigLong(char *key) { char *s = CtdlGetConfigStr(key); if (s) return atol(s); return 0; @@ -447,7 +446,6 @@ void CtdlGetSysConfigBackend(long msgnum, void *userdata) { } - /* * This is for fetching longer configuration sets which are stored in the message base. */ @@ -506,18 +504,24 @@ char *CtdlGetSysConfig(char *sysconfname) { } +/* + * This is for storing longer configuration sets which are stored in the message base. + */ void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) { - long msgnum = -1; + long old_msgnum = -1; + long new_msgnum = -1; - // Search for the previous copy of this config item, deleting it if it is found. - msgnum = CtdlGetConfigLong(sysconfname); - if (msgnum > 0) { - CtdlDeleteMessages(SYSCONFIGROOM, &msgnum, 1, ""); - } + // Search for the previous copy of this config item, so we can delete it + old_msgnum = CtdlGetConfigLong(sysconfname); // Go ahead and save it, and write the new msgnum to the config database so we can find it again - msgnum = CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 0); - if (msgnum > 0) { - CtdlSetConfigLong(sysconfname, msgnum); + new_msgnum = CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 0); + if (new_msgnum > 0) { + CtdlSetConfigLong(sysconfname, new_msgnum); + + // Now delete the old copy + if (old_msgnum > 0) { + CtdlDeleteMessages(SYSCONFIGROOM, &old_msgnum, 1, ""); + } } }