]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/anno.c
Factor out code to read mailboxes, and make read more robust
[empserver] / src / lib / update / anno.c
index 8895ef4b8a9a683e233803f668ab816c54e4f682..adc765735ccc88c4eda1d372a69fae82fdd1b02d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  anno.c: Delete announcements older than ANNO_KEEP_DAYS
- * 
+ *
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Doug Hay, 1998
  *     Steve McClure, 2000
+ *     Ron Koenderink, 2004
  */
 
-#include "misc.h"
-#include "tel.h"
-#include <stdio.h>
-#include <fcntl.h>
-#if !defined(_WIN32)
-#include <sys/file.h>
-#include <unistd.h>
+#include <config.h>
+
+#if defined(_WIN32) && defined(__GNUC__)
+#include <io.h>
 #endif
+#include <stdio.h>
+#include <time.h>
+#include "tel.h"
 #include "update.h"
-#include "optlist.h"
-#include "common.h"
 
 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
                           char *tmp_filename, time_t expiry_time);
+static int copy_sink(char *, size_t, void *);
 
 void
 delete_old_announcements(void)
@@ -100,6 +100,9 @@ delete_old_announcements(void)
        if (rename(tmp_filename, annfil) != 0)
            logerror("can't move temporary telegram file %s "
                     "to telegram file %s", tmp_filename, annfil);
+    } else {
+       if (remove(tmp_filename) < 0)
+           logerror("can't delete telegram file %s", tmp_filename);
     }
 }
 
@@ -108,33 +111,18 @@ copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
                time_t expiry_time)
 {
     struct telstr tgm;
-    int writeit;
-    char message[MAXTELSIZE];
+    int res, writeit;
     int deleted = 0;
     int saved = 0;
     int first = 1;
 
-    while (fread(&tgm, sizeof(tgm), 1, annfp) == 1) {
-       writeit = 1;
-       if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
-           logerror("bad telegram file header (length=%ld)",
-                    tgm.tel_length);
-           return 0;
-       }
-       if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
-           logerror("bad telegram file header (type=%d)",
-                    tgm.tel_type);
-           return 0;
-       }
-
+    while ((res = tel_read_header(annfp, annfil, &tgm)) > 0) {
+       writeit = tgm.tel_date >= expiry_time;
        if (first) {
            first = 0;
-           if (tgm.tel_date >= expiry_time)
+           if (writeit)
                return 0;
        }
-       if (tgm.tel_date < expiry_time)
-           writeit = 0;
-
        if (writeit) {
            if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
                logerror("error writing header to temporary "
@@ -144,22 +132,25 @@ copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
            ++saved;
        } else
            ++deleted;
-       if (fread(message, sizeof(char), tgm.tel_length, annfp) !=
-           (size_t)tgm.tel_length) {
-           logerror("error reading body from telegram file %s",
-                    annfil);
+       res = tel_read_body(annfp, annfil, &tgm,
+                           writeit ? copy_sink : NULL, tmpfp);
+       if (res < 0)
            return 0;
-       }
-       if (writeit) {
-           if (fwrite(message, sizeof(char), tgm.tel_length, tmpfp) !=
-               (size_t)tgm.tel_length) {
-               logerror("error writing body to temporary telegram "
-                        "file %s", tmp_filename);
-               return 0;
-           }
-       }
     }
+
+    if (res < 0)
+       return 0;
     logerror("%d announcements deleted; %d announcements saved",
             deleted, saved);
     return 1;
 }
+
+static int
+copy_sink(char *chunk, size_t sz, void *fp)
+{
+    if (fwrite(chunk, 1, sz, fp) != sz) {
+       logerror("error writing to %s.tmp", annfil);
+       return -1;
+    }
+    return 0;
+}