dbck-cache.c 23.8 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: dbck-cache.c,v 0.25 1996/07/24 23:35:14 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1992, 1993, 1994, 1995  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27 28 29 30 31 32 33 34
/*
 * This module contains some very 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.
 *   Also save time as a time_t instead of a struct tm.
 */

35
static char *rcsid = "$Id: dbck-cache.c,v 0.25 1996/07/24 23:35:14 ceder Exp $";
36 37
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
38

Per Cederqvist's avatar
Per Cederqvist committed
39
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
40
#ifndef SEEK_END
Per Cederqvist's avatar
Per Cederqvist committed
41
#  include <fcntl.h>
Per Cederqvist's avatar
Per Cederqvist committed
42 43
#endif
#ifndef SEEK_END
Per Cederqvist's avatar
Per Cederqvist committed
44 45 46 47 48
#  include <unistd.h>
#endif
#ifndef SEEK_END
#  define SEEK_SET 0
#  define SEEK_END 2
Per Cederqvist's avatar
Per Cederqvist committed
49
#endif
Per Cederqvist's avatar
Per Cederqvist committed
50 51 52
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
53
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
54
#include <sys/types.h>
Per Cederqvist's avatar
Per Cederqvist committed
55 56 57
#ifdef TIME_SYNC
#  include <sys/resource.h>
#endif
58 59 60
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
61

62
#include "ldifftime.h"
Per Cederqvist's avatar
Per Cederqvist committed
63 64
#include "exp.h"
#include "misc-types.h"
65
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
66 67
#include "kom-types.h"
#include "kom-errno.h"
Per Cederqvist's avatar
Per Cederqvist committed
68
#include "cache.h"
Per Cederqvist's avatar
Per Cederqvist committed
69 70 71 72
#include "tmp-limits.h"
#include "debug.h"
#include "server/smalloc.h"
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
73
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
74
#include "kom-memory.h"
Per Cederqvist's avatar
Per Cederqvist committed
75 76
#include "ram-parse.h"
#include "ram-output.h"
Per Cederqvist's avatar
Per Cederqvist committed
77
#include "param.h"
Per Cederqvist's avatar
Per Cederqvist committed
78

Per Cederqvist's avatar
Per Cederqvist committed
79 80 81 82
/*
 * All functions that can fail sets kom_errno to a suitable value
 * if they fail.
 */
Per Cederqvist's avatar
Per Cederqvist committed
83 84 85 86 87 88 89 90 91 92 93

Person		* pers_arr[ MAX_CONF ];
Conference 	* conf_arr[ MAX_CONF ];
Conf_type	  conf_type_arr[ MAX_CONF ];
String		  name_list [ MAX_CONF ]; /* "cache" list */
			        	  /* Global var auto init to NULL */
int		  next_free_num = 1;

Text_stat 	* text_arr[ MAX_TEXT ];
int next_text_num = 1;

David Byers's avatar
David Byers committed
94 95 96 97 98 99 100 101 102 103 104 105
Info		  kom_info =
{
#include "version.incl"
    ,				/* version */
    1,				/* conf_pres_conf */
    2,				/* pers_pres_conf */
    3,				/* motd_conf */
    4,				/* kom_news_conf */
    0				/* motd_of_lyskom */
};


Per Cederqvist's avatar
Per Cederqvist committed
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
/* Defined in ramkomd.c */

static FILE          *text_file = NULL;
static FILE	     *new_text_file = NULL; /* Used when garbage collecting. */

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)
#define INRANGE(str, num, retval) if ( num == 0 || num >= next_free_num ) \
{			\
    return retval;	\
    printf("%s: conf_no %d out of range 1 ... %d ", str, num, next_free_num); \
    fflush(stdout);	\
    fflush(stderr);	\
    abort();		\
}

#define TEXT_RANGE(str, num, retval) if ( num == 0 || num >= next_text_num ) \
{	\
    return retval;\
    printf("Text_no out of range 1 ... %d ", next_text_num); \
    printf(str);	\
    fflush(stdout);	\
    fflush(stderr);	\
    abort(); 		\
}

Per Cederqvist's avatar
Per Cederqvist committed
138 139 140 141 142 143 144 145 146 147
#define VOID_TEXT_RANGE(str, num) if ( num == 0 || num >= next_text_num ) \
{	\
    return;\
    printf("Text_no out of range 1 ... %d ", next_text_num); \
    printf(str);	\
    fflush(stdout);	\
    fflush(stderr);	\
    abort(); 		\
}

