src/lib/update/anno.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  anno.c: Delete announcements older than ANNO_KEEP_DAYS
00029  * 
00030  *  Known contributors to this file:
00031  *     Ken Stevens, 1995
00032  *     Doug Hay, 1998
00033  *     Steve McClure, 2000
00034  *     Ron Koenderink, 2004
00035  */
00036 
00037 #include <config.h>
00038 
00039 #if defined(_WIN32) && defined(__GNUC__)
00040 #include <io.h>
00041 #endif
00042 #include <stdio.h>
00043 #include <time.h>
00044 #include "tel.h"
00045 #include "update.h"
00046 
00047 static int copy_and_expire(FILE *annfp, FILE *tmpfp,
00048                            char *tmp_filename, time_t expiry_time);
00049 
00050 void
00051 delete_old_announcements(void)
00052 {
00053     time_t now;
00054     time_t old;
00055     FILE *annfp;
00056     FILE *tmpfp;
00057     char tmp_filename[1024];
00058     int copy_file;
00059 
00060     if (anno_keep_days < 0)
00061         return;
00062 
00063     time(&now);
00064     old = now - days(anno_keep_days);
00065     logerror("Deleting annos older than %s", ctime(&old));
00066 
00067     if ((annfp = fopen(annfil, "rb")) == NULL) {
00068         logerror("can't open telegram file %s for reading", annfil);
00069         return;
00070     }
00071     sprintf(tmp_filename, "%s.tmp", annfil);
00072     if ((tmpfp = fopen(tmp_filename, "wb")) == NULL) {
00073         logerror("can't open telegram file %s for writing",
00074                  tmp_filename);
00075         if (fclose(annfp) != 0)
00076             logerror("can't close telegram file %s", annfil);
00077         return;
00078     }
00079     copy_file = copy_and_expire(annfp, tmpfp, tmp_filename, old);
00080 
00081     if (fclose(annfp) != 0) {
00082         logerror("can't close telegram file %s", annfil);
00083         copy_file = 0;
00084     }
00085     if (fclose(tmpfp) != 0) {
00086         logerror("can't close temporary telegram file %s",
00087                  tmp_filename);
00088         copy_file = 0;
00089     }
00090 #if defined(_WIN32)
00091     if (copy_file) {
00092         if (unlink(annfil) != 0) {
00093             logerror("can't delete telegram file %s", annfil);
00094             copy_file = 0;
00095         }
00096     }
00097 #endif
00098     if (copy_file) {
00099         if (rename(tmp_filename, annfil) != 0)
00100             logerror("can't move temporary telegram file %s "
00101                      "to telegram file %s", tmp_filename, annfil);
00102     } else {
00103         if (remove(tmp_filename) < 0)
00104             logerror("can't delete telegram file %s", tmp_filename);
00105     }
00106 }
00107 
00108 static int
00109 copy_and_expire(FILE *annfp, FILE *tmpfp, char *tmp_filename,
00110                 time_t expiry_time)
00111 {
00112     struct telstr tgm;
00113     int writeit;
00114     char message[MAXTELSIZE];   /* UTF-8 */
00115     int deleted = 0;
00116     int saved = 0;
00117     int first = 1;
00118 
00119     while (fread(&tgm, sizeof(tgm), 1, annfp) == 1) {
00120         writeit = 1;
00121         if (tgm.tel_length < 0 || tgm.tel_length > MAXTELSIZE) {
00122             logerror("bad telegram file header (length=%ld)",
00123                      tgm.tel_length);
00124             return 0;
00125         }
00126         if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
00127             logerror("bad telegram file header (type=%d)",
00128                      tgm.tel_type);
00129             return 0;
00130         }
00131 
00132         if (first) {
00133             first = 0;
00134             if (tgm.tel_date >= expiry_time)
00135                 return 0;
00136         }
00137         if (tgm.tel_date < expiry_time)
00138             writeit = 0;
00139 
00140         if (writeit) {
00141             if (fwrite(&tgm, sizeof(tgm), 1, tmpfp) != 1) {
00142                 logerror("error writing header to temporary "
00143                          "telegram file %s", tmp_filename);
00144                 return 0;
00145             }
00146             ++saved;
00147         } else
00148             ++deleted;
00149         if (fread(message, 1, tgm.tel_length, annfp) !=
00150             (size_t)tgm.tel_length) {
00151             logerror("error reading body from telegram file %s",
00152                      annfil);
00153             return 0;
00154         }
00155         if (writeit) {
00156             if (fwrite(message, 1, tgm.tel_length, tmpfp) !=
00157                 (size_t)tgm.tel_length) {
00158                 logerror("error writing body to temporary telegram "
00159                          "file %s", tmp_filename);
00160                 return 0;
00161             }
00162         }
00163     }
00164     logerror("%d announcements deleted; %d announcements saved",
00165              deleted, saved);
00166     return 1;
00167 }

Generated on Fri Mar 28 11:01:16 2008 for empserver by  doxygen 1.5.2