]> git.pond.sub.org Git - empserver/blob - src/lib/update/anno.c
Fix trailing whitespace
[empserver] / src / lib / update / anno.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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
50 void
51 delete_old_announcements(void)
52 {
53     time_t now;
54     time_t old;
55     FILE *annfp;
56     FILE *tmpfp;
57     char tmp_filename[1024];
58     int copy_file;
59
60     if (anno_keep_days < 0)
61         return;
62
63     time(&now);
64     old = now - days(anno_keep_days);
65     logerror("Deleting annos older than %s", ctime(&old));
66
67     if ((annfp = fopen(annfil, "rb")) == NULL) {
68         logerror("can't open telegram file %s for reading", annfil);
69         return;
70     }
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",
74                  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",
87                  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 writeit;
114     char message[MAXTELSIZE];   /* UTF-8 */
115     int deleted = 0;
116     int saved = 0;
117     int first = 1;
118
119     while (fread(&tgm, sizeof(tgm), 1, annfp) == 1) {
120         writeit = 1;
121         if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
122             logerror("bad telegram file header (length=%ld)",
123                      tgm.tel_length);
124             return 0;
125         }
126         if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
127             logerror("bad telegram file header (type=%d)",
128                      tgm.tel_type);
129             return 0;
130         }
131
132         if (first) {
133             first = 0;
134             if (tgm.tel_date >= expiry_time)
135                 return 0;
136         }
137         if (tgm.tel_date < expiry_time)
138             writeit = 0;
139
140         if (writeit) {
141             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
142                 logerror("error writing header to temporary "
143                          "telegram file %s", tmp_filename);
144                 return 0;
145             }
146             ++saved;
147         } else
148             ++deleted;
149         if (fread(message, 1, tgm.tel_length, annfp) !=
150             (size_t)tgm.tel_length) {
151             logerror("error reading body from telegram file %s",
152                      annfil);
153             return 0;
154         }
155         if (writeit) {
156             if (fwrite(message, 1, tgm.tel_length, tmpfp) !=
157                 (size_t)tgm.tel_length) {
158                 logerror("error writing body to temporary telegram "
159                          "file %s", tmp_filename);
160                 return 0;
161             }
162         }
163     }
164     logerror("%d announcements deleted; %d announcements saved",
165              deleted, saved);
166     return 1;
167 }