File: expire.c

package info (click to toggle)
nn 6.7.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,480 kB
  • ctags: 3,198
  • sloc: ansic: 32,028; sh: 1,491; awk: 138; makefile: 96
file content (646 lines) | stat: -rw-r--r-- 14,976 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
409
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
 *
 *	Expire will remove all entries in the index and data files
 *	corresponding to the articles before the first article registered
 *	in the active file.  No attempt is made to eliminate other
 *	expired articles.
 */

#include <stdlib.h>
#include <ctype.h>
#include "config.h"
#include "global.h"
#include "db.h"
#include "master.h"
#include "nntp.h"
#include "nn_term.h"

/* expire.c */

static int      sort_art_list(register article_number * f1, register article_number * f2);
static article_number *get_article_list(char *dir);
static long     expire_in_database(register group_header * gh);
static long     expire_sliding(register group_header * gh);
static void     block_group(register group_header * gh);
static void     unblock_group(register group_header * gh);


extern int      trace, debug_mode;
extern int      nntp_failed;

/*
 *	Expire methods:
 *	1: read directory and reuse database info.
 *	2: "slide" index and datafiles (may leave unexpired art. in database)
 *	3: recollect group to expire (also if "min" still exists)
 */

int             expire_method = 1;	/* expire method */
int             recollect_method = 1;	/* recollection method -- see
					 * do_expire */
int             expire_level = 0;	/* automatic expiration detection */

#ifdef HAVE_DIRECTORY
static article_number *article_list = NULL;
static long     art_list_length = 0;

static int
sort_art_list(register article_number * f1, register article_number * f2)
{
    return (*f1 < *f2) ? -1 : (*f1 == *f2) ? 0 : 1;
}

static article_number *
get_article_list(char *dir)
{
    DIR            *dirp;
    register Direntry *dp;
    register char   c, *pp, *cp;
    register article_number *art;
    register long   count;	/* No. of completions plus one */

    if ((dirp = opendir(dir)) == NULL)
	return NULL;		/* tough luck */

    art = article_list;
    count = 0;

    while ((dp = readdir(dirp)) != NULL) {
	cp = dp->d_name;

#ifdef FAKED_DIRECTORY
	if (dp->d_ino == 0)
	    continue;
	cp[14] = NUL;
#endif

	for (pp = cp; (c = *pp++);)
	    if (!isascii(c) || !isdigit(c))
		break;
	if (c)
	    continue;

	if (count == art_list_length) {
	    art_list_length += 250;
	    article_list = resizeobj(article_list, article_number, art_list_length + 1);
	    art = article_list + count;
	}
	*art++ = atol(cp);
	count++;
    }
    closedir(dirp);

    if (article_list != NULL) {
	*art = 0;
	if (count > 1)
	    quicksort(article_list, count, article_number, sort_art_list);
    }
    return article_list;
}

