]> 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-2020, 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 "optlist.h"
45 #include "prototypes.h"
46 #include "tel.h"
47 #include "update.h"
48
49 static int copy_and_expire(FILE *, FILE *, char *, time_t);
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", tmp_filename);
76         if (fclose(annfp) != 0)
77             logerror("can't close telegram file %s", annfil);
78         return;
79     }
80     copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
81
82     if (fclose(annfp) != 0) {
83         logerror("can't close telegram file %s", annfil);
84         copy_file = 0;
85     }
86     if (fclose(tmpfp) != 0) {
87         logerror("can't close temporary telegram file %s", tmp_filename);
88         copy_file = 0;
89     }
90 #if defined(_WIN32)
91     if (copy_file) {
92         if (unlink(annfil) != 0) {
93             logerror("can't delete telegram file %s", annfil);
94             copy_file = 0;
95         }
96     }
97 #endif
98     if (copy_file) {
99         if (rename(tmp_filename, annfil) != 0)
100             logerror("can't move temporary telegram file %s "
101                      "to telegram file %s", tmp_filename, annfil);
102     } else {
103         if (remove(tmp_filename) < 0)
104             logerror("can't delete telegram file %s", tmp_filename);
105     }
106 }
107
108 static int
109 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
110                 time_t expiry_time)
111 {
112     struct telstr tgm;
113     int res, writeit;
114     int deleted = 0;
115     int saved = 0;
116     int first = 1;
117
118     while ((res = tel_read_header(annfp, annfil, &tgm)) > 0) {
119         writeit = tgm.tel_date >= expiry_time;
120         if (first) {
121             first = 0;
122             if (writeit)
123                 return 0;
124         }
125         if (writeit) {
126             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
127                 logerror("error writing header to temporary "
128                          "telegram file %s", tmp_filename);
129                 return 0;
130             }
131             ++saved;
132         } else
133             ++deleted;
134         res = tel_read_body(annfp, annfil, &tgm,
135                             writeit ? copy_sink : NULL, tmpfp);
136         if (res < 0)
137             return 0;
138     }
139
140     if (res < 0)
141         return 0;
142     logerror("%d announcements deleted; %d announcements saved",
143              deleted, saved);
144     return 1;
145 }
146
147 static int
148 copy_sink(char *chunk, size_t sz, void *fp)
149 {
150     if (fwrite(chunk, 1, sz, fp) != sz) {
151         logerror("error writing to %s.tmp", annfil);
152         return -1;
153     }
154     return 0;
155 }