2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * anno.c: Delete announcements older than ANNO_KEEP_DAYS
29 * Known contributors to this file:
33 * Ron Koenderink, 2004-2006
34 * Markus Armbruster, 2004-2010
39 #if defined(_WIN32) && defined(__GNUC__)
47 static int copy_and_expire(FILE *, FILE *, char *, time_t);
48 static int copy_sink(char *, size_t, void *);
51 delete_old_announcements(void)
57 char tmp_filename[1024];
60 if (anno_keep_days < 0)
64 old = now - days(anno_keep_days);
65 logerror("Deleting annos older than %s", ctime(&old));
67 if ((annfp = fopen(annfil, "rb")) == NULL) {
68 logerror("can't open telegram file %s for reading", annfil);
71 sprintf(tmp_filename, "%s.tmp", annfil);
72 if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
73 logerror("can't open telegram file %s for writing", tmp_filename);
74 if (fclose(annfp) != 0)
75 logerror("can't close telegram file %s", annfil);
78 copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
80 if (fclose(annfp) != 0) {
81 logerror("can't close telegram file %s", annfil);
84 if (fclose(tmpfp) != 0) {
85 logerror("can't close temporary telegram file %s", tmp_filename);
90 if (unlink(annfil) != 0) {
91 logerror("can't delete telegram file %s", annfil);
97 if (rename(tmp_filename, annfil) != 0)
98 logerror("can't move temporary telegram file %s "
99 "to telegram file %s", tmp_filename, annfil);
101 if (remove(tmp_filename) < 0)
102 logerror("can't delete telegram file %s", tmp_filename);
107 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
116 while ((res = tel_read_header(annfp, annfil, &tgm)) > 0) {
117 writeit = tgm.tel_date >= expiry_time;
124 if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
125 logerror("error writing header to temporary "
126 "telegram file %s", tmp_filename);
132 res = tel_read_body(annfp, annfil, &tgm,
133 writeit ? copy_sink : NULL, tmpfp);
140 logerror("%d announcements deleted; %d announcements saved",
146 copy_sink(char *chunk, size_t sz, void *fp)
148 if (fwrite(chunk, 1, sz, fp) != sz) {
149 logerror("error writing to %s.tmp", annfil);