# include <time.h>
# endif
#endif
+#ifdef HAVE_SYSCALL_H
+# include <syscall.h>
+#else
+# if HAVE_SYS_SYSCALL_H
+# include <sys/syscall.h>
+# endif
+#endif
#include <sys/wait.h>
#include <string.h>
void cmd_snet(char *argbuf) {
char tempfilename[PATH_MAX];
char filename[PATH_MAX];
- char buf[SIZ];
- FILE *fp, *newfp;
+ int TmpFD;
+ StrBuf *Line;
+ struct stat StatBuf;
+ long len;
+ int rc;
unbuffer_output();
}
else if (CtdlAccessCheck(ac_room_aide)) return;
- CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
- assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
+ len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
+ memcpy(tempfilename, filename, len + 1);
- fp = fopen(tempfilename, "w");
- if (fp == NULL) {
- cprintf("%d Cannot open %s: %s\n",
+ memset(&StatBuf, 0, sizeof(struct stat));
+ if (stat(filename, &StatBuf) == -1)
+ StatBuf.st_size = 80; /* Not there? guess 80 chars line. */
+
+ sprintf(tempfilename + len, ".%d", CC->cs_pid);
+
+ TmpFD = open(tempfilename, O_CREAT|O_EXCL);
+
+ if (TmpFD > 0)
+ {
+ char *tmp = malloc(StatBuf.st_size * 2);
+ memset(tmp, 0, StatBuf.st_size * 2);
+ rc = write(TmpFD, tmp, StatBuf.st_size * 2);
+ free(tmp);
+ if (rc <= 0)
+ {
+ close(TmpFD);
+ cprintf("%d Unable to allocate the space required for %s: %s\n",
+ ERROR + INTERNAL_ERROR,
+ tempfilename,
+ strerror(errno));
+ return;
+ }
+ lseek(TmpFD, SEEK_SET, 0);
+ }
+ else {
+ cprintf("%d Unable to allocate the space required for %s: %s\n",
ERROR + INTERNAL_ERROR,
tempfilename,
strerror(errno));
+
+
}
+ Line = NewStrBuf();
cprintf("%d %s\n", SEND_LISTING, tempfilename);
- while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
- fprintf(fp, "%s\n", buf);
+
+ len = 0;
+ while (rc = CtdlClientGetLine(Line),
+ (rc >= 0))
+ {
+ if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
+ break;
+ StrBufAppendBufPlain(Line, HKEY("\n"), 0);
+ write(TmpFD, ChrPtr(Line), StrLength(Line));
+ len += StrLength(Line);
}
- fclose(fp);
+ FreeStrBuf(&Line);
+ ftruncate(TmpFD, len + 1);
+ 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);
- fp = fopen(tempfilename, "r");
- if (fp != NULL) {
- newfp = fopen(filename, "w");
- if (newfp != NULL) {
- while (fgets(buf, sizeof buf, fp) != NULL) {
- fprintf(newfp, "%s", buf);
- }
- fclose(newfp);
- }
- fclose(fp);
- }
+ rename(filename, tempfilename);
end_critical_section(S_NETCONFIGS);
- unlink(tempfilename);
}
*/
msg = CtdlFetchMessage(msgnum, 1);
if (msg != NULL) {
-
+ if (msg->cm_fields['V'] == NULL){
+ /* local message, no enVelope */
+ StrBuf *Buf;
+ Buf = NewStrBuf();
+ StrBufAppendBufPlain(Buf, msg->cm_fields['O'], -1, 0);
+ StrBufAppendBufPlain(Buf, HKEY("@"), 0);
+ StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
+
+ msg->cm_fields['K'] = SmashStrBuf(&Buf);
+ }
+ else {
+ msg->cm_fields['K'] = strdup (msg->cm_fields['V']);
+ }
/* Set the 'List-ID' header */
if (msg->cm_fields['L'] != NULL) {
free(msg->cm_fields['L']);
int writenfree_spoolcontrol_file(SpoolControl **scc, char *filename)
{
+ char tempfilename[PATH_MAX];
FILE *fp;
SpoolControl *sc;
namelist *nptr = NULL;
maplist *mptr = NULL;
+ long len;
+ time_t unixtime;
+ struct timeval tv;
+ long reltid; /* if we don't have SYS_gettid, use "random" value */
+ StrBuf *Cfg;
+ int rc;
+
+ len = strlen(filename);
+ memcpy(tempfilename, filename, len + 1);
+
+
+#if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
+ reltid = syscall(SYS_gettid);
+#endif
+ gettimeofday(&tv, NULL);
+ /* Promote to time_t; types differ on some OSes (like darwin) */
+ unixtime = tv.tv_sec;
+ sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
sc = *scc;
- fp = fopen(filename, "w");
+ fp = fopen(tempfilename, "w");
+ Cfg = NewStrBuf();
if (fp == NULL) {
CtdlLogPrintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
filename, strerror(errno));
* same time. Am I clever or what? :)
*/
while (sc->listrecps != NULL) {
- fprintf(fp, "listrecp|%s\n", sc->listrecps->name);
+ StrBufAppendPrintf(Cfg, "listrecp|%s\n", sc->listrecps->name);
nptr = sc->listrecps->next;
free(sc->listrecps);
sc->listrecps = nptr;
}
/* Do the same for digestrecps */
while (sc->digestrecps != NULL) {
- fprintf(fp, "digestrecp|%s\n", sc->digestrecps->name);
+ StrBufAppendPrintf(Cfg, "digestrecp|%s\n", sc->digestrecps->name);
nptr = sc->digestrecps->next;
free(sc->digestrecps);
sc->digestrecps = nptr;
}
/* Do the same for participates */
while (sc->participates != NULL) {
- fprintf(fp, "participate|%s\n", sc->participates->name);
+ StrBufAppendPrintf(Cfg, "participate|%s\n", sc->participates->name);
nptr = sc->participates->next;
free(sc->participates);
sc->participates = nptr;
}
while (sc->ignet_push_shares != NULL) {
- fprintf(fp, "ignet_push_share|%s", sc->ignet_push_shares->remote_nodename);
+ StrBufAppendPrintf(Cfg, "ignet_push_share|%s", sc->ignet_push_shares->remote_nodename);
if (!IsEmptyStr(sc->ignet_push_shares->remote_roomname)) {
- fprintf(fp, "|%s", sc->ignet_push_shares->remote_roomname);
+ StrBufAppendPrintf(Cfg, "|%s", sc->ignet_push_shares->remote_roomname);
}
- fprintf(fp, "\n");
+ StrBufAppendPrintf(Cfg, "\n");
mptr = sc->ignet_push_shares->next;
free(sc->ignet_push_shares);
sc->ignet_push_shares = mptr;
}
if (sc->misc != NULL) {
- fwrite(sc->misc, strlen(sc->misc), 1, fp);
+ StrBufAppendBufPlain(Cfg, sc->misc, -1, 0);
}
free(sc->misc);
- fclose(fp);
+ rc = fwrite(ChrPtr(Cfg), StrLength(Cfg), 1, fp);
+ if (rc >=0)
+ {
+ fclose(fp);
+ rename(filename, tempfilename);
+ }
+ else {
+ CtdlLogPrintf(CTDL_EMERG,
+ "unable to write %s; not enough space on the disk?\n",
+ tempfilename);
+ fclose(fp);
+ }
+ FreeStrBuf(&Cfg);
free(sc);
*scc=NULL;
}
ctdl_netout_dir, nexthop,
filename
);
- CtdlLogPrintf(CTDL_DEBUG, "\033[31m%s\033[0m\n", cmd);
system(cmd);
network_talking_to(nexthop, NTT_REMOVE);
}
char buf[SIZ];
static char pbuf[IGNET_PACKET_SIZE];
char tempfilename[PATH_MAX];
- char filename[PATH_MAX];
+ char permfilename[PATH_MAX];
long plen;
FILE *fp;
snprintf(tempfilename,
- sizeof tempfilename,
- "%s/%s.%ld",
- ctdl_nettmp_dir,
- remote_nodename,
- (long) getpid()
+ sizeof tempfilename,
+ "%s/%s.%lx%x",
+ ctdl_nettmp_dir,
+ remote_nodename,
+ time(NULL),
+ rand()
+ );
+
+ snprintf(permfilename,
+ sizeof permfilename,
+ "%s/%s.%lx%x",
+ ctdl_netin_dir,
+ remote_nodename,
+ time(NULL),
+ rand()
);
+
if (sock_puts(sock, "NDOP") < 0) return;
if (sock_getln(sock, buf, sizeof buf) < 0) return;
CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
}
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);
+ 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;
}
- 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) {
+
+ 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;
}
- fwrite((char *) pbuf, plen, 1, fp);
- bytes_received = bytes_received + plen;
+ 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())
{
/* Now move the temp file to its permanent location.
*/
- snprintf(filename,
- sizeof filename,
- "%s/%s.%ld",
- ctdl_netin_dir,
- remote_nodename,
- (long) getpid()
- );
-
- if (link(tempfilename, filename) != 0) {
+ if (link(tempfilename, permfilename) != 0) {
CtdlLogPrintf(CTDL_ALERT, "Could not link %s to %s: %s\n",
- tempfilename, filename, strerror(errno)
+ tempfilename, permfilename, strerror(errno)
);
}
unlink(tempfilename);
* with a static variable instead.
*/
if (doing_queue) {
+ CtdlClearSystemContext();
return NULL;
}
doing_queue = 1;
else {
CtdlLogPrintf(CTDL_DEBUG, "network: Task STOPPED.\n");
}
+ CtdlClearSystemContext();
return NULL;
}