]> git.pond.sub.org Git - empserver/blob - src/lib/update/anno.c
80e46189735643329ff74473121cac0821edf854
[empserver] / src / lib / update / anno.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
35
36 #include "misc.h"
37 #include "tel.h"
38 #include <stdio.h>
39 #include <fcntl.h>
40 #if !defined(_WIN32)
41 #include <sys/file.h>
42 #include <unistd.h>
43 #endif
44 #include "update.h"
45 #include "optlist.h"
46 #include "common.h"
47
48 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
49                            char *tmp_filename, time_t expiry_time);
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     time(&now);
62     old = now - days(ANNO_KEEP_DAYS);
63     logerror("Deleting annos older than %s", ctime(&old));
64
65     if ((annfp = fopen(annfil, "rb")) == NULL) {
66         logerror("can't open telegram file %s for reading", annfil);
67         return;
68     }
69     sprintf(tmp_filename, "%s.tmp", annfil);
70     if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
71         logerror("can't open telegram file %s for writing",
72                  tmp_filename);
73         if (fclose(annfp) != 0)
74             logerror("can't close telegram file %s", annfil);
75         return;
76     }
77     copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
78
79     if (fclose(annfp) != 0) {
80         logerror("can't close telegram file %s", annfil);
81         copy_file = 0;
82     }
83     if (fclose(tmpfp) != 0) {
84         logerror("can't close temporary telegram file %s",
85                  tmp_filename);
86         copy_file = 0;
87     }
88 #if defined(_WIN32)
89     if (copy_file) {
90         if (unlink(annfil) != 0) {
91             logerror("can't delete telegram file %s", annfil);
92             copy_file = 0;
93         }
94     }
95 #endif
96     if (copy_file) {
97         if (rename(tmp_filename, annfil) != 0)
98             logerror("can't move temporary telegram file %s "
99                      "to telegram file %s", tmp_filename, annfil);
100     }
101 }
102
103 static int
104 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
105                 time_t expiry_time)
106 {
107     struct telstr tgm;
108     int writeit;
109     char message[MAXTELSIZE];
110     int deleted = 0;
111     int saved = 0;
112     int first = 1;
113
114     while (fread((void *)&tgm, sizeof(tgm), 1, annfp) == 1) {
115         writeit = 1;
116         if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
117             logerror("bad telegram file header (length=%ld)",
118                      tgm.tel_length);
119             return 0;
120         }
121         if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
122             logerror("bad telegram file header (type=%d)",
123                      tgm.tel_type);
124             return 0;
125         }
126
127         if (first) {
128             first = 0;
129             if (tgm.tel_date >= expiry_time)
130                 return 0;
131         }
132         if (tgm.tel_date < expiry_time)
133             writeit = 0;
134
135         if (writeit) {
136             if (fwrite((void *)&tgm, sizeof(tgm), 1, tmpfp) != 1) {
137                 logerror("error writing header to temporary "
138                          "telegram file %s", tmp_filename);
139                 return 0;
140             }
141             ++saved;
142         } else
143             ++deleted;
144         if (fread(message, sizeof(char), tgm.tel_length, annfp) !=
145             (size_t)tgm.tel_length) {
146             logerror("error reading body from telegram file %s",
147                      annfil);
148             return 0;
149         }
150         if (writeit) {
151             if (fwrite(message, sizeof(char), tgm.tel_length, tmpfp) !=
152                 (size_t)tgm.tel_length) {
153                 logerror("error writing body to temporary telegram "
154                          "file %s", tmp_filename);
155                 return 0;
156             }
157         }
158     }
159     logerror("%d announcements deleted; %d announcements saved",
160              deleted, saved);
161     return 1;
162 }