simple-cache.c 41 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: simple-cache.c,v 0.27 1992/11/02 18:46:31 ceder Exp $
Linus Tolke Y's avatar
Linus Tolke Y committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (C) 1991  Lysator Academic Computer Association.
 *
 * 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 31 32 33 34 35
/*
 * 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.
 */

36
static char *rcsid = "$Id: simple-cache.c,v 0.27 1992/11/02 18:46:31 ceder Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
37 38


Per Cederqvist's avatar
Per Cederqvist committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

/*
 * 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>
Per Cederqvist's avatar
Per Cederqvist committed
57 58
#include <unistd.h>
#include <errno.h>
Per Cederqvist's avatar
Per Cederqvist committed
59 60 61

#include <kom-errno.h>
#include <kom-types.h>
Per Cederqvist's avatar
Per Cederqvist committed
62
#include "s-collat-tabs.h"
63
#include <server/smalloc.h>
Per Cederqvist's avatar
Per Cederqvist committed
64
#include <parser.h>
Per Cederqvist's avatar
Per Cederqvist committed
65 66 67 68 69 70
#include "cache.h"
#include <debug.h>
#include "lyskomd.h"
#include "ram-parse.h"
#include "log.h"
#include "com.h"
71
#include "isc-interface.h"
Per Cederqvist's avatar
Per Cederqvist committed
72 73 74 75 76 77 78
#include "ram-output.h"
#include "connections.h"
#include "prot-a-output.h"
#include "send-async.h"
#include "memory.h"
#include "exp.h"

79
#include "tmp-limits.h"
80
#include "cache-node.h"
Per Cederqvist's avatar
Per Cederqvist committed
81
#include "disk-cache.h"
Per Cederqvist's avatar
Per Cederqvist committed
82
static Small_conf	* small_conf_arr[ MAX_CONF ];
83 84
static Cache_node_mcb	* pers_mcb;
static Cache_node_mcb 	* conf_mcb;
Per Cederqvist's avatar
Per Cederqvist committed
85 86
static int		  next_free_num = 1;

87 88
static Cache_node_mcb 	* text_mcb;
static int		  next_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
89 90

/* Defined in ramkomd.c */
91 92 93
extern char datafilename[BUFSIZ];
extern char backupfilename[BUFSIZ];
extern char textfilename[BUFSIZ];
Per Cederqvist's avatar
Per Cederqvist committed
94 95 96 97 98 99 100

