Fix the build problems revolving around CitContext and context.h
[citadel.git] / citadel / modules / migrate / serv_migrate.c
index 4441a09b9363a834bd807396cbc3426170d97825..0e85bbc42f3f20b6c490ffbedd58971c18d45813 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * $Id: serv_migrate.c 7274 2009-03-27 15:11:14Z ajc $
+ * $Id$
+ *
  * Copyright (c) 2000-2009 by the citadel.org development team
  *
  * This module dumps and/or loads the Citadel database in XML format.
@@ -56,7 +57,6 @@
 #include "database.h"
 #include "msgbase.h"
 #include "user_ops.h"
-#include "room_ops.h"
 #include "control.h"
 #include "euidindex.h"
 
@@ -158,7 +158,7 @@ void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
 
        /* message list goes inside this tag */
 
-       getroom(&CC->room, buf->QRname);
+       CtdlGetRoom(&CC->room, buf->QRname);
        client_write("<room_messages>", 15);
        client_write("<FRname>", 8);    xml_strout(CC->room.QRname);    client_write("</FRname>\n", 10);
        client_write("<FRmsglist>", 11);
@@ -174,7 +174,7 @@ void migr_export_rooms(void) {
        char cmd[SIZ];
        migr_global_message_list = fopen(migr_tempfilename1, "w");
        if (migr_global_message_list != NULL) {
-               ForEachRoom(migr_export_rooms_backend, NULL);
+               CtdlForEachRoom(migr_export_rooms_backend, NULL);
                fclose(migr_global_message_list);
        }
 
@@ -198,7 +198,7 @@ void migr_export_floors(void) {
         for (i=0; i < MAXFLOORS; ++i) {
                client_write("<floor>\n", 8);
                cprintf("<f_num>%d</f_num>\n", i);
-                getfloor(&qfbuf, i);
+                CtdlGetFloor(&qfbuf, i);
                buf = &qfbuf;
                cprintf("<f_flags>%u</f_flags>\n", buf->f_flags);
                client_write("<f_name>", 8); xml_strout(buf->f_name); client_write("</f_name>\n", 10);
@@ -254,6 +254,25 @@ void migr_export_message(long msgnum) {
        struct CtdlMessage *msg;
        struct ser_ret smr;
 
+       /* We can use a static buffer here because there will never be more than
+        * one of this operation happening at any given time, and it's really best
+        * to just keep it allocated once instead of torturing malloc/free.
+        * Call this function with msgnum "-1" to free the buffer when finished.
+        */
+       static int encoded_alloc = 0;
+       static char *encoded_msg = NULL;
+
+       if (msgnum < 0) {
+               if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
+                       free(encoded_msg);
+                       encoded_alloc = 0;
+                       encoded_msg = NULL;
+               }
+               return;
+       }
+
+       /* Ok, here we go ... */
+
        msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) return;        /* fail silently */
 
@@ -267,14 +286,21 @@ void migr_export_message(long msgnum) {
        serialize_message(&smr, msg);
        CtdlFreeMessage(msg);
 
-       int encoded_len = 0;
-       int encoded_alloc = smr.len * 14 / 10;  /* well-tested formula for predicting encoded size */
-       char *encoded_msg = malloc(encoded_alloc);
+       /* Predict the buffer size we need.  Expand the buffer if necessary. */
+       int encoded_len = smr.len * 15 / 10 ;
+       if (encoded_len > encoded_alloc) {
+               encoded_alloc = encoded_len;
+               encoded_msg = realloc(encoded_msg, encoded_alloc);
+       }
 
-       if (encoded_msg != NULL) {
+       if (encoded_msg == NULL) {
+               /* Questionable hack that hopes it'll work next time and we only lose one message */
+               encoded_alloc = 0;
+       }
+       else {
+               /* Once we do the encoding we know the exact size */
                encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1);
                client_write(encoded_msg, encoded_len);
-               free(encoded_msg);
        }
 
        free(smr.ser);
@@ -313,7 +339,7 @@ void migr_export_messages(void) {
        char buf[SIZ];
        long msgnum;
        int count = 0;
-       t_context *Ctx;
+       CitContext *Ctx;
 
        Ctx = CC;
        migr_global_message_list = fopen(migr_tempfilename1, "r");
@@ -333,6 +359,8 @@ void migr_export_messages(void) {
                CtdlLogPrintf(CTDL_INFO, "Exported %d messages.\n", count);
        else
                CtdlLogPrintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
+
+       migr_export_message(-1L);       /* This frees the encoding buffer */
 }
 
 
@@ -340,7 +368,7 @@ void migr_export_messages(void) {
 void migr_do_export(void) {
        struct config *buf;
        buf = &config;
-       t_context *Ctx;
+       CitContext *Ctx;
 
        Ctx = CC;
        cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
@@ -691,7 +719,7 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "u_fullname"))                 safestrncpy(usbuf.fullname, migr_chardata, sizeof usbuf.fullname);
 
        else if (!strcasecmp(el, "user")) {
-               putuser(&usbuf);
+               CtdlPutUser(&usbuf);
                CtdlLogPrintf(CTDL_INFO, "Imported user: %s\n", usbuf.fullname);
        }
 
@@ -732,7 +760,7 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "QRdefaultview"))              qrbuf.QRdefaultview = atoi(migr_chardata);
 
        else if (!strcasecmp(el, "room")) {
-               putroom(&qrbuf);
+               CtdlPutRoom(&qrbuf);
                CtdlLogPrintf(CTDL_INFO, "Imported room: %s\n", qrbuf.QRname);
        }
 
@@ -790,7 +818,7 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "f_ep_expire_value"))          flbuf.f_ep.expire_value = atoi(migr_chardata);
 
        else if (!strcasecmp(el, "floor")) {
-               putfloor(&flbuf, floornum);
+               CtdlPutFloor(&flbuf, floornum);
                CtdlLogPrintf(CTDL_INFO, "Imported floor #%d (%s)\n", floornum, flbuf.f_name);
        }
 
@@ -895,6 +923,23 @@ void migr_do_import(void) {
 
 
 
+/*
+ * Dump out the pathnames of directories which can be copied "as is"
+ */
+void migr_do_listdirs(void) {
+       cprintf("%d Don't forget these:\n", LISTING_FOLLOWS);
+       cprintf("bio|%s\n",             ctdl_bio_dir);
+       cprintf("files|%s\n",           ctdl_file_dir);
+       cprintf("userpics|%s\n",        ctdl_usrpic_dir);
+       cprintf("messages|%s\n",        ctdl_message_dir);
+       cprintf("netconfigs|%s\n",      ctdl_netcfg_dir);
+       cprintf("keys|%s\n",            ctdl_key_dir);
+       cprintf("images|%s\n",          ctdl_image_dir);
+       cprintf("info|%s\n",            ctdl_info_dir);
+       cprintf("000\n");
+}
+
+
 /*
  * Common code appears in this section
  */
@@ -917,6 +962,9 @@ void cmd_migr(char *cmdbuf) {
                else if (!strcasecmp(cmd, "import")) {
                        migr_do_import();
                }
+               else if (!strcasecmp(cmd, "listdirs")) {
+                       migr_do_listdirs();
+               }
                else {
                        cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
                }
@@ -942,5 +990,5 @@ CTDL_MODULE_INIT(migrate)
        }
        
        /* return our Subversion id for the Log */
-       return "$Id: serv_migrate.c 7274 2009-03-27 15:11:14Z ajc $";
+       return "$Id$";
 }