2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * anno.c: Delete announcements older than ANNO_KEEP_DAYS
30 * Known contributors to this file:
48 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
49 char *tmp_filename, time_t expiry_time);
52 delete_old_announcements(void)
58 char tmp_filename[1024];
61 if (anno_keep_days < 0)
65 old = now - days(anno_keep_days);
66 logerror("Deleting annos older than %s", ctime(&old));
68 if ((annfp = fopen(annfil, "rb")) == NULL) {
69 logerror("can't open telegram file %s for reading", annfil);
72 sprintf(tmp_filename, "%s.tmp", annfil);
73 if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
74 logerror("can't open telegram file %s for writing",
76 if (fclose(annfp) != 0)
77 logerror("can't close telegram file %s", annfil);
80 copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
82 if (fclose(annfp) != 0) {
83 logerror("can't close telegram file %s", annfil);
86 if (fclose(tmpfp) != 0) {
87 logerror("can't close temporary telegram file %s",
93 if (unlink(annfil) != 0) {
94 logerror("can't delete telegram file %s", annfil);
100 if (rename(tmp_filename, annfil) != 0)
101 logerror("can't move temporary telegram file %s "
102 "to telegram file %s", tmp_filename, annfil);
107 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
112 char message[MAXTELSIZE];
117 while (fread((void *)&tgm, sizeof(tgm), 1, annfp) == 1) {
119 if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
120 logerror("bad telegram file header (length=%ld)",
124 if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
125 logerror("bad telegram file header (type=%d)",
132 if (tgm.tel_date >= expiry_time)
135 if (tgm.tel_date < expiry_time)
139 if (fwrite((void *)&tgm, sizeof(tgm), 1, tmpfp) != 1) {
140 logerror("error writing header to temporary "
141 "telegram file %s", tmp_filename);
147 if (fread(message, sizeof(char), tgm.tel_length, annfp) !=
148 (size_t)tgm.tel_length) {
149 logerror("error reading body from telegram file %s",
154 if (fwrite(message, sizeof(char), tgm.tel_length, tmpfp) !=
155 (size_t)tgm.tel_length) {
156 logerror("error writing body to temporary telegram "
157 "file %s", tmp_filename);
162 logerror("%d announcements deleted; %d announcements saved",