* move initialization of context to the top
[citadel.git] / citadel / modules / network / serv_network.c
index 8b0df4ec175b0e24ba055233409e437a1a290ff0..b3d3d4617300430a06a41abc62813c794fa1885a 100644 (file)
@@ -1873,53 +1873,57 @@ void receive_spool(int *sock, char *remote_nodename) {
        }
        download_len = extract_long(&buf[4], 0);
 
-       bytes_received = 0L;
-       fp = fopen(tempfilename, "w");
-       if (fp == NULL) {
-               CtdlLogPrintf(CTDL_CRIT, "cannot open download file locally: %s\n",
-                       strerror(errno));
-               return;
-       }
-
-       while (bytes_received < download_len) {
-               /*
-                * If shutting down we can exit here and unlink the temp file.
-                * this shouldn't loose us any messages.
-                */
-               if (CtdlThreadCheckStop())
-               {
-                       fclose(fp);
-                       unlink(tempfilename);
-                       return;
-               }
-               snprintf(buf, sizeof buf, "READ %ld|%ld",
-                       bytes_received,
-                    ((download_len - bytes_received > IGNET_PACKET_SIZE)
-                ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
-               if (sock_puts(sock, buf) < 0) {
-                       fclose(fp);
-                       unlink(tempfilename);
+       if (download_len>0) {
+               bytes_received = 0L;
+               fp = fopen(tempfilename, "w");
+               if (fp == NULL) {
+                       CtdlLogPrintf(CTDL_CRIT, "cannot open download file locally: %s\n",
+                                     strerror(errno));
                        return;
                }
-               if (sock_getln(sock, buf, sizeof buf) < 0) {
-                       fclose(fp);
-                       unlink(tempfilename);
-                       return;
-               }
-               if (buf[0] == '6') {
-                       plen = extract_long(&buf[4], 0);
-                       if (sock_read(sock, pbuf, plen, 1) < 0) {
+
+               CtdlLogPrintf(CTDL_DEBUG, "For this download we are expecting %d bytes\n", download_len);
+               while (bytes_received < download_len) {
+                       /*
+                        * If shutting down we can exit here and unlink the temp file.
+                        * this shouldn't loose us any messages.
+                        */
+                       if (CtdlThreadCheckStop())
+                       {
                                fclose(fp);
                                unlink(tempfilename);
                                return;
                        }
-                       fwrite((char *) pbuf, plen, 1, fp);
-                       bytes_received = bytes_received + plen;
+                       snprintf(buf, sizeof buf, "READ %ld|%ld",
+                                bytes_received,
+                                ((download_len - bytes_received > IGNET_PACKET_SIZE)
+                                 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
+                       
+                       if (sock_puts(sock, buf) < 0) {
+                               fclose(fp);
+                               unlink(tempfilename);
+                               return;
+                       }
+                       if (sock_getln(sock, buf, sizeof buf) < 0) {
+                               fclose(fp);
+                               unlink(tempfilename);
+                               return;
+                       }
+                       
+                       if (buf[0] == '6') {
+                               plen = extract_long(&buf[4], 0);
+                               if (sock_read(sock, pbuf, plen, 1) < 0) {
+                                       fclose(fp);
+                                       unlink(tempfilename);
+                                       return;
+                               }
+                               fwrite((char *) pbuf, plen, 1, fp);
+                               bytes_received = bytes_received + plen;
+                       }
                }
-       }
-
-       fclose(fp);
 
+               fclose(fp);
+       }
        /* Last chance for shutdown exit */
        if (CtdlThreadCheckStop())
        {
@@ -2228,6 +2232,7 @@ void *network_do_queue(void *args) {
         * with a static variable instead.
         */
        if (doing_queue) {
+               CtdlClearSystemContext();
                return NULL;
        }
        doing_queue = 1;
@@ -2315,6 +2320,7 @@ void *network_do_queue(void *args) {
        else {
                CtdlLogPrintf(CTDL_DEBUG, "network: Task STOPPED.\n");
        }
+       CtdlClearSystemContext();
        return NULL;
 }