text-garb.c 6.41 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: text-garb.c,v 0.35 2000/03/13 11:51:12 ceder Exp $
3
 * Copyright (C) 1991-1995, 1997-1999  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27 28 29 30
/*
 * This file contains the functions that deletes old texts.
 *
 * Author: Per Cederqvist.
 */

David Byers's avatar
David Byers committed
31 32 33 34 35

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

36
static const char *
37
rcsid = "$Id: text-garb.c,v 0.35 2000/03/13 11:51:12 ceder Exp $";
38 39
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
40

Per Cederqvist's avatar
Per Cederqvist committed
41
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
42
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
43
#include <sys/types.h>
44
#include <setjmp.h>
Per Cederqvist's avatar
Per Cederqvist committed
45

46
#include "ldifftime.h"
47
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
48 49
#include "kom-types.h"
#include "text-garb.h"
David Byers's avatar
Server  
David Byers committed
50
#include "kom-errno.h"
Per Cederqvist's avatar
Per Cederqvist committed
51 52
#include "misc-types.h"
#include "debug.h"
Per Cederqvist's avatar
Per Cederqvist committed
53
#include "cache.h"
Per Cederqvist's avatar
Per Cederqvist committed
54
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
55 56
#include "log.h"
#include "internal-services.h"
57
#include "param.h"
58
#include "server-time.h"
David Byers's avatar
Server  
David Byers committed
59 60 61 62
#include "com.h"
#include "async.h"
#include "connections.h"
#include "manipulate.h"
63 64 65 66
#ifdef DEBUG_CALLS
#  include "send-async.h"
#  include "services.h"
#endif
Per Cederqvist's avatar
Per Cederqvist committed
67

David Byers's avatar
David Byers committed
68 69
BUGDECL;

70 71 72
/*
 * This comment is a description of how this _could_ be done in a more
 * efficient way.  It is not yet implemented.  Design by Inge Wallin
73
 * and Per Cederqvist 1992-10-07. +++FIXME.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
 *
 * Today, all text statuses are read into the core during the garbage
 * collection phase. Due to step 3 below we think that we can reduce
 * the number of text-status-fetches by 95 %.
 *
 * 1) Allocate a bitmap with one bit for each text. Clear all bits.
 *    This bit is set as soon as it is certain that this text should
 *    not be deleted during this garbage collection pass.
 * 2) Loop over all persons:
 *	+ Set the bit for all his/her marked texts.
 *    (This step may, or may not, be efficient. Profile your code!)
 * 3) Loop over all conferences:
 *	+ Loop over all texts in the conference:
 *		+ If the bit is already set, skip the text.
 *		+ Retrieve the text status from the data base.
 *		+ If it is old enough (in all the recipient conferences)
 *			delete it.
 *		  else
 *		        set the bit.
 *		+ If it is too young to be deleted (only
 *    		  considering this conferene)
 *			+ Set the bit for this text and all subsequent
 *    			  texts in this conference.
 * 4) Loop over all remaining texts:
 *	+ If the bit is not set,
 *		+ delete the text. (It has no recipients).
 * 5) Deallocate the bitmap and wait 24 hours.
 */
Per Cederqvist's avatar
Per Cederqvist committed
102

103 104 105
static Text_no last_checked = 0;
static time_t  last_start = NO_TIME;

Per Cederqvist's avatar
Per Cederqvist committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/*
 * Delete old texts.
 *
 * Return value is TRUE if there is nothing more to do right now,
 * FALSE if this function should be called again as soon as the
 * server has some time over.
 */

Bool
garb_text(void)
{
    static long	   deleted_texts = 0;

    Text_stat	  *text_s;
    Misc_info 	  *misc;
121
    u_short 	   nmisc, naux;
Per Cederqvist's avatar
Per Cederqvist committed
122 123
    double	   age;			/* How many seconds is this text? */
    double	   limit = 24 * 3600;
124 125 126 127

    if (param.garb_enable == FALSE)
	return TRUE;

Per Cederqvist's avatar
Per Cederqvist committed
128 129 130
    if ( last_checked == 0 )
    {
	if ( last_start != NO_TIME
131
	    && ldifftime(current_time, last_start) < param.garb_interval * 60 )
Per Cederqvist's avatar
Per Cederqvist committed
132 133 134 135
	{
	    return TRUE;
	}
	
David Byers's avatar
David Byers committed
136
	kom_log("MSG: garb started.\n");
137
	last_start = current_time;
Per Cederqvist's avatar
Per Cederqvist committed
138
    }
139 140

    tell_cache_garb_text(1);
Per Cederqvist's avatar
Per Cederqvist committed
141 142 143 144
    last_checked = traverse_text( last_checked );

    if ( last_checked == 0 )
    {
145 146
	kom_log("MSG: garb ready. %lu texts deleted.\n",
		(u_long)deleted_texts);
147
	tell_cache_garb_text(0);
148 149 150
#ifdef DEBUG_CALLS
	async_garb_ended(deleted_texts);
#endif
David Byers's avatar
Server  
David Byers committed
151
	deleted_texts = 0;
Per Cederqvist's avatar
Per Cederqvist committed
152 153 154 155 156
	return FALSE;
    }
    
    if ( (text_s = cached_get_text_stat( last_checked )) == NULL )
    {
David Byers's avatar
David Byers committed
157
	kom_log("ERROR: garb_text(): Can't get text-stat.\n");
158
	tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
159 160 161
	return FALSE;
    }

162
    age = ldifftime(current_time, text_s->creation_time );
Per Cederqvist's avatar
Per Cederqvist committed
163 164 165
    
    if ( text_s->no_of_marks > 0 || age < 24 * 3600 )
    {
166
	tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
167 168 169
	return FALSE;
    }

170
    for (naux = 0; naux < text_s->aux_item_list.length; ++naux)
171 172 173 174 175 176 177 178
    {
        if (text_s->aux_item_list.items[naux].flags.dont_garb)
        {
            tell_cache_garb_text(0);
            return FALSE;
        }
    }

Per Cederqvist's avatar
Per Cederqvist committed
179 180 181 182 183 184 185
    for (nmisc = text_s->no_of_misc, misc = text_s->misc_items;
	 nmisc > 0;
	 --nmisc, ++misc )
    {
	switch ( misc->type )
	{
	case recpt:
186 187
	    if ( cached_conf_exists( misc->datum.recipient ) )
	    {
188
		limit = (24 * 3600.0 *
189 190
			 cached_get_garb_nice( misc->datum.recipient ));
		if ( age < limit )	
191 192
		{
		    tell_cache_garb_text(0);
193
		    return FALSE;
194
		}
195 196
	    }

Per Cederqvist's avatar
Per Cederqvist committed
197 198 199
	    break;

	case cc_recpt:
200 201
	    if ( cached_conf_exists( misc->datum.cc_recipient ))
	    {
202
		limit = (24 * 3600.0
203 204
			 * cached_get_garb_nice( misc->datum.cc_recipient ));
		if ( age < limit )
205 206
		{
		    tell_cache_garb_text(0);
207
		    return FALSE;
208
		}
209
	    }
Per Cederqvist's avatar
Per Cederqvist committed
210 211 212

	    break;

213 214 215
	case bcc_recpt:
	    if ( cached_conf_exists( misc->datum.bcc_recipient ))
	    {
216
		limit = (24 * 3600.0
217 218 219 220 221 222 223 224 225 226
			 * cached_get_garb_nice( misc->datum.bcc_recipient ));
		if ( age < limit )
		{
		    tell_cache_garb_text(0);
		    return FALSE;
		}
	    }

	    break;

Per Cederqvist's avatar
Per Cederqvist committed
227 228 229 230 231 232 233 234 235 236 237 238 239
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	    limit = 24 * 3600;
	    break;
	    
	case loc_no:
	case rec_time:
	case sent_by:
	    break;
	    
	case sent_at:
240
	    if (ldifftime(current_time, misc->datum.sent_at) < limit)
Per Cederqvist's avatar
Per Cederqvist committed
241
	    {
242
		tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
243 244 245 246 247 248 249 250
		return FALSE;
	    }
	    		
	    break;

#ifndef COMPILE_CHECKS
	default:
#endif
251 252
	case unknown_info:
	    restart_kom("garb_text(): Illegal misc-item.\n");
Per Cederqvist's avatar
Per Cederqvist committed
253 254 255
	}
    }

256
    VBUG(("garb_text: deleting %lu\n", last_checked));
Per Cederqvist's avatar
Per Cederqvist committed
257 258
    do_delete_text ( last_checked, text_s );
    deleted_texts++;
259
    tell_cache_garb_text(0);
Per Cederqvist's avatar
Per Cederqvist committed
260 261 262
    return FALSE;
}
    
263 264 265 266
#ifdef DEBUG_CALLS
Success
start_garb(void)
{
David Byers's avatar
Server  
David Byers committed
267
    CHK_CONNECTION(FAILURE);
268 269 270 271 272 273
    kom_log("MSG: garb restarted.\n");
    last_checked = 0;
    last_start = NO_TIME;
    return OK;
}
#endif