memcpy(tempfilename, filename, len + 1);
memset(&StatBuf, 0, sizeof(struct stat));
- if (stat(filename, &StatBuf) == -1)
- StatBuf.st_size = 80; /* Not there? guess 80 chars line. */
+ if ((stat(filename, &StatBuf) == -1) || (StatBuf.st_size == 0))
+ StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
sprintf(tempfilename + len, ".%d", CC->cs_pid);
+ errno = 0;
+ TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
- TmpFD = open(tempfilename, O_CREAT|O_EXCL);
-
- if (TmpFD > 0)
+ if ((TmpFD > 0) && (errno == 0))
{
char *tmp = malloc(StatBuf.st_size * 2);
- memset(tmp, 0, StatBuf.st_size * 2);
+ memset(tmp, ' ', StatBuf.st_size * 2);
rc = write(TmpFD, tmp, StatBuf.st_size * 2);
free(tmp);
- if (rc <= 0)
+ if ((rc <= 0) || (rc != StatBuf.st_size * 2))
{
close(TmpFD);
cprintf("%d Unable to allocate the space required for %s: %s\n",
ERROR + INTERNAL_ERROR,
tempfilename,
strerror(errno));
+ unlink(tempfilename);
return;
}
lseek(TmpFD, SEEK_SET, 0);
ERROR + INTERNAL_ERROR,
tempfilename,
strerror(errno));
-
-
+ unlink(tempfilename);
+ return;
}
Line = NewStrBuf();
len += StrLength(Line);
}
FreeStrBuf(&Line);
- ftruncate(TmpFD, len + 1);
+ ftruncate(TmpFD, len);
close(TmpFD);
/* Now copy the temp file to its permanent location.
* (We copy instead of link because they may be on different filesystems)
*/
begin_critical_section(S_NETCONFIGS);
- rename(filename, tempfilename);
+ rename(tempfilename, filename);
end_critical_section(S_NETCONFIGS);
}
*/
msg = CtdlFetchMessage(msgnum, 1);
if (msg != NULL) {
+ int len, rlen;
+ char *pCh;
+
if (msg->cm_fields['V'] == NULL){
/* local message, no enVelope */
StrBuf *Buf;
if (msg->cm_fields['U'] == NULL) {
msg->cm_fields['U'] = strdup("(no subject)");
}
- snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
- free(msg->cm_fields['U']);
- msg->cm_fields['U'] = strdup(buf);
+
+ len = strlen(msg->cm_fields['U']);
+ rlen = strlen(CC->room.QRname);
+ pCh = strstr(msg->cm_fields['U'], CC->room.QRname);
+ if ((pCh == NULL) ||
+ (*(pCh + rlen) != ']') ||
+ (pCh == msg->cm_fields['U']) ||
+ (*(pCh - 1) != '[')
+ )
+ {
+ char *pBuff;
+
+ rlen += len + 4;
+ pBuff = malloc (rlen * sizeof(char));
+
+ snprintf(pBuff, rlen, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
+ free(msg->cm_fields['U']);
+ msg->cm_fields['U'] = pBuff;
+ }
+ /* else we won't modify the buffer, since the roomname is already here. */
/* Set the recipient of the list message to the
* email address of the room itself.
int writenfree_spoolcontrol_file(SpoolControl **scc, char *filename)
{
char tempfilename[PATH_MAX];
- FILE *fp;
+ int TmpFD;
SpoolControl *sc;
namelist *nptr = NULL;
maplist *mptr = NULL;
sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
sc = *scc;
- fp = fopen(tempfilename, "w");
+ errno = 0;
+ TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
Cfg = NewStrBuf();
- if (fp == NULL) {
+ if ((TmpFD < 0) || (errno != 0)) {
CtdlLogPrintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
filename, strerror(errno));
free_spoolcontrol_struct(scc);
+ unlink(tempfilename);
}
else {
- fprintf(fp, "lastsent|%ld\n", sc->lastsent);
+ StrBufAppendPrintf(Cfg, "lastsent|%ld\n", sc->lastsent);
/* Write out the listrecps while freeing from memory at the
* same time. Am I clever or what? :)
}
free(sc->misc);
- rc = fwrite(ChrPtr(Cfg), StrLength(Cfg), 1, fp);
- if (rc >=0)
+ rc = write(TmpFD, ChrPtr(Cfg), StrLength(Cfg));
+ if ((rc >=0 ) && (rc == StrLength(Cfg)))
{
- fclose(fp);
- rename(filename, tempfilename);
+ close(TmpFD);
+ rename(tempfilename, filename);
}
else {
CtdlLogPrintf(CTDL_EMERG,
- "unable to write %s; not enough space on the disk?\n",
- tempfilename);
- fclose(fp);
+ "unable to write %s; [%s]; not enough space on the disk?\n",
+ tempfilename,
+ strerror(errno));
+ close(TmpFD);
+ unlink(tempfilename);
}
FreeStrBuf(&Cfg);
free(sc);
* 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,
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);
+ StrBuf *pbuf = NewStrBuf();
+ if (socket_read_blob(sock, pbuf, plen, CLIENT_TIMEOUT) != plen) {
+ CtdlLogPrintf(CTDL_INFO, "Short read from peer; aborting.\n");
fclose(fp);
unlink(tempfilename);
+ FreeStrBuf(&pbuf);
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(ChrPtr(pbuf), plen, 1, fp);
+ bytes_received += plen;
+ FreeStrBuf(&pbuf);
}
-
- fclose(fp);
}
+
+ fclose(fp);
+
/* Last chance for shutdown exit */
if (CtdlThreadCheckStop())
{
unlink(tempfilename);
return;
}
- if (download_len > 0) {
- CtdlLogPrintf(CTDL_NOTICE, "Received %ld octets from <%s>\n", download_len, remote_nodename);
- }
+
CtdlLogPrintf(CTDL_DEBUG, "%s\n", buf);
-
- /* Now move the temp file to its permanent location.
+
+ /*
+ * 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);
}
}
CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
- CCC->sReadBuf = NewStrBuf();
+ CCC->SBuf.Buf = NewStrBuf();
CCC->sMigrateBuf = NewStrBuf();
- CCC->sPos = NULL;
+ CCC->SBuf.ReadWritePointer = NULL;
/* Read the server greeting */
if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
sock_puts(&sock, "QUIT");
bail:
- FreeStrBuf(&CCC->sReadBuf);
+ FreeStrBuf(&CCC->SBuf.Buf);
FreeStrBuf(&CCC->sMigrateBuf);
if (sock != -1)
sock_close(sock);