MIGR: use dynamic buffers to overcome max line length limits; Thanks to dtx for...
[citadel.git] / citadel / modules / migrate / serv_migrate.c
index d8a742acd5f04b3aa62fd3e089e06d097b90b456..f6fd819cec52bd951819722a273d5975773e599b 100644 (file)
@@ -1,21 +1,15 @@
 /*
  * This module dumps and/or loads the Citadel database in XML format.
  *
- * Copyright (c) 1987-2010 by the citadel.org team
+ * Copyright (c) 1987-2012 by the citadel.org team
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
+ * This program is open source software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "sysdep.h"
 #include "user_ops.h"
 #include "control.h"
 #include "euidindex.h"
-
-
 #include "ctdl_module.h"
 
-
-
 #define END_OF_MESSAGE "---eom---dbd---"
 
 char migr_tempfilename1[PATH_MAX];
@@ -68,14 +58,10 @@ char migr_tempfilename2[PATH_MAX];
 FILE *migr_global_message_list;
 
 
-
-
 /*
  * Code which implements the export appears in this section
  */
 
-
-
 /*
  * Output a string to the client with these characters escaped:  & < >
  */
@@ -85,10 +71,10 @@ void xml_strout(char *str) {
 
        while (*c != 0) {
                if (*c == '\"') {
-                       client_write("&quot;", 4);
+                       client_write("&quot;", 6);
                }
                else if (*c == '\'') {
-                       client_write("&apos;", 4);
+                       client_write("&apos;", 6);
                }
                else if (*c == '<') {
                        client_write("&lt;", 4);
@@ -146,7 +132,11 @@ void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
        cprintf("<QRhighest>%ld</QRhighest>\n", buf->QRhighest);
        cprintf("<QRgen>%ld</QRgen>\n", (long)buf->QRgen);
        cprintf("<QRflags>%u</QRflags>\n", buf->QRflags);
-       client_write("<QRdirname>", 11);        xml_strout(buf->QRdirname);     client_write("</QRdirname>\n", 13);
+       if (buf->QRflags & QR_DIRECTORY) {
+               client_write("<QRdirname>", 11);
+               xml_strout(buf->QRdirname);
+               client_write("</QRdirname>\n", 13);
+       }
        cprintf("<QRinfo>%ld</QRinfo>\n", buf->QRinfo);
        cprintf("<QRfloor>%d</QRfloor>\n", buf->QRfloor);
        cprintf("<QRmtime>%ld</QRmtime>\n", (long)buf->QRmtime);
@@ -212,6 +202,23 @@ void migr_export_floors(void) {
 }
 
 
+/*
+ * Return nonzero if the supplied string contains only characters which are valid in a sequence set.
+ */
+int is_sequence_set(char *s) {
+       if (!s) return(0);
+
+       char *c = s;
+       char ch;
+       while (ch = *c++, ch) {
+               if (!strchr("0123456789*,:", ch)) {
+                       return(0);
+               }
+       }
+       return(1);
+}
+
+
 
 /* 
  *  Traverse the visits file...
@@ -235,7 +242,7 @@ void migr_export_visits(void) {
                cprintf("<v_usernum>%ld</v_usernum>\n", vbuf.v_usernum);
 
                client_write("<v_seen>", 8);
-               if (!IsEmptyStr(vbuf.v_seen)) {
+               if ( (!IsEmptyStr(vbuf.v_seen)) && (is_sequence_set(vbuf.v_seen)) ) {
                        xml_strout(vbuf.v_seen);
                }
                else {
@@ -243,7 +250,12 @@ void migr_export_visits(void) {
                }
                client_write("</v_seen>", 9);
 
-               client_write("<v_answered>", 12); xml_strout(vbuf.v_answered); client_write("</v_answered>\n", 14);
+               if ( (!IsEmptyStr(vbuf.v_answered)) && (is_sequence_set(vbuf.v_answered)) ) {
+                       client_write("<v_answered>", 12);
+                       xml_strout(vbuf.v_answered);
+                       client_write("</v_answered>\n", 14);
+               }
+
                cprintf("<v_flags>%u</v_flags>\n", vbuf.v_flags);
                cprintf("<v_view>%d</v_view>\n", vbuf.v_view);
                client_write("</visit>\n", 9);
@@ -285,8 +297,8 @@ void migr_export_message(long msgnum) {
        client_write("<msg_meta_content_type>", 23); xml_strout(smi.meta_content_type); client_write("</msg_meta_content_type>\n", 25);
 
        client_write("<msg_text>", 10);
-       serialize_message(&smr, msg);
-       CtdlFreeMessage(msg);
+       CtdlSerializeMessage(&smr, msg);
+       CM_Free(msg);
 
        /* Predict the buffer size we need.  Expand the buffer if necessary. */
        int encoded_len = smr.len * 15 / 10 ;
@@ -347,7 +359,7 @@ void migr_export_messages(void) {
        migr_global_message_list = fopen(migr_tempfilename1, "r");
        if (migr_global_message_list != NULL) {
                syslog(LOG_INFO, "Opened %s\n", migr_tempfilename1);
-               while ((Ctx->kill_me != 1) && 
+               while ((Ctx->kill_me == 0) && 
                       (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
                        msgnum = atol(buf);
                        if (msgnum > 0L) {
@@ -357,7 +369,7 @@ void migr_export_messages(void) {
                }
                fclose(migr_global_message_list);
        }
-       if (Ctx->kill_me != 1)
+       if (Ctx->kill_me == 0)
                syslog(LOG_INFO, "Exported %d messages.\n", count);
        else
                syslog(LOG_ERR, "Export aborted due to client disconnect! \n");
@@ -368,8 +380,6 @@ void migr_export_messages(void) {
 
 
 void migr_do_export(void) {
-       struct config *buf;
-       buf = &config;
        CitContext *Ctx;
 
        Ctx = CC;
@@ -397,7 +407,6 @@ void migr_do_export(void) {
        cprintf("<c_restrict>%d</c_restrict>\n", config.c_restrict);
        client_write("<c_site_location>", 17);  xml_strout(config.c_site_location);     client_write("</c_site_location>\n", 19);
        client_write("<c_sysadm>", 10);         xml_strout(config.c_sysadm);            client_write("</c_sysadm>\n", 12);
-       cprintf("<c_setup_level>%d</c_setup_level>\n", config.c_setup_level);
        cprintf("<c_maxsessions>%d</c_maxsessions>\n", config.c_maxsessions);
        client_write("<c_ip_addr>", 11);        xml_strout(config.c_ip_addr);           client_write("</c_ip_addr>\n", 13);
        cprintf("<c_port_number>%d</c_port_number>\n", config.c_port_number);
@@ -433,7 +442,6 @@ void migr_do_export(void) {
        cprintf("<c_pop3s_port>%d</c_pop3s_port>\n", config.c_pop3s_port);
        cprintf("<c_smtps_port>%d</c_smtps_port>\n", config.c_smtps_port);
        cprintf("<c_auto_cull>%d</c_auto_cull>\n", config.c_auto_cull);
-       cprintf("<c_instant_expunge>%d</c_instant_expunge>\n", config.c_instant_expunge);
        cprintf("<c_allow_spoofing>%d</c_allow_spoofing>\n", config.c_allow_spoofing);
        cprintf("<c_journal_email>%d</c_journal_email>\n", config.c_journal_email);
        cprintf("<c_journal_pubmsgs>%d</c_journal_pubmsgs>\n", config.c_journal_pubmsgs);
@@ -468,12 +476,12 @@ void migr_do_export(void) {
        cprintf("<control_version>%d</control_version>\n", CitControl.version);
        client_write("</control>\n", 11);
 
-       if (Ctx->kill_me != 1)  migr_export_users();
-       if (Ctx->kill_me != 1)  migr_export_openids();
-       if (Ctx->kill_me != 1)  migr_export_rooms();
-       if (Ctx->kill_me != 1)  migr_export_floors();
-       if (Ctx->kill_me != 1)  migr_export_visits();
-       if (Ctx->kill_me != 1)  migr_export_messages();
+       if (Ctx->kill_me == 0)  migr_export_users();
+       if (Ctx->kill_me == 0)  migr_export_openids();
+       if (Ctx->kill_me == 0)  migr_export_rooms();
+       if (Ctx->kill_me == 0)  migr_export_floors();
+       if (Ctx->kill_me == 0)  migr_export_visits();
+       if (Ctx->kill_me == 0)  migr_export_messages();
        client_write("</citadel_migrate_data>\n", 24);
        client_write("000\n", 4);
        Ctx->dont_term = 0;
@@ -632,7 +640,6 @@ void migr_xml_end(void *data, const char *el) {
        else if (!strcasecmp(el, "c_restrict"))                 config.c_restrict = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_site_location"))            safestrncpy(config.c_site_location, migr_chardata, sizeof config.c_site_location);
        else if (!strcasecmp(el, "c_sysadm"))                   safestrncpy(config.c_sysadm, migr_chardata, sizeof config.c_sysadm);
-       else if (!strcasecmp(el, "c_setup_level"))              config.c_setup_level = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_maxsessions"))              config.c_maxsessions = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_ip_addr"))                  safestrncpy(config.c_ip_addr, migr_chardata, sizeof config.c_ip_addr);
        else if (!strcasecmp(el, "c_port_number"))              config.c_port_number = atoi(migr_chardata);
@@ -668,7 +675,6 @@ void migr_xml_end(void *data, const char *el) {
        else if (!strcasecmp(el, "c_pop3s_port"))               config.c_pop3s_port = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_smtps_port"))               config.c_smtps_port = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_auto_cull"))                config.c_auto_cull = atoi(migr_chardata);
-       else if (!strcasecmp(el, "c_instant_expunge"))          config.c_instant_expunge = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_allow_spoofing"))           config.c_allow_spoofing = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_journal_email"))            config.c_journal_email = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_journal_pubmsgs"))          config.c_journal_pubmsgs = atoi(migr_chardata);
@@ -805,7 +811,7 @@ void migr_xml_end(void *data, const char *el) {
                        msglist = NULL;
                        msglist_alloc = 0;
                        syslog(LOG_DEBUG, "Imported %d messages.\n", msgcount);
-                       if (CtdlThreadCheckStop()) {
+                       if (server_shutting_down) {
                                return;
                }
        }
@@ -883,12 +889,12 @@ void migr_xml_end(void *data, const char *el) {
  * Import begins here
  */
 void migr_do_import(void) {
-       char buf[SIZ];
+       StrBuf *Buf;
        XML_Parser xp;
        int linelen;
        
        unbuffer_output();
-
+       Buf = NewStrBufPlain(NULL, SIZ);
        xp = XML_ParserCreate(NULL);
        if (!xp) {
                cprintf("%d Failed to create XML parser instance\n", ERROR+INTERNAL_ERROR);
@@ -902,22 +908,22 @@ void migr_do_import(void) {
        cprintf("%d sock it to me\n", SEND_LISTING);
        unbuffer_output();
 
-       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
-               linelen = strlen(buf);
-               strcpy(&buf[linelen++], "\n");
+       while (CtdlClientGetLine(Buf) >= 0 && strcmp(ChrPtr(Buf), "000")) {
+               linelen = StrLength(Buf);
+               StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
 
-               if (CtdlThreadCheckStop())
+               if (server_shutting_down)
                        break;  // Should we break or return?
                
-               if (buf[0] == '\0')
+               if (linelen == 0)
                        continue;
 
-               XML_Parse(xp, buf, linelen, 0);
+               XML_Parse(xp, ChrPtr(Buf), linelen, 0);
        }
 
        XML_Parse(xp, "", 0, 1);
        XML_ParserFree(xp);
-       
+       FreeStrBuf(&Buf);
        rebuild_euid_index();
        rebuild_usersbynumber();
        CC->dont_term = 0;
@@ -991,6 +997,6 @@ CTDL_MODULE_INIT(migrate)
                CtdlRegisterProtoHook(cmd_migr, "ARTV", "Across-the-wire migration (legacy syntax)");
        }
        
-       /* return our Subversion id for the Log */
+       /* return our module name for the log */
        return "migrate";
 }