simple-cache.c 36.5 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * 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.
 */


/*
 * Possible improvements: +++
 *  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).
 *
 *  In pre_sync: compute size of, and allocate disk space for file B.
 */

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/file.h>

#include <kom-errno.h>
#include <kom-types.h>
#include "s-collat-tables.h"
#include "smalloc.h"
#include "cache.h"
#include <debug.h>
#include "lyskomd.h"
#include "parser.h"
#include "ram-parse.h"
#include "log.h"
#include "com.h"
#include "isc.h"
#include "ram-output.h"
#include "connections.h"
#include "prot-a-output.h"
#include "send-async.h"
#include "memory.h"
#include "exp.h"

#include "limits.h"
51
#include "cache-node.h"
Per Cederqvist's avatar
Per Cederqvist committed
52
53

static Small_conf	* small_conf_arr[ MAX_CONF ];
54
55
static Cache_node_mcb	* pers_mcb;
static Cache_node_mcb 	* conf_mcb;
Per Cederqvist's avatar
Per Cederqvist committed
56
57
static int		  next_free_num = 1;

58
59
static Cache_node_mcb 	* text_mcb;
static int		  next_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

/* Defined in ramkomd.c */
extern char datafilename[1024];
extern char backupfilename[1024];
extern char textfilename[1024];

/*
 * The elements in the following lists with same index refers to the same
 * conference.
 */
static int    no_of_match_info;
EXPORT Matching_info *match_table = NULL;
EXPORT Conf_no       *conf_table  = NULL;


static FILE	*text_file= NULL;
static FILE	*file_a = NULL;	/* Current file. */
static FILE	*file_b = NULL;	/* File under construction. */

/*
 * Four state variables for the background save.
 */
static enum {
    sync_save_conf,
    sync_save_pers,
    sync_save_text,
    sync_error,
    sync_wait,
    sync_ready
} sync_state;

static long sync_next;

static Conf_no highest_conf_no;
static Text_no highest_text_no;

BUGDECL;


/* 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 *
read_person(FILE *fp,
	    long pos,
	    long size)
{
    Person *p;

    p = alloc_person();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_person(fp, p) != OK )
    {
	free_person(p);
	return NULL;
    }
    else
	return p;
}



static Conference *
read_conference(FILE *fp,
		long pos,
		long size)
{
    Conference *c;

    c = alloc_conference();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_conference(fp, c) != OK )
    {
	free_conference(c);
	return NULL;
    }
    else
	return c;
}
	

static Text_stat *
read_text_stat(FILE *fp,
	       long pos,
	       long size)
{
    Text_stat *t;

    t = alloc_text_stat();
    fseek(fp, pos+1, SEEK_SET);	/* Skip '+' */
    if ( fparse_text_stat(fp, t) != OK )
    {
	free_text_stat(t);
	return NULL;
    }
    else
	return t;
}

