]> git.pond.sub.org Git - empserver/blob - src/lib/update/anno.c
Factor out code to read mailboxes, and make read more robust
[empserver] / src / lib / update / anno.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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
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",
75                  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",
88                  tmp_filename);
89         copy_file = 0;
90     }
91 #if defined(_WIN32)
92     if (copy_file) {
93         if (unlink(annfil) != 0) {
94             logerror("can't delete telegram file %s", annfil);
95             copy_file = 0;
96         }
97     }
98 #endif
99     if (copy_file) {
100         if (rename(tmp_filename, annfil) != 0)
101             logerror("can't move temporary telegram file %s "
102                      "to telegram file %s", tmp_filename, annfil);
103     } else {
104         if (remove(tmp_filename) < 0)
105             logerror("can't delete telegram file %s", tmp_filename);
106     }
107 }
108
109 static int
110 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
111                 time_t expiry_time)
112 {
113     struct telstr tgm;
114     int res, writeit;
115     int deleted = 0;
116     int saved = 0;
117     int first = 1;
118
119     while ((res = tel_read_header(annfp, annfil, &tgm)) > 0) {
120         writeit = tgm.tel_date >= expiry_time;
121         if (first) {
122             first = 0;
123             if (writeit)
124                 return 0;
125         }
126         if (writeit) {
127             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
128                 logerror("error writing header to temporary "
129                          "telegram file %s", tmp_filename);
130                 return 0;
131             }
132             ++saved;
133         } else
134             ++deleted;
135         res = tel_read_body(annfp, annfil, &tgm,
136                             writeit ? copy_sink : NULL, tmpfp);
137         if (res < 0)
138             return 0;
139     }
140
141     if (res < 0)
142         return 0;
143     logerror("%d announcements deleted; %d announcements saved",
144              deleted, saved);
145     return 1;
146 }
147
148 static int
149 copy_sink(char *chunk, size_t sz, void *fp)
150 {
151     if (fwrite(chunk, 1, sz, fp) != sz) {
152         logerror("error writing to %s.tmp", annfil);
153         return -1;
154     }
155     return 0;
156 }