/*
 * 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;
Per Cederqvist's avatar
Per Cederqvist committed
101
EXPORT Conf_no       *conf_table  = NULL; /* Used in conference.c. */
Per Cederqvist's avatar
Per Cederqvist committed
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192


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
193
pers_set_mru(Pers_no pers_no)
Per Cederqvist's avatar
Per Cederqvist committed
194
{
195
    set_mru(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
196 197 198
}

static void
199
text_set_mru(Text_no text_no)
Per Cederqvist's avatar
Per Cederqvist committed
200
{
201
    set_mru(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
202 203 204
}

static void
205
conf_set_mru(Conf_no conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
206
{
207 208
    set_mru(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
209

210 211 212
static Cache_node *
get_pers_node(Pers_no pers_no)
{
213 214 215
    if ( pers_no >= next_free_num )
	return NULL;

216
    return get_cache_node(pers_mcb, pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
217 218 219
}

static void
220
unlink_text_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
221
{
222
    unlink_lru (node, &text_mcb->lru, &text_mcb->mru);
Per Cederqvist's avatar
Per Cederqvist committed
223 224
}

225 226 227 228 229
static void
unlink_conf_lru (Cache_node *node)
{
    unlink_lru (node, &conf_mcb->lru, &conf_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
230 231

static void
232
unlink_pers_lru (Cache_node *node)
Per Cederqvist's avatar
Per Cederqvist committed
233
{
234 235
    unlink_lru (node, &pers_mcb->lru, &pers_mcb->mru);
}
Per Cederqvist's avatar
Per Cederqvist committed
236

237 238 239
static Cache_node *
get_conf_node(Conf_no conf_no)
{
240 241 242
    if ( conf_no >= next_free_num )
	return NULL;

243 244
    return get_cache_node(conf_mcb, conf_no);
}
Per Cederqvist's avatar
Per Cederqvist committed
245

246 247 248
static Cache_node *
get_text_node(Text_no text_no)
{
249 250 251
    if ( text_no >= next_text_num )
	return NULL;

252
    return get_cache_node(text_mcb, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}

	
/*
 * 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 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);
284 285 286 287 288

    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
289 290 291 292 293 294 295 296 297 298 299 300
    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)
{
301 302 303 304
    Cache_node *node;

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

309
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
310 311 312
    pers_set_mru( pers_no );
}

313 314 315 316 317 318 319 320 321 322 323
/*
 * 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
324 325 326 327

void
mark_conference_as_changed(Conf_no	conf_no)
{
328
    Cache_node *node;
329
    Conference *conf_c;
330 331 332

    node = get_conf_node (conf_no);

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

337
    node->s.dirty = 1;
Per Cederqvist's avatar
Per Cederqvist committed
338
    conf_set_mru( conf_no );
339 340 341

    conf_c = (Conference *) node->ptr;

Per Cederqvist's avatar
Per Cederqvist committed
342
    small_conf_arr[ conf_no ]->highest_local_no
343 344 345 346
	= (conf_c->texts.first_local_no - 1 + conf_c->texts.no_of_texts );

    small_conf_arr[ conf_no ]->nice = conf_c->nice;
    small_conf_arr[ conf_no ]->type = conf_c->type;
Per Cederqvist's avatar
Per Cederqvist committed
347 348 349 350 351
}

void
mark_text_as_changed( Text_no text_no )
{
352 353 354 355
    Cache_node *node;

    node = get_text_node (text_no);

Per Cederqvist's avatar
Per Cederqvist committed
356 357
    TRACE2("Text %d is changed.\n", text_no);
    if ( text_no < 1 || text_no >= next_text_num
358
	|| node == NULL || node->s.exists == 0)
Per Cederqvist's avatar
Per Cederqvist committed
359 360 361 362
    {
	restart_kom("mark_text_as_changed(%d): nonexistent.\n", text_no);
    }

363 364
    node->s.dirty = 1;
    text_set_mru (text_no);
Per Cederqvist's avatar
Per Cederqvist committed
365 366 367 368 369 370 371 372 373 374 375 376
}    



/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
377 378
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
379 380 381 382 383 384 385 386
    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);
    }

387
    if ( get_pers_node(person) != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
388 389 390 391 392
    {
	restart_kom("cached_create_person(%d): Person existed.\n",
		    person);
    }

393 394 395 396 397 398 399 400 401
    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
402 403 404 405 406 407 408 409
    pers_set_mru( person );
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
410 411
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
412 413 414 415 416 417 418
    TRACE2("cached_get_person_stat %d\n", person);

    if ( person == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
419

420 421 422 423 424 425
    if ( person >= next_free_num )
    {
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

426 427
    node = get_pers_node (person);

428
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
429 430 431 432 433
    {
	kom_errno = KOM_UNDEF_PERS;
	return NULL;
    }

434
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
435 436
    {
	pers_set_mru( person );
437
	++pers_mcb->hits;
438
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
439 440
    }

441
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
442
    {
443
	node->ptr = copy_person (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
444
	pers_set_mru (person);
445
	++pers_mcb->hits;
446
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
447 448
    }

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

451
    ++pers_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
452
    pers_set_mru (person);
453
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
454 455 456 457 458 459
}


/*
 * Conference-related calls
 */
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487


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);
    }
}


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

    return s;
}


488 489 490 491 492 493
/*
 * 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
494 495 496 497
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;
498
    Cache_node  *node;
Per Cederqvist's avatar
Per Cederqvist committed
499 500 501 502 503 504 505 506 507 508 509 510 511

    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++;

512 513 514 515 516 517 518 519 520
    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
521 522
    conf_set_mru(conf_no);    

523
    zero_init_cache_node (pers_mcb, conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
524 525 526 527 528 529
    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);

530
    TRACE2("Created conference number %d\n", conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
531 532 533 534 535 536 537 538
    
    return conf_no;
}


extern Success
cached_delete_conf( Conf_no	conf )
{
539 540
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
541 542 543 544 545
    if ( conf == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
546

547 548 549 550 551 552
    if ( conf >= next_free_num )
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

553 554
    node = get_conf_node (conf);

555
    if ( node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
556 557 558 559 560
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

561 562 563
    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
564

565 566 567
    free_conference(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
568 569 570 571 572 573 574

    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
575 576
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
577 578 579 580 581
    if ( pers == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return FAILURE;
    }
582

583 584 585 586 587 588 589 590
    if ( pers >= next_free_num )
    {
	log("cached_delete_person(%lu): next_free_num == %lu\n",
	    (u_long)pers, (u_long)next_free_num);
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

591 592 593
    node = get_pers_node (pers);

    if ( pers >= next_free_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
594 595 596 597 598 599 600
    {
	log("cached_delete_person(): attempt to"
	    " delete non-existing person.\n");
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

601
    if ( node->lock_cnt > 0 )
602 603
	log("cached_delete_pers(%lu): lock_cnt === %lu\n",
	    (u_long)pers, (u_long)node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
604

605 606 607
    free_person (node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
608 609 610 611 612 613
    return OK;
}

Success
cached_delete_text(Text_no text)
{
614 615
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
616 617 618 619 620
    if ( text == 0 )
    {
	kom_errno = KOM_TEXT_ZERO;
	return FAILURE;
    }
621 622 623 624 625

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL
	|| node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
626 627 628 629 630 631 632
    {
	log("cached_delete_text(): attempt to"
	    " delete non-existing text %d.\n", text);
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

633
    if ( node->lock_cnt > 0 )
Per Cederqvist's avatar
Per Cederqvist committed
634
	log("cached_delete_text(%d): lock_cnt === %d\n",
635
	    text, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
636 637


638 639 640
    free_text_stat(node->ptr);
    node->ptr = NULL;
    node->s.exists = 0;
Per Cederqvist's avatar
Per Cederqvist committed
641 642 643 644 645 646

    return OK;
}


extern Conference *
647
cached_get_conf_stat (Conf_no   conf_no)
Per Cederqvist's avatar
Per Cederqvist committed
648
{
649 650
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
651 652 653 654 655 656 657
    TRACE2("cached_get_conf_stat %d\n", conf_no);

    if ( conf_no == 0 )
    {
	kom_errno = KOM_CONF_ZERO;
	return NULL;
    }
658 659 660 661

    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
662 663 664 665 666
    {
	kom_errno = KOM_UNDEF_CONF;
	return NULL;
    }

667
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
668
    {
669
	conf_set_mru (conf_no);
670
	++conf_mcb->hits;
671
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
672 673
    }

674
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
675
    {
676
	node->ptr = copy_conf (node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
677
	conf_set_mru (conf_no);
678
	++conf_mcb->hits;
679
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
680 681
    }

682
    node->ptr = read_conference(file_a, node->pos, node->size);
683
    ++conf_mcb->misses;
Per Cederqvist's avatar
Per Cederqvist committed
684 685

    conf_set_mru (conf_no);
686
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
687 688 689 690 691 692 693 694
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
695 696 697 698 699 700 701 702
    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
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
}

    
/*
 * 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 )
{
746 747
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
748 749 750 751 752 753 754 755
    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;
    }
756 757 758 759

    node = get_text_node (text);

    if ( text >= next_text_num || node == NULL || node->s.exists == 0 )
Per Cederqvist's avatar
Per Cederqvist committed
760 761 762 763 764 765
    {
	TRACE1("cached_get_text_stat: no such text.\n");
	kom_errno = KOM_NO_SUCH_TEXT;
	return NULL;
    }

766
    if ( node->ptr != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
767 768 769
    {
	TRACE1("Found in ptr.\n");
	text_set_mru( text );
770
	++text_mcb->hits;
771
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
772 773
    }

774
    if ( node->snap_shot != NULL )
Per Cederqvist's avatar
Per Cederqvist committed
775 776
    {
	TRACE1("Found in snap_shot\n");
777
	node->ptr = copy_text_stat(node->snap_shot);
Per Cederqvist's avatar
Per Cederqvist committed
778
	text_set_mru (text);
779
	++text_mcb->hits;
780
	return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
781 782 783
    }

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

    text_set_mru (text);
787
    ++text_mcb->misses;
788
    return node->ptr;
Per Cederqvist's avatar
Per Cederqvist committed
789 790 791 792 793 794
}



/*
 * The text is set up with an empty misc-field. The misc field is
795
 * then initialized by create_text.
Per Cederqvist's avatar
Per Cederqvist committed
796 797 798 799 800 801
 */

extern Text_no
cached_create_text( String message)
{
    Text_no tno;
802
    Cache_node *node;
Per Cederqvist's avatar
Per Cederqvist committed
803 804 805 806 807 808 809 810 811 812 813 814 815

    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;
    }

816 817
    create_cache_node(text_mcb, tno);
    node = get_text_node (tno);
Per Cederqvist's avatar
Per Cederqvist committed
818

819
    if ( node == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
820
	restart_kom("cached_create_text(): couldn't create cache-node.\n");
821 822 823 824 825 826 827 828 829
    
    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
830
    fseek(text_file, 0, SEEK_END);
831
    ((Text_stat *)node->ptr)->file_pos = ftell(text_file);
Per Cederqvist's avatar
Per Cederqvist committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848

    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;
}


849
EXPORT Text_no
Per Cederqvist's avatar
Per Cederqvist committed
850 851
traverse_text(Text_no seed)
{
852 853
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
854 855
    seed++;
    
856
    while ( seed < next_text_num )
Per Cederqvist's avatar
Per Cederqvist committed
857
    {
858 859 860 861
	node = get_text_node (seed);
	if ( node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
862 863 864 865 866 867 868 869 870
	seed++;
    }
    
    return (seed >= next_text_num) ? 0 : seed ;
}

Pers_no
traverse_person(Pers_no seed)
{
871 872
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
873 874
    seed++;

875
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
876
    {
877 878 879 880
	node = get_pers_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
881 882 883 884 885 886 887 888 889
	seed++;
    }

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

Conf_no
traverse_conference(Conf_no seed)
{
890 891
    Cache_node *node;

Per Cederqvist's avatar
Per Cederqvist committed
892 893
    seed++;
    
894
    while ( seed < next_free_num )
Per Cederqvist's avatar
Per Cederqvist committed
895
    {
896 897 898 899
	node = get_conf_node (seed);
	if (node != NULL && node->s.exists != 0 )
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
	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)
{
923 924 925 926 927
    Cache_node *node;

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

930
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
931
    {
932
	Person *pers_stat_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
933

934
	pers_stat_ptr = cached_get_person_stat( pers_no );
Per Cederqvist's avatar
Per Cederqvist committed
935

936
	if ( pers_stat_ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
937 938 939
	    restart_kom("cached_lock_person(%d): couldn't read in person.\n",
			pers_no);

940 941 942 943
	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
944 945
    }

946
    node->lock_cnt++;
Per Cederqvist's avatar
Per Cederqvist committed
947 948 949 950 951 952
}

/* Decrease reference count. If zero, unlock person. */
void
cached_unlock_person(Pers_no pers_no)
{
953 954 955 956 957 958 959 960
    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
961 962
    {
	log("cached_unlock_person(%d): lock_cnt == %d.\n",
963
	    pers_no, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
964

965
	node->lock_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
966 967
    }
    else
968
	node->lock_cnt--;
Per Cederqvist's avatar
Per Cederqvist committed
969 970 971
}


972
/* Lock a conf struct in memory. Increase a reference count. */
Per Cederqvist's avatar
Per Cederqvist committed
973 974 975
void
cached_lock_conf(Conf_no conf_no)
{
976 977 978 979 980 981 982 983
    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
984 985
	restart_kom("cached_lock_conf(%d): nonexistent.\n", conf_no);

986
    if ( node->ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
987
    {
988
	Conference *conference_ptr;
Per Cederqvist's avatar
Per Cederqvist committed
989

990
	conference_ptr = cached_get_conf_stat( conf_no );
Per Cederqvist's avatar
Per Cederqvist committed
991

992
	if ( conference_ptr == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
993 994 995
	    restart_kom("cached_lock_conf(%d): couldn't read in conf.\n",
			conf_no);

996 997 998 999
	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
1000 1001
    }

1002
    node->lock_cnt++;
Per Cederqvist's avatar
Per Cederqvist committed
1003 1004 1005 1006 1007 1008
}

/* Decrease reference count. If zero, unlock conf. */
void
cached_unlock_conf(Conf_no conf_no)
{
1009 1010 1011 1012 1013 1014 1015
    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
1016 1017
    {
	log("cached_unlock_conf(%d): lock_cnt == %d.\n",
1018
	    conf_no, node->lock_cnt);
Per Cederqvist's avatar
Per Cederqvist committed
1019

1020
	node->lock_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1021 1022
    }
    else
1023
	node->lock_cnt--;
Per Cederqvist's avatar
Per Cederqvist committed
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
}



/*
 * 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;
Per Cederqvist's avatar
Per Cederqvist committed
1072
	    match->tokens = tokenize(match->name, s_fcrea_str(WHITESPACE));
Per Cederqvist's avatar
Per Cederqvist committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
	    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,
Per Cederqvist's avatar
Per Cederqvist committed
1100
		s_fcrea_str(WHITESPACE), DEFAULT_COLLAT_TAB);
Per Cederqvist's avatar
Per Cederqvist committed
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

    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;
    }
}


Per Cederqvist's avatar
Per Cederqvist committed
1160
static void
Per Cederqvist's avatar
Per Cederqvist committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
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++ )
    {
1177
	node = get_conf_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

	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 )
		{
1189
		    unlink_conf_lru(node);
Per Cederqvist's avatar
Per Cederqvist committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
		    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++ )
    {
1205
	node = get_pers_node(i);
Per Cederqvist's avatar
Per Cederqvist committed
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

	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 )
		{
1217
		    unlink_pers_lru(node);
Per Cederqvist's avatar
Per Cederqvist committed
1218 1219