- if (dp == NULL) return;
- while (d = readdir(dp), d != NULL) {
- if (
- (strcmp(d->d_name, "."))
- && (strcmp(d->d_name, ".."))
- && (strchr(d->d_name, '@') != NULL)
- ) {
- nexthoplen = safestrncpy(nexthop, d->d_name, sizeof nexthop);
- ptr = strchr(nexthop, '@');
- if (ptr) {
- *ptr = 0;
- nexthoplen = ptr - nexthop;
- }
-
- snprintf(filename,
- sizeof filename,
- "%s/%s",
- ctdl_netout_dir,
- d->d_name
- );
-
- syslog(LOG_DEBUG, "Consolidate %s to %s\n", filename, nexthop);
- if (network_talking_to(nexthop, nexthoplen, NTT_CHECK)) {
- syslog(LOG_DEBUG,
- "Currently online with %s - skipping for now\n",
- nexthop
+ if (dp == NULL) {
+ free(d);
+ return;
+ }
+
+ NextHop = NewStrBuf();
+ memset(&IOB, 0, sizeof(IOBuffer));
+ memset(&FDIO, 0, sizeof(FDIOBuffer));
+ FDIO.IOB = &IOB;
+
+ while ((readdir_r(dp, d, &filedir_entry) == 0) &&
+ (filedir_entry != NULL))
+ {
+#ifdef _DIRENT_HAVE_D_NAMELEN
+ d_namelen = filedir_entry->d_namelen;
+#else
+
+#ifndef DT_UNKNOWN
+#define DT_UNKNOWN 0
+#define DT_DIR 4
+#define DT_REG 8
+#define DT_LNK 10
+
+#define IFTODT(mode) (((mode) & 0170000) >> 12)
+#define DTTOIF(dirtype) ((dirtype) << 12)
+#endif
+ d_namelen = strlen(filedir_entry->d_name);
+#endif
+ if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
+ continue; /* Ignore backup files... */
+
+ if ((d_namelen == 1) &&
+ (filedir_entry->d_name[0] == '.'))
+ continue;
+
+ if ((d_namelen == 2) &&
+ (filedir_entry->d_name[0] == '.') &&
+ (filedir_entry->d_name[1] == '.'))
+ continue;
+
+ pch = strchr(filedir_entry->d_name, '@');
+ if (pch == NULL)
+ continue;
+
+ snprintf(filename,
+ sizeof filename,
+ "%s/%s",
+ ctdl_netout_dir,
+ filedir_entry->d_name);
+
+ StrBufPlain(NextHop,
+ filedir_entry->d_name,
+ pch - filedir_entry->d_name);
+
+ snprintf(spooloutfilename,
+ sizeof spooloutfilename,
+ "%s/%s",
+ ctdl_netout_dir,
+ ChrPtr(NextHop));
+
+ syslog(LOG_DEBUG, "Consolidate %s to %s\n", filename, ChrPtr(NextHop));
+ if (network_talking_to(SKEY(NextHop), NTT_CHECK)) {
+ nFailed++;
+ syslog(LOG_DEBUG,
+ "Currently online with %s - skipping for now\n",
+ ChrPtr(NextHop)
+ );
+ }
+ else {
+ size_t dsize;
+ size_t fsize;
+ int fd;
+ const char *err = NULL;
+ network_talking_to(SKEY(NextHop), NTT_ADD);
+
+ IOB.fd = open(filename, O_RDONLY);
+ if (IOB.fd == -1) {
+ nFailed++;
+ syslog(LOG_ERR,
+ "failed to open %s for reading due to %s; skipping.\n",
+ filename, strerror(errno)
+ );
+ network_talking_to(SKEY(NextHop), NTT_REMOVE);
+ continue;
+ }
+
+ fd = open(spooloutfilename,
+ O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
+ S_IRUSR|S_IWUSR);
+ if (fd == -1)
+ {
+ fd = open(spooloutfilename,
+ O_EXCL|O_NONBLOCK|O_WRONLY,
+ S_IRUSR | S_IWUSR);
+ }
+ if (fd == -1) {
+ nFailed++;
+ syslog(LOG_ERR,
+ "failed to open %s for reading due to %s; skipping.\n",
+ spooloutfilename, strerror(errno)