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

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

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

Per Cederqvist's avatar
Per Cederqvist committed
36
37
38
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
39
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
40
41
42
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
43
#include "timewrap.h"
44
#include <sys/types.h>
45
46
47
48
49
50
#ifdef HAVE_CRYPT_H
#  include <crypt.h>
#endif
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
51
52
53
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
54
#include <assert.h>
David Byers's avatar
Server    
David Byers committed
55
#include <setjmp.h>
56
#include <errno.h>
57

Per Cederqvist's avatar
Per Cederqvist committed
58
#include "getopt.h"
Per Cederqvist's avatar
Per Cederqvist committed
59
#include "misc-types.h"
60
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
61
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
62
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
63
64
#include "log.h"
#include "server/smalloc.h"
Per Cederqvist's avatar
Per Cederqvist committed
65
#include "misc-parser.h"
Per Cederqvist's avatar
Per Cederqvist committed
66
#include "cache.h"
David Byers's avatar
David Byers committed
67
#include "kom-config.h"
Per Cederqvist's avatar
Per Cederqvist committed
68
#include "debug.h"
Per Cederqvist's avatar
Per Cederqvist committed
69
#include "dbck-cache.h"
70
#include "conf-file.h"
71
72
#include "param.h"
#include "server-config.h"
David Byers's avatar
Server    
David Byers committed
73
74
75
#include "async.h"
#include "com.h"
#include "connections.h"
76
#include "kom-errno.h"
77
#include "manipulate.h"
Per Cederqvist's avatar
Per Cederqvist committed
78
#include "version-info.h"
79
#include "unused.h"
80
#include "ram-io.h"
81
#include "ram-output.h"
82
#include "local-to-global.h"
83
#include "lockdb.h"
Per Cederqvist's avatar
Per Cederqvist committed
84
#include "linkansi.h"
85
#include "eintr.h"
86
#include "kom-memory.h"
87
#include "server-time.h"
Per Cederqvist's avatar
Per Cederqvist committed
88

89
90
91
92
93
94
95
96

#define OPT_PERS_PRESENTATION_CONF      1
#define OPT_CONF_PRESENTATION_CONF      2
#define OPT_MOTD_CONF                   3
#define OPT_MOTD_OF_KOM                 4
#define OPT_KOM_NEWS_CONF               5


Per Cederqvist's avatar
Per Cederqvist committed
97
98
99
100
101
102
103
104
105
106
107
108
static struct option longopts[] = {
    {"compact-text-mass", 0, 0, 'g' },
    {"interactive", 0, 0, 'i' },
    {"auto-repair", 0, 0, 'r' },
    {"verbose", 0, 0, 'v' },
    {"print-statistics", 0, 0, 's' },
    {"list-text-no", 0, 0, 't' },
    {"set-change-name", 0, 0, 'c' },
    {"clear-password", required_argument, 0, 'P' },
    {"grant-all", required_argument, 0, 'G' },
    {"output-version", required_argument, 0, 'o' },
    {"force-output", 0, 0, 'F' },
109
110
111
112
113
114
115
    {"pers-pres-conf", required_argument, 0, 
     OPT_PERS_PRESENTATION_CONF },
    {"conf-pres-conf", required_argument, 0, 
     OPT_CONF_PRESENTATION_CONF },
    {"motd-conf", required_argument, 0, OPT_MOTD_CONF },
    {"motd-of-kom", required_argument, 0, OPT_MOTD_OF_KOM },
    {"kom-news-conf", required_argument, 0, OPT_KOM_NEWS_CONF },
Per Cederqvist's avatar
Per Cederqvist committed
116
117
118
119
120
    {"help", 0, 0, 'h' },
    { 0, 0, 0, 0 }
};

    
121

122
extern Info kom_info;
Per Cederqvist's avatar
Per Cederqvist committed
123

124
125
126
127
/* This is set to TRUE if init_cache finds out that the last part of the
   database is missing. */
Bool truncated_texts = FALSE;

Per Cederqvist's avatar
Per Cederqvist committed
128
int vflag=0;			/* Verbose - list statistics also. */
Per Cederqvist's avatar
Per Cederqvist committed
129
130
131
132
static int iflag=0;		/* Interactive - prompt user and repair. */
static int rflag=0;		/* Repair simple error without confirmation. */
static int gflag=0;		/* Garbage collect text-file. */
static int sflag=0;		/* Statistic flag. */
Per Cederqvist's avatar
Per Cederqvist committed
133
134
static Pers_no reset_pwd=0;	/* Person whose password should be cleared. */
static Pers_no grant_all=0;	/* Person which should receive all bits. */
135

Per Cederqvist's avatar
Per Cederqvist committed
136
static long force_output=0;	/* Force sync (for conversions) */
137

138
139
140
141
142
/* The following variables hold kom_info modifications */

int pers_pres_conf = -1;
int conf_pres_conf = -1;
int motd_conf = -1;
143
Text_no motd_of_lyskom = (Text_no)-1;
144
145
146
int kom_news_conf = -1;


147
148
/* The following variable corresponds to the -c flag, and is
   present here due to a bug in lyskomd 1.6.1. */
Per Cederqvist's avatar
Per Cederqvist committed
149
static int unset_change_name_is_error=0;
150

151
152
/* A list of all available text numbers was useful when developing
   new data structures in lyskomd in 1995.  Option: -t. */
Per Cederqvist's avatar
Per Cederqvist committed
153
static int dump_text_numbers=0;
154

Per Cederqvist's avatar
Per Cederqvist committed
155
156
157
158
159
160
161
162
int modifications = 0;

typedef struct {
    int	created_confs;
} Person_scratchpad;

static const Person_scratchpad   EMPTY_PERSON_SCRATCHPAD = { 0 };

David Byers's avatar
David Byers committed
163
static Person_scratchpad **person_scratchpad = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
164

David Byers's avatar
David Byers committed
165
166
int buglevel = 0;
BUGDECL;
Per Cederqvist's avatar
Per Cederqvist committed
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
193
194
195
196
197
198
199