Per Cederqvist's avatar
Per Cederqvist committed
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

extern Conf_type
cached_get_conf_type (Conf_no conf_no)
{
    return conf_arr [ conf_no ]->type;
}



#define IMPL(retval) if (1) { fflush(stderr); fflush(stdout); abort(); }


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

void
mark_person_as_changed(Pers_no	pers_no)
{
    TRACE2("Person %d is changed\n", pers_no);
    return;    
}


void
mark_conference_as_changed(Conf_no	conf_no)
{
    TRACE2("Conf.  %d is changed\n", conf_no);
    return;
}

void
mark_text_as_changed( Text_no text_no )
{
182
    TRACE2("Text %lu is changed.\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
183
    VOID_TEXT_RANGE("mark_text_as_changed\n", text_no);
Per Cederqvist's avatar
Per Cederqvist committed
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 210 211 212 213 214 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 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
}    


/*
 * Person-related calls
 */


extern Success
cached_create_person( Pers_no person )
{
    TRACE2("Person %d is being created.\n", person);
    INRANGE("cached_create_person\n", person, FAILURE);

    if ( pers_arr[ person ] != NULL )
    {
	TRACE1("pers_arr not NULL");
	fflush(stdout);
	fflush(stderr);
	abort();
    }
    
    pers_arr[ person ] = alloc_person();
    return OK;
}


extern Person *
cached_get_person_stat( Pers_no	person )
{
    INRANGE("cached_get_person_stat\n", person, NULL);
    TRACE2("cached_get_person_stat %d\n", person);
    kom_errno = KOM_UNDEF_PERS;
    
    return pers_arr[ person ];
}



/*
 * Conference-related calls
 */
extern Conf_no	/* Also cache the name */
cached_create_conf (String  name)
{
    Conference * conf_c;
    Conf_no	 conf_no;

    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++;
    conf_c  = alloc_conference();

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

    TRACE2(" number %d\n", conf_no);

    conf_arr[ conf_no ] = conf_c;
    pers_arr[ conf_no ] = NULL;
    
    return conf_no;
}

extern Success
cached_delete_conf( Conf_no	conf )
{
    INRANGE("cached_delete_conf()", conf, FAILURE);
    if ( conf_arr[ conf ] == NULL )
    {
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }
    free_conference(conf_arr[ conf ]);
    conf_arr[ conf ] = NULL;
    return OK;
}

Success
cached_delete_person(Pers_no pers)
{
    INRANGE("cached_delete_person()", pers, FAILURE);
    if ( pers_arr[ pers ] == NULL )
    {
	kom_errno = KOM_UNDEF_PERS;
	return FAILURE;
    }

    pers_arr[ pers ] = NULL;
    return OK;
}

Success
cached_delete_text(Text_no text)
{
    TEXT_RANGE("cached_delete_text()", text, FAILURE);
    if ( text_arr[ text ] == NULL )
    {
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }
    free_text_stat(text_arr[ text ]);
    text_arr[ text ] = NULL;
    return OK;
    
}


extern Conference *
cached_get_conf_stat(	Conf_no		conf_no )
{
    INRANGE("cached_get_conf_stat\n", conf_no, NULL);
    TRACE2("cached_get_conf_stat %d\n", conf_no);
    kom_errno = KOM_UNDEF_CONF;
    
    return conf_arr[ conf_no ];
}

/*
 * Return TRUE if conf_no exists.
 */
Bool
cached_conf_exists(Conf_no conf_no)
{
    return conf_arr[ conf_no ] != NULL ? TRUE : FALSE;
}

    
/*
 * Calls to handle texts
 */

extern String
cached_get_text( Text_no text )
{
    String  the_string;

    TEXT_RANGE("cached_get_text\n", text, EMPTY_STRING);
330
    TRACE2("cached_get_text %lu\n", text);
Per Cederqvist's avatar
Per Cederqvist committed
331 332 333 334 335 336 337 338 339 340 341

    if ( text_arr[ text ] == NULL )
	return EMPTY_STRING;
    else
    {
	the_string.string = tmp_alloc( text_arr[text]->no_of_chars );
	the_string.len = text_arr[text]->no_of_chars;
	fseek(text_file, text_arr[ text ]->file_pos, SEEK_SET);
	if ( fread(the_string.string, sizeof(char), the_string.len, text_file)
	    != the_string.len )
	{
Per Cederqvist's avatar
Per Cederqvist committed
342 343
	    log("%s read enough characters of text %d\n",
		"WARNING: cached_get_text: couldn't",
Per Cederqvist's avatar
Per Cederqvist committed
344 345 346 347 348 349 350 351 352 353 354 355
		text);
	}
		    
	return the_string;
    }
}

extern Text_stat *	/* NULL on error */
cached_get_text_stat(	Text_no		text )
{
    kom_errno = KOM_NO_SUCH_TEXT;
    TEXT_RANGE("cached_get_text_stat\n", text, NULL);
356
    TRACE2("cached_get_text_stat %lu\n", text);
Per Cederqvist's avatar
Per Cederqvist committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    
    return text_arr[ text ];
}



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

#if 0
extern Text_no
cached_create_text( String message)
{
    Text_no tno;

    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;
    }
        
    text_arr[ tno ] = alloc_text_stat();
    text_arr[ tno ]->no_of_misc = 0;
    text_arr[ tno ]->misc_items = NULL;
    text_arr[ tno ]->no_of_marks = 0;
    text_arr[ tno ]->no_of_lines = 0;
    text_arr[ tno ]->no_of_chars = 0;
    fseek(text_file, 0, SEEK_END);
    text_arr[ tno ]->file_pos = ftell(text_file);

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

Per Cederqvist's avatar
Per Cederqvist committed
409
void
Per Cederqvist's avatar
Per Cederqvist committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 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 488 489 490 491 492 493 494 495 496 497 498 499
cached_flush_text(Text_no text_no,
		  String message)
{
    if ( text_no >= next_text_num )
    {
	log("cached_flush_text(%lu, string): only text %lu exists.",
	    (u_long) text_no, (u_long) next_text_num);
	return;
    }
        
    fseek(new_text_file, 0, SEEK_END);
    text_arr[ text_no ]->file_pos = ftell(new_text_file);

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

    fflush(new_text_file);
}

Text_no
traverse_text(Text_no seed)
{
    seed++;
    
    while ( seed < next_text_num && text_arr[ seed ] == NULL )
	seed++;

    return (seed == next_text_num) ? 0 : seed ;
}

Pers_no
traverse_person(Pers_no seed)
{
    seed++;
    
    while ( seed < next_free_num && pers_arr[ seed ] == NULL )
	seed++;

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

Conf_no
traverse_conference(Conf_no seed)
{
    seed++;
    
    while ( seed < next_free_num && conf_arr[ seed ] == NULL )
	seed++;

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

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

extern Local_text_no
cached_get_highest_local_no (Conf_no conf_no)
{
    return ( conf_arr [ conf_no ]->texts.first_local_no
	    - 1 + conf_arr [ conf_no ]->texts.no_of_texts );
}

/* Lock a person struct in memory. Increase a referenc count. */
void
cached_lock_person(Pers_no pers_no)
{}

/* Decrease reference count. If zero, unlock person. */
void
cached_unlock_person(Pers_no pers_no)
{}

/* Lock a conf struct in memory. Increase a referenc count. */
void
cached_lock_conf(Conf_no conf_no)
{}

/* Decrease reference count. If zero, unlock conf. */
void
cached_unlock_conf(Conf_no conf_no)
{}


David Byers's avatar
David Byers committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
static long
get_version(const char *fn)
{
    FILE	 *fp;
    char	  c;
    long	  version;
    
    if ( (fp = fopen(fn, "rb")) == NULL)
	return -1;

    fseek(fp, 5, SEEK_SET);
    if ( (c = getc(fp)) == '\n')
    {
	fclose(fp);
	return 0;
    }
    version = fparse_long(fp);
    fclose(fp);

    return version;
}
Per Cederqvist's avatar
Per Cederqvist committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557


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

#ifdef TIME_SYNC
static long
timerdiff(struct timeval a,
	   struct timeval b)
{
    return a.tv_sec - b.tv_sec;
}
#endif

extern void			/* Write out everything. */
cache_sync(void)
{
    FILE *fp;
    int i;
David Byers's avatar
David Byers committed
558
    extern long oformat;
Per Cederqvist's avatar
Per Cederqvist committed
559 560 561 562 563 564 565 566 567 568 569 570 571

#ifdef TIME_SYNC
    struct rusage start, after_confs, after_persons, after_text_stats,
    	after_text_masses;
    time_t st, ac, ap, ats, atm;
    void getrusage(int who, struct rusage *rusage);
#endif
    
#ifdef TIME_SYNC
    getrusage(0, &start);
    time(&st);
#endif

Per Cederqvist's avatar
Per Cederqvist committed
572
    if ( is_clean(param.datafile_name) )
Per Cederqvist's avatar
Per Cederqvist committed
573
    {
Per Cederqvist's avatar
Per Cederqvist committed
574
	if ( rename(param.datafile_name, param.backupfile_name) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
575 576 577 578 579
	    log("WARNING: cache_sync: can't backup.\n");
    }
    else
	log("cache_sync: datafile not clean. No backup taken.\n");
    
Per Cederqvist's avatar
Per Cederqvist committed
580
    if ( (fp=fopen(param.datafile_name, "w") ) == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
581 582 583 584 585
    {
	log("WARNING: cache_sync: can't open file to save in.\n");
	return;
    }

David Byers's avatar
David Byers committed
586 587 588 589
    if (oformat == 0)
	fprintf(fp, "DIRTY\n");		 /* DIRTY-FLAG */
    else
	fprintf(fp, "DIRTY:%05ld\n", oformat);
Per Cederqvist's avatar
Per Cederqvist committed
590

David Byers's avatar
David Byers committed
591 592 593 594 595 596 597
    if (oformat == 0)
	fprintf(fp, "%d\n", next_free_num);	  /* NEXT_FREE_NUM */
    else
      {
	fprintf(fp, "#C %d\n", next_free_num);
	fprintf(fp, "#T %d\n", next_text_num);
      }
Per Cederqvist's avatar
Per Cederqvist committed
598

599 600 601 602 603 604 605
    if (oformat == 1)
      fprintf(fp, "I %d %d %d %d %ld\n",
	      kom_info.conf_pres_conf,
	      kom_info.pers_pres_conf,
	      kom_info.motd_conf,
	      kom_info.kom_news_conf,
	      kom_info.motd_of_lyskom);
606

Per Cederqvist's avatar
Per Cederqvist committed
607 608
    for ( i = 1; i < next_free_num; i++ ) /* CONFS */
    {
David Byers's avatar
David Byers committed
609
	if ( conf_arr[ i ] == NULL && oformat == 0)
Per Cederqvist's avatar
Per Cederqvist committed
610 611 612
	    fprintf(fp, "@");
	else
	{
David Byers's avatar
David Byers committed
613 614 615 616 617
	    if (oformat == 0)
		fprintf(fp, "+ ");
	    else
		fprintf(fp, "C %d ", i);
	    
Per Cederqvist's avatar
Per Cederqvist committed
618 619 620 621 622 623 624 625 626 627
	    foutput_conference(fp, conf_arr[ i ]);
	}
	putc('\n', fp);
    }
#ifdef TIME_SYNC
    getrusage(0, &after_confs);
    time(&ac);
#endif
    for ( i = 1; i < next_free_num; i++ ) /* PERSONS */
    {
David Byers's avatar
David Byers committed
628 629 630 631 632
	if ( pers_arr[ i ] == NULL)
	{
	    if (oformat == 0)
		fprintf(fp, "@\n");
	}
Per Cederqvist's avatar
Per Cederqvist committed
633 634
	else
	{
David Byers's avatar
David Byers committed
635 636 637 638
	    if (oformat == 0)
		fprintf(fp, "+ %dH", PASSWD_LEN);
	    else
		fprintf(fp, "P %d %dH", i, PASSWD_LEN);
Per Cederqvist's avatar
Per Cederqvist committed
639 640
	    fwrite(pers_arr[ i ]->pwd, PASSWD_LEN, 1, fp);
	    foutput_person(fp, pers_arr[ i ]);
David Byers's avatar
David Byers committed
641
	    putc('\n', fp);
Per Cederqvist's avatar
Per Cederqvist committed
642 643 644 645 646 647 648
	}
    }
#ifdef TIME_SYNC
    getrusage(0, &after_persons);
    time(&ap);
#endif

David Byers's avatar
David Byers committed
649 650
    if (oformat == 0)
	fprintf(fp, "%d\n", next_text_num);	/* NEXT_TEXT_NUM */
Per Cederqvist's avatar
Per Cederqvist committed
651 652 653

    for ( i = 1; i < next_text_num; i++ ) /* TEXT_STATS */
    {
David Byers's avatar
David Byers committed
654
	if ( text_arr[ i ] == NULL && oformat == 0)
Per Cederqvist's avatar
Per Cederqvist committed
655 656 657
	    fprintf(fp, "@");
	else
	{
David Byers's avatar
David Byers committed
658 659 660 661
	    if (oformat == 0)
		fprintf(fp, "+ ");
	    else
		fprintf(fp, "T %d ", i);
Per Cederqvist's avatar
Per Cederqvist committed
662 663 664 665 666
	    foutput_text_stat(fp, text_arr[ i ]);
	}
	putc('\n', fp);
    }

David Byers's avatar
David Byers committed
667 668 669 670 671 672
    if (oformat != 0)
    {
	putc('$', fp);
	putc('\n', fp);
    }
    
Per Cederqvist's avatar
Per Cederqvist committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
#ifdef TIME_SYNC
    getrusage(0, &after_text_stats);
    time(&ats);

    getrusage(0, &after_text_masses);
    time(&atm);
#endif

    rewind(fp);
    fprintf(fp, "CLEAN");
    fclose(fp);

#ifdef TIME_SYNC
    log("Sync ready.\n"
    	"User: %4ld sec (%4ld conf, %4ld pers, %4ld stat, %4ld text)\n"
    	"Sys:  %4ld sec (%4ld conf, %4ld pers, %4ld stat, %4ld text)\n"
	"Real: %4ld sec (%4ld conf, %4ld pers, %4ld stat, %4ld text)\n"
	"Page faults: %4ld. Swapped: %4ld. Outblocked: %4ld.\n",
	
	timerdiff(after_text_masses.ru_utime, start.ru_utime),
	timerdiff(after_confs.ru_utime, start.ru_utime),
	timerdiff(after_persons.ru_utime, after_confs.ru_utime),
	timerdiff(after_text_stats.ru_utime, after_persons.ru_utime),
	timerdiff(after_text_masses.ru_utime, after_text_stats.ru_utime),
	
	timerdiff(after_text_masses.ru_stime, start.ru_stime),
	timerdiff(after_confs.ru_stime, start.ru_stime),
	timerdiff(after_persons.ru_stime, after_confs.ru_stime),
	timerdiff(after_text_stats.ru_stime, after_persons.ru_stime),
	timerdiff(after_text_masses.ru_stime, after_text_stats.ru_stime),

704 705 706 707 708
	(u_long)ldifftime(atm, st),
	(u_long)ldifftime(ac, st),
	(u_long)ldifftime(ap, ac),
	(u_long)ldifftime(ats, ap),
	(u_long)ldifftime(atm, ats),
Per Cederqvist's avatar
Per Cederqvist committed
709 710 711 712 713 714 715

	after_text_masses.ru_majflt - start.ru_majflt,
	after_text_masses.ru_nswap - start.ru_nswap,
	after_text_masses.ru_oublock - start.ru_oublock);
#endif    
}

Per Cederqvist's avatar
Per Cederqvist committed
716
void
Per Cederqvist's avatar
Per Cederqvist committed
717 718
cache_open_new_text_file(void)
{
Per Cederqvist's avatar
Per Cederqvist committed
719
    if ( ( new_text_file = fopen(param.textfile_name, "w")) == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
720 721 722 723 724 725 726 727 728 729 730 731
    {
	log("Can't open file to save new texts. Goodbye.\n");
	exit(1);
    }
}

extern Success
init_cache(void)
{
    FILE *fp = NULL;
    int i;
    extern int vflag;		/* from dbck.c */
732
    extern Bool truncated_texts; /* from dbck.c */
David Byers's avatar
David Byers committed
733 734 735
    long num;
    long data_file_version;
    char done, read_text_num, read_conf_num;
736 737 738 739 740 741 742 743 744 745 746
    extern int modifications;
    extern long oformat;
    extern int pers_pres_conf;
    extern int conf_pres_conf;
    extern int motd_conf;
    extern int motd_of_lyskom;
    extern int kom_news_conf;
       
    read_text_num = 0;
    read_conf_num = 0;
        
David Byers's avatar
David Byers committed
747 748 749 750 751 752 753 754
    for (i = 0; i < MAX_CONF; i++)
	conf_arr[i] = NULL;
    for (i = 0; i < MAX_CONF; i++)
	pers_arr[i] = NULL;
    for (i = 0; i < MAX_TEXT; i++)
	text_arr[i] = NULL;
    
    
Per Cederqvist's avatar
Per Cederqvist committed
755 756
    new_text_file = NULL;
    
Per Cederqvist's avatar
Per Cederqvist committed
757
    if ( (text_file = fopen(param.textfile_name, "rb")) == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
758
    {
Per Cederqvist's avatar
Per Cederqvist committed
759
	perror(param.textfile_name);
760
	restart_kom("ERROR: init_cache: can't open text file %s.\n",
Per Cederqvist's avatar
Per Cederqvist committed
761
		    param.textfile_name);
Per Cederqvist's avatar
Per Cederqvist committed
762 763
    }

Per Cederqvist's avatar
Per Cederqvist committed
764
    if ( is_clean(param.datafile_name) )
Per Cederqvist's avatar
Per Cederqvist committed
765
    {
David Byers's avatar
David Byers committed
766
	data_file_version = get_version(param.datafile_name);
Per Cederqvist's avatar
Per Cederqvist committed
767
	if ( (fp = fopen(param.datafile_name, "rb")) == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
768 769 770 771 772 773
	{
	    log("WARNING: init_cache: can't open datafile.\n");
	    return FAILURE;
	}
	log("MSG: init_cache: using datafile.\n");
    }
Per Cederqvist's avatar
Per Cederqvist committed
774
    else if ( is_clean(param.backupfile_name) )
Per Cederqvist's avatar
Per Cederqvist committed
775
    {
David Byers's avatar
David Byers committed
776
	data_file_version = get_version(param.backupfile_name);
Per Cederqvist's avatar
Per Cederqvist committed
777
	if ( (fp = fopen(param.backupfile_name, "rb")) == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
778 779 780 781 782 783 784 785 786 787 788 789
	{
	    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;
    }

790 791 792
    if (oformat == -1)
        oformat = data_file_version;

David Byers's avatar
David Byers committed
793 794 795 796 797 798 799 800 801 802
    if (data_file_version == 0)
	fseek(fp, 6, SEEK_SET);	/* skip clean/dirty flag. */
    else
	fseek(fp, 12, SEEK_SET);

    if (data_file_version == 0)
      {
	next_free_num = fparse_long(fp);	  /* NEXT_FREE_NUM */
	read_conf_num = 1;
      }
Per Cederqvist's avatar
Per Cederqvist committed
803 804

    if ( vflag )
David Byers's avatar
David Byers committed
805 806 807
	log("Data file version is '%c'\n", data_file_version);
    
    if ( vflag && data_file_version == 0)
Per Cederqvist's avatar
Per Cederqvist committed
808 809 810
	log("Reading %d conferences, starting at pos %d.\n",
	    next_free_num-1, ftell(fp));

David Byers's avatar
David Byers committed
811 812 813 814
    done = 0;
    for ( i = 1;
	 !done && (i < next_free_num || data_file_version != 0);
	 i++ ) /* CONFS */
Per Cederqvist's avatar
Per Cederqvist committed
815
    {
David Byers's avatar
David Byers committed
816
	num = i;
Per Cederqvist's avatar
Per Cederqvist committed
817 818 819 820
	fskipwhite(fp);
	switch(getc(fp))
	{
	case '@':
David Byers's avatar
David Byers committed
821 822 823 824 825 826 827
	    
	    if (data_file_version != 0)
		restart_kom("init_cache(): empty record in new datafile.\n");
	    conf_arr[ num ] = NULL;
	    break;

	case 'I':
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	    switch (data_file_version)
	    {
	    case 0:
#if 1
		log("init_cache(): 'I' record in old file ignored\n");
#else
		/* dbck.c does not yet export confirm() and rflag and... */
		log("'I' record found in version 0 data file.\n");
		if (rflag || confirm("Repair by ignoring it? "))
		{
		    modifications++;
		    log("Repair by ignoring it.");
		}
		else
		    error++;
#endif
		break;
	    case 1:
		kom_info.conf_pres_conf = fparse_long(fp);
		kom_info.pers_pres_conf = fparse_long(fp);
		kom_info.motd_conf = fparse_long(fp);
		kom_info.kom_news_conf = fparse_long(fp);
		kom_info.motd_of_lyskom = fparse_long(fp);
		break;
	    default:
		restart_kom("init_cache(): 'I' record; unknown version\n");
	    }
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

            if (pers_pres_conf != -1)
            {
                modifications += (kom_info.pers_pres_conf != pers_pres_conf);
                kom_info.pers_pres_conf = pers_pres_conf;
            }
            if (conf_pres_conf != -1)
            {
                modifications += (kom_info.conf_pres_conf != conf_pres_conf);
                kom_info.conf_pres_conf = conf_pres_conf;
            }
            if (motd_conf != -1)
            {
                modifications += (kom_info.motd_conf != motd_conf);
                kom_info.motd_conf = motd_conf;
            }
            if (kom_news_conf != -1)
            {
                modifications += (kom_info.kom_news_conf != kom_news_conf);
                kom_info.kom_news_conf = kom_news_conf;
            }
            if (motd_of_lyskom != -1)
            {
                modifications += (kom_info.motd_of_lyskom != motd_of_lyskom);
                kom_info.motd_of_lyskom = motd_of_lyskom;
            }
David Byers's avatar
David Byers committed
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
	    break;
	    
	case 'C':
	    if (data_file_version == 0)
		restart_kom("init_cache(): new type record in old file\n");

	    num = fparse_long(fp);
	    if (conf_arr[num])
		free_conference(conf_arr[num]);
	    conf_arr[num] = alloc_conference();
	    if (fparse_conference(fp, conf_arr[num]) != OK)
		restart_kom("init_cache(): %s. i == %d\n", 
			    "fparse_conference failed", i);
	    name_list[num] = EMPTY_STRING;
	    s_strcpy(&name_list[num], conf_arr[ num ]->name);
Per Cederqvist's avatar
Per Cederqvist committed
896 897
	    break;

David Byers's avatar
David Byers committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
	case 'P':
	    if (data_file_version == 0)
		restart_kom("init_cache(): new type record in old file\n");
	    num = fparse_long(fp);
	    if (pers_arr[num])
		free_person(pers_arr[num]);
	    pers_arr[num] = alloc_person();
	    if ( fparse_person(fp, pers_arr[ num ]) != OK )
		restart_kom("init_cache: fparse_person failed. i == %d\n",
			    i);
	    break;
	    
	case 'T':
	    if (data_file_version == 0)
		restart_kom("init_cache(): new type record in old file\n");

	    num = fparse_long(fp);
	    if (text_arr[num])
		free_text_stat(text_arr[num]);
	    text_arr[ num ] = alloc_text_stat();
	    if ( fparse_text_stat(fp, text_arr[ num ]) != OK )
	    {
		/* Simply ignore the following texts.  The cause of this
		   error might be that the file has been truncated (maybe
		   due to an overfull disk). */
		log("init_cache(): fparse_text_stat failed for text %d.\n%s",
		    i, "All remaining texts are lost.\n");
		next_text_num = i;
		read_text_num = 1;
		truncated_texts = TRUE;
		done = 1;
	    }
	    break;	
    
Per Cederqvist's avatar
Per Cederqvist committed
932
	case '+':
David Byers's avatar
David Byers committed
933 934 935 936
	    if (data_file_version != 0)
		restart_kom("init_cache(): %s\n",
			    "Old type record found in new type file");
	    
Per Cederqvist's avatar
Per Cederqvist committed
937 938
	    conf_arr[ i ] = alloc_conference();
	    if ( fparse_conference(fp, conf_arr[ i ]) != OK )
Per Cederqvist's avatar
Per Cederqvist committed
939 940
		restart_kom("init_cache(): %s. i == %d\n", 
			    "fparse_conference failed", i);
Per Cederqvist's avatar
Per Cederqvist committed
941 942 943
	    name_list[i] = EMPTY_STRING;
	    s_strcpy(&name_list[i], conf_arr[ i ]->name);
	    break;
David Byers's avatar
David Byers committed
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

	case '#':
	  if (data_file_version == 0)
	    restart_kom("init_cache(): %s\n",
			"New type record found in old datafile");
	  switch (fgetc(fp))
	  {
	  case 'C':
	      next_free_num = fparse_long(fp);
	      read_conf_num = 1;
	      break;
	  case 'T':
	      next_text_num = fparse_long(fp);
	      read_text_num = 1;
	      break;
	  default:
	      restart_kom("init_cache(): %s\n",
			  "Bad number specification");
	  }
	  break;

	case '-':
	    if (data_file_version == 0)
		restart_kom("init_cache(): %s\n",
			    "New type record found in old datafile");
	    fskipwhite(fp);
	    switch(fgetc(fp))
	    {
	    case 'C':
		num = fparse_long(fp);
		if (conf_arr[num])
		    free_conference(conf_arr[num]);
		conf_arr[num] = NULL;
		break;
	    case 'P':
		num = fparse_long(fp);
		if (pers_arr[num])
		    free_person(pers_arr[num]);
		pers_arr[num] = NULL;
		break;
	    case 'T':
		num = fparse_long(fp);
		if (text_arr[num])
		    free_text_stat(text_arr[num]);
		text_arr[num] = NULL;
		break;
	    }
	    break;

	case '$':
	    done = 1;
	    break;

	default:
	    restart_kom("init_cache(): bad conf format at %d\n", i);
Per Cederqvist's avatar
Per Cederqvist committed
999 1000 1001 1002 1003 1004 1005
	}
    }

    if ( vflag )
	log("Reading %d persons, starting at pos %d.\n",
	    next_free_num-1, ftell(fp));

David Byers's avatar
David Byers committed
1006
    if (data_file_version == 0)
Per Cederqvist's avatar
Per Cederqvist committed
1007
    {
David Byers's avatar
David Byers committed
1008
	for ( i = 1; i < next_free_num; i++ ) /* PERSONS */
Per Cederqvist's avatar
Per Cederqvist committed
1009
	{
David Byers's avatar
David Byers committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
	    fskipwhite(fp);
	    switch(getc(fp))
	    {
	    case '@':
		pers_arr[ i ] = NULL;
		break;

	    case '+':
		pers_arr[ i ] = alloc_person();
		if ( fparse_person(fp, pers_arr[ i ]) != OK )
		    restart_kom("init_cache: fparse_person failed. i == %d\n",
				i);
		break;
	    }
Per Cederqvist's avatar
Per Cederqvist committed
1024 1025
	}

David Byers's avatar
David Byers committed
1026 1027
	next_text_num = fparse_long(fp);	/* NEXT_TEXT_NUM */
	read_text_num = 1;
Per Cederqvist's avatar
Per Cederqvist committed
1028

David Byers's avatar
David Byers committed
1029 1030 1031
	if ( vflag )
	    log("Reading %d texts, starting at pos %d.\n",
		next_text_num-1, ftell(fp));
Per Cederqvist's avatar
Per Cederqvist committed
1032

David Byers's avatar
David Byers committed
1033
	for ( i = 1; i < next_text_num; i++ ) /* TEXT_STATS */
Per Cederqvist's avatar
Per Cederqvist committed
1034
	{
David Byers's avatar
David Byers committed
1035 1036
	    fskipwhite(fp);
	    switch(getc(fp))
1037
	    {
David Byers's avatar
David Byers committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	    case '@':
		text_arr[ i ] = NULL;
		break;

	    case '+':
		text_arr[ i ] = alloc_text_stat();
		if ( fparse_text_stat(fp, text_arr[ i ]) != OK )
		{
		    /* Simply ignore the following texts.  The cause of this
		       error might be that the file has been truncated (maybe
		       due to an overfull disk). */

		    log("init_cache(): fparse_text_stat failed for text %d.\n%s",
			i, "All remaining texts are lost.\n");
		    next_text_num = i;
		    truncated_texts = TRUE;
		}
		break;
1056
	    }
Per Cederqvist's avatar
Per Cederqvist committed
1057
	}
David Byers's avatar
David Byers committed
1058
    } /* if (datafile_version == 0) */
Per Cederqvist's avatar
Per Cederqvist committed
1059 1060 1061 1062

    log("Read %d confs/persons and %d texts, eof at %d\n",
	next_free_num-1, next_text_num-1, ftell(fp));

David Byers's avatar
David Byers committed
1063 1064
    if (read_text_num == 0 || read_conf_num == 0)
      restart_kom("init_cache(): Failed to read next_conf or text num.\n");
Per Cederqvist's avatar
Per Cederqvist committed
1065 1066 1067 1068
    fclose(fp);

    return OK;
}