]> 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-2012, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  anno.c: Delete announcements older than ANNO_KEEP_DAYS
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Doug Hay, 1998
32  *     Steve McClure, 2000
33  *     Ron Koenderink, 2004-2006
34  *     Markus Armbruster, 2004-2010
35  */
36
37 #include <config.h>
38
39 #if defined(_WIN32) && defined(__GNUC__)
40 #include <io.h>
41 #endif
42 #include <stdio.h>
43 #include <time.h>
44 #include "tel.h"
45 #include "update.h"
46
47 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
48                            char *tmp_filename, time_t expiry_time);
49 static int copy_sink(char *, size_t, void *);
50
51 void
52 delete_old_announcements(void)
53 {
54     time_t now;
55     time_t old;
56     FILE *annfp;
57     FILE *tmpfp;
58     char tmp_filename[1024];
59     int copy_file;
60
61     if (anno_keep_days < 0)
62         return;
63
64     time(&now);
65     old = now - days(anno_keep_days);
66     logerror("Deleting annos older than %s", ctime(&old));
67
68     if ((annfp = fopen(annfil, "rb")) == NULL) {
69         logerror("can't open telegram file %s for reading", annfil);
70         return;
71     }
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", tmp_filename);
75         if (fclose(annfp) != 0)
76             logerror("can't close telegram file %s", annfil);
77         return;
78     }
79     copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
80
81     if (fclose(annfp) != 0) {
82         logerror("can't close telegram file %s", annfil);
83         copy_file = 0;
84     }
85     if (fclose(tmpfp) != 0) {
86         logerror("can't close temporary telegram file %s", tmp_filename);
87         copy_file = 0;
88     }
89 #if defined(_WIN32)
90     if (copy_file) {
91         if (unlink(annfil) != 0) {
92             logerror("can't delete telegram file %s", annfil);
93             copy_file = 0;
94         }
95     }
96 #endif
97     if (copy_file) {
98         if (rename(tmp_filename, annfil) != 0)
99             logerror("can't move temporary telegram file %s "
100                      "to telegram file %s", tmp_filename, annfil);
101     } else {
102         if (remove(tmp_filename) < 0)
103             logerror("can't delete telegram file %s", tmp_filename);
104     }
105 }
106
107 static int
108 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
109                 time_t expiry_time)
110 {
111     struct telstr tgm;
112     int res, writeit;
113     int deleted = 0;
114     int saved = 0;
115     int first = 1;
116
117     while ((res = tel_read_header(annfp, annfil, &tgm)) > 0) {
118         writeit = tgm.tel_date >= expiry_time;
119         if (first) {
120             first = 0;
121             if (writeit)
122                 return 0;
123         }
124         if (writeit) {
125             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
126                 logerror("error writing header to temporary "
127                          "telegram file %s", tmp_filename);
128                 return 0;
129             }
130             ++saved;
131         } else
132             ++deleted;
133         res = tel_read_body(annfp, annfil, &tgm,
134                             writeit ? copy_sink : NULL, tmpfp);
135         if (res < 0)
136             return 0;
137     }
138
139     if (res < 0)
140         return 0;
141     logerror("%d announcements deleted; %d announcements saved",
142              deleted, saved);
143     return 1;
144 }
145
146 static int
147 copy_sink(char *chunk, size_t sz, void *fp)
148 {
149     if (fwrite(chunk, 1, sz, fp) != sz) {
150         logerror("error writing to %s.tmp", annfil);
151         return -1;
152     }
153     return 0;
154 }