]> git.pond.sub.org Git - empserver/blob - src/lib/update/anno.c
Update copyright notice
[empserver] / src / lib / update / anno.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  anno.c: Delete announcements older than ANNO_KEEP_DAYS
29  *
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Doug Hay, 1998
33  *     Steve McClure, 2000
34  *     Ron Koenderink, 2004-2006
35  *     Markus Armbruster, 2004-2009
36  */
37
38 #include <config.h>
39
40 #if defined(_WIN32) && defined(__GNUC__)
41 #include <io.h>
42 #endif
43 #include <stdio.h>
44 #include <time.h>
45 #include "tel.h"
46 #include "update.h"
47
48 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
49                            char *tmp_filename, time_t expiry_time);
50 static int copy_sink(char *, size_t, void *);
51
52 void
53 delete_old_announcements(void)
54 {
55     time_t now;
56     time_t old;
57     FILE *annfp;
58     FILE *tmpfp;
59     char tmp_filename[1024];
60     int copy_file;
61
62     if (anno_keep_days < 0)
63         return;
64
65     time(&now);
66     old = now - days(anno_keep_days);
67     logerror("Deleting annos older than %s", ctime(&old));
68
69     if ((annfp = fopen(annfil, "rb")) == NULL) {
70         logerror("can't open telegram file %s for reading", annfil);
71         return;
72     }
73     sprintf(tmp_filename, "%s.tmp", annfil);
74     if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
75         logerror("can't open telegram file %s for writing",
76                  tmp_filename);
77         if (fclose(annfp) != 0)
78             logerror("can't close telegram file %s", annfil);
79         return;
80     }
81     copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
82
83     if (fclose(annfp) != 0) {
84         logerror("can't close telegram file %s", annfil);
85         copy_file = 0;
86     }
87     if (fclose(tmpfp) != 0) {
88         logerror("can't close temporary telegram file %s",
89                  tmp_filename);
90         copy_file = 0;
91     }
92 #if defined(_WIN32)
93     if (copy_file) {
94         if (unlink(annfil) != 0) {
95             logerror("can't delete telegram file %s", annfil);
96             copy_file = 0;
97         }
98     }
99 #endif
100     if (copy_file) {
101         if (rename(tmp_filename, annfil) != 0)
102             logerror("can't move temporary telegram file %s "
103                      "to telegram file %s", tmp_filename, annfil);
104     } else {
105         if (remove(tmp_filename) < 0)
106             logerror("can't delete telegram file %s", tmp_filename);
107     }
108 }
109
110 static int
111 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
112                 time_t expiry_time)
113 {
114     struct telstr tgm;
115     int res, writeit;
116     int deleted = 0;
117     int saved = 0;
118     int first = 1;
119
120     while ((res = tel_read_header(annfp, annfil, &tgm)) > 0) {
121         writeit = tgm.tel_date >= expiry_time;
122         if (first) {
123             first = 0;
124             if (writeit)
125                 return 0;
126         }
127         if (writeit) {
128             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
129                 logerror("error writing header to temporary "
130                          "telegram file %s", tmp_filename);
131                 return 0;
132             }
133             ++saved;
134         } else
135             ++deleted;
136         res = tel_read_body(annfp, annfil, &tgm,
137                             writeit ? copy_sink : NULL, tmpfp);
138         if (res < 0)
139             return 0;
140     }
141
142     if (res < 0)
143         return 0;
144     logerror("%d announcements deleted; %d announcements saved",
145              deleted, saved);
146     return 1;
147 }
148
149 static int
150 copy_sink(char *chunk, size_t sz, void *fp)
151 {
152     if (fwrite(chunk, 1, sz, fp) != sz) {
153         logerror("error writing to %s.tmp", annfil);
154         return -1;
155     }
156     return 0;
157 }