]> git.pond.sub.org Git - empserver/blob - src/lib/update/anno.c
Remove useless multiplications with sizeof(char) and sizeof(s_char).
[empserver] / src / lib / update / anno.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 #include "misc.h"
40 #include "tel.h"
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <time.h>
44 #if !defined(_WIN32)
45 #include <unistd.h>
46 #endif
47 #include "update.h"
48 #include "optlist.h"
49 #include "common.h"
50
51 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
52                            char *tmp_filename, time_t expiry_time);
53
54 void
55 delete_old_announcements(void)
56 {
57     time_t now;
58     time_t old;
59     FILE *annfp;
60     FILE *tmpfp;
61     char tmp_filename[1024];
62     int copy_file;
63
64     if (anno_keep_days < 0)
65         return;
66
67     time(&now);
68     old = now - days(anno_keep_days);
69     logerror("Deleting annos older than %s", ctime(&old));
70
71     if ((annfp = fopen(annfil, "rb")) == NULL) {
72         logerror("can't open telegram file %s for reading", annfil);
73         return;
74     }
75     sprintf(tmp_filename, "%s.tmp", annfil);
76     if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
77         logerror("can't open telegram file %s for writing",
78                  tmp_filename);
79         if (fclose(annfp) != 0)
80             logerror("can't close telegram file %s", annfil);
81         return;
82     }
83     copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
84
85     if (fclose(annfp) != 0) {
86         logerror("can't close telegram file %s", annfil);
87         copy_file = 0;
88     }
89     if (fclose(tmpfp) != 0) {
90         logerror("can't close temporary telegram file %s",
91                  tmp_filename);
92         copy_file = 0;
93     }
94 #if defined(_WIN32)
95     if (copy_file) {
96         if (unlink(annfil) != 0) {
97             logerror("can't delete telegram file %s", annfil);
98             copy_file = 0;
99         }
100     }
101 #endif
102     if (copy_file) {
103         if (rename(tmp_filename, annfil) != 0)
104             logerror("can't move temporary telegram file %s "
105                      "to telegram file %s", tmp_filename, annfil);
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 writeit;
115     char message[MAXTELSIZE];   /* UTF-8 */
116     int deleted = 0;
117     int saved = 0;
118     int first = 1;
119
120     while (fread(&tgm, sizeof(tgm), 1, annfp) == 1) {
121         writeit = 1;
122         if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
123             logerror("bad telegram file header (length=%ld)",
124                      tgm.tel_length);
125             return 0;
126         }
127         if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
128             logerror("bad telegram file header (type=%d)",
129                      tgm.tel_type);
130             return 0;
131         }
132
133         if (first) {
134             first = 0;
135             if (tgm.tel_date >= expiry_time)
136                 return 0;
137         }
138         if (tgm.tel_date < expiry_time)
139             writeit = 0;
140
141         if (writeit) {
142             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
143                 logerror("error writing header to temporary "
144                          "telegram file %s", tmp_filename);
145                 return 0;
146             }
147             ++saved;
148         } else
149             ++deleted;
150         if (fread(message, 1, tgm.tel_length, annfp) !=
151             (size_t)tgm.tel_length) {
152             logerror("error reading body from telegram file %s",
153                      annfil);
154             return 0;
155         }
156         if (writeit) {
157             if (fwrite(message, 1, tgm.tel_length, tmpfp) !=
158                 (size_t)tgm.tel_length) {
159                 logerror("error writing body to temporary telegram "
160                          "file %s", tmp_filename);
161                 return 0;
162             }
163         }
164     }
165     logerror("%d announcements deleted; %d announcements saved",
166              deleted, saved);
167     return 1;
168 }