static long
expire_in_database(register group_header * gh)
{
    FILE           *old, *data = NULL, *ix = NULL;
    off_t           old_max_offset;
    register article_number *list;
    article_number  old_last_article;
    long            count;

    if (gh->first_db_article > gh->last_db_article)
	return 0;

    if (gh->last_db_article == 0)
	return 0;

    if (!init_group(gh))
	return 0;

    if (debug_mode == 1) {
	printf("\t\tExp %s (%ld..%ld)\r",
	       gh->group_name, gh->first_db_article, gh->last_db_article);
	fl;
    }
    count = 0;

    /* get list of currently available articles in the group */

#ifdef NNTP
    if (use_nntp)
	list = nntp_get_article_list(gh);
    else
#endif

	list = get_article_list(".");

    if (list == NULL || *list == 0) {

#ifdef NNTP
	if (nntp_failed == 2) {
	    log_entry('N', "NNTP server supports neither LISTGROUP nor XHDR");
	    sys_error("Cannot use specified expire method (NNTP limitations)");
	}
	if (nntp_failed)
	    return -1;
#endif

	if (debug_mode == 1) {
	    printf("\rempty");
	    fl;
	}
	/* group is empty - clean it */
	count = gh->last_db_article - gh->first_db_article + 1;
	clean_group(gh);
	gh->last_db_article = gh->last_a_article;
	gh->first_db_article = gh->last_db_article + 1;
	db_write_group(gh);
	return count;
    }

    /*
     * Clean & block the group while expire is working on it.
     */

    gh->first_db_article = 0;
    old_last_article = gh->last_db_article;
    gh->last_db_article = 0;

    gh->index_write_offset = 0;
    old_max_offset = gh->data_write_offset;
    gh->data_write_offset = 0;

    gh->master_flag &= ~M_EXPIRE;
    gh->master_flag |= M_BLOCKED;

    db_write_group(gh);

    /*
     * We ignore the old index file, and we unlink the data file immediately
     * after open because we want to write a new.
     */

    (void) open_data_file(gh, 'x', -1);
    old = open_data_file(gh, 'd', OPEN_READ | OPEN_UNLINK);
    if (old == NULL)
	goto out;

    data = open_data_file(gh, 'd', OPEN_CREATE | MUST_EXIST);
    ix = open_data_file(gh, 'x', OPEN_CREATE | MUST_EXIST);

    while (ftell(old) < old_max_offset) {
	if (s_hangup) {
	    /* ok, this is what we got -- let collect get the rest */
	    old_last_article = gh->last_db_article;
	    break;
	}

	/*
	 * maybe not enough articles, or last one is incomplete we take what
	 * there is, and leave the rest to do_collect(void) It may actually
	 * be ok if the last articles have been expired/ cancelled!
	 */

	if (db_read_art(old) <= 0) {
	    break;
	}
	if (debug_mode == 1) {
	    printf("\r%ld", (long) db_hdr.dh_number);
	    fl;
	}
	/* check whether we want this article */

	while (*list && db_hdr.dh_number > *list) {
	    /* potentially, we have a problem here: there might be an */
	    /* article in the directory which is not in the database! */
	    /* For the moment we just ignore this - it might be a bad */
	    /* article which has been rejected!! */
	    list++;
	}

	if (*list == 0) {
	    /* no more articles in the directory - the rest must be */
	    /* expired.  So we ignore the rest of the data file */
	    break;
	}
	if (db_hdr.dh_number < *list) {
	    /* the current article from the data file isn't in the */
	    /* article list, so it must be expired! */
	    count++;
	    if (debug_mode == 1) {
		printf("\t%ld", count);
		fl;
	    }
	    continue;
	}
	if (gh->first_db_article == 0) {
	    gh->first_db_article = db_hdr.dh_number;
	    gh->last_db_article = db_hdr.dh_number - 1;
	}
	if (gh->last_db_article < db_hdr.dh_number) {
	    gh->data_write_offset = ftell(data);

	    /* must fill gab between last index and current article */
	    while (gh->last_db_article < db_hdr.dh_number) {
		if (!db_write_offset(ix, &(gh->data_write_offset)))
		    write_error();
		gh->last_db_article++;
	    }
	}
	if (db_write_art(data) < 0)
	    write_error();
    }

    if (gh->first_db_article == 0) {
	gh->first_db_article = old_last_article + 1;
	gh->last_db_article = old_last_article;
    } else {
	gh->data_write_offset = ftell(data);
	while (gh->last_db_article < old_last_article) {
	    /* must fill gab between last index and last article */
	    ++gh->last_db_article;
	    if (!db_write_offset(ix, &(gh->data_write_offset)))
		write_error();
	}
	gh->index_write_offset = ftell(ix);
    }

    gh->master_flag &= ~M_BLOCKED;

    db_write_group(gh);

out:
    if (old)
	fclose(old);
    if (data)
	fclose(data);
    if (ix)
	fclose(ix);

    if (debug_mode)
	putchar(NL);

    return count;
}

#else
#define expire_in_database expire_sliding
#endif

static long
expire_sliding(register group_header * gh)
{
    FILE           *old_x, *old_d;
    FILE           *new;
    long            data_offset, new_offset;
    long            count, expire_count, index_offset;
    char           *err_message;

#define expire_error(msg) { \
    err_message = msg; \
    goto error_handler; \
}

    if (!init_group(gh))
	return 0;

