/* * hostfiles - functions to read / write the host specific time / id files * * Copyright (C) 1992/93/94 Stephen Hebditch . * All rights reserved. TQM Communications, BCM Box 225, London, WC1N 3XX. * * See README for more information and disclaimers * * $Id: hostfiles.c,v 1.9 1995/01/10 12:54:59 root Exp $ * * $Log: hostfiles.c,v $ * Revision 1.9 1995/01/10 12:54:59 root * Initial coding, moved from slurp.c and renamed. * * */ #include "conf.h" /* POSIX headers */ #define _POSIX_SOURCE 1 #include #include #include #include #include #include #include #include #ifndef NOUNISTD #include #endif /* Local headers */ #include "slurp.h" #include "syslog.h" /* * read_hostfile - Get the start time for this NEWNEWS for a host and any * message ids awaiting collection as appropriate. Returns 0 if a file * for the current host can be opened, otherwise 1. */ void read_hostfile (void) { FILE *hostfilefp; char *pos; char line [BUFSIZ]; int first_time = TRUE; int len; /* Return if have nothing to do */ if ((nn_time != NULL) && (no_id_read_flag)) return; /* Attempt to open the host file - only care it exists when need time */ if ((hostfilefp = fopen (hostfile, "r")) == NULL) { if (nn_time == NULL) log_sys ("read_hostfile: error opening %s", hostfile); else return; } /* Read in the time */ (void) fgets (line, sizeof (line), hostfilefp); if (ferror (hostfilefp)) log_sys ("read_hostfile: error reading %s", hostfile); /* If time not already set then set it */ if (nn_time == NULL) { if ((nn_time = (char *) malloc ((size_t) 14)) == NULL) log_sys ("read_hostfile: malloc 14 bytes"); (void) strncpy (nn_time, line, 14); nn_time [13] = '\0'; } /* Load in any message ids following */ if (!no_id_read_flag) { for (;;) { (void) fgets (line, sizeof (line), hostfilefp); if (feof (hostfilefp)) break; if (ferror (hostfilefp)) log_sys ("read_hostfile: error reading %s", hostfile); if ((debug_flag) && (first_time)) { (void) fprintf (stderr, "Loading message IDs from %s\n", hostfile); first_time = FALSE; } if ((pos = strchr (line, '\n')) != NULL) *pos = '\0'; len = strlen (line); if (len > 0) if ((line [0] == '<') && (line [len - 1] == '>')) process_id (line); } } (void) fclose (hostfilefp); if (debug_flag) (void) fprintf (stderr, "%d ids loaded\n", waiting); } /* * write_hostfile - Set the start time for the next NEWNEWS for a host and * any message ids still awaiting collection as appropriate. */ void write_hostfile (void) { FILE *backupfp; FILE *hostfilefp; IDNODE *idnode; char backup [PATH_MAX]; char line [BUFSIZ]; int ids_written = 0; long ndate; long ntime; struct tm *tmtime; /* Return if have nothing to do */ if ((no_id_write_flag) && (no_time_write_flag)) return; /* Copy the file to a backup */ (void) strcpy (backup, hostfile); (void) strcat (backup, ".o"); if (unlink (backup)) if (errno != ENOENT) log_ret ("write_hostfile: error unlinking %s", backup); if (rename (hostfile, backup)) if (errno != ENOENT) log_ret ("write_hostfile: error renaming %s to %s", hostfile, backup); /* Open new file */ if ((hostfilefp = fopen (hostfile, "w")) == NULL) log_sys ("write_hostfile: error opening %s", hostfile); /* If not writing time then copy from old */ if (no_time_write_flag) { if ((backupfp = fopen (backup, "r")) == NULL) log_sys ("write_hostfile: error opening %s", backup); (void) fgets (line, sizeof (line), backupfp); if (ferror (hostfilefp)) log_sys ("write_hostfile: error reading %s", hostfile); (void) fputs (line, hostfilefp); if (ferror (hostfilefp)) log_sys ("write_hostfile: error writing %s", hostfile); (void) fclose (backupfp); } /* Otherwise calculate new time and date values */ else { tmtime = gmtime (&nexttime); ndate = (tmtime -> tm_year%100 * 10000) + ((tmtime -> tm_mon + 1) * 100) + tmtime -> tm_mday; ntime = (tmtime -> tm_hour * 10000) + (tmtime -> tm_min * 100) + tmtime -> tm_sec; /* Write the new time for current host */ (void) fprintf (hostfilefp, "%06ld %06ld\n", ndate, ntime); if (ferror (hostfilefp)) log_sys ("write_hostfile: error writing %s", hostfile); } /* Write out any message ids of articles not transferred */ if (!no_id_write_flag) { /* First uncollected articles */ idnode = first_id (); while (idnode != NULL) { if (!idnode -> used) { (void) fprintf (hostfilefp, "%s\n", idnode -> msgid); if (ferror (hostfilefp)) log_sys ("write_hostfile: error writing %s, hostfile"); ids_written++; } idnode = next_id (); } /* Then any overflow that wouldn't fit in the cache */ if (overflowfp != NULL) { rewind (overflowfp); while (!feof (overflowfp)) { (void) fgets (line, sizeof (line), overflowfp); if (ferror (overflowfp)) log_sys ("write_hostfile: error reading overflow"); (void) fputs (line, hostfilefp); if (ferror (hostfilefp)) log_sys ("write_hostfile: error writing %s", hostfile); ids_written++; } (void) fclose (overflowfp); } } (void) fclose (hostfilefp); /* Report how many ids we wrote to hostfile */ #ifdef SYSLOG if (!debug_flag) { if (ids_written > 0) syslog (LOG_INFO, "Wrote %d Message-IDs to %s", ids_written, hostfile); } else #endif (void) fprintf (stderr, "Wrote %d Message-IDs to %s\n", ids_written, hostfile); } /* END-OF-FILE */