00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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];
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 }