static void
164
pers_set_mru(Pers_no pers_no)
Per Cederqvist's avatar
Per Cederqvist committed
165
{
166
    set_mru(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
167
168
169
}

static void
170
text_set_mru(Text_no text_no)
Per Cederqvist's avatar
Per Cederqvist committed
171
{
172
    set_mru(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
173
174
175
}

static void
176
conf_set_mru(Conf_no conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
177
{
178
179
    set_mru(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
180

181
182
183
184
static Cache_node *
get_pers_node(Pers_no pers_no)
{
    return get_cache_node(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
185
186
187
}

static void
188
unlink_text_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
189
{
190
    unlink_lru (node, &text_mcb->lru, &text_mcb->mru);
Per Cederqvist's avatar
Per Cederqvist committed
191
192
}

193
194
195
196
197
static void
unlink_conf_lru (Cache_node *node)
{
    unlink_lru (node, &conf_mcb->lru, &conf_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
198
199

static void
200
unlink_pers_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
201
{
202
203
    unlink_lru (node, &pers_mcb->lru, &pers_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
204

205
206
207
208
209
static Cache_node *
get_conf_node(Conf_no conf_no)
{
    return get_cache_node(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
210

211
212
213
214
static Cache_node *
get_text_node(Text_no text_no)
{
    return get_cache_node(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
}

	
/*
 * 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 )
	restart_kom("cached_change_name(%d, ----): next_free_num==%d",
		    name_num, next_free_num);

    s_clear( &small_conf_arr[name_num]->name );
    s_strcpy( &small_conf_arr[name_num]->name, new_name);
    build_matching_info();
}


extern void
cached_set_conf_type (Conf_no   conf_no,
		      Conf_type type)
{
    if ( conf_no < 1 || conf_no >= next_free_num )
	restart_kom("cached_set_conf_type(%d, ----): next_free_num==%d",
		    conf_no, next_free_num);

    small_conf_arr[ conf_no ]->type = type;
}


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);
258
259
260
261
262

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

Per Cederqvist's avatar
Per Cederqvist committed
263
264
265
266
267
268
269
270
271
272
273
274
    return small_conf_arr [ conf_no ]->type;
}



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

void
mark_person_as_changed(Pers_no	pers_no)
{
275
276
277
278
    Cache_node *node;

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

283
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
284
285
286
287
288
289
290
    pers_set_mru( pers_no );
}


void
mark_conference_as_changed(Conf_no	conf_no)
{
291
292
293
294
    Cache_node *node;

    node = get_conf_node (conf_no);

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

299
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
300
301
    conf_set_mru( conf_no );
    small_conf_arr[ conf_no ]->highest_local_no
302
303
	= (((Conference *)node->ptr)->texts.first_local_no - 1
	   + ((Conference *)node->ptr)->texts.no_of_texts );
Per Cederqvist's avatar
Per Cederqvist committed
304
305
306
307
308
}

void
mark_text_as_changed( Text_no text_no )
{
309
310
311
312
    Cache_node *node;

    node = get_text_node (text_no);

Per Cederqvist's avatar
Per Cederqvist committed
313
314
    TRACE2("Text %d is changed.\n", text_no);
    if ( text_no < 1 || text_no >= next_text_num
315
	|| node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
316
317
318
319
    {
	restart_kom("mark_text_as_changed(%d): nonexistent.\n", text_no);
    }

320
321
    node->s.dirty = 1;
    text_set_mru (text_no);
Per Cederqvist's avatar
Per Cederqvist committed
322
323
324
325
326
327
328
329
330
331
332
333
}    



/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
334
335
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
336
337
338
339
340
341
342
343
    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);
    }

344
    if ( get_pers_node(person) != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
345
346
347
348
349
    {
	restart_kom("cached_create_person(%d): Person existed.\n",
		    person);
    }

350
351
352
353
354
355
356
357
358
    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");
    
    node->ptr = alloc_person();
    node->s.dirty = 1;
    node->s.exists = 1;
Per Cederqvist's avatar
Per Cederqvist committed
359
360
361
362
363
364
365
366
    pers_set_mru( person );
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
367
368
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
369
370
371
372
373
374
375
    TRACE2("cached_get_person_stat %d\n", person);

    if ( person == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
376
377
378
379
380

    node = get_pers_node (person);

    if ( person >= next_free_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
381
382
383
384
385
    {
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

386
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
387
388
    {
	pers_set_mru( person );
389
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
390
391
    }

392
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
393
    {
394
	node->ptr = copy_person (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
395
	pers_set_mru (person);
396
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
397
398
    }

399
    node->ptr = read_person(file_a, node->pos, node->size);
Per Cederqvist's avatar
Per Cederqvist committed
400
401

    pers_set_mru (person);
402
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
403
404
405
406
407
408
409
410
411
412
413
}


/*
 * Conference-related calls
 */
extern Conf_no	/* Also cache the name */
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;
414
    Cache_node  *node;
Per Cederqvist's avatar
Per Cederqvist committed
415
416
417
418
419
420
421
422
423
424
425
426
427

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

    if ( next_free_num >= MAX_CONF )
    {
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return 0;
    }
    
    conf_no = next_free_num++;

428
429
430
431
432
433
434
435
436
    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
437
438
    conf_set_mru(conf_no);    

439
    zero_init_cache_node (pers_mcb, conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
440
441
442
443
444
445
    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);

446
    TRACE2("Created conference number %d\n", conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
447
448
449
450
451
452
453
454
    
    return conf_no;
}


extern Success
cached_delete_conf( Conf_no	conf )
{
455
456
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
457
458
459
460
461
    if ( conf == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
462
463
464
465

    node = get_conf_node (conf);

    if ( conf >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
466
467
468
469
470
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

471
472
473
    if ( node->lock_cnt > 0 )
	log("WNG: cached_delete_conf(%d): lock_cnt === %d\n",
	    conf, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
474

475
476
477
    free_conference(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
478
479
480
481
482
483
484

    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
485
486
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
487
488
489
490
491
    if ( pers == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
492
493
494
495

    node = get_pers_node (pers);

    if ( pers >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
496
497
498
499
500
501
502
    {
	log("cached_delete_person(): attempt to"
	    " delete non-existing person.\n");
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

503
    if ( node->lock_cnt > 0 )
Per Cederqvist's avatar
Per Cederqvist committed
504
	log("cached_delete_pers(%d): lock_cnt === %d\n",
505
	    pers, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
506

507
508
509
    free_person (node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
510
511
512
513
514
515
    return OK;
}

Success
cached_delete_text(Text_no text)
{
516
517
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
518
519
520
521
522
    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return FAILURE;
    }
523
524
525
526
527

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
528
529
530
531
532
533
534
    {
	log("cached_delete_text(): attempt to"
	    " delete non-existing text %d.\n", text);
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

535
    if ( node->lock_cnt > 0 )
Per Cederqvist's avatar
Per Cederqvist committed
536
	log("cached_delete_text(%d): lock_cnt === %d\n",
537
	    text, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
538
539


540
541
542
    free_text_stat(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
543
544
545
546
547
548

    return OK;
}


extern Conference *
549
cached_get_conf_stat (Conf_no   conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
550
{
551
552
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
553
554
555
556
557
558
559
    TRACE2("cached_get_conf_stat %d\n", conf_no);

    if ( conf_no == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
560
561
562
563

    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
564
565
566
567
568
    {
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }

569
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
570
    {
571
572
	conf_set_mru (conf_no);
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
573
574
    }

575
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
576
    {
577
	node->ptr = copy_conf (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
578
	conf_set_mru (conf_no);
579
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
580
581
    }

582
    node->ptr = read_conference(file_a, node->pos, node->size);
Per Cederqvist's avatar
Per Cederqvist committed
583
584

    conf_set_mru (conf_no);
585
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
586
587
588
589
590
591
592
593
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
594
595
596
597
598
599
600
601
    Cache_node *node;

    if (conf_no == 0 || conf_no >= next_free_num )
	return FALSE;

    node = get_conf_node (conf_no);

    return node != NULL && node->s.exists != 0;
Per Cederqvist's avatar
Per Cederqvist committed
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
644
}

    
/*
 * Calls to handle texts
 */

/*
 * +++ Should return Success.
 */
extern String
cached_get_text( Text_no text )
{
    String  the_string;
    Text_stat *t_stat;

    TRACE2("cached_get_text %d\n", text);

    if ( (t_stat = cached_get_text_stat (text)) == NULL )
	return EMPTY_STRING;
    else
    {
	the_string.string = tmp_alloc( t_stat->no_of_chars );
	the_string.len = t_stat->no_of_chars;
	fseek(text_file, t_stat->file_pos, SEEK_SET);

	if ( fread(the_string.string, sizeof(char), the_string.len, text_file)
	    != the_string.len )
	{
	    log("WARNING: cached_get_text: "
		"couldn't read enough characters of text %d\n",
		text);
	    return EMPTY_STRING;
	}
		    
	return the_string;
    }
}


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

Per Cederqvist's avatar
Per Cederqvist committed
647
648
649
650
651
652
653
654
    TRACE2("cached_get_text_stat(%d);  next_text_num == ", text);
    TRACE2("%d\n", next_text_num);

    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return NULL;
    }
655
656
657
658

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
659
660
661
662
663
664
    {
	TRACE1("cached_get_text_stat: no such text.\n");
	kom_errno = KOM_NO_SUCH_TEXT;
	return NULL;
    }

665
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
666
667
668
    {
	TRACE1("Found in ptr.\n");
	text_set_mru( text );
669
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
670
671
    }

672
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
673
674
    {
	TRACE1("Found in snap_shot\n");
675
	node->ptr = copy_text_stat(node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
676
	text_set_mru (text);
677
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
678
679
680
    }

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

    text_set_mru (text);
684
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
685
686
687
688
689
690
691
692
693
694
695
696
697
}



/*
 * The text is set up with an empty misc-field. The misc field is
 * than initialized by create_text.
 */

extern Text_no
cached_create_text( String message)
{
    Text_no tno;
698
    Cache_node *node;
Per Cederqvist's avatar
Per Cederqvist committed
699
700
701
702
703
704
705
706
707
708
709
710
711

    tno = next_text_num++;

    TRACE2("cached_create_text (len=%d)\n", message.len);

    if ( tno >= MAX_TEXT )
    {
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	next_text_num = MAX_TEXT;
	
	return 0;
    }

712
713
    create_cache_node(text_mcb, tno);
    node = get_text_node (tno);
Per Cederqvist's avatar
Per Cederqvist committed
714

715
716
717
718
719
720
721
722
723
724
725
    if ( node == NULL )
	restart_kom("cached_create_text(): could'nt create cache-node.\n");
    
    node->s.exists = 1;
    node->s.dirty = 1;
    node->ptr = alloc_text_stat();
    ((Text_stat *)node->ptr)->no_of_misc = 0;
    ((Text_stat *)node->ptr)->misc_items = NULL;
    ((Text_stat *)node->ptr)->no_of_marks = 0;
    ((Text_stat *)node->ptr)->no_of_lines = 0;
    ((Text_stat *)node->ptr)->no_of_chars = 0;
Per Cederqvist's avatar
Per Cederqvist committed
726
    fseek(text_file, 0, SEEK_END);
727
    ((Text_stat *)node->ptr)->file_pos = ftell(text_file);
Per Cederqvist's avatar
Per Cederqvist committed
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747

    text_set_mru( tno );

    if ( fwrite(message.string, sizeof(char), message.len, text_file)
	!= message.len ) {
	log("WARNING: cached_create_text: Couldn't write the text %d\n",
	    tno);
    }

    fflush(text_file);
    
    TRACE2("cached_create_text -> %d\n", tno);
    
    return tno;
}


Text_no
traverse_text(Text_no seed)
{
748
749
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
750
751
    seed++;
    
752
    while ( seed < next_text_num )
Per Cederqvist's avatar
Per Cederqvist committed
753
    {
754
755
756
757
	node = get_text_node (seed);
	if ( node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
758
759
760
761
762
763
764
765
766
	seed++;
    }
    
    return (seed >= next_text_num) ? 0 : seed ;
}

Pers_no
traverse_person(Pers_no seed)
{
767
768
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
769
770
    seed++;

771
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
772
    {
773
774
775
776
	node = get_pers_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
777
778
779
780
781
782
783
784
785
	seed++;
    }

    return (seed >= next_free_num) ? 0 : seed ;
}

Conf_no
traverse_conference(Conf_no seed)
{
786
787
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
788
789
    seed++;
    
790
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
791
    {
792
793
794
795
	node = get_conf_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
	seed++;
    }

    return (seed >= next_free_num) ? 0 : seed ;
}

extern Garb_nice
cached_get_garb_nice (Conf_no conf_no)
{
    return small_conf_arr [ conf_no ]->nice;
}


extern Local_text_no
cached_get_highest_local_no (Conf_no conf_no)
{
    return small_conf_arr[ conf_no ]->highest_local_no;
}

/* Lock a person struct in memory. Increase a referenc count. */
void
cached_lock_person(Pers_no pers_no)
{
819
820
821
822
823
    Cache_node *node;

    node = get_pers_node(pers_no);
    
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
824
825
	restart_kom("cached_lock_person(%d): nonexistent.\n", pers_no);

826
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
827
    {
828
	Person *pers_stat_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
829

830
	pers_stat_ptr = cached_get_person_stat( pers_no );
Per Cederqvist's avatar
Per Cederqvist committed
831

832
	if ( pers_stat_ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
833
834
835
	    restart_kom("cached_lock_person(%d): couldn't read in person.\n",
			pers_no);

836
837
838
839
	if ( pers_stat_ptr != node->ptr )
	    restart_kom("cached_lock_person(%d): pers_stat_ptr == %d, node"
			"->ptr == %d.\n", pers_no, pers_stat_ptr,
			node->ptr);
Per Cederqvist's avatar
Per Cederqvist committed
840
841
    }

842
    node->lock_cnt++;
Per Cederqvist's avatar
Per Cederqvist committed
843
844
845
846
847
848
}

/* Decrease reference count. If zero, unlock person. */
void
cached_unlock_person(Pers_no pers_no)
{
849
850
851
852
853
854
855
856
    Cache_node *node;

    node = get_pers_node (pers_no);

    if ( node == NULL )
	restart_kom("cached_unlock_person(): couldn't get cache-node.\n");

    if ( node->lock_cnt <= 0 )
Per Cederqvist's avatar
Per Cederqvist committed
857
858
    {
	log("cached_unlock_person(%d): lock_cnt == %d.\n",
859
	    pers_no, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
860

861
	node->lock_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
862
863
    }
    else
864
	node->lock_cnt--;
Per Cederqvist's avatar
Per Cederqvist committed
865
866
867
868
869
870
871
}


/* Lock a conf struct in memory. Increase a referenc count. */
void
cached_lock_conf(Conf_no conf_no)
{
872
873
874
875
876
877
878
879
    Cache_node *node;

    node = get_conf_node(conf_no);

    if ( node == NULL)
	restart_kom("cached_lock_conf(): can't get cache-node.\n");

    if ( node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
880
881
	restart_kom("cached_lock_conf(%d): nonexistent.\n", conf_no);

882
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
883
    {
884
	Conference *conference_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
885

886
	conference_ptr = cached_get_conf_stat( conf_no );
Per Cederqvist's avatar
Per Cederqvist committed
887

888
	if ( conference_ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
889
890
891
	    restart_kom("cached_lock_conf(%d): couldn't read in conf.\n",
			conf_no);

892
893
894
895
	if ( conference_ptr != node->ptr )
	    restart_kom("cached_lock_conf(%d): conference_ptr == %d, node"
			"->ptr == %d.\n", conf_no, conference_ptr,
			node->ptr);
Per Cederqvist's avatar
Per Cederqvist committed
896
897
    }

898
    node->lock_cnt++;
Per Cederqvist's avatar
Per Cederqvist committed
899
900
901
902
903
904
}

/* Decrease reference count. If zero, unlock conf. */
void
cached_unlock_conf(Conf_no conf_no)
{
905
906
907
908
909
910
911
    Cache_node *node;

    node = get_conf_node(conf_no);
    if ( node == NULL )
	restart_kom("cached_unlock_conf(): can't get node.\n");

    if ( node->lock_cnt <= 0 )
Per Cederqvist's avatar
Per Cederqvist committed
912
913
    {
	log("cached_unlock_conf(%d): lock_cnt == %d.\n",
914
	    conf_no, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
915

916
	node->lock_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
917
918
    }
    else
919
	node->lock_cnt--;
Per Cederqvist's avatar
Per Cederqvist committed
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
}



/*
 * And here comes some functions to deal with lookup_names.
 */

/* Free the _contents_ of a match_table. The table itself i _not_ freed. */

static void
free_match_table(Matching_info *match)
{
    if ( match == NULL )
	return;
    
    while ( ! s_empty( match->name ))
    {
	free_tokens( match->tokens );
	/* match->name is not freed since it points into conf_arr[]. */

	++match;
    }
}    

extern Success
build_matching_info(void)
{
    Conf_no i;
    Matching_info *match;
    Conf_no	  *conf;

    free_match_table(match_table);
    
    match_table = srealloc(match_table, next_free_num * sizeof(Matching_info));
    conf_table = srealloc(conf_table, next_free_num * sizeof(Conf_no));

    no_of_match_info = 0;

    match = match_table;
    conf = conf_table;
    
    for ( i = 1; i < next_free_num; i++ )
    {
	if ( small_conf_arr[ i ] != NULL
	    && ! s_empty ( small_conf_arr[ i ]->name ) )
	{
	    match->name = small_conf_arr[ i ]->name;
	    match->tokens = tokenize(match->name, s_fcrea_str(" \t"));
	    match->priority = 7;
	    *conf = i;
	    ++match;
	    ++conf;
	    ++no_of_match_info;
	}
    }

    match->name = EMPTY_STRING;
    match->tokens = NULL;
    match->priority = 0;
    *conf = 0;
    
    return OK;
}



/* Map conference name to number */
extern Success
cached_lookup_name(const String name,
		   Conf_list_old *result)
{
    Parse_info tmp;
    int i;
    
    tmp = parse(name, match_table, FALSE, FALSE,
		s_fcrea_str(" \t"), DEFAULT_COLLAT_TAB);

    if ( tmp.no_of_matches == -1 )
	return FAILURE;
    
    if ( tmp.no_of_matches == 1 && tmp.indexes[ 0 ] == -1 )
    {
	/* Return the entire list. */
	result->no_of_conf_nos = no_of_match_info;
	result->conf_nos = tmp_alloc(no_of_match_info * sizeof(Conf_no));
	result->type_of_conf = tmp_alloc(no_of_match_info * sizeof(Conf_type));

	for ( i = 0; i < no_of_match_info; i++ )
	{
	    result->conf_nos[ i ] = conf_table[ i ];
	    result->type_of_conf[ i ] = small_conf_arr[ conf_table[i] ]->type;
	}
    }
    else
    {
	/* Return the conferences whose conf_nos are in indexes[]. */
	result->no_of_conf_nos = tmp.no_of_matches;
	result->conf_nos = tmp_alloc(tmp.no_of_matches * sizeof(Conf_no));
	result->type_of_conf = tmp_alloc(tmp.no_of_matches
					 * sizeof(Conf_type));

	for ( i = 0; i < tmp.no_of_matches; i++ )
	{
	    result->conf_nos[ i ] = conf_table[ tmp.indexes[ i ] ];
	    result->type_of_conf[ i ]
		= small_conf_arr[ conf_table[ tmp.indexes[ i ] ] ]->type;
	}
    }		
    sfree(tmp.indexes);
    return OK;
}


static Bool
is_clean(const char *fn)
{
    FILE *fp;

    if ( (fp = fopen(fn, "rb")) == NULL )
	return FALSE;

    if ( getc(fp) == 'C' && getc(fp) == 'L' && getc(fp) == 'E'
	&& getc(fp) == 'A' && getc(fp) == 'N' )
    {
	fclose(fp);
	return TRUE;
    }
    else
    {
	fclose(fp);
	return FALSE;
    }
}


extern void
pre_sync(void)
{
    long i;
    Cache_node *node;

    async_sync_db();

    /* Mark up what to save.*/

    BUG(("Sync starting\n"));

    highest_text_no = next_text_num;
    highest_conf_no = next_free_num;

    for ( i = 1; i < highest_conf_no; i++ )
    {
1073
	node = get_conf_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084

	if ( node != NULL )
	{
	    node->s.exists_b = node->s.exists;

	    if ( node->s.dirty != 0 && node->s.exists != 0)
	    {
		free_conference(node->snap_shot);

		if ( node->lock_cnt == 0 )
		{
1085
		    unlink_conf_lru(node);
Per Cederqvist's avatar
Per Cederqvist committed
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
		    node->snap_shot = node->ptr;
		    node->ptr = NULL;
		}
		else
		{
		    node->snap_shot = copy_conf(node->ptr);
		}
		
		node->s.dirty = 0;
	    }
	}
    }

    for ( i = 1; i < highest_conf_no; i++ )
    {
1101
	node = get_pers_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112

	if ( node != NULL )
	{
	    node->s.exists_b = node->s.exists;

	    if ( node->s.dirty != 0 && node->s.exists != 0)
	    {
		free_person(node->snap_shot);

		if ( node->lock_cnt == 0 )
		{
1113
		    unlink_pers_lru(node);
Per Cederqvist's avatar
Per Cederqvist committed
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
		    node->snap_shot = node->ptr;
		    node->ptr = NULL;
		}
		else
		{
		    node->snap_shot
			= copy_person(node->ptr);
		}
		
		node->s.dirty = 0;
	    }
	}
    }

    for ( i = 1; i < highest_text_no; i++ )
    {
1130
	node = get_text_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141

	if ( node != NULL )
	{
	    node->s.exists_b = node->s.exists;

	    if ( node->s.dirty != 0 && node->s.exists != 0)
	    {
		free_text_stat(node->snap_shot);

		if ( node->lock_cnt == 0 )
		{
1142
		    unlink_text_lru(node);
Per Cederqvist's avatar
Per Cederqvist committed
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
		    node->snap_shot = node->ptr;
		    node->ptr = NULL;
		}
		else
		{
		    node->snap_shot
			= copy_text_stat(node->ptr);
		}
		
		node->s.dirty = 0;
	    }
	}
    }

    /* All marking is done. Now open file B. */
    
    if ( is_clean(datafilename) )
    {
	if ( rename(datafilename, backupfilename) != 0 )
	    restart_kom("pre_sync: can't backup.\n");
    }
    else
	log("pre_sync: datafile not clean. No backup taken.\n");
1166
1167
1168
1169
1170
1171

    if ( file_b != NULL )
    {
	log("pre_sync: Save in progress aborted.\n");
	fclose(file_b);
    }
Per Cederqvist's avatar
Per Cederqvist committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201

    if ( (file_b=fopen(datafilename, "wb") ) == NULL )
    {
	log("WARNING: pre_sync: can't open file to save in.\n");
	sync_state = sync_wait;

	return;
    }

    fprintf(file_b, "DIRTY\n");		 /* DIRTY-FLAG */

    fprintf(file_b, "%d\n", highest_conf_no);	  /* NEXT_FREE_NUM */
    sync_state = sync_save_conf;
    sync_next = 1;
}

static void
copy_file(FILE *from,
	  FILE *to,
	  long from_pos,
	  long len)
{
    char *buf;
    long result;

    buf = smalloc(len);
    if ( fseek(from, from_pos, SEEK_SET) == -1 )
    {
	sync_state = sync_error;
	log("sync: copy_file(): fseek failed.\n");
1202
	sfree(buf);
Per Cederqvist's avatar
Per Cederqvist committed
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
	return;
    }
    
    if ( (result = fread(buf, 1, len, from)) != len )
    {
	log("sync: copy_file(): fread failed.\n"
	    "from_pos = %d, len = %d, result = %d\n",
	    from_pos, len, result);

	sync_state = sync_error;
1213
	sfree(buf);
Per Cederqvist's avatar
Per Cederqvist committed
1214
1215
1216
1217
1218
1219
1220
	return;
    }

    if ( fseek(to, 0, SEEK_END) == -1 )
    {
	sync_state = sync_error;
	log("sync: copy_file(): second fseek failed.\n");
1221
	sfree(buf);
Per Cederqvist's avatar
Per Cederqvist committed
1222
1223
1224
1225
1226
1227
1228
	return;
    }

    if ( fwrite(buf, 1, len, to) != len )
    {
	sync_state = sync_error;
	log("sync: copy_file(): fwrite failed.\n");
1229
	sfree(buf);
Per Cederqvist's avatar
Per Cederqvist committed
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
	return;
    }

    sfree(buf);
}    

static void
save_one_conf(void)
{
    Cache_node *cn;
    
    if (sync_next < highest_conf_no)
    {
1243
	cn = get_conf_node (sync_next);
Per Cederqvist's avatar
Per Cederqvist committed
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298

	if ( cn == NULL )
	{
	    putc('@', file_b);
	    putc('\n', file_b);
	}
	else 
	{
	    cn->pos_b = ftell(file_b);

	    if ( cn->s.exists_b == 0 )
		putc('@', file_b);
	    else if ( cn->snap_shot != NULL )
	    {
		fprintf(file_b, "+ ");
		foutput_conference(file_b, cn->snap_shot);
	    }
	    else if ( cn->s.dirty == 0 && cn->ptr != NULL )
	    {
		fprintf(file_b, "+ ");
		foutput_conference(file_b, cn->ptr);
	    }
	    else
	    {
		copy_file(file_a, file_b, cn->pos, cn->size - 1);
	    }

	    putc('\n', file_b);
	    cn->size_b = ftell(file_b) - cn->pos_b;
	}
	sync_next++;
    }
    else			/* All conferences are written. */
    {
	sync_next = 1;
	sync_state = sync_save_pers;
    }
}

static void
write_pers(FILE *fp,
	   Person *p)
{
    fprintf(fp, "+ %dH", PASSWD_LEN);
    fwrite(p->pwd, PASSWD_LEN, 1, fp);
    foutput_person(fp, p);
}

static void
save_one_pers(void)
{
    Cache_node *cn;
    
    if (sync_next < highest_conf_no)
    {
1299
	cn = get_pers_node (sync_next);
Per Cederqvist's avatar
Per Cederqvist committed
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360

	if ( cn == NULL )
	{
	    putc('@', file_b);
	    putc('\n', file_b);
	}
	else 
	{
	    cn->pos_b = ftell(file_b);

	    if ( cn->s.exists_b == 0 )
		putc('@', file_b);
	    else if ( cn->snap_shot != NULL )
	    {
		write_pers(file_b, cn->snap_shot);
	    }
	    else if ( cn->s.dirty == 0 && cn->ptr != NULL )
	    {
		write_pers(file_b, cn->ptr);
	    }
	    else
	    {
		copy_file(file_a, file_b, cn->pos, cn->size - 1);
	    }

	    putc('\n', file_b);
	    cn->size_b = ftell(file_b) - cn->pos_b;
	}
	sync_next++;
    }
    else			/* All persons are written. */
    {
	fprintf(file_b, "%d\n", highest_text_no);	/* NEXT_TEXT_NUM */
	sync_next = 1;
	sync_state = sync_save_text;
    }
}

static void
post_sync(void)
{
    int i;
    Cache_node *node;

    async_sync_db();

    if ( file_a == NULL )
	log("WARNING: post_sync(): file_a == NULL. This is only normal "
	    "if this is the first sync ever on this data file.\n");
    else
	fclose(file_a);

    if ( ( file_a = fopen(datafilename, "rb") ) == NULL )
    {
	log("post_sync: can't open the file I just saved.\n");
	sync_state = sync_wait;
	return;
    }

    for ( i = 1; i < highest_conf_no; i++ )
    {
1361
	node = get_conf_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1362
1363
1364
1365
1366
1367
	if ( node != NULL )
	{
	    node->pos = node->pos_b;
	    node->size = node->size_b;
	    free_conference(node->snap_shot);
	    node->snap_shot = NULL;
1368
	    /* +++ Delete if it no longer exists. */
Per Cederqvist's avatar
Per Cederqvist committed
1369
1370
1371
1372
1373
	}
    }

    for ( i = 1; i < highest_conf_no; i++ )
    {
1374
	node = get_pers_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1375
1376
1377
1378
1379
1380
	if ( node != NULL )
	{
	    node->pos = node->pos_b;
	    node->size = node->size_b;
	    free_person(node->snap_shot);
	    node->snap_shot = NULL;
1381
	    /* +++ Delete if it no longer exists. */
Per Cederqvist's avatar
Per Cederqvist committed
1382
1383
1384
1385
1386
	}
    }

    for ( i = 1; i < highest_text_no; i++ )
    {
1387
	node = get_text_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1388
1389
1390
1391
1392
1393
	if ( node != NULL )
	{
	    node->pos = node->pos_b;
	    node->size = node->size_b;
	    free_text_stat(node->snap_shot);
	    node->snap_shot = NULL;
1394
	    /* +++ Delete if it no longer exists. */
Per Cederqvist's avatar
Per Cederqvist committed
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
	}
    }
}

    

static void
save_one_text(void)
{
    Cache_node *cn;
    
    if (sync_next < highest_text_no)
    {
1408
	cn = get_text_node(sync_next);
Per Cederqvist's avatar
Per Cederqvist committed
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444

	if ( cn == NULL )
	{
	    putc('@', file_b);
	    putc('\n', file_b);
	}
	else 
	{
	    cn->pos_b = ftell(file_b);

	    if ( cn->s.exists_b == 0 )
		putc('@', file_b);
	    else if ( cn->snap_shot != NULL )
	    {
		fprintf(file_b, "+ ");
		foutput_text_stat(file_b, cn->snap_shot);
	    }
	    else if ( cn->s.dirty == 0 && cn->ptr != NULL )
	    {
		fprintf(file_b, "+ ");
		foutput_text_stat(file_b, cn->ptr);
	    }
	    else
	    {
		copy_file(file_a, file_b, cn->pos, cn->size - 1);
	    }

	    putc('\n', file_b);
	    cn->size_b = ftell(file_b) - cn->pos_b;
	}
	sync_next++;
    }
    else			/* All texts are written. */
    {
	if ( ferror(file_b) != 0 )
	{
1445
	    log ("save_one_text(): ferror() detected.\n");
Per Cederqvist's avatar
Per Cederqvist committed
1446
1447
1448
1449
1450
1451
1452
	    sync_state = sync_error;
	    return;
	}
	
	rewind(file_b);
	if ( ferror(file_b)  != 0 )
	{
1453
	    log ("save_one_text(): rewind failed.\n");
Per Cederqvist's avatar
Per Cederqvist committed
1454
1455
1456
1457
1458
1459
1460
	    sync_state = sync_error;
	    return;
	}
	
	fprintf(file_b, "CLEAN");
	if ( ferror(file_b)  != 0 )
	{
1461
	    log ("save_one_text(): fprintf(CLEAN) failed.\n");
Per Cederqvist's avatar
Per Cederqvist committed
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
	    sync_state = sync_error;
	    return;
	}
	
	if ( fclose(file_b) != 0 )
	{
	    file_b = NULL;

	    log("Sync: fclose() failed in save_one_text. Deleting "
		"file and retrying.\n");
	    remove(datafilename);
	    sync_state = sync_wait;
	    return;
	}

1477
1478
	file_b = NULL;

Per Cederqvist's avatar
Per Cederqvist committed
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
	sync_state = sync_ready;

	BUG(("Sync ready\n"));
	post_sync();
    }
}

/*
 * Sync_part() should be called often as long as it returns FALSE.
 * Sync_part() returns TRUE when everything is written to a file.
 * Sync_part() should be called once in a while even when it returns
 * TRUE (because sync() checks to see if it is time to start yet
 * another save.)
 */
EXPORT Bool
sync_part(void)
{
    static time_t last_sync_start = NO_TIME;

    if ( last_sync_start == NO_TIME )
    {
	last_sync_start = time(NULL);
	sync_state = sync_ready;
    }

    switch(sync_state)
    {
    case sync_save_conf:
	save_one_conf();
	break;

    case sync_save_pers:
	save_one_pers();
	break;

    case sync_save_text:
	save_one_text();
	break;

    case sync_ready:
	if ( difftime(time(NULL), last_sync_start) < 60 * SYNC_INTERVAL )
	    return TRUE;

	last_sync_start = time(NULL);
    
	pre_sync();
	break;

    case sync_wait:
	if ( difftime(time(NULL), last_sync_start) < 60 * SYNC_RETRY_INTERVAL )
	    return TRUE;

	last_sync_start = time(NULL);

	pre_sync();
	break;

    case sync_error:
	log("sync: Error saving new file. Discarding file and "
	    "trying again.\n");
	fclose(file_b);
	file_b = NULL;
	remove(datafilename);
	sync_state = sync_wait;
	break;

    default:
	restart_kom("sync(): sync_state==%d", sync_state);
    }

1549
    if ( file_b != NULL && ferror(file_b) != 0)
Per Cederqvist's avatar
Per Cederqvist committed
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
	sync_state = sync_error;

    return FALSE;
}

static Small_conf *
alloc_small_conf(void)
{
    Small_conf *s;
    s = smalloc(sizeof(Small_conf));
    *s = EMPTY_SMALL_CONF;

    return s;
}

static void
setup_small_conf(Conf_no conf_no,
		 Conference *conf_c)
{
    small_conf_arr[ conf_no ] = alloc_small_conf();
    s_strcpy(&small_conf_arr[ conf_no ]->name, conf_c->name);
    small_conf_arr[ conf_no ]->type = conf_c->type;
    small_conf_arr[ conf_no ]->highest_local_no
	= (conf_c->texts.first_local_no - 1
	   + conf_c->texts.no_of_texts );
    small_conf_arr[ conf_no ]->nice = conf_c->nice;
}


    
extern Success
init_cache(void)
{
    int i;
    Cache_node *node;
    Conference tmp_conf = EMPTY_CONFERENCE;
    Person tmp_pers = EMPTY_PERSON;
    Text_stat tmp_text = EMPTY_TEXT_STAT;

1589
1590
1591
1592
1593
1594
    pers_mcb = create_cache_node_mcb(100, MAX_CONF);
    conf_mcb = create_cache_node_mcb(100, MAX_CONF);
    text_mcb = create_cache_node_mcb(100, MAX_TEXT);



Per Cederqvist's avatar
Per Cederqvist committed
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
    if ( (text_file = fopen(textfilename, "a+b")) == NULL )
    {
	restart_kom("ERROR: init_cache: can't open text file.\n");
    }

    if ( is_clean(datafilename) )
    {
	if ( (file_a = fopen(datafilename, "rb")) == NULL )
	{
	    log("WARNING: init_cache: can't open datafile.\n");
	    return FAILURE;
	}
	log("MSG: init_cache: using datafile.\n");
    }
    else if ( is_clean(backupfilename) )
    {
	if ( (file_a = fopen(backupfilename, "rb")) == NULL )
	{
	    log("WARNING: init_cache: can't open backupfile.\n");
	    return FAILURE;
	}
	log("MSG: init_cache: using backup file.\n");
    }
    else
    {
	log("WARNING: init_cache: can't find old data base.\n");
	return FAILURE;
    }
        
    fseek(file_a, 6, SEEK_SET);	/* skip clean/dirty flag. */

    next_free_num = fparse_long(file_a);	  /* NEXT_FREE_NUM */

    for ( i = 1; i < next_free_num; i++ ) /* CONFS */
    {
	fskipwhite(file_a);
	switch(getc(file_a))
	{
	case '@':
1634
	    zero_init_cache_node(conf_mcb, i);
Per Cederqvist's avatar
Per Cederqvist committed
1635
1636
1637
	    break;

	case '+':
1638
1639
	    create_cache_node(conf_mcb, i);
	    node = get_conf_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
	    node->s.exists = 1;

	    node->pos = ftell(file_a) - 1; /* Don't forget the '+' */

	    if ( fparse_conference(file_a, &tmp_conf) != OK )
		restart_kom("init_cache(): fparse_conference failed. "
			    "i == %d\n", i);

	    node->size = ftell(file_a) - node->pos;
	    setup_small_conf(i, &tmp_conf);
	    clear_conference(&tmp_conf);
	    
	    break;
	}
    }

    build_matching_info();
    
    for ( i = 1; i < next_free_num; i++ ) /* PERSONS */
    {
	fskipwhite(file_a);
	switch(getc(file_a))
	{
	case '@':
1664
	    zero_init_cache_node(pers_mcb, i);
Per Cederqvist's avatar
Per Cederqvist committed
1665
1666
1667
	    break;

	case '+':
1668
1669
	    create_cache_node(pers_mcb, i);
	    node = get_pers_node(i); 
Per Cederqvist's avatar
Per Cederqvist committed
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
	    node->s.exists = 1;
	    node->pos = ftell(file_a) - 1; /* Don't forget the '+' */
	    if ( fparse_person(file_a, &tmp_pers) != OK )
		restart_kom("init_cache: fparse_person failed. i == %d\n", i);
	    
	    node->size = ftell(file_a) - node->pos;
	    clear_person(&tmp_pers);

	    break;
	}
    }

    next_text_num = fparse_long(file_a);	/* NEXT_TEXT_NUM */

    for ( i = 1; i < next_text_num; i++ ) /* TEXT_STATS */
    {
	fskipwhite(file_a);
	switch(getc(file_a))
	{
	case '@':
1690
	    zero_init_cache_node(text_mcb, i);
Per Cederqvist's avatar
Per Cederqvist committed
1691
1692
1693
	    break;

	case '+':
1694
1695
	    create_cache_node(text_mcb, i);
	    node = get_text_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
	    node->s.exists = 1;

	    node->pos = ftell(file_a) - 1; /* Don't forget the '+' */

	    if ( fparse_text_stat(file_a, &tmp_text) != OK )
		restart_kom("init_cache(): fparse_text_stat failed. i == %d\n",
			    i);

	    node->size = ftell(file_a) - node->pos;
	    clear_text_stat(&tmp_text);

	    break;
	}
    }

    log("Read %d confs/persons and %d texts\n",
	next_free_num, next_text_num);

    return OK;
}

/* ramkomd compatibility: */
extern void
cache_sync(void)
{
}

extern void
cache_sync_all(void)
{
    pre_sync();
    while ( sync_part() != TRUE )
	;
}
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931

void
free_all_cache (void)
{
    int i;
    Cache_node *node;

    for ( i = 0; i < next_free_num; i++ )
    {
	node = get_conf_node(i);
	if ( node != NULL )
	{
	    if ( node->snap_shot != NULL )
	    {
		free_conference (node->snap_shot);
		node->snap_shot = NULL;
	    }

	    if ( node->ptr != NULL )
	    {
		free_conference (node->ptr);
		node->ptr = NULL;
	    }
	}

	destruct_cache_node (conf_mcb, i);

	node = get_pers_node(i);
	if ( node != NULL )
	{
	    if ( node->snap_shot != NULL )
	    {
		free_person (node->snap_shot);
		node->snap_shot = NULL;
	    }

	    if ( node->ptr != NULL )
	    {
		free_person (node->ptr);
		node->ptr = NULL;
	    }
	}

	destruct_cache_node (pers_mcb, i);

	s_clear ( &small_conf_arr[i]->name );
    }

    for ( i = 0; i < next_text_num; i++ )
    {
	node = get_text_node(i);
	if ( node != NULL )
	{
	    if ( node->snap_shot != NULL )
	    {
		free_text_stat (node->snap_shot);
		node->snap_shot = NULL;
	    }

	    if ( node->ptr != NULL )
	    {
		free_text_stat (node->ptr);
		node->ptr = NULL;
	    }
	}

	destruct_cache_node (text_mcb, i);
    }

    free_match_table(match_table);

    sfree (match_table);
    sfree (conf_table);
}


static void
limit_pers()
{
    Cache_node *node;
    int i;
    
    node = pers_mcb->mru;

    /* Skip first CACHE_PERSONS clean persons. */
    for ( i = 0; node != NULL && i < CACHE_PERSONS; i++ )
    {
	while ( node != NULL && node->s.dirty == 0
	       && node->snap_shot == NULL && node->lock_cnt == 0 )
	{
	    node = node->next;
	}