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 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 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 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
|
/* rt-mt.c
*/
/* This software is copyrighted as detailed in the LICENSE file. */
#include "EXTERN.h"
#include "common.h"
#include "list.h"
#include "intrp.h"
#include "trn.h"
#include "hash.h"
#include "cache.h"
#include "bits.h"
#include "kfile.h"
#include "ngdata.h"
#include "nntpclient.h"
#include "datasrc.h"
#include "nntp.h"
#include "ng.h"
#include "rcln.h"
#include "util.h"
#include "util2.h"
#include "rthread.h"
#include "rt-process.h"
#include "INTERN.h"
#include "rt-mt.h"
#include "rt-mt.ih"
static FILE* fp;
static bool word_same, long_same;
static BMAP my_bmap, mt_bmap;
static char* strings = NULL;
static WORD* author_cnts = 0;
static WORD* ids = 0;
static ARTICLE** article_array = 0;
static SUBJECT** subject_array = 0;
static char** author_array = 0;
static TOTAL total;
static PACKED_ROOT p_root;
static PACKED_ARTICLE p_article;
/* Initialize our thread code by determining the byte-order of the thread
** files and our own current byte-order. If they differ, set flags to let
** the read code know what we'll need to translate.
*/
bool
mt_init()
{
int i;
long size;
bool success = TRUE;
datasrc->flags &= ~DF_TRY_THREAD;
word_same = long_same = TRUE;
#ifdef SUPPORT_XTHREAD
if (!datasrc->thread_dir) {
if (nntp_command("XTHREAD DBINIT") <= 0)
return FALSE;
size = nntp_readcheck();
if (size >= 0)
size = nntp_read((char*)&mt_bmap, (long)sizeof (BMAP));
}
else
#endif
{
if ((fp = fopen(filexp(DBINIT), FOPEN_RB)) != NULL)
size = fread((char*)&mt_bmap, 1, sizeof (BMAP), fp);
else
size = 0;
}
if (size >= (long)(sizeof (BMAP)) - 1) {
if (mt_bmap.version != DB_VERSION) {
printf("\nMthreads database is the wrong version -- ignoring it.\n")
FLUSH;
return FALSE;
}
mybytemap(&my_bmap);
for (i = 0; i < sizeof (LONG); i++) {
if (i < sizeof (WORD)) {
if (my_bmap.w[i] != mt_bmap.w[i])
word_same = FALSE;
}
if (my_bmap.l[i] != mt_bmap.l[i])
long_same = FALSE;
}
} else
success = FALSE;
#ifdef SUPPORT_XTHREAD
if (!datasrc->thread_dir) {
while (nntp_read(ser_line, (long)sizeof ser_line))
; /* trash any extraneous bytes */
}
else
#endif
if (fp != NULL)
fclose(fp);
if (success)
datasrc->flags |= DF_TRY_THREAD;
return success;
}
/* Open and process the data in the group's thread file. Returns TRUE unless
** we discovered a bogus thread file, destroyed the cache, and re-built it.
*/
int
mt_data()
{
int ret = 1;
#ifdef SUPPORT_XTHREAD /* use remote thread file? */
long size;
if (!datasrc->thread_dir) {
if (nntp_command("XTHREAD THREAD") <= 0)
return 0;
size = nntp_readcheck();
if (size < 0)
return 0;
#ifdef VERBOSE
IF(verbose)
printf("\nGetting thread file."), fflush(stdout);
#endif
if (nntp_read((char*)&total, (long)sizeof (TOTAL)) < sizeof (TOTAL))
goto exit;
}
else
#endif
{
if ((fp = fopen(mt_name(ngname), FOPEN_RB)) == NULL)
return 0;
#ifdef VERBOSE
IF(verbose)
printf("\nReading thread file."), fflush(stdout);
#endif
if (fread((char*)&total, 1, sizeof (TOTAL), fp) < sizeof (TOTAL))
goto exit;
}
lp_bmap(&total.first, 4);
wp_bmap(&total.root, 5);
if (!total.root) {
tweak_data();
goto exit;
}
#ifdef SUPPORT_NNTP
if (!datasrc->thread_dir && total.last > lastart)
total.last = lastart;
#endif
if (read_authors()
&& read_subjects()
&& read_roots()
&& read_articles()
&& read_ids())
{
tweak_data();
first_cached = absfirst;
last_cached = (total.last < absfirst ? absfirst-1: total.last);
cached_all_in_range = TRUE;
goto exit;
}
/* Something failed. Safefree takes care of checking if some items
** were already freed. Any partially-allocated structures were freed
** before we got here. All other structures are cleaned up now.
*/
close_cache();
safefree0(strings);
safefree0(article_array);
safefree0(subject_array);
safefree0(author_array);
safefree0(ids);
datasrc->flags &= ~DF_TRY_THREAD;
build_cache();
datasrc->flags |= DF_TRY_THREAD;
ret = -1;
exit:
#ifdef SUPPORT_XTHREAD
if (!datasrc->thread_dir) {
while (nntp_read(ser_line, (long)sizeof ser_line))
; /* trash any extraneous bytes */
}
else
#endif
fclose(fp);
return ret;
}
/* Change a newsgroup name into the name of the thread data file. We
** subsitute any '.'s in the group name into '/'s (unless LONG_THREAD_NAMES
** is defined), prepend the path, and append the '/.thread' or '.th' on to
** the end.
*/
static char*
mt_name(group)
char* group;
{
#ifdef LONG_THREAD_NAMES
sprintf(buf, "%s/%s", datasrc->thread_dir, group);
#else
register char* cp;
cp = strcpy(buf, datasrc->thread_dir) + strlen(datasrc->thread_dir);
*cp++ = '/';
strcpy(cp, group);
while ((cp = index(cp, '.')))
*cp = '/';
if (datasrc->thread_dir == datasrc->spool_dir)
strcat(buf, MT_FILE_NAME);
else
strcat(buf, ".th");
#endif
return buf;
}
static char* subject_strings, *string_end;
/* The author information is an array of use-counts, followed by all the
** null-terminated strings crammed together. The subject strings are read
** in at the same time, since they are appended to the end of the author
** strings.
*/
static int
read_authors()
{
register int count;
register char* string_ptr;
register char** author_ptr;
if (!read_item((char**)&author_cnts, (MEM_SIZE)total.author*sizeof (WORD)))
return 0;
safefree0(author_cnts); /* we don't need these */
if (!read_item(&strings, (MEM_SIZE)total.string1))
return 0;
string_ptr = strings;
string_end = string_ptr + total.string1;
if (string_end[-1] != '\0') {
/*error("first string table is invalid.\n");*/
return 0;
}
/* We'll use this array to point each article at its proper author
** (the packed values were saved as indexes).
*/
author_array = (char**)safemalloc(total.author * sizeof (char*));
author_ptr = author_array;
for (count = total.author; count; count--) {
if (string_ptr >= string_end)
break;
*author_ptr++ = string_ptr;
string_ptr += strlen(string_ptr) + 1;
}
subject_strings = string_ptr;
if (count) {
/*error("author unpacking failed.\n");*/
return 0;
}
return 1;
}
/* The subject values consist of the crammed-together null-terminated strings
** (already read in above) and the use-count array. They were saved in the
** order that the roots require while being unpacked.
*/
static int
read_subjects()
{
register int count;
register char* string_ptr;
register SUBJECT** subj_ptr;
WORD* subject_cnts;
if (!read_item((char**)&subject_cnts,
(MEM_SIZE)total.subject * sizeof (WORD))) {
/* (Error already logged.) */
return 0;
}
free((char*)subject_cnts); /* we don't need these */
/* Use this array when unpacking the article's subject offset. */
subject_array = (SUBJECT**)safemalloc(total.subject * sizeof (SUBJECT*));
subj_ptr = subject_array;
string_ptr = subject_strings; /* string_end is already set */
for (count = total.subject; count; count--) {
int len;
ARTICLE arty;
if (string_ptr >= string_end)
break;
len = strlen(string_ptr);
arty.subj = 0;
set_subj_line(&arty, string_ptr, len);
if (len == 72)
arty.subj->flags |= SF_SUBJTRUNCED;
arty.subj->thread_link = NULL;
string_ptr += len + 1;
*subj_ptr++ = arty.subj;
}
if (count || string_ptr != string_end) {
/*error("subject data is invalid.\n");*/
return 0;
}
return 1;
}
/* Read in the packed root structures to set each subject's thread article
** offset. This gets turned into a real pointer later.
*/
static int
read_roots()
{
register SUBJECT** subj_ptr;
register int i;
SUBJECT* sp;
SUBJECT* prev_sp;
int count;
int ret;
subj_ptr = subject_array;
for (count = total.root; count--; ) {
#ifdef SUPPORT_XTHREAD
if (!datasrc->thread_dir)
ret = nntp_read((char*)&p_root, (long)sizeof (PACKED_ROOT));
else
#endif
ret = fread((char*)&p_root, 1, sizeof (PACKED_ROOT), fp);
if (ret != sizeof (PACKED_ROOT)) {
/*error("failed root read -- %d bytes instead of %d.\n",
ret, sizeof (PACKED_ROOT));*/
return 0;
}
wp_bmap(&p_root.articles, 3); /* converts subject_cnt too */
if (p_root.articles < 0 || p_root.articles >= total.article) {
/*error("root has invalid values.\n");*/
return 0;
}
i = p_root.subject_cnt;
if (i <= 0 || (subj_ptr - subject_array) + i > total.subject) {
/*error("root has invalid values.\n");*/
return 0;
}
for (prev_sp = *subj_ptr; i--; prev_sp = sp, subj_ptr++) {
sp = *subj_ptr;
if (sp->thread_link == NULL) {
sp->thread_link = prev_sp->thread_link;
prev_sp->thread_link = sp;
}
else {
while (sp != prev_sp && sp->thread_link != *subj_ptr)
sp = sp->thread_link;
if (sp == prev_sp)
continue;
sp->thread_link = prev_sp->thread_link;
prev_sp->thread_link = *subj_ptr;
}
}
}
return 1;
}
static bool invalid_data;
/* A simple routine that checks the validity of the article's subject value.
** A -1 means that it is NULL, otherwise it should be an offset into the
** subject array we just unpacked.
*/
static SUBJECT*
the_subject(num)
int num;
{
if (num == -1)
return NULL;
if (num < 0 || num >= total.subject) {
/*printf("Invalid subject in thread file: %d [%ld]\n", num, art_num);*/
invalid_data = TRUE;
return NULL;
}
return subject_array[num];
}
/* Ditto for author checking. */
static char*
the_author(num)
int num;
{
if (num == -1)
return NULL;
if (num < 0 || num >= total.author) {
/*error("invalid author in thread file: %d [%ld]\n", num, art_num);*/
invalid_data = TRUE;
return NULL;
}
return savestr(author_array[num]);
}
/* Our parent/sibling information is a relative offset in the article array.
** zero for none. Child values are always found in the very next array
** element if child_cnt is non-zero.
*/
static ARTICLE*
the_article(relative_offset, num)
int relative_offset;
int num;
{
union { ARTICLE* ap; int num; } uni;
if (!relative_offset)
return NULL;
num += relative_offset;
if (num < 0 || num >= total.article) {
/*error("invalid article offset in thread file.\n");*/
invalid_data = TRUE;
return NULL;
}
uni.num = num+1;
return uni.ap; /* slip them an offset in disguise */
}
/* Read the articles into their trees. Point everything everywhere. */
static int
read_articles()
{
register int count;
register ARTICLE* article;
register ARTICLE** art_ptr;
int ret;
/* Build an array to interpret interlinkages of articles. */
article_array = (ARTICLE**)safemalloc(total.article * sizeof (ARTICLE*));
art_ptr = article_array;
invalid_data = FALSE;
for (count = 0; count < total.article; count++) {
#ifdef SUPPORT_XTHREAD
if (!datasrc->thread_dir)
ret = nntp_read((char*)&p_article, (long)sizeof (PACKED_ARTICLE));
else
#endif
ret = fread((char*)&p_article, 1, sizeof (PACKED_ARTICLE), fp);
if (ret != sizeof (PACKED_ARTICLE)) {
/*error("failed article read -- %d bytes instead of %d.\n",
ret, sizeof (PACKED_ARTICLE));*/
return 0;
}
lp_bmap(&p_article.num, 2);
wp_bmap(&p_article.subject, 8);
#ifdef SUPPORT_NNTP
article = *art_ptr++ = allocate_article(p_article.num > lastart?
0 : p_article.num);
#else
article = *art_ptr++ = allocate_article(p_article.num);
#endif
article->date = p_article.date;
#ifndef DBM_XREFS
if (olden_days < 2 && !(p_article.flags & HAS_XREFS))
article->xrefs = nullstr;
#endif
article->from = the_author(p_article.author);
article->parent = the_article(p_article.parent, count);
article->child1 = the_article((WORD)(p_article.child_cnt?1:0), count);
article->sibling = the_article(p_article.sibling, count);
article->subj = the_subject(p_article.subject);
if (invalid_data) {
/* (Error already logged.) */
return 0;
}
/* This is OK because parent articles precede their children */
if (article->parent) {
union { ARTICLE* ap; int num; } uni;
uni.ap = article->parent;
article->parent = article_array[uni.num-1];
}
else
article->sibling = NULL;
if (article->subj) {
article->flags |= AF_FROMTRUNCED | AF_THREADED
| ((p_article.flags & ROOT_ARTICLE)? 0 : AF_HAS_RE);
/* Give this subject to any faked parent articles */
while (article->parent && !article->parent->subj) {
article->parent->subj = article->subj;
article = article->parent;
}
} else
article->flags |= AF_FAKE;
}
/* We're done with most of the pointer arrays at this point. */
safefree0(subject_array);
safefree0(author_array);
safefree0(strings);
return 1;
}
/* Read the message-id strings and attach them to each article. The data
** format consists of the mushed-together null-terminated strings (a domain
** name followed by all its unique-id prefixes) and then the article offsets
** to which they belong. The first domain name was omitted, as it is a null
** domain for those truly weird message-id's without '@'s.
*/
static int
read_ids()
{
register ARTICLE* article;
register char* string_ptr;
register int i, count, len, len2;
if (!read_item(&strings, (MEM_SIZE)total.string2)
|| !read_item((char**)&ids,
(MEM_SIZE)(total.article+total.domain+1) * sizeof (WORD))) {
return 0;
}
wp_bmap(ids, total.article + total.domain + 1);
string_ptr = strings;
string_end = string_ptr + total.string2;
if (string_end[-1] != '\0') {
/*error("second string table is invalid.\n");*/
return 0;
}
for (i = 0, count = total.domain + 1; count--; i++) {
if (i) {
if (string_ptr >= string_end) {
/*error("error unpacking domain strings.\n");*/
return 0;
}
sprintf(buf, "@%s", string_ptr);
len = strlen(string_ptr) + 1;
string_ptr += len;
} else {
*buf = '\0';
len = 0;
}
if (ids[i] != -1) {
if (ids[i] < 0 || ids[i] >= total.article) {
/*error("error in id array.\n");*/
return 0;
}
article = article_array[ids[i]];
for (;;) {
if (string_ptr >= string_end) {
/*error("error unpacking domain strings.\n");*/
return 0;
}
len2 = strlen(string_ptr);
article->msgid = safemalloc(len2 + len + 2 + 1);
sprintf(article->msgid, "<%s%s>", string_ptr, buf);
string_ptr += len2 + 1;
if (msgid_hash) {
HASHDATUM data;
data = hashfetch(msgid_hash, article->msgid, len2+len+2);
if (data.dat_len) {
article->autofl = data.dat_len&(AUTO_SELS|AUTO_KILLS);
if ((data.dat_len & KF_AGE_MASK) == 0)
article->autofl |= AUTO_OLD;
else
kf_changethd_cnt++;
data.dat_len = 0;
free(data.dat_ptr);
}
data.dat_ptr = (char*)article;
hashstorelast(data);
}
if (++i >= total.article + total.domain + !count) {
/*error("overran id array unpacking domains.\n");*/
return 0;
}
if (ids[i] != -1) {
if (ids[i] < 0 || ids[i] >= total.article)
return 0;
article = article_array[ids[i]];
} else
break;
}
}
}
safefree0(ids);
safefree0(strings);
return 1;
}
/* And finally, turn all the links into real pointers and mark missing
** articles as read.
*/
static void
tweak_data()
{
register int count;
register ARTICLE* ap;
register ARTICLE** art_ptr;
union { ARTICLE* ap; int num; } uni;
int fl;
art_ptr = article_array;
for (count = total.article; count--; ) {
ap = *art_ptr++;
if (ap->child1) {
uni.ap = ap->child1;
ap->child1 = article_array[uni.num-1];
}
if (ap->sibling) {
uni.ap = ap->sibling;
ap->sibling = article_array[uni.num-1];
}
if (!ap->parent)
link_child(ap);
}
art_ptr = article_array;
for (count = total.article; count--; ) {
ap = *art_ptr++;
if (ap->subj && !(ap->flags & AF_FAKE))
cache_article(ap);
else
onemissing(ap);
}
art_ptr = article_array;
for (count = total.article; count--; ) {
ap = *art_ptr++;
if ((fl = ap->autofl) != 0)
perform_auto_flags(ap, fl, fl, fl);
}
safefree0(article_array);
}
/* A shorthand for reading a chunk of the file into a malloc'ed array.
*/
static int
read_item(dest, len)
char** dest;
MEM_SIZE len;
{
long ret;
*dest = safemalloc(len);
#ifdef SUPPORT_XTHREAD
if (!datasrc->thread_dir)
ret = nntp_read(*dest, (long)len);
else
#endif
ret = fread(*dest, 1, (int)len, fp);
if (ret != len) {
free(*dest);
*dest = NULL;
return 0;
}
putchar('.');
fflush(stdout);
return 1;
}
/* Determine this machine's byte map for WORDs and LONGs. A byte map is an
** array of BYTEs (sizeof (WORD) or sizeof (LONG) of them) with the 0th BYTE
** being the byte number of the high-order byte in my <type>, and so forth.
*/
static void
mybytemap(map)
BMAP* map;
{
union {
BYTE b[sizeof (LONG)];
WORD w;
LONG l;
} u;
register BYTE *mp;
register int i, j;
mp = &map->w[sizeof (WORD)];
u.w = 1;
for (i = sizeof (WORD); i > 0; i--) {
for (j = 0; j < sizeof (WORD); j++) {
if (u.b[j] != 0)
break;
}
if (j == sizeof (WORD))
goto bad_news;
*--mp = j;
while (u.b[j] != 0 && u.w)
u.w <<= 1;
}
mp = &map->l[sizeof (LONG)];
u.l = 1;
for (i = sizeof (LONG); i > 0; i--) {
for (j = 0; j < sizeof (LONG); j++) {
if (u.b[j] != 0)
break;
}
if (j == sizeof (LONG)) {
bad_news:
/* trouble -- set both to *something* consistent */
for (j = 0; j < sizeof (WORD); j++)
map->w[j] = j;
for (j = 0; j < sizeof (LONG); j++)
map->l[j] = j;
return;
}
*--mp = j;
while (u.b[j] != 0 && u.l)
u.l <<= 1;
}
}
/* Transform each WORD's byte-ordering in a buffer of the designated length.
*/
static void
wp_bmap(buf, len)
WORD* buf;
int len;
{
union {
BYTE b[sizeof (WORD)];
WORD w;
} in, out;
register int i;
if (word_same)
return;
while (len--) {
in.w = *buf;
for (i = 0; i < sizeof (WORD); i++)
out.b[my_bmap.w[i]] = in.b[mt_bmap.w[i]];
*buf++ = out.w;
}
}
/* Transform each LONG's byte-ordering in a buffer of the designated length.
*/
static void
lp_bmap(buf, len)
LONG* buf;
int len;
{
union {
BYTE b[sizeof (LONG)];
LONG l;
} in, out;
register int i;
if (long_same)
return;
while (len--) {
in.l = *buf;
for (i = 0; i < sizeof (LONG); i++)
out.b[my_bmap.l[i]] = in.b[mt_bmap.l[i]];
*buf++ = out.l;
}
}
|