#ifdef RENUMBER_DANGER

    /*
     * check whether new first article is collected
     */

    if (!art_collected(gh, gh->first_a_article)) {
	expire_count = gh->first_db_article - gh->last_db_article + 1;
	err_message = NULL;
	goto error_handler;	/* renumbering, collect from start */
    }
#else
    if (gh->first_a_article <= gh->first_db_article)
	return 0;
#endif

    expire_count = gh->first_a_article - gh->first_db_article;

    new = NULL;

    /*
     * Open old files, unlink after open
     */

    old_x = open_data_file(gh, 'x', OPEN_READ | OPEN_UNLINK);
    old_d = open_data_file(gh, 'd', OPEN_READ | OPEN_UNLINK);

    if (old_x == NULL || old_d == NULL)
	expire_error("INDEX or DATA file missing");

    /*
     * Create new index file; copy from old
     */

    new = open_data_file(gh, 'x', OPEN_CREATE);
    if (new == NULL)
	expire_error("INDEX: cannot create");

    /*
     * index_offset is the offset into the old index file for the first entry
     * in the new index file
     */

    index_offset = get_index_offset(gh, gh->first_a_article);

    /*
     * adjust the group's index write offset (the next free entry)
     */

    gh->index_write_offset -= index_offset;

    /*
     * calculate the number of entries to copy
     */

    count = gh->index_write_offset / sizeof(long);

    /*
     * data offset is the offset into the old data file for the first byte in
     * the new data file; it is initialized in the loop below, by reading the
     * entry in the old index file at offset 'index_offset'.
     */

    data_offset = (off_t) 0;

    /*
     * read 'count' entries from the old index file starting from
     * index_offset, subtract the 'data_offset', and output the new offset to
     * the new index file.
     */

    fseek(old_x, index_offset, 0);

    while (--count >= 0) {
	if (!db_read_offset(old_x, &new_offset))
	    expire_error("INDEX: too short");

	if (data_offset == 0)
	    data_offset = new_offset;

	new_offset -= data_offset;
	if (!db_write_offset(new, &new_offset))
	    expire_error("NEW INDEX: cannot write");
    }

    fclose(new);
    fclose(old_x);
    old_x = NULL;

    /*
     * copy from old data file to new data file
     */

    new = open_data_file(gh, 'd', OPEN_CREATE);
    if (new == NULL)
	expire_error("DATA: cannot create");

    /*
     * calculate offset for next free entry in the new data file
     */

    gh->data_write_offset -= data_offset;

    /*
     * calculate number of bytes to copy (piece of cake)
     */

    count = gh->data_write_offset;

    /*
     * copy 'count' bytes from the old data file, starting at offset
     * 'data_offset', to the new data file
     */

    fseek(old_d, data_offset, 0);
    while (count > 0) {
	char            block[1024];
	int             count1;

	count1 = fread(block, sizeof(char), 1024, old_d);
	if (count1 <= 0)
	    expire_error("DATA: read error");

	if (fwrite(block, sizeof(char), count1, new) != count1)
	    expire_error("DATA: write error");

	count -= count1;
    }

    fclose(new);
    fclose(old_d);

    /*
     * Update group entry
     */

    gh->first_db_article = gh->first_a_article;

    /*
     * Return number of expired articles
     */

    return expire_count;

    /*
     * Errors end up here. We simply recollect the whole group once more.
     */

error_handler:

    if (new)
	fclose(new);
    if (old_x)
	fclose(old_x);
    if (old_d)
	fclose(old_d);

    if (err_message)
	log_entry('E', "Expire Error (%s): %s", gh->group_name, err_message);

    clean_group(gh);

    /* will be saved & unblocked later */

    /*
     * We cannot say whether any articles actually had to be expired, but
     * then we must guess...
     */

    return expire_count;
}

static void
block_group(register group_header * gh)
{
    if ((gh->master_flag & M_BLOCKED) == 0) {
	gh->master_flag |= M_BLOCKED;
	db_write_group(gh);
    }
}

static void
unblock_group(register group_header * gh)
{
    if (gh->master_flag & M_BLOCKED) {
	gh->master_flag &= ~(M_BLOCKED | M_EXPIRE);
	db_write_group(gh);
    }
}

int
do_expire(void)
{
    register group_header *gh;
    long            exp_article_count, temp;
    int             exp_group_count, must_expire;
    time_t          start_time;

    must_expire = 0;

    Loop_Groups_Header(gh) {
	if (s_hangup)
	    break;
	if (gh->master_flag & M_IGNORE_GROUP)
	    continue;

	if ((gh->master_flag & M_VALID) == 0) {
	    log_entry('X', "Group %s removed", gh->group_name);
	    gh->master_flag |= M_IGNORE_A;
	    clean_group(gh);
	    continue;
	}

#ifdef RENUMBER_DANGER
	if (gh->last_db_article > gh->last_a_article ||
	    gh->first_db_article > gh->first_a_article) {
	    log_entry('X', "group %s renumbered", gh->group_name);
	    clean_group(gh);
	    continue;
	}
#endif

	if (gh->master_flag & M_AUTO_RECOLLECT) {
	    switch (recollect_method) {
		case 1:	/* expire when new articles arrive */
		    if (gh->last_a_article <= gh->last_db_article)
			break;
		    /* FALLTHRU */
		case 2:	/* expire unconditionally */
		    gh->master_flag |= M_EXPIRE;
		    must_expire = 1;
		    continue;

		case 3:	/* clean when new articles arrive */
		    if (gh->last_a_article <= gh->last_db_article)
			break;
		    /* FALLTHRU */
		case 4:	/* clean unconditionally */
		    gh->master_flag |= M_MUST_CLEAN;
		    continue;
		default:	/* ignore auto-recollect */
		    break;
	    }
	}
	if (gh->index_write_offset > 0) {
	    if (gh->first_a_article > gh->last_db_article) {
		if (trace)
		    log_entry('T', "%s expire void", gh->group_name);
		if (debug_mode)
		    printf("%s expire void\n", gh->group_name);
		clean_group(gh);
		continue;
	    }
	}
	if (gh->master_flag & M_EXPIRE) {
	    must_expire = 1;
	    continue;
	}
	if (expire_level > 0 &&
	    (gh->first_db_article + expire_level) <= gh->first_a_article) {
	    if (trace)
		log_entry('T', "%s expire level", gh->group_name);
	    if (debug_mode)
		printf("Expire level: %s\n", gh->group_name);
	    gh->master_flag |= M_EXPIRE;
	    must_expire = 1;
	    continue;
	}
    }

    if (!must_expire)
	return 1;

    start_time = cur_time();
    exp_article_count = exp_group_count = 0;
    temp = 0;

    Loop_Groups_Header(gh) {
	if (s_hangup) {
	    temp = -1;
	    break;
	}
	if (gh->master_flag & M_IGNORE_GROUP)
	    continue;
	if ((gh->master_flag & M_EXPIRE) == 0)
	    continue;
	if (gh->master_flag & M_MUST_CLEAN)
	    continue;

	if (trace)
	    log_entry('T', "Exp %s (%ld -> %ld)", gh->group_name,
		   (long) gh->first_db_article, (long) gh->first_a_article);

	switch (expire_method) {
	    case 1:
		block_group(gh);
		temp = expire_in_database(gh);
		unblock_group(gh);
		break;

	    case 2:
		block_group(gh);
		temp = expire_sliding(gh);
		unblock_group(gh);
		break;

	    case 4:
		temp = gh->first_a_article - gh->first_db_article;
		if (temp <= 0)
		    break;
		/* FALLTHRU */
	    case 3:
		gh->master_flag |= M_MUST_CLEAN;
		break;

	}

#ifdef NNTP
	if (nntp_failed) {
	    /* connection broken while reading article list */
	    /* so no harm was done - leave the group to be expired */
	    /* again on next expire */
	    break;
	}
#endif

	if (temp > 0) {
	    exp_article_count += temp;
	    exp_group_count++;
	}
    }

    if (exp_article_count > 0)
	log_entry('X', "Expire: %ld art, %d gr, %ld s",
		  exp_article_count, exp_group_count,
		  (long) (cur_time() - start_time));

    return temp >= 0;
}