struct delete_list {
    struct delete_list *next;
    Local_text_no lno;
};

static void
delete_list_append(struct delete_list **head,
		   Local_text_no lno)
{
    struct delete_list *item = smalloc(sizeof(struct delete_list));
    item->next = *head;
    item->lno =  lno;
    *head = item;
}

static void
execute_deletions(Local_to_global *l2g,
		  struct delete_list **head)
{
    struct delete_list *item;

    while (*head != NULL)
    {
	item = *head;
	*head = item->next;
	l2g_delete(l2g, item->lno);
	sfree(item);
    }
}


Per Cederqvist's avatar
Per Cederqvist committed
200
201
202
203
204
205
206
207
208
209
static Person_scratchpad *
alloc_person_scratchpad(void)
{
    Person_scratchpad *p;

    p = smalloc(sizeof(Person_scratchpad));
    *p = EMPTY_PERSON_SCRATCHPAD;
    return p;
}

210
211
212
213
214
215
216
static void
free_person_scratchpad(Person_scratchpad *p)
{
    if (p != NULL)
	sfree(p);
}

Per Cederqvist's avatar
Per Cederqvist committed
217
218
219
220
221
222
223
224
225
226
227
228

static Bool
is_comment_to(Text_no    comment,
	      Text_stat *parent)
{
    int i;

    for ( i = 0; i < parent->no_of_misc; i++ )
    {
	switch( parent->misc_items[ i ].type )
	{
	case comm_in:
229
	    if ( parent->misc_items[ i ].datum.text_link == comment )
Per Cederqvist's avatar
Per Cederqvist committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}

static Bool
is_commented_in(Text_no    parent,
		Text_stat *child)
{
    int i;

    for ( i = 0; i < child->no_of_misc; i++ )
    {
	switch( child->misc_items[ i ].type )
	{
	case comm_to:
251
	    if ( child->misc_items[ i ].datum.text_link == parent )
Per Cederqvist's avatar
Per Cederqvist committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}
	
static Bool
is_footnote_to(Text_no    footnote,
	       Text_stat *parent)
{
    int i;

    for ( i = 0; i < parent->no_of_misc; i++ )
    {
	switch( parent->misc_items[ i ].type )
	{
	case footn_in:
273
	    if ( parent->misc_items[ i ].datum.text_link == footnote )
Per Cederqvist's avatar
Per Cederqvist committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}

static Bool
is_footnoted_in(Text_no    parent,
		Text_stat *child)
{
    int i;

    for ( i = 0; i < child->no_of_misc; i++ )
    {
	switch( child->misc_items[ i ].type )
	{
	case footn_to:
295
	    if ( child->misc_items[ i ].datum.text_link == parent )
Per Cederqvist's avatar
Per Cederqvist committed
296
297
298
299
300
301
302
303
304
305
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}

306
Member *
Per Cederqvist's avatar
Per Cederqvist committed
307
308
309
locate_member(Pers_no      pers_no,
	      Conference * conf_c)
{
310
    Member * mbr;
Per Cederqvist's avatar
Per Cederqvist committed
311
312
    int      i;

313
314
    for(mbr = conf_c->members.members, i = conf_c->members.no_of_members;
	i > 0; i--, mbr++)
Per Cederqvist's avatar
Per Cederqvist committed
315
    {
316
	if ( mbr->member == pers_no )
Per Cederqvist's avatar
Per Cederqvist committed
317
	{
318
	    return mbr;
Per Cederqvist's avatar
Per Cederqvist committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
	}
    }

    return NULL;
}

/*
 * Delete a misc_info.
 * If it is a recpt, cc_recpt, comm_to or footn_to delete any
 * loc_no, rec_time, sent_by or sent_at that might follow it.
 *
 * Note that the Misc_info is not reallocated.
 */

static void
delete_misc (Text_stat *tstat,
	     Misc_info *misc)	/* Pointer to first misc_item to delete. */
{
    int del = 1;		/* Number of items to delete. */
    				/* Always delete at least one item. */
339
    Bool ready = FALSE;
Per Cederqvist's avatar
Per Cederqvist committed
340
341
342
343
344
345
    
    /* Check range of misc */

    if (misc < tstat->misc_items
	|| misc >= tstat->misc_items + tstat->no_of_misc )
    {
346
	restart_kom("delete_misc() - misc out of range\n");
Per Cederqvist's avatar
Per Cederqvist committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    }

    while (ready == FALSE
	   && misc + del < tstat->misc_items + tstat->no_of_misc )
    {
	switch ( misc[ del ].type )
	{
	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    del++;
	    break;

	case recpt:
	case cc_recpt:
363
        case bcc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
364
365
366
367
368
369
370
371
372
373
	case footn_to:
	case footn_in:
	case comm_to:
	case comm_in:
	    ready = TRUE;
	    break;

#ifndef COMPILE_CHECKS
	default:
#endif
374
375
	case unknown_info:
	    restart_kom("delete_misc() - illegal misc found.\n");
Per Cederqvist's avatar
Per Cederqvist committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
	}
    }

    tstat->no_of_misc -= del;

    /* Move items beyond the deleted ones. */

    while ( misc < tstat->misc_items + tstat->no_of_misc )
    {
	misc[ 0 ] = misc[ del ];
	misc++;
    }
}

static int
391
confirm(const char *question)
Per Cederqvist's avatar
Per Cederqvist committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
{
    if ( iflag )
    {
	fputs(question, stdout);
	fputs(" (y/n) ", stdout);
	while(1)
	    switch(getchar())
	    {
	    case 'y':
	    case 'Y':
		return 1;
	    case 'n':
	    case 'N':
	    case EOF:
		return 0;
	    default:
		break;
	    }
    }
    else
	return 0;
}
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


static long
check_misc_any_recipient(Text_no tno,
			 Text_stat *tstat,
			 const char *pretty_type,
			 Conf_no rcpt,
			 Local_text_no lno,
			 Misc_info *previous,
			 const Misc_info **misc)
{
    Conference *c;
    long errors = 0;
    Local_text_no conf_min;
    Local_text_no conf_max;

    c = cached_get_conf_stat(rcpt);
    if (c == NULL && rcpt == 0)
    {
	kom_log("Conference 0 is a %s of text %lu.\n",
		pretty_type, (unsigned long)tno);
	if (rflag || confirm("Repair by deleting misc_item? "))
	{
	    delete_misc(tstat, previous);
	    kom_log("Repaired: Conference 0 is no longer a %s.\n",
		    pretty_type);
	    mark_text_as_changed(tno);
	    modifications++;
	    *misc = previous;
	}
	else
	    errors++;

	return errors;
    }

    if (c == NULL)
	return errors;

    /* Check loc_no */
    conf_min = l2g_lookup(&c->texts, 0);
    conf_max = l2g_first_appendable_key(&c->texts);

    if (lno < conf_min)
    {
	kom_log("Text %lu: %s %lu<%lu>: loc_no is less than %lu\n",
		(unsigned long)tno,
		pretty_type,
		(unsigned long)rcpt,
		(unsigned long)lno,
		(unsigned long)conf_min);
	errors++;
    }
    else if (lno >= conf_max)
    {
	kom_log("Text %lu: %s %lu<%lu>: loc_no is greater than %lu\n",
		(unsigned long)tno,
		pretty_type,
		(unsigned long)rcpt,
		(unsigned long)lno,
		(unsigned long)conf_max);
	errors++;
    }
    else if (l2g_lookup(&c->texts, lno) != tno)
    {
	kom_log("Text %lu: %s %lu<%lu>: that local number is mapped to %lu.\n",
		(unsigned long)tno,
		pretty_type,
		(unsigned long)rcpt,
		(unsigned long)lno,
		(unsigned long)l2g_lookup(&c->texts, lno));
	errors++;
    }

    return errors;
}

Per Cederqvist's avatar
Per Cederqvist committed
491
492
493
494
495
496
497
498
499
    
static long
check_misc_infos(Text_no    tno,
		 Text_stat *tstat)
{
    const Misc_info   * misc = tstat->misc_items;
    Misc_info   * previous;
    Misc_info_group group;
    Text_stat *t;
500
    
501
    long errors=0;
Per Cederqvist's avatar
Per Cederqvist committed
502
503
504
505
506
507
508
509
510

    while (previous = (Misc_info *)misc,
	   group = parse_next_misc(&misc,
				   tstat->misc_items + tstat->no_of_misc),
	   group.type != m_end_of_list && group.type != m_error )
    {
	switch ( group.type )
	{
	case m_recpt:
511
512
513
	    check_misc_any_recipient(tno, tstat, "recipient",
				     group.recipient, group.local_no,
				     previous, &misc);
Per Cederqvist's avatar
Per Cederqvist committed
514
515
516
	    break;

	case m_cc_recpt:
517
518
519
	    check_misc_any_recipient(tno, tstat, "cc_recipient",
				     group.cc_recipient, group.local_no,
				     previous, &misc);
Per Cederqvist's avatar
Per Cederqvist committed
520
521
	    break;

522
	case m_bcc_recpt:
523
524
525
	    check_misc_any_recipient(tno, tstat, "bcc_recipient",
				     group.bcc_recipient, group.local_no,
				     previous, &misc);
526
527
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
528
529
530
531
532
	case m_comm_to:
	    t = cached_get_text_stat(group.comment_to);

	    if ( t == NULL )
	    {
David Byers's avatar
David Byers committed
533
		kom_log("Text %lu is a comment to %lu, which doesn't exist.\n",
534
		    (unsigned long)tno, (unsigned long)group.comment_to);
Per Cederqvist's avatar
Per Cederqvist committed
535
536
537
538
539
540

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
541
		    kom_log("Repaired: Comment-link deleted.\n");
Per Cederqvist's avatar
Per Cederqvist committed
542
543
544
		    misc = previous;
		}
		else
545
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
546
		    
547
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
548
549
550
	    }
	    else if (!is_comment_to(tno, t))
	    {
David Byers's avatar
David Byers committed
551
		kom_log("Text %lu is a comment to %lu, but not the reverse.\n",
552
		    (unsigned long)tno, (unsigned long)group.comment_to);
553
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
554
555
556
557
558
559
560
561
562
	    }

	    break;

	case m_comm_in:
	    t = cached_get_text_stat(group.commented_in);

	    if ( t == NULL )
	    {
David Byers's avatar
David Byers committed
563
		kom_log("Text %lu is commented in %lu, which doesn't exist.\n",
564
		    (unsigned long)tno, (unsigned long)group.commented_in);
Per Cederqvist's avatar
Per Cederqvist committed
565
566
567
568
569
570

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
571
		    kom_log("Repaired: Comment-link deleted.\n");
Per Cederqvist's avatar
Per Cederqvist committed
572
573
574
		    misc = previous;
		}
		else
575
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
576
577
578
	    }
	    else if (!is_commented_in(tno, t))
	    {
David Byers's avatar
David Byers committed
579
		kom_log("Text %lu is commented in %lu, but not the reverse.\n",
580
		    (unsigned long)tno, (unsigned long)group.commented_in);
581
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
582
583
584
585
586
587
588
589
590
	    }

	    break;

	case m_footn_to:
	    t = cached_get_text_stat(group.footnote_to);

	    if ( t == NULL )
	    {
David Byers's avatar
David Byers committed
591
		kom_log("Text %lu is a footnote to %lu, which doesn't exist.\n",
592
		    (unsigned long)tno, (unsigned long)group.footnote_to);
Per Cederqvist's avatar
Per Cederqvist committed
593
594
595
596
597
598

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
599
		    kom_log("Repaired: Footnote-link deleted.\n");
Per Cederqvist's avatar
Per Cederqvist committed
600
601
602
		    misc = previous;
		}
		else
603
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
604
605
606
	    }
	    else if (!is_footnote_to(tno, t))
	    {
David Byers's avatar
David Byers committed
607
		kom_log("Text %lu is a footnote to %lu, but not the reverse.\n",
608
		    (unsigned long)tno, (unsigned long)group.footnote_to);
609
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
610
611
612
613
614
615
616
617
618
	    }

	    break;
	    
	case m_footn_in:
	    t = cached_get_text_stat(group.footnoted_in);

	    if ( t == NULL )
	    {
David Byers's avatar
David Byers committed
619
		kom_log("Text %lu is footnoted in %lu, which doesn't exist.\n",
620
		    (unsigned long)tno, (unsigned long)group.footnoted_in);
Per Cederqvist's avatar
Per Cederqvist committed
621
622
623
624
625
626

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
627
		    kom_log("Repaired: Footnote-link deleted.\n");
Per Cederqvist's avatar
Per Cederqvist committed
628
629
630
		    misc = previous;
		}
		else
631
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
632
633
634
	    }
	    else if (!is_footnoted_in(tno, t))
	    {
David Byers's avatar
David Byers committed
635
		kom_log("Text %lu is footnoted in %lu, but not the reverse.\n",
636
		    (unsigned long)tno, (unsigned long)group.footnoted_in);
637
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
638
639
640
641
642
	    }

	    break;

	default:
David Byers's avatar
David Byers committed
643
	    kom_log("check_misc_infos(): parse_next_misc returned type %lu\n",
644
		(unsigned long)group.type);
Per Cederqvist's avatar
Per Cederqvist committed
645
646
647
648
649
650
	    break;
	}
    }

    if ( group.type == m_error )
    {
David Byers's avatar
David Byers committed
651
	kom_log("Text %lu has a bad misc_info_list.\n", (unsigned long)tno);
652
	errors++;
Per Cederqvist's avatar
Per Cederqvist committed
653
654
    }

655
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
656
657
658
659
660
661
662
663
664
665
666
667
}

		    
		     
		 
static long
check_texts(void)
{
    Text_no    ct = 0;
    Text_stat *ctp=NULL;
    long	errors = 0;
    Text_no	number_of_texts = 0;
668
669
    unsigned long	bytes=0;
    unsigned long	max_bytes=0;
Per Cederqvist's avatar
Per Cederqvist committed
670
671
    Text_no	max_text=0;

672
    while ( (ct = traverse_text(ct)) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
673
674
675
676
677
678
    {
	number_of_texts++;
	
	ctp = cached_get_text_stat( ct );
	if ( ctp == NULL )
	{
David Byers's avatar
David Byers committed
679
	    kom_log("Text %lu nonexistent.\n", ct);
Per Cederqvist's avatar
Per Cederqvist committed
680
681
682
683
	    errors++;
	}
	else
	{
684
	    if (dump_text_numbers)
David Byers's avatar
David Byers committed
685
		kom_log("Checking text_no %ld\n", (unsigned long)ct);
686

Per Cederqvist's avatar
Per Cederqvist committed
687
	    bytes += ctp->no_of_chars;
David Byers's avatar
David Byers committed
688
	    if ( (unsigned long)ctp->no_of_chars > max_bytes )
Per Cederqvist's avatar
Per Cederqvist committed
689
690
691
692
693
	    {
		max_bytes = ctp->no_of_chars;
		max_text = ct;
	    }
	    
694
	    /* FIXME (bug 147): no_of_marks is not yet checked. */
Per Cederqvist's avatar
Per Cederqvist committed
695
696
697
698
699
700
701
	    errors += check_misc_infos(ct, ctp);
	}
    }

    if (vflag)
    {
	if ( number_of_texts == 0 )
David Byers's avatar
David Byers committed
702
	    kom_log("WARNING: No texts found.\n");
Per Cederqvist's avatar
Per Cederqvist committed
703
704
	else
	{
David Byers's avatar
David Byers committed
705
	    kom_log("Total of %lu texts (total %lu bytes, avg. %lu bytes/text).\n",
706
707
708
		(unsigned long)number_of_texts,
		(unsigned long)bytes,
		(unsigned long)(bytes/number_of_texts));
David Byers's avatar
David Byers committed
709
	    kom_log("Longest text is %lu (%lu bytes).\n",
710
		(unsigned long)max_text, (unsigned long)max_bytes);
Per Cederqvist's avatar
Per Cederqvist committed
711
712
713
714
715
716
717
718
719
	}
    }
    
    return errors;
}


static int
check_created_texts(Pers_no pno,
720
		    Local_to_global *created)
Per Cederqvist's avatar
Per Cederqvist committed
721
722
{
    Text_stat *t;
723
    int errors=0;
724
725
    L2g_iterator iter;
    struct delete_list *del_list = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
726

727
    for (l2gi_searchall(&iter, created); !iter.search_ended; l2gi_next(&iter))
Per Cederqvist's avatar
Per Cederqvist committed
728
    {
729
730
	assert(iter.tno != 0);
	assert(iter.lno != 0);
Per Cederqvist's avatar
Per Cederqvist committed
731

732
733
734
	t = cached_get_text_stat(iter.tno);
	if ( t != NULL && t->author != pno)
	{
David Byers's avatar
David Byers committed
735
	    kom_log("Person %lu is author of text %lu whose author is %lu.\n",
736
737
738
		(unsigned long)pno, (unsigned long)iter.tno,
		(unsigned long)t->author);
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
739
740
	}

741
	if ( t == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
742
	{
David Byers's avatar
David Byers committed
743
	    kom_log("Person %lu is author of text %lu, which doesn't exist.\n",
744
745
746
747
748
749
750
		(unsigned long)pno, (unsigned long)iter.tno);
	    if ( rflag ||
		 confirm("Repair by setting to text_no to 0 in local map"))
	    {
		delete_list_append(&del_list, iter.lno);
		mark_person_as_changed(pno);
		modifications++;
David Byers's avatar
David Byers committed
751
		kom_log("Repaired: created_texts corrected.\n");
752
753
754
	    }
	    else
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
755
	}
756
	
Per Cederqvist's avatar
Per Cederqvist committed
757
    }
758
    execute_deletions(created, &del_list);
Per Cederqvist's avatar
Per Cederqvist committed
759
    
760
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
761
762
763
764
}

static int
check_membership(Pers_no pno,
David Byers's avatar
David Byers committed
765
		 Membership *mship)
Per Cederqvist's avatar
Per Cederqvist committed
766
{
767
    int errors=0;
Per Cederqvist's avatar
Per Cederqvist committed
768
769
    Conference *conf;
    Local_text_no last=0;
David Byers's avatar
David Byers committed
770
    Member *mem;
771
772
773
774
    struct read_range *begin;
    struct read_range *end;
    struct read_range *ptr;
    
Per Cederqvist's avatar
Per Cederqvist committed
775
776
777
778
    
    conf = cached_get_conf_stat(mship->conf_no);
    if ( conf == NULL )
    {
David Byers's avatar
David Byers committed
779
	kom_log("Person %lu is a member in the non-existing conference %lu.\n",
780
	    (unsigned long)pno, (unsigned long)mship->conf_no);
781
	errors++;
Per Cederqvist's avatar
Per Cederqvist committed
782
783
784
785
786
    }
    else
    {
	/* Check read texts */

787
788
	last = 0;
	if (mship->no_of_read_ranges > 0)
Per Cederqvist's avatar
Per Cederqvist committed
789
	{
790
791
792
793
	    begin = &mship->read_ranges[0];
	    end = begin + mship->no_of_read_ranges;

	    for (ptr = begin; ptr < end; ptr++)
Per Cederqvist's avatar
Per Cederqvist committed
794
	    {
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
		if (ptr->first_read > ptr->last_read)
		{
		    kom_log("Person %lu's membership in %lu is corrupt: "
			    "bad range: %lu-%lu.\n",
			    (unsigned long)pno, (unsigned long)mship->conf_no,
			    (unsigned long)ptr->first_read,
			    (unsigned long)ptr->last_read);
		    errors++;
		}
		if (ptr != begin && last + 1 == ptr->first_read)
		{
		    kom_log("Person %lu's membership in %lu is corrupt: "
			    "adjoining ranges not joined at around %lu.\n",
			    (unsigned long)pno, (unsigned long)mship->conf_no,
			    (unsigned long)ptr->first_read);
		    errors++;
		}
		if (ptr != begin && last >= ptr->first_read)
		{
		    kom_log("Person %lu's membership in %lu is corrupt: "
			    "overlapping ranges: %lu-%lu, %lu-%lu.\n",
			    (unsigned long)pno, (unsigned long)mship->conf_no,
			    (unsigned long)(ptr-1)->first_read,
			    (unsigned long)(ptr-1)->last_read,
			    (unsigned long)ptr->first_read,
			    (unsigned long)ptr->last_read);
		    errors++;
		}
		last = ptr->last_read;
Per Cederqvist's avatar
Per Cederqvist committed
824
	    }
825
826
827
828
829
830
831
832
833
834
835
	}
	
	if (last >= l2g_first_appendable_key(&conf->texts))
	{
	    kom_log("Person %lu has read text %lu in conf %lu, "
		    "which only has %lu texts.\n",
		    (unsigned long)pno,
		    (unsigned long)last,
		    (unsigned long)mship->conf_no,
		    (unsigned long)(l2g_first_appendable_key(&conf->texts)-1));
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
836
837
838
	}

	/* Check that he is a member */
David Byers's avatar
David Byers committed
839
	if ( (mem = locate_member(pno, conf)) == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
840
	{
David Byers's avatar
David Byers committed
841
	    kom_log("Person %lu is a member in %lu in which he isn't a member.\n",
842
		(unsigned long)pno, (unsigned long)mship->conf_no);
843
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
844
	}
David Byers's avatar
David Byers committed
845
846
847
848
849
850
851
852
853
854
        else
        {
            /* Check duplicated information
               Short circuit and in each check sets foo_e
               if an error has been detected
            */

            if (mem->type.invitation != mship->type.invitation ||
                mem->type.passive != mship->type.passive ||
                mem->type.secret != mship->type.secret ||
855
856
                mem->type.passive_message_invert !=
		mship->type.passive_message_invert ||
David Byers's avatar
David Byers committed
857
858
859
860
861
862
863
                mem->type.reserved2 != mship->type.reserved2 ||
                mem->type.reserved3 != mship->type.reserved3 ||
                mem->type.reserved4 != mship->type.reserved4 ||
                mem->type.reserved5 != mship->type.reserved5 ||
                mem->added_at != mship->added_at ||
                mem->added_by != mship->added_by)
            {
David Byers's avatar
David Byers committed
864
                kom_log("Person %lu membership in %lu does not match member record.\n",
David Byers's avatar
David Byers committed
865
866
867
868
869
870
871
872
873
874
875
                    (unsigned long)pno,
                    (unsigned long)mship->conf_no
                    );

                errors++;

                fputs("Membership:", stdout);
                printf("  type:      %s%s%s%s%s%s%s%s\n",
                       mship->type.invitation ? "invitation " : "",
                       mship->type.passive ? "passive " : "",
                       mship->type.secret ? "secret " : "",
876
877
                       mship->type.passive_message_invert ?
		       "passive_message_invert "  : "",
David Byers's avatar
David Byers committed
878
879
880
881
882
883
884
885
886
887
888
                       mship->type.reserved2 ? "rsv2 "  : "",
                       mship->type.reserved3 ? "rsv3 "  : "",
                       mship->type.reserved4 ? "rsv4 "  : "",
                       mship->type.reserved5 ? "rsv5"  : "");
                printf("  added_by: %lu\n", (unsigned long)mship->added_by);
                printf("  added_at: %lu\n", (unsigned long)mship->added_at);
                fputs("Member:", stdout);
                printf("  type:      %s%s%s%s%s%s%s%s\n",
                       mem->type.invitation ? "invitation " : "",
                       mem->type.passive ? "passive " : "",
                       mem->type.secret ? "secret " : "",
889
890
                       mem->type.passive_message_invert ?
		       "passive_message_invert "  : "",
David Byers's avatar
David Byers committed
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
                       mem->type.reserved2 ? "rsv2 "  : "",
                       mem->type.reserved3 ? "rsv3 "  : "",
                       mem->type.reserved4 ? "rsv4 "  : "",
                       mem->type.reserved5 ? "rsv5"  : "");
                printf("  added_by: %lu\n", (unsigned long)mem->added_by);
                printf("  added_at: %lu\n", (unsigned long)mem->added_at);

                if (confirm("Copy membership to member"))
                {
                    mem->added_at = mship->added_at;
                    mem->added_by = mship->added_by;
                    mem->type = mship->type;
                    modifications++;
                }
                else if (confirm("Copy member to membership"))
                {
                    mship->added_at = mem->added_at;
                    mship->added_by = mem->added_by;
                    mship->type = mem->type;
                    modifications++;
                }
            }
        }
Per Cederqvist's avatar
Per Cederqvist committed
914
915
    }

916
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
}

	    

static int
check_membership_list(Pers_no pno,
		      const Membership_list *mlist)
{
    int errors=0;
    int i;
    
    for (i = 0; i < mlist->no_of_confs; i++)
	errors += check_membership(pno, &mlist->confs[i]);

    return errors;
}


static int
check_persons(void)
{
Per Cederqvist's avatar
Per Cederqvist committed
938
939
    static char crypt_seed[] = 
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
Per Cederqvist's avatar
Per Cederqvist committed
940
941
942
943
944
945
    Pers_no     cp = 0;
    Person     *pstat=NULL;
    Conference *cstat=NULL;
    long	errors = 0;
    Pers_no	number_of_persons=0;

946
    while ( (cp = traverse_person(cp)) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
947
948
949
950
951
952
953
    {
	number_of_persons++;
	pstat = cached_get_person_stat (cp);
	cstat = cached_get_conf_stat (cp);
	
	if ( pstat == NULL )
	{
David Byers's avatar
David Byers committed
954
	    kom_log("Person %lu nonexistent.\n", (unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
955
956
957
958
	    errors++;
	}
	else if (cstat == NULL)
	{
David Byers's avatar
David Byers committed
959
	    kom_log("Person %lu has no conference.\n", (unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
960
961
962
963
	    errors++;
	}
	else if (!cstat->type.letter_box)
	{
David Byers's avatar
David Byers committed
964
	    kom_log("Person %lu's conference is not a letter_box.\n",
965
		(unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
966
967
968
969
970
971
972
	    errors++;
	}
	else
	{
	    errors += (check_created_texts(cp, &pstat->created_texts)
		       + check_membership_list(cp, &pstat->conferences));
	}
973
974
975
976

	if (unset_change_name_is_error == 1
	    && pstat->privileges.change_name == 0)
	{
David Byers's avatar
David Byers committed
977
	    kom_log("Person %lu has no change_name capability.\n",
978
		(unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
979

980
981
982
983
984
985
986
987
988
	    if (rflag || confirm("Grant him the capability"))
	    {
		pstat->privileges.change_name = 1;
		mark_person_as_changed(cp);
		modifications++;
	    }
	    else
		errors++;
	}
Per Cederqvist's avatar
Per Cederqvist committed
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
	if (cp == reset_pwd)
	{
	    char salt[3];
	  
	    salt[0] = crypt_seed [rand() % (sizeof (crypt_seed) - 1)];
	    salt[1] = crypt_seed [rand() % (sizeof (crypt_seed) - 1)];
	    salt[2] = '\0';
	  
	    strcpy((char *)pstat->pwd, (const char *)crypt("", salt));
	    mark_person_as_changed(cp);
	    modifications++;
	}

	if (cp == grant_all)
	{
	    pstat->privileges.wheel = 1;
	    pstat->privileges.admin = 1;
	    pstat->privileges.statistic = 1;
	    pstat->privileges.create_pers = 1;
	    pstat->privileges.create_conf = 1;
	    pstat->privileges.change_name = 1;
1010
	    pstat->privileges.flg7 = 1;
Per Cederqvist's avatar
Per Cederqvist committed
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
	    pstat->privileges.flg8 = 1;
	    pstat->privileges.flg9 = 1;
	    pstat->privileges.flg10 = 1;
	    pstat->privileges.flg11 = 1;
	    pstat->privileges.flg12 = 1;
	    pstat->privileges.flg13 = 1;
	    pstat->privileges.flg14 = 1;
	    pstat->privileges.flg15 = 1;
	    pstat->privileges.flg16 = 1;
	    mark_person_as_changed(cp);
	    modifications++;
	}
Per Cederqvist's avatar
Per Cederqvist committed
1023
1024
1025
    }

    if (vflag)
David Byers's avatar
David Byers committed
1026
	kom_log("Total of %lu persons.\n", (unsigned long)number_of_persons);
Per Cederqvist's avatar
Per Cederqvist committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
    
    return errors;
}

static Bool
is_recipient(Conf_no	 conf_no,
	     Text_stat * t_stat)
{
    int i;
    
    for ( i = 0; i < t_stat->no_of_misc; i++ )
    {
	switch( t_stat->misc_items[ i ].type )
	{
	case recpt:
	case cc_recpt:
1043
	case bcc_recpt:
1044
	    if ( t_stat->misc_items[ i ].datum.recipient == conf_no )
1045
1046
1047
1048
	    {
		return TRUE;
	    }
	    break;
1049

Per Cederqvist's avatar
Per Cederqvist committed
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
	case rec_time:
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	case sent_by:
	case sent_at:
	case loc_no:
	    break;
	    
#ifndef COMPILE_CHECKS
	default:
#endif
1063
1064
	case unknown_info:
	    restart_kom("is_recipient(): illegal misc_item\n");
Per Cederqvist's avatar
Per Cederqvist committed
1065
1066
1067
1068
1069
1070
1071
1072
	}
    }

    return FALSE;
}

static int
check_texts_in_conf(Conf_no cc,
1073
		    Local_to_global *tlist)
Per Cederqvist's avatar
Per Cederqvist committed
1074
1075
{
    Text_stat *t;
1076
    int errors=0;
1077
1078
1079
1080
    L2g_iterator iter;
    struct delete_list *del_list = NULL;
    
    for (l2gi_searchall(&iter, tlist); !iter.search_ended; l2gi_next(&iter))
Per Cederqvist's avatar
Per Cederqvist committed
1081
    {
1082
1083
1084
1085
	assert(iter.lno != 0);
	assert(iter.tno != 0);
	t = cached_get_text_stat(iter.tno);
	if (t == NULL)
Per Cederqvist's avatar
Per Cederqvist committed
1086
	{
David Byers's avatar
David Byers committed
1087
	    kom_log("Text %lu<%lu> in conference %lu is non-existent.\n",
1088
1089
1090
1091
1092
		(unsigned long)iter.tno,
		(unsigned long)iter.lno,
		(unsigned long)cc);

	    if (rflag || confirm("Repair by deleting the Text_no in the map?"))
Per Cederqvist's avatar
Per Cederqvist committed
1093
	    {
1094
1095
1096
		delete_list_append(&del_list, iter.lno);
		mark_conference_as_changed(cc);
		modifications++;
David Byers's avatar
David Byers committed
1097
		kom_log("Repaired: %lu is no longer a recipient.\n",
1098
		    (unsigned long)cc);
1099
1100
1101
1102
1103
1104
1105
1106
	    }
	    else
		errors++;
	}
	else
	{
	    if (!is_recipient(cc, t))
	    {
David Byers's avatar
David Byers committed
1107
		kom_log("Text %lu<%lu> in conference %lu %s.\n",
1108
1109
1110
1111
		    (unsigned long)iter.tno,
		    (unsigned long)iter.lno,
		    (unsigned long)cc,
		    "doesn't have the conference as recipient");
Per Cederqvist's avatar
Per Cederqvist committed
1112

1113
		if (confirm("Repair by deleting Text_no from the map?") )
Per Cederqvist's avatar
Per Cederqvist committed
1114
		{
1115
		    delete_list_append(&del_list, iter.lno);
Per Cederqvist's avatar
Per Cederqvist committed
1116
1117
		    mark_conference_as_changed(cc);
		    modifications++;
David Byers's avatar
David Byers committed
1118
		    kom_log("Repaired: %lu is no longer a recipient.\n",
1119
			(unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1120
1121
		}
		else
1122
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
1123
1124
1125
	    }
	}
    }
1126
    execute_deletions(tlist, &del_list);
Per Cederqvist's avatar
Per Cederqvist committed
1127

1128
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
1129
1130
}

1131
Membership *
1132
1133
locate_membership(Conf_no       conf_no,
		  const Person *pers_p)
Per Cederqvist's avatar
Per Cederqvist committed
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
{
    Membership * confp;
    int    i;

    for(confp = pers_p->conferences.confs, i = pers_p->conferences.no_of_confs;
	i > 0; i--, confp++)
    {
	if ( confp->conf_no == conf_no )
	{
	    return confp;
	}
    }

    return NULL;
}

static int
check_member(Conf_no cc,
	     Member *memb)
{
    Person *pp;
1155
    int errors=0;
Per Cederqvist's avatar
Per Cederqvist committed
1156
1157
1158
1159

    pp = cached_get_person_stat(memb->member);
    if ( pp == NULL )
    {
David Byers's avatar
David Byers committed
1160
	kom_log("Person %lu, supposedly a member in conf %lu, is nonexistent.\n",
1161
	    (unsigned long)memb->member, (unsigned long)cc);
1162
	errors++;
Per Cederqvist's avatar
Per Cederqvist committed
1163
1164
1165
1166
1167
    }
    else
    {
	if ( locate_membership(cc, pp) == NULL )
	{
David Byers's avatar
David Byers committed
1168
	    kom_log("Person %lu is not a member in conf %lu.\n",
1169
1170
		(unsigned long)memb->member,
		(unsigned long)cc);
1171
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
1172
1173
1174
	}
    }

1175
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
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
1202
1203
1204
1205
1206
1207
}

static int
check_member_list(Conf_no cc,
		  const Member_list *mlist)
{
    int errors=0;
    int i;
    
    for (i = 0; i < mlist->no_of_members; i++)
	errors += check_member(cc, &mlist->members[i]);

    return errors;
}


static int
check_confs(void)
{
    Conf_no     cc = 0;
    Person     *pstat=NULL;
    Conference *cstat=NULL;
    long	errors = 0;
    Conf_no	number_of_confs = 0;

    while ( (cc = traverse_conference(cc)) != 0 )
    {
	number_of_confs++;
	cstat = cached_get_conf_stat (cc);
	
	if ( cstat == NULL )
	{
David Byers's avatar
David Byers committed
1208
	    kom_log("Conference %lu nonexistent.\n", (unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1209
1210
1211
1212
1213
1214
1215
1216
1217
	    errors++;
	}
	else
	{
	    if (cstat->type.letter_box)
	    {
		pstat = cached_get_person_stat(cc);
		if (pstat == NULL)
		{
David Byers's avatar
David Byers committed
1218
		    kom_log("Mailbox %lu has no person.\n", (unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
		    errors++;
		}
	    }
	    else		/* not letter_box */
	    {
		/* Remember that the creator might no longer exist. */
		if ( person_scratchpad[ cstat->creator ] != NULL )
		    ++person_scratchpad[ cstat->creator ]->created_confs;
	    }

	    errors += (check_texts_in_conf(cc, &cstat->texts)
		       + check_member_list(cc, &cstat->members));
	}
    }

    if ( vflag )
David Byers's avatar
David Byers committed
1235
	kom_log("Total of %lu conferences.\n", (unsigned long)number_of_confs);
Per Cederqvist's avatar
Per Cederqvist committed
1236
1237
1238
1239
1240
1241
1242
1243

    return errors;
}

static void
init_person_scratch(void)
{
    Pers_no pno = 0;
1244
    Pers_no i = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1245
    
David Byers's avatar
David Byers committed
1246
1247
1248
    if (person_scratchpad == NULL)
    {
        person_scratchpad = smalloc(sizeof(*person_scratchpad) * param.max_conf);
1249
1250
        for (i = 0; i < param.max_conf; i++)
            person_scratchpad[i] = NULL;
David Byers's avatar
David Byers committed
1251
1252
    }

Per Cederqvist's avatar
Per Cederqvist committed
1253
1254
1255
1256
1257
1258
    while( (pno = traverse_person(pno)) != 0 )
    {
	person_scratchpad[pno] = alloc_person_scratchpad();
    }
}

1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
static void
free_person_scratch(void)
{
    Pers_no i;

    if (person_scratchpad != NULL)
    {
	for (i = 0; i < param.max_conf; i++)
	    free_person_scratchpad(person_scratchpad[i]);
	sfree(person_scratchpad);
    }
}

Per Cederqvist's avatar
Per Cederqvist committed
1272
1273
1274
1275
1276
1277
1278
static long
post_check_persons(void)
{
    long errors = 0;
    
    Pers_no pers_no = 0;
    Person *pstat;
Per Cederqvist's avatar
Per Cederqvist committed
1279
1280
  
    while ((pers_no = traverse_person(pers_no)) != 0)
Per Cederqvist's avatar
Per Cederqvist committed
1281
    {
Per Cederqvist's avatar
Per Cederqvist committed
1282
	if ((pstat = cached_get_person_stat(pers_no)) == NULL)
Per Cederqvist's avatar
Per Cederqvist committed
1283
	{
David Byers's avatar
David Byers committed
1284
	    kom_log("%s(): can't cached_get_person_stat(%d).\n", 
Per Cederqvist's avatar
Per Cederqvist committed
1285
		"INTERNAL DBCK ERROR: post_check_persons", pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
	}
    }

    return errors;
}

		
/*
 * Returns 0 if the database seems to be correct.
 */
static long
check_data_base(void)
{
    long errors;

    init_person_scratch();
    errors = check_texts() + check_persons() + check_confs();
    return errors + post_check_persons();
}

static void
1307
init_data_base(void)
Per Cederqvist's avatar
Per Cederqvist committed
1308
1309
1310
{
    if ( vflag )
    {
David Byers's avatar
David Byers committed
1311
1312
1313
1314
	kom_log("Database = %s\n", param.datafile_name);
	kom_log("Backup   = %s\n", param.backupfile_name);
	kom_log("Text     = %s\n", param.textfile_name);
	kom_log("Textback = %s\n", param.textbackupfile_name);
Per Cederqvist's avatar
Per Cederqvist committed
1315
1316
1317
1318
1319
1320
    }
    
    if ( init_cache() == FAILURE )
	restart_kom("Can't find database.\n");
}

Per Cederqvist's avatar
Per Cederqvist committed
1321
static void
Per Cederqvist's avatar
Per Cederqvist committed
1322
1323
1324
1325
1326
garb_text_file(void)
{
    Text_no tno = 0;
    String text;

David Byers's avatar
David Byers committed
1327
    kom_log("Renaming %s to %s\n", param.textfile_name, param.textbackupfile_name);
1328
1329
1330
1331
    if (i_rename(param.textfile_name, param.textbackupfile_name) < 0)
    {
	restart_kom("rename failed: %s\n", strerror(errno));
    }
David Byers's avatar
David Byers committed
1332
    kom_log("Writing texts to (new) %s\n", param.textfile_name);
Per Cederqvist's avatar
Per Cederqvist committed
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
    fflush(stdout);
    fflush(stderr);
    cache_open_new_text_file();

    while ( (tno = traverse_text(tno)) != 0 )
    {
	text = cached_get_text(tno);
	cached_flush_text(tno, text);
	free_tmp();
    }
David Byers's avatar
David Byers committed
1343
    kom_log("Writing datafile with new indexes.\n");
Per Cederqvist's avatar
Per Cederqvist committed
1344
1345
    fflush(stdout);
    fflush(stderr);
1346
    cache_sync_all();
Per Cederqvist's avatar
Per Cederqvist committed
1347
1348
1349
1350
}

	
	
Per Cederqvist's avatar
Per Cederqvist committed
1351
static void
1352
1353
1354
1355
1356
1357
1358
print_statistics(void)
{
    Text_stat *ts;
    Text_no    t;
    int  *hist;
    int i;

1359
    hist = calloc(param.text_len, sizeof(int));
1360

Per Cederqvist's avatar
Per Cederqvist committed
1361
1362
1363
1364
1365
1366
    if (hist == NULL)
    {
	perror("dbck: print_statistics(): can't calloc()");
	return;
    }
    
1367
1368
1369
1370
1371
    for (t=0; (t=traverse_text(t)) != 0;)
    {
	ts = cached_get_text_stat(t);
	if (ts == NULL)
	{
David Byers's avatar
David Byers committed
1372
	    kom_log("print_statistics(): Can't get text_stat.\n");
1373
1374
1375
1376
1377
1378
	    return;
	}
		
	hist[ts->no_of_chars]++;
    }

David Byers's avatar
David Byers committed
1379
    kom_log("Length  Frequency\n");
1380
    for(i=0; i<param.text_len; i++)
1381
	if(hist[i] != 0)
David Byers's avatar
David Byers committed