simple-cache.c 69.5 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
2
3
 * $Id: simple-cache.c,v 0.117 2005/12/18 22:18:15 ceder Exp $
 * Copyright (C) 1991-2005  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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.
 *
Per Cederqvist's avatar
Per Cederqvist committed
23
 * Please report bugs at http://bugzilla.lysator.liu.se/. 
Linus Tolke's avatar
Linus Tolke committed
24
 */
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
29
30
31
32
33
/*
 * This module contains some simple simulations of the routines in
 * cache.c.
 *
 * Extracted from ram-cache.c and rewritten by ceder.
 *
 * New database format with texts in their own file by Inge Wallin.
 *
 * New save algorithm by ceder.
David Byers's avatar
David Byers committed
34
 * Attempt at newer algorithm by byers (FASTSAVE)
Per Cederqvist's avatar
Per Cederqvist committed
35
36
 */

David Byers's avatar
David Byers committed
37
38
39
40
41

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

Per Cederqvist's avatar
Per Cederqvist committed
42
43
#include <errno.h>
#include <stdio.h>
David Byers's avatar
Server:    
David Byers committed
44
45
46
#ifdef HAVE_STDLIB_H
#   include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
47
#include <sys/types.h>
48
49
50
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
51
52
53
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
54
55
56
57
#ifndef SEEK_END
#  include <fcntl.h>
#endif
#ifndef SEEK_END
Per Cederqvist's avatar
Per Cederqvist committed
58
59
60
#  define SEEK_SET 0
#  define SEEK_END 2
#endif
61
#include "timewrap.h"
Per Cederqvist's avatar
Per Cederqvist committed
62
63
#include <setjmp.h>

64
#include "ldifftime.h"
Per Cederqvist's avatar
Per Cederqvist committed
65
66
#include "exp.h"
#include "misc-types.h"
67
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
68
69
70
71
#include "kom-types.h"
#include "cache-node.h"
#include "cache.h"
#include "parser.h"
72
#include "ram-io.h"
Per Cederqvist's avatar
Per Cederqvist committed
73
74
75
76
77
78
79
80
81
#include "ram-parse.h"
#include "ram-output.h"
#include "server/smalloc.h"
#include "kom-memory.h"
#include "lyskomd.h"
#include "debug.h"
#include "kom-errno.h"
#include "log.h"
#include "com.h"
82
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
83
84
#include "connections.h"
#include "send-async.h"
85
#include "conf-file.h"
86
#include "param.h"
David Byers's avatar
David Byers committed
87
#include "kom-config.h"
David Byers's avatar
David Byers committed
88
#include "admin.h"
89
#include "unused.h"
90
#include "local-to-global.h"
91
#include "server-time.h"
92
#include "eintr.h"
93
#include "timeval-util.h"
94
#include "stats.h"
95
96
#include "services.h"
#include "manipulate.h"
Per Cederqvist's avatar
Per Cederqvist committed
97
98

/*
99
 * Possible improvements:
Per Cederqvist's avatar
Per Cederqvist committed
100
 *
101
102
103
 *  FIXME (bug 167): When there are consecutive items in file A that
 *  shall be copied to file B, copy them in one transfer (up to a
 *  certain limit).
Per Cederqvist's avatar
Per Cederqvist committed
104
105
106
107
108
109
110
 */

/*
 * All functions that can fail sets kom_errno to a suitable value
 * if they fail.
 */

David Byers's avatar
David Byers committed
111
static Small_conf      ** small_conf_arr;
112
113
static Cache_node_mcb	* pers_mcb;
static Cache_node_mcb 	* conf_mcb;
114
static Conf_no		  next_free_num = 1;
115
static Conf_no            existing_confs = 0;
Per Cederqvist's avatar
Per Cederqvist committed
116

117
static Cache_node_mcb 	* text_mcb;
118
static Text_no		  next_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
119
120
121
122
123

static int    no_of_match_info;
EXPORT Matching_info *match_table = NULL;

static FILE	*text_file= NULL;
124
125
static struct dbfile	*file_a = NULL;	/* Current file. */
static struct dbfile	*file_b = NULL;	/* File under construction. */
David Byers's avatar
David Byers committed
126
#ifdef FASTSAVE
127
static struct dbfile     *file_b_r = NULL; /* Read from file under construction */
David Byers's avatar
David Byers committed
128
#endif
Per Cederqvist's avatar
Per Cederqvist committed
129
130
131
132
133

/*
 * Four state variables for the background save.
 */
static enum {
134
    sync_idle,
Per Cederqvist's avatar
Per Cederqvist committed
135
136
137
138
139
140
141
142
    sync_save_conf,
    sync_save_pers,
    sync_save_text,
    sync_error,
    sync_wait,
    sync_ready
} sync_state;

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* The state machine sync_state works like this:
 *
 *  Old state              action           new state
 *  none               sync_part called     sync_idle
 *                     for the first time
 *  sync_idle	       SYNC_INTERVAL        sync_save_conf
 *  sync_save_conf     all confs saved      sync_save_pers
 *  sync_save_pers     all persons saved    sync_save_text
 *  sync_save_text     all texts saved      sync_ready
 *  sync_ready         sync_part called     sync_idle
 *  any                error occurs         sync_error
 *  sync_error         sync_part called     sync_wait
 *  sync_wait          SYNC_RETRY_INTERVAL  sync_save_conf
 */

David Byers's avatar
David Byers committed
158
#ifndef FASTSAVE
159
160
161
162
163
/* sync_next should be split in next_text_to_sync and
   next_conf_to_sync to avoid warnings about the different sizes of
   Conf_no and Text_no.  Since that is indeed done if FASTSAVE is set,
   don't bother doing it in the old code that will anyhow soon be
   replaced. */
164
static unsigned long sync_next;
David Byers's avatar
David Byers committed
165
#endif
Per Cederqvist's avatar
Per Cederqvist committed
166

167
168
static Conf_no highest_conf_no = 0;
static Text_no highest_text_no = 0;
Per Cederqvist's avatar
Per Cederqvist committed
169
170
171

BUGDECL;

172
173
174
175
176
177
178
#ifdef DEBUG_CALLS

/* This is used by cache_sync_start() and cache_sync_finish().  */
static int block_after_pre_sync = 0;

#endif

179
180
181
182
/* Define LOGACCESSES if you want to be able to log all accesses to
   the data base. */

#ifdef LOGACCESSES
183
enum log_type {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    lt_restart,
    lt_text_stat,
    lt_text_mass,
    lt_conf_stat,
    lt_pers_stat,
    
    lt_text_def,
    lt_conf_def,
    lt_pers_def,

    lt_create_text,
    lt_garb_text,
    lt_delete_text,
    lt_create_conf,
    lt_delete_conf,
    lt_create_pers,
    lt_delete_pers,

    lt_lock_conf,
    lt_unlock_conf,
    lt_lock_pers,
    lt_unlock_pers,

    lt_get_highest,		/* Get highest Local_text_no for a conf. */
    lt_get_conf_type
    /* Note: mark_*_as_changed is not logged. */
210
};
211
212
213
214
215

static FILE *logfile = NULL;
static int syncing_or_saving = 0;
static int garb_running = 0;

216
static void log_access(enum log_type t,
217
218
		       int id)
{
219
220
    extern int putw(int, FILE *);

221
222
223
224
225
226
227
228
229
230
231
    if (garb_running + syncing_or_saving == 0)
    {
	putc(t, logfile);
	putw(id, logfile);
    }
}
#define LOGACC(a,b) {if (logfile) log_access(a, b);}
#else
#define LOGACC(a,b)
#endif

232
233
234
235
static Static_server_info boottime_info = {
    0, 0, EMPTY_STRING_i, 0, 0, 0, 0, 0
};

David Byers's avatar
Server:    
David Byers committed
236
237
238
239
240
241
242
243
244

/* Local functions */

static Success build_matching_info(void);
static Success rebuild_matching_info_entry(Conf_no conf_no);
static Matching_info *find_matching_info(Conf_no conf_no);



Per Cederqvist's avatar
Per Cederqvist committed
245
246
247
248
249
250
251
252
253

/* Macros */

#define TRACE2(format, arg) if ( buglevel > 2 ) printf(format, arg)
#define TRACE1(format) if ( buglevel > 2 ) printf(format)
#define TRACESTR(str)  if ( buglevel > 2 ) s_puts(str)


static Person *
254
read_person(struct dbfile *fp,
Per Cederqvist's avatar
Per Cederqvist committed
255
	    long pos,
256
	    long size)
Per Cederqvist's avatar
Per Cederqvist committed
257
258
{
    Person *p;
259
260
    long pers_no;
    long new_pos;
Per Cederqvist's avatar
Per Cederqvist committed
261

262
263
264
    if (fparse_set_pos(fp, pos+1) != OK)	/* Skip 'P' */
	return NULL;

Per Cederqvist's avatar
Per Cederqvist committed
265
    p = alloc_person();
266
    pers_no = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
267
268
269
270
271
    if ( fparse_person(fp, p) != OK )
    {
	free_person(p);
	return NULL;
    }
272

273
    new_pos = dbfile_ftell(fp);
274
275
276
277
278
279
    if (new_pos < 0)
    {
	kom_log("ftell failed: %s.  Ignored.\n", strerror(errno));
    }
    else if (new_pos != pos + size)
    {
280
281
282
	restart_kom("Person %ld at %ld should be %ld bytes, "
		    "but %ld bytes was read\n",
		    pers_no, pos, size, new_pos - pos);
283
284
    }
    return p;
Per Cederqvist's avatar
Per Cederqvist committed
285
286
287
288
289
}



static Conference *
290
read_conference(struct dbfile *fp,
Per Cederqvist's avatar
Per Cederqvist committed
291
		long pos,
292
		long size)
Per Cederqvist's avatar
Per Cederqvist committed
293
294
{
    Conference *c;
295
296
    long conf_no;
    long new_pos;
Per Cederqvist's avatar
Per Cederqvist committed
297

298
299
300
    if (fparse_set_pos(fp, pos+1) != OK)	/* Skip 'C' */
	return NULL;

Per Cederqvist's avatar
Per Cederqvist committed
301
    c = alloc_conference();
302
    conf_no = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
303
304
305
306
307
    if ( fparse_conference(fp, c) != OK )
    {
	free_conference(c);
	return NULL;
    }
308

309
    new_pos = dbfile_ftell(fp);
310
311
312
313
314
315
    if (new_pos < 0)
    {
	kom_log("ftell failed: %s.  Ignored.\n", strerror(errno));
    }
    else if (new_pos != pos + size)
    {
316
317
318
	restart_kom("Conference %ld at %ld should be %ld bytes, "
		    "but %ld bytes was read\n",
		    conf_no, pos, size, new_pos - pos);
319
320
    }
    return c;
Per Cederqvist's avatar
Per Cederqvist committed
321
322
323
324
}
	

static Text_stat *
325
read_text_stat(struct dbfile *fp,
Per Cederqvist's avatar
Per Cederqvist committed
326
	       long pos,
327
	       long UNUSED(size)) /* FIXME (bug 170): sanity-check the size */
Per Cederqvist's avatar
Per Cederqvist committed
328
329
{
    Text_stat *t;
David Byers's avatar
David Byers committed
330
    long dummy;
Per Cederqvist's avatar
Per Cederqvist committed
331

332
333
334
    if (fparse_set_pos(fp, pos+1) != OK)	/* Skip 'T' */
	return NULL;

Per Cederqvist's avatar
Per Cederqvist committed
335
    t = alloc_text_stat();
David Byers's avatar
David Byers committed
336
    dummy = fparse_long(fp);
Per Cederqvist's avatar
Per Cederqvist committed
337
338
339
340
341
342
343
344
345
346
    if ( fparse_text_stat(fp, t) != OK )
    {
	free_text_stat(t);
	return NULL;
    }
    else
	return t;
}

static void
347
pers_set_mru(Pers_no pers_no)
Per Cederqvist's avatar
Per Cederqvist committed
348
{
349
    set_mru(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
350
351
352
}

static void
353
text_set_mru(Text_no text_no)
Per Cederqvist's avatar
Per Cederqvist committed
354
{
355
    set_mru(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
356
357
358
}

static void
359
conf_set_mru(Conf_no conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
360
{
361
362
    set_mru(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
363

364
365
366
static Cache_node *
get_pers_node(Pers_no pers_no)
{
367
    if (pers_no >= next_free_num || pers_no < 1)
368
369
	return NULL;

370
    return get_cache_node(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
371
372
373
}

static void
374
unlink_text_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
375
{
376
    unlink_lru (node, &text_mcb->lru, &text_mcb->mru);
Per Cederqvist's avatar
Per Cederqvist committed
377
378
}

379
380
381
382
383
static void
unlink_conf_lru (Cache_node *node)
{
    unlink_lru (node, &conf_mcb->lru, &conf_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
384
385

static void
386
unlink_pers_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
387
{
388
389
    unlink_lru (node, &pers_mcb->lru, &pers_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
390

391
392
393
static Cache_node *
get_conf_node(Conf_no conf_no)
{
394
    if (conf_no >= next_free_num || conf_no < 1)
395
396
	return NULL;

397
398
    return get_cache_node(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
399

400
401
402
static Cache_node *
get_text_node(Text_no text_no)
{
403
    if (text_no >= next_text_num || text_no < 1)
404
405
	return NULL;

406
    return get_cache_node(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
}

	
/*
 * Name caching routines
 */

/*
 * change_name changes the cached conference name. It is only called when
 * a conference name is changed or a conference is deleted.
 */
void
cached_change_name( Conf_no name_num,
		    String new_name )
{
    if ( name_num < 1 || name_num >= next_free_num )
423
	restart_kom("cached_change_name(%d, ----): next_free_num==%d\n",
Per Cederqvist's avatar
Per Cederqvist committed
424
425
426
427
		    name_num, next_free_num);

    s_clear( &small_conf_arr[name_num]->name );
    s_strcpy( &small_conf_arr[name_num]->name, new_name);
David Byers's avatar
Server:    
David Byers committed
428
    rebuild_matching_info_entry(name_num);
Per Cederqvist's avatar
Per Cederqvist committed
429
430
431
432
433
434
435
436
437
}


extern Conf_type
cached_get_conf_type (Conf_no conf_no)
{
    if ( conf_no < 1 || conf_no >= next_free_num )
	restart_kom("cached_get_conf_type(%d): next_free_num==%d",
		    conf_no, next_free_num);
438
439
440
441
442

    if ( small_conf_arr [ conf_no ] == NULL )
	restart_kom("cached_get_conf_type(%d): conference does not exist.\n",
		    conf_no);

443
444
    LOGACC(lt_get_conf_type, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
445
446
447
    return small_conf_arr [ conf_no ]->type;
}

448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
extern Conf_no
cached_get_conf_supervisor(Conf_no conf_no)
{
    if (conf_no < 1 || conf_no >= next_free_num)
	restart_kom("cached_get_conf_supervisor(%d): next_free_num==%d\n",
		    conf_no, next_free_num);

    if (small_conf_arr[conf_no] == NULL)
	restart_kom("cached_get_conf_supervisor(%d):"
		    " conference does not exist.\n", conf_no);

    LOGACC(lt_get_conf_supervisor, conf_no);

    return small_conf_arr[conf_no]->supervisor;
}

464
465
466
467
468
469
470
/*
 * Return number of conferences present.  (Actually, return a number
 * at least as large as the number of conferences present).
 */
extern Conf_no
cached_no_of_existing_conferences(void)
{
471
    return existing_confs;
472
}
Per Cederqvist's avatar
Per Cederqvist committed
473
474
475
476
477
478
479
480

/*
 * Various function calls to tell the cache that something is changed.
 */

void
mark_person_as_changed(Pers_no	pers_no)
{
481
482
483
484
    Cache_node *node;

    node = get_pers_node(pers_no);
    
Per Cederqvist's avatar
Per Cederqvist committed
485
    TRACE2("Person %d is changed\n", pers_no);
486
    if ( node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
487
488
	restart_kom("mark_person_as_changed(%d): nonexistent.\n", pers_no);

489
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
490
491
492
    pers_set_mru( pers_no );
}

493
494
495
496
497
498
499
500
501
502
503
/*
 * Mark the conference as dirty, so that it will be written to
 * the disk.
 *
 * Also update all fields in the Small_conf except then name, so that
 * they are always current.
 *
 * NOTE: You must call cached_change_name when the name changes.
 *       It is not necessary to call cached_change_name after
 *       cached_create_conf.
 */
Per Cederqvist's avatar
Per Cederqvist committed
504
505
506
507

void
mark_conference_as_changed(Conf_no	conf_no)
{
508
    Cache_node *node;
509
    Conference *conf_c;
510
511
512

    node = get_conf_node (conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
513
    TRACE2("Conf.  %d is changed\n", conf_no);
514
    if ( node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
515
516
	restart_kom("mark_conference_as_changed(%d): nonexistent.\n", conf_no);

517
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
518
    conf_set_mru( conf_no );
519
520
521

    conf_c = (Conference *) node->ptr;

522
    small_conf_arr[conf_no]->highest_local_no
523
	= l2g_first_appendable_key(&conf_c->texts) - 1;
524

525
    small_conf_arr[conf_no]->nice = conf_c->nice;
526
    small_conf_arr[conf_no]->keep_commented = conf_c->keep_commented;
527
528
    small_conf_arr[conf_no]->type = conf_c->type;
    small_conf_arr[conf_no]->supervisor = conf_c->supervisor;
Per Cederqvist's avatar
Per Cederqvist committed
529
530
531
532
533
}

void
mark_text_as_changed( Text_no text_no )
{
534
535
536
537
    Cache_node *node;

    node = get_text_node (text_no);

538
    TRACE2("Text %lu is changed.\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
539
    if ( text_no < 1 || text_no >= next_text_num
540
	|| node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
541
    {
542
	restart_kom("mark_text_as_changed(%lu): nonexistent.\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
543
544
    }

545
546
    node->s.dirty = 1;
    text_set_mru (text_no);
Per Cederqvist's avatar
Per Cederqvist committed
547
548
549
}    


550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
 * Store and retrieve the highest number used.
 */

static int
write_number_file(void)
{
    FILE *fp;

    if ((fp = i_fopen(param.numberfile_tmp_name, "w")) == NULL)
    {
	kom_log("opening %s: %s\n", param.numberfile_tmp_name,
		strerror(errno));
	return -1;
    }
    fprintf(fp, "Text_no: %lu Conf_no: %lu End.\n",
	    (unsigned long)next_text_num,
	    (unsigned long)next_free_num);
    if (fflush(fp) != 0)
    {
	kom_log("fflush to %s failed\n", param.numberfile_tmp_name);
	i_fclose(fp);
	return -1;
    }

    if (ferror(fp) != 0)
    {
	kom_log("fprintf to %s failed\n", param.numberfile_tmp_name);
	i_fclose(fp);
	return -1;
    }

    if (i_fclose(fp) != 0)
    {
	kom_log("fclose %s failed\n", param.numberfile_tmp_name);
	return -1;
    }

    if (i_rename(param.numberfile_tmp_name, param.numberfile_name) != 0)
    {
	kom_log("failed to rename %s to %s: %s\n",
		param.numberfile_tmp_name, param.numberfile_name,
		strerror(errno));
	return -1;
    }
    return 0;
}

static void
read_number_file(void)
{
    FILE *fp;
    unsigned long txt = 0;
    unsigned long cno = 0;
    Text_no text_no;
    Conf_no conf_no;

    if ((fp = i_fopen(param.numberfile_name, "r")) == NULL)
    {
	if (errno == ENOENT)
	    kom_log("WARN: %s: No such file\n", param.numberfile_name);
	else
	    restart_kom("opening %s: %s\n", param.numberfile_name,
			strerror(errno));
	return;
    }
    if (fscanf(fp, "Text_no: %lu Conf_no: %lu", &txt, &cno) != 2
	|| getc(fp) != ' ' || getc(fp) != 'E' || getc(fp) != 'n'
	|| getc(fp) != 'd' || getc(fp) != '.')
    {
	kom_log("WARN: %s: broken file (ignored)\n", param.numberfile_name);
	i_fclose(fp);
	return;
    }
    i_fclose(fp);

    text_no = txt;
    conf_no = cno;
    
    if (text_no > next_text_num)
    {
	kom_log("WARN: Texts %lu - %lu were lost.\n",
		(unsigned long)next_text_num,
		(unsigned long)text_no - 1);
	next_text_num = text_no;
    }
    if (conf_no > next_free_num)
    {
	kom_log("WARN: Confs %lu - %lu were lost.\n",
		(unsigned long)next_free_num,
		(unsigned long)conf_no - 1);
	next_free_num = conf_no;
    }
}
Per Cederqvist's avatar
Per Cederqvist committed
644
645
646
647
648
649
650
651
652

/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
653
654
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
655
656
657
658
659
660
661
662
    TRACE2("Person %d is being created.\n", person);

    if ( person < 1 || person >= next_free_num )
    {
	restart_kom("cached_create_person(%d): next_free_num == %d.\n",
		    person, next_free_num);
    }

663
    if ( get_pers_node(person) != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
664
665
666
667
668
    {
	restart_kom("cached_create_person(%d): Person existed.\n",
		    person);
    }

669
670
671
672
673
    create_cache_node (pers_mcb, person);
    node = get_pers_node (person);

    if ( node == NULL )
	restart_kom("cached_create_person(): couldn't get cache_node.\n");
674

675
676
677
    node->ptr = alloc_person();
    node->s.dirty = 1;
    node->s.exists = 1;
Per Cederqvist's avatar
Per Cederqvist committed
678
    pers_set_mru( person );
679
680
681

    LOGACC(lt_create_pers, person);

Per Cederqvist's avatar
Per Cederqvist committed
682
683
684
685
686
687
688
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
689
690
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
691
692
693
694
    TRACE2("cached_get_person_stat %d\n", person);

    if ( person == 0 )
    {
695
        err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
696
697
698
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
699

700
701
    if ( person >= next_free_num )
    {
702
        err_stat = person;
703
704
705
706
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

707
708
    node = get_pers_node (person);

709
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
710
    {
711
        err_stat = person;
Per Cederqvist's avatar
Per Cederqvist committed
712
713
714
715
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

716
717
    LOGACC(lt_pers_stat, person);

718
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
719
720
    {
	pers_set_mru( person );
721
	++pers_mcb->hits;
722
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
723
724
    }

725
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
726
    {
727
	node->ptr = copy_person (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
728
	pers_set_mru (person);
729
	++pers_mcb->hits;
730
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
731
732
    }

733
    node->ptr = read_person(file_a, node->pos, node->size);
Per Cederqvist's avatar
Per Cederqvist committed
734

735
    ++pers_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
736
    pers_set_mru (person);
737
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
738
739
740
741
742
743
}


/*
 * Conference-related calls
 */
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759


static int no_of_allocated_small_confs = 0;

static void
free_small_conf (Small_conf *sc)
{
    if ( sc != NULL )
    {
	--no_of_allocated_small_confs;
	s_clear ( &sc->name );
	sfree (sc);
    }
}


760
761
762
763
764
static void
init_small_conf(Small_conf *sc)
{
    sc->name = EMPTY_STRING;
    init_conf_type(&sc->type);
765
    sc->supervisor = 0;
766
767
    sc->highest_local_no = 0;
    sc->nice = param.default_nice;
768
    sc->keep_commented = param.default_keep_commented;
769
770
}

771
772
773
774
775
static Small_conf *
alloc_small_conf(void)
{
    Small_conf *s;
    s = smalloc(sizeof(Small_conf));
776
    init_small_conf(s);
777
778
779
780
781
782
    ++no_of_allocated_small_confs;

    return s;
}


783
784
785
786
787
788
/*
 * Create a conference.
 *
 * Set up a Conference and cache the name in the small_conf_array.
 */
extern Conf_no
Per Cederqvist's avatar
Per Cederqvist committed
789
790
791
792
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;
793
    Cache_node  *node;
Per Cederqvist's avatar
Per Cederqvist committed
794
795
796
797
798

    TRACE1("cached_create_conf( ");
    TRACESTR(name);
    TRACE1(" )\n");

David Byers's avatar
David Byers committed
799
    if ( next_free_num >= param.max_conf )
Per Cederqvist's avatar
Per Cederqvist committed
800
    {
801
        err_stat = next_free_num;
Per Cederqvist's avatar
Per Cederqvist committed
802
803
804
805
806
807
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return 0;
    }
    
    conf_no = next_free_num++;

808
809
810
811
812
813
814
815
    if (write_number_file() < 0)
    {
	next_free_num--;
	err_stat = 0;
	kom_errno = KOM_TEMPFAIL;
	return 0;
    }

816
817
    ++existing_confs;

818
819
820
821
822
823
824
825
826
    create_cache_node (conf_mcb, conf_no);
    node = get_conf_node (conf_no);

    if ( node == NULL )
	restart_kom("cached_create_conf(): failed to allocate cache_node.\n");
    
    node->s.exists = 1;
    node->s.dirty = 1;
    node->ptr = conf_c = alloc_conference();
Per Cederqvist's avatar
Per Cederqvist committed
827
828
    conf_set_mru(conf_no);    

829
    zero_init_cache_node (pers_mcb, conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
830
831
832
833
834
835
    small_conf_arr[ conf_no ] = alloc_small_conf();

    conf_c->name = EMPTY_STRING;
    s_strcpy(&conf_c->name, name);
    cached_change_name( conf_no, name);

836
    TRACE2("Created conference number %d\n", conf_no);
837
838
839

    LOGACC(lt_create_conf, conf_no);

Per Cederqvist's avatar
Per Cederqvist committed
840
841
842
843
844
845
846
    return conf_no;
}


extern Success
cached_delete_conf( Conf_no	conf )
{
847
848
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
849
850
    if ( conf == 0 )
    {
851
        err_stat = conf;
Per Cederqvist's avatar
Per Cederqvist committed
852
853
854
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
855

856
857
    if ( conf >= next_free_num )
    {
858
        err_stat = conf;
859
860
861
862
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

863
864
    node = get_conf_node (conf);

865
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
866
    {
867
        err_stat = conf;
Per Cederqvist's avatar
Per Cederqvist committed
868
869
870
871
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

872
    if ( node->lock_cnt > 0 )
David Byers's avatar
David Byers committed
873
	kom_log("WNG: cached_delete_conf(%d): lock_cnt === %d\n",
874
	    conf, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
875

David Byers's avatar
David Byers committed
876
877
878
    free_small_conf(small_conf_arr[conf]);
    small_conf_arr[conf] = NULL;

879
880
881
    free_conference(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
882

883
884
    --existing_confs;

885
886
    LOGACC(lt_delete_conf, conf);

David Byers's avatar
Server:    
David Byers committed
887
    rebuild_matching_info_entry(conf);
Per Cederqvist's avatar
Per Cederqvist committed
888
889
890
891
892
893
    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
894
895
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
896
897
    if ( pers == 0 )
    {
898
        err_stat = pers;
Per Cederqvist's avatar
Per Cederqvist committed
899
900
901
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
902

903
904
    if ( pers >= next_free_num )
    {
David Byers's avatar
David Byers committed
905
	kom_log("cached_delete_person(%lu): next_free_num == %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
906
	    (unsigned long)pers, (unsigned long)next_free_num);
907
        err_stat = pers;
908
909
910
911
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

912
913
914
    node = get_pers_node (pers);

    if ( pers >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
915
    {
David Byers's avatar
David Byers committed
916
	kom_log("cached_delete_person(): attempt to delete void person.\n");
917
        err_stat = pers;
Per Cederqvist's avatar
Per Cederqvist committed
918
919
920
921
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

922
    if ( node->lock_cnt > 0 )
David Byers's avatar
David Byers committed
923
	kom_log("cached_delete_pers(%lu): lock_cnt === %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
924
	    (unsigned long)pers, (unsigned long)node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
925

926
927
    LOGACC(lt_delete_pers, pers);

928
929
930
    free_person (node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
931
932
933
934
935
936
    return OK;
}

Success
cached_delete_text(Text_no text)
{
937
938
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
939
940
    if ( text == 0 )
    {
941
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
942
943
944
	kom_errno = KOM_TEXT_ZERO;
	return FAILURE;
    }
945
946
947
948
949

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
950
    {
David Byers's avatar
David Byers committed
951
	kom_log("cached_delete_text(): attempt to delete void text %lu.\n", text);
952
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
953
954
955
956
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

957
    if ( node->lock_cnt > 0 )
David Byers's avatar
David Byers committed
958
	kom_log("cached_delete_text(%lu): lock_cnt === %d\n",
959
	    text, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
960
961


962
963
964
    free_text_stat(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
965

966
967
968
#ifdef LOGACCESSES
    if (garb_running)
    {
969
	LOGACC(lt_garb_text, text);
970
971
    }
    else
972
	LOGACC(lt_delete_text, text);
973
#endif
Per Cederqvist's avatar
Per Cederqvist committed
974
975
976
977
978
    return OK;
}


extern Conference *
979
cached_get_conf_stat (Conf_no   conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
980
{
981
982
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
983
984
985
986
    TRACE2("cached_get_conf_stat %d\n", conf_no);

    if ( conf_no == 0 )
    {
987
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
988
989
990
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
991
992
993
994

    node = get_conf_node (conf_no);

    if ( conf_no >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
995
    {
996
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
997
998
999
1000
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }

1001
1002
    LOGACC(lt_conf_stat, conf_no);

1003
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1004
    {
1005
	conf_set_mru (conf_no);
1006
	++conf_mcb->hits;
1007
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1008
1009
    }

1010
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1011
    {
1012
	node->ptr = copy_conference (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
1013
	conf_set_mru (conf_no);
1014
	++conf_mcb->hits;
1015
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1016
1017
    }

1018
    node->ptr = read_conference(file_a, node->pos, node->size);
1019
    ++conf_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
1020
1021

    conf_set_mru (conf_no);
1022
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1023
1024
1025
1026
1027
1028
1029
1030
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
1031
1032
1033
    if (conf_no == 0 || conf_no >= next_free_num )
	return FALSE;

David Byers's avatar
David Byers committed
1034
    return small_conf_arr[conf_no] != NULL ? TRUE : FALSE;
Per Cederqvist's avatar
Per Cederqvist committed
1035
1036
1037
1038
1039
1040
1041
1042
}

    
/*
 * Calls to handle texts
 */

/*
1043
 * FIXME (bug 171): cached_get_text() should return Success.
Per Cederqvist's avatar
Per Cederqvist committed
1044
1045
1046
1047
1048
1049
1050
 */
extern String
cached_get_text( Text_no text )
{
    String  the_string;
    Text_stat *t_stat;

1051
    TRACE2("cached_get_text %lu\n", text);
Per Cederqvist's avatar
Per Cederqvist committed
1052
1053
1054

    if ( (t_stat = cached_get_text_stat (text)) == NULL )
	return EMPTY_STRING;
1055
    else if (t_stat->generation == 0)
Per Cederqvist's avatar
Per Cederqvist committed
1056
    {
1057
	LOGACC(lt_text_mass, text);
Per Cederqvist's avatar
Per Cederqvist committed
1058
1059
	the_string.string = tmp_alloc( t_stat->no_of_chars );
	the_string.len = t_stat->no_of_chars;
1060
1061
1062
1063
1064
1065
	if (fseek(text_file, t_stat->file_pos, SEEK_SET) != 0)
	{
	    kom_log("Failed to seek to %ld in text mass file: %s.\n",
		    t_stat->file_pos, strerror(errno));
	    return EMPTY_STRING;
	}
Per Cederqvist's avatar
Per Cederqvist committed
1066
1067

	if ( fread(the_string.string, sizeof(char), the_string.len, text_file)
David Byers's avatar
David Byers committed
1068
	    != (size_t)the_string.len )
Per Cederqvist's avatar
Per Cederqvist committed
1069
	{
David Byers's avatar
David Byers committed
1070
	    kom_log("WARNING: cached_get_text: premature end on text %lu\n",
Per Cederqvist's avatar
Per Cederqvist committed
1071
1072
1073
1074
1075
1076
		text);
	    return EMPTY_STRING;
	}
		    
	return the_string;
    }
1077
1078
1079
1080
1081
    else
    {
#warning Generation > 0 not yet handled
	restart_kom("Only generation 0 is handled.\n");
    }
Per Cederqvist's avatar
Per Cederqvist committed
1082
1083
1084
1085
1086
1087
}


extern Text_stat *	/* NULL on error */
cached_get_text_stat(	Text_no		text )
{
1088
1089
    Cache_node *node;

1090
    TRACE2("cached_get_text_stat(%lu);  next_text_num == ", text);
1091
    TRACE2("%lu\n", (unsigned long)next_text_num);
Per Cederqvist's avatar
Per Cederqvist committed
1092
1093
1094

    if ( text == 0 )
    {
1095
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
1096
1097
1098
	kom_errno = KOM_TEXT_ZERO;
	return NULL;
    }
1099
1100
1101
1102

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
1103
1104
    {
	TRACE1("cached_get_text_stat: no such text.\n");
1105
        err_stat = text;
Per Cederqvist's avatar
Per Cederqvist committed
1106
1107
1108
1109
	kom_errno = KOM_NO_SUCH_TEXT;
	return NULL;
    }

1110
1111
    LOGACC(lt_text_stat, text);

1112
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1113
1114
1115
    {
	TRACE1("Found in ptr.\n");
	text_set_mru( text );
1116
	++text_mcb->hits;
1117
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1118
1119
    }

1120
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
1121
1122
    {
	TRACE1("Found in snap_shot\n");
1123
	node->ptr = copy_text_stat(node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
1124
	text_set_mru (text);
1125
	++text_mcb->hits;
1126
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1127
1128
1129
    }

    TRACE1("Found in file A.\n");
1130
    node->ptr = read_text_stat(file_a, node->pos, node->size);
Per Cederqvist's avatar
Per Cederqvist committed
1131
1132

    text_set_mru (text);
1133
    ++text_mcb->misses;
1134
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
1135
1136
1137
1138
1139
1140
}



/*
 * The text is set up with an empty misc-field. The misc field is
1141
 * then initialized by create_text.
Per Cederqvist's avatar
Per Cederqvist committed
1142
1143
1144
 */

extern Text_no
1145
cached_create_text(const String message)
Per Cederqvist's avatar
Per Cederqvist committed
1146
1147
{
    Text_no tno;
1148
    Cache_node *node;
1149
    long file_pos;
Per Cederqvist's avatar
Per Cederqvist committed
1150
1151
1152

    tno = next_text_num++;

1153
#warning Generation > 0 not yet handled
1154
    TRACE2("cached_create_text (len=%lu)\n", message.len);
Per Cederqvist's avatar
Per Cederqvist committed
1155

David Byers's avatar
David Byers committed
1156
    if ( tno >= param.max_text )
Per Cederqvist's avatar
Per Cederqvist committed
1157
    {
1158
        err_stat = tno;
Per Cederqvist's avatar
Per Cederqvist committed
1159
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
David Byers's avatar
David Byers committed
1160
	next_text_num = param.max_text;
Per Cederqvist's avatar
Per Cederqvist committed
1161
1162
1163
1164
	
	return 0;
    }

1165
1166
1167
1168
1169
1170
1171