From c3116944f097a28b58508b8789b475757daa4623 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Fri, 17 Dec 2010 14:12:36 -0500 Subject: [PATCH] Cleaned up the loop that receives network spool. NOT THE FIX --- citadel/modules/network/serv_network.c | 114 +++++++++++++------------ 1 file changed, 58 insertions(+), 56 deletions(-) diff --git a/citadel/modules/network/serv_network.c b/citadel/modules/network/serv_network.c index d0ab94092..7f5d5dd30 100644 --- a/citadel/modules/network/serv_network.c +++ b/citadel/modules/network/serv_network.c @@ -1921,13 +1921,13 @@ void network_consolidate_spoolout(void) { * receive network spool from the remote system */ void receive_spool(int *sock, char *remote_nodename) { - long download_len = 0L; - long bytes_received = 0L; + int download_len = 0L; + int bytes_received = 0L; char buf[SIZ]; static char pbuf[IGNET_PACKET_SIZE]; char tempfilename[PATH_MAX]; char permfilename[PATH_MAX]; - long plen; + int plen; FILE *fp; snprintf(tempfilename, @@ -1954,59 +1954,62 @@ void receive_spool(int *sock, char *remote_nodename) { if (buf[0] != '2') { return; } + download_len = extract_long(&buf[4], 0); + if (download_len <= 0) { + return; + } - 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)); + bytes_received = 0L; + fp = fopen(tempfilename, "w"); + if (fp == NULL) { + CtdlLogPrintf(CTDL_CRIT, "Cannot create %s: %s\n", tempfilename, strerror(errno)); + return; + } + + CtdlLogPrintf(CTDL_DEBUG, "Expecting to transfer %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; } - - 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; - } - 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) { + snprintf(buf, sizeof buf, "READ %d|%d", + 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_int(&buf[4], 0); + if (sock_read(sock, pbuf, plen, 1) < 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; - } + fwrite((char *) pbuf, plen, 1, fp); + bytes_received += plen; } - - fclose(fp); } + + fclose(fp); + /* Last chance for shutdown exit */ if (CtdlThreadCheckStop()) { @@ -2028,16 +2031,15 @@ void receive_spool(int *sock, char *remote_nodename) { } CtdlLogPrintf(CTDL_DEBUG, "%s\n", buf); - if (download_len > 0) { - CtdlLogPrintf(CTDL_NOTICE, "Received %ld octets from <%s>\n", download_len, remote_nodename); - /* - * Now move the temp file to its permanent location. - */ - if (link(tempfilename, permfilename) != 0) { - CtdlLogPrintf(CTDL_ALERT, "Could not link %s to %s: %s\n", - tempfilename, permfilename, strerror(errno) - ); - } + CtdlLogPrintf(CTDL_NOTICE, "Received %ld octets from <%s>\n", download_len, remote_nodename); + + /* + * Now move the temp file to its permanent location. + */ + if (link(tempfilename, permfilename) != 0) { + CtdlLogPrintf(CTDL_ALERT, "Could not link %s to %s: %s\n", + tempfilename, permfilename, strerror(errno) + ); } unlink(tempfilename); -- 2.39.2