File: activutil.c

package info (click to toggle)
leafnode 1.6.2-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 296 kB
  • ctags: 198
  • sloc: ansic: 3,961; sh: 208; makefile: 139; perl: 51
file content (477 lines) | stat: -rw-r--r-- 11,157 bytes parent folder | download
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
/*
libutil -- deal with active file

Written by Arnt Gulbrandsen <agulbra@troll.no> and copyright 1995
Troll Tech AS, Postboks 6133 Etterstad, 0602 Oslo, Norway, fax +47
22646949.
Modified by Cornelius Krasel <krasel@wpxx02.toxi.uni-wuerzburg.de>
and Randolf Skerka <rskerka@metronet.de>.
Copyright of the modifications 1997.
Modified by Kent Robotti <robotti@erols.com>. Copyright of the
modifications 1998.
Modified by Markus Enzenberger <enz@cip.physik.uni-muenchen.de>.
Copyright of the modifications 1998.
Modified by Cornelius Krasel <krasel@wpxx02.toxi.uni-wuerzburg.de>.
Copyright of the modifications 1998.

See file COPYING for restrictions on the use of this software.
*/

#include <fcntl.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>

#include "leafnode.h"

#define   STRNCMPEQ(s1, s2, n)    (*(s1) == *(s2) && strncmp((s1), (s2), n) == 0)

extern int errno;
extern int h_errno;
extern struct state _res;

struct newsgroup * active;
struct newsgroup * blocked;

/* insert or update a newsgroup in the global list */
void insertgroup(const char * name, int first, int last, const char * desc,
		 int age ) {
    struct newsgroup ** a;
    int c;

    if ( !desc )
	desc = "";

    a = &active;
    while(a) {
	if (*a) {
	    c = strcmp((*a)->name, name);
	    if (c<0)
		a = &((*a)->left);
	    else if (c>0)
		a = &((*a)->right);
	    else {
	    	/* update newsgroup description, nothing else */
		if (desc && *desc) {
		    /* This leaks memory if the new desc > old one, but I
		       cannot see a way round this... */
		    if (strlen((*a)->desc) >= strlen(desc))
			strcpy((*a)->desc, desc);
		    else
			(*a)->desc = strdup( desc );
		}
		return;
	    }
	} else {
	    /* create new newsgroup entry */
	    *a = (struct newsgroup *)
		 critmalloc(sizeof(struct newsgroup) + 2 +
			    strlen( name ) + strlen( desc ),
			    "Building newsgroups info list");
	    if (!*a)
		return;
	    (*a)->left = (*a)->right = NULL;
	    (*a)->first = first;
	    (*a)->last = last;
	    (*a)->age = age;
	    (*a)->name = (char*)(1+*a);
	    strcpy( (*a)->name, name);
	    (*a)->desc = strlen((*a)->name)+1+((*a)->name);
	    strcpy( (*a)->desc, desc );
	    return;
	}
    }
}


/* find a group by name */
struct newsgroup * findgroup(const char* name) {
    struct newsgroup * a;
    int c;

    a = active;
    while ( a ) {
	c = strcmp( a->name, name );
	if ( c < 0 )
	    a = a->left;
	else if ( c > 0 )
	    a = a->right;
	else
	    return a;
    }
    return a;
}


/* find a blocked group by name */
/* from leafnode-1.5(enz)
int findblocked(const char* name) {
    struct newsgroup * a;
    int c;

    a = blocked;
    while ( a ) {
        c = strcmp( a->name, name );
        if ( c < 0 )
            a = a->left;
        else if ( c > 0 )
            a = a->right;
        else
            return TRUE;
    }
    return FALSE;
}
*/

static void freeactive( struct newsgroup * g ) {
    if ( g ) {
	freeactive( g->right );
	freeactive( g->left );
	free( (char*)g );
    }
}


/* this uses a nifty technique for building a binary tree from a
   sorted list... the basic observation is that if you count from one
   end, with 1 being the rightmost node, then the number of nodes
   between N and the leaf nodes is equal to the number of zero bits
   at the small end of N.  this tree probably doesn't make it obvious:

   1 2 3 4 5 6 7

         1
     1   0   1
   1 0 1 0 1 0 1
   | | | | | | + no zeroes: leaf node
   | | | | | +-- one zero: one step up
   | | | | +---- no zeroes: leaf node
   | | | +------ two zeroes: two steps up
   | | +-------- no zeroes: leaf node
   | +---------- one zero: one step up
   +------------ no zeroes: leaf node

   this can be used to build a smallest-height tree, and with a bit more
   care, a _perfectly_ balanced tree */

void readactive( void ) {
    char * p;
    char * q;
    int first, last;
    time_t age;
    int fd;
    struct newsgroup * g;
    struct stat st;

    int line;
    struct newsgroup *levels[32]; /* theoretically finite size :) */
    int l;

    static char * stuff;

    if ( stuff ) {
	freeactive( active );
	active = NULL;
	free( (char*)stuff );
	stuff = NULL;
    }

    strcpy(s, spooldir);
    strcat(s, "/leaf.node/groupinfo");
    fd = open( s, O_RDONLY );
    if ( stat( s, &st ) ) {
	syslog( LOG_ERR, "can't stat %s: %m", s );
	return;
    }
    stuff = critmalloc( st.st_size+1, "Reading group info" );
    if ( (fd=open( s, O_RDONLY))<0 ||
	 (read( fd, stuff, st.st_size ) < st.st_size) ) {
	syslog( LOG_ERR, "can't open/read %s: %m", s );
	return;
    } else {
	close( fd );
	stuff[st.st_size] = '\0'; /* 0-terminate string */
    }

    line = 1;
    for( l=0; l<32; levels[l++] = NULL )
	;

    p = stuff;
    while ( p && *p ) {
	q = p;
	p = strchr(p, ' ');
	if (p && *p)
	    *p++ = '\0';
	else {
	    syslog( LOG_ERR, "groupinfo file may be corrupted" );
	    *q = '\0' ;		/* skip defective group */
	}
	if ( *q ) {
	    last = p ? strtol( p, &p, 10 ) : 0;
	    first = p ? strtol( p, &p, 10 ) : 1;
	    age = p ? strtol( p, &p, 10 ) : 1;
	    while (p && *p && isspace(*p))
		p++;
	    if (first < 1)
		first = 1;

	    g = (struct newsgroup *) critmalloc(sizeof(struct newsgroup),
						"reading groupinfo");
	    g->right = g->left = NULL;
	    g->first = first;
	    g->last = last;
	    g->age = age;
	    g->name = q;
	    g->desc = p;

	    for( l=0; (line&(1<<l))==0; l++ )
		;
	    if ( l ) {
		g->right = levels[l-1];
	    }
	    if ( active == g->right )
		active = g;
	    if ( l<31 && levels[l+1] && !levels[l+1]->left )
		levels[l+1]->left = g;
	    levels[l] = g;

	    p = strchr( p, '\n' );
	    if ( p )
		*p++ = '\0';
	    line++;
	} else {
	    p = strchr( p, '\n' );
	    if ( p )
		p++;
	}
    }
    g = NULL;
    for ( l=0; l<31; l++ ) {
	if ( levels[l] ) {
	    if ( g && levels[l] &&
		 levels[l]->left == NULL && levels[l]->right != g ) {
		levels[l]->left = g;
		g = NULL;
	    }
	    if ( !levels[l+1] ||
		 ( levels[l] != levels[l+1]->left &&
		   levels[l] != levels[l+1]->right ) ) {
		if ( g )
		    syslog( LOG_ERR, "2+5=2" );
		g = levels[l];
	    }
	}
    }
}

/* from leafnode-1.5(enz). This is more-or-less copied from readactive()
void readblocked( void ) {
    char * p;
    char * q;
    int fd;
    struct newsgroup * g;
    struct stat st;

    int line;
    struct newsgroup *levels[32];
    int l;
    int skiptoeol;

    static char * stuff;

    if ( stuff ) {
        freeactive( blocked );
        free( (char*)stuff );
        stuff = NULL;
    }
    blocked = NULL;

    strcpy(s, spooldir);
    strcat(s, "/leaf.node/blocked");
    fd = open( s, O_RDONLY );
    if ( stat( s, &st ) ) {
        return;
    }
    stuff = critmalloc( st.st_size+1, "Reading blocked group info" );
    if ( (fd=open( s, O_RDONLY))<0 ||
         (read( fd, stuff, st.st_size ) < st.st_size) ) {
        syslog( LOG_ERR, "can't open/read %s: %m", s );
        return;
    } else {
        close( fd );
        stuff[st.st_size] = '\0';
    }

    line = 1;
    for( l=0; l<32; levels[l++] = NULL )
        ;

    p = stuff;
    while ( p && *p ) {
        q = p;
        while (!isspace(*p)) { p++; }
        skiptoeol = (*p != '\n');
        *p++ = '\0';
        if ( *q ) {
            g = (struct newsgroup *) critmalloc(sizeof(struct newsgroup),
                                                "reading blocked");
            g->right = g->left = NULL;
            g->first = 0;
            g->last = 0;
            g->name = q;
            g->desc = NULL;

            for( l=0; (line&(1<<l))==0; l++ )
                ;
            if ( l ) {
                g->right = levels[l-1];
            }
            if ( blocked == g->right )
                blocked = g;
            if ( l<31 && levels[l+1] && !levels[l+1]->left )
                levels[l+1]->left = g;
            levels[l] = g;

            if (skiptoeol) {
                p = strchr( p, '\n' );
                if ( p )
                    *p++ = '\0';
            }
            line++;
        } else {
            if (skiptoeol) {
                p = strchr( p, '\n' );
                if ( p )
                    *p++ = '\0';
            }
        }
    }
    g = NULL;
    for ( l=0; l<31; l++ ) {
        if ( levels[l] ) {
            if ( g && levels[l] &&
                 levels[l]->left == NULL && levels[l]->right != g ) {
                levels[l]->left = g;
                g = NULL;
            }
            if ( !levels[l+1] ||
                 ( levels[l] != levels[l+1]->left &&
                   levels[l] != levels[l+1]->right ) ) {
                if ( g )
                    syslog( LOG_ERR, "2+5=2" );
                g = levels[l];
            }
        }
    }
}
*/

/*
 * returns 1 if something could be written, 0 otherwise
 */
static int helpwriteactive ( struct newsgroup *g, FILE * f ) {
    if (g) {
        if ( helpwriteactive (g->right, f) ) {
            if ( fprintf(f, "%s %d %d %lu %s\n", g->name, g->last, g->first,
                 g->age, g->desc && *(g->desc) ? g->desc : "-x-" ) ) {
        	if ( helpwriteactive (g->left, f) )
		    return 1;
		else
		    return 0;
	    } else
		return 0;
	} else
	    return 0;
    }
    return 1;
}

void writeactive( void ) {
    FILE * a;
    char c[PATH_MAX];
    int err;

    strcpy(s, spooldir);
    strcat(s, "/leaf.node/groupinfo.new");
    a = fopen( s, "w" );
    if (!a) {
	syslog( LOG_ERR, "cannot open new groupinfo file: %m" );
	return;
    }
    err = helpwriteactive( active, a );
    fclose( a );

    if ( err != 0 ) {
	strcpy(c, spooldir);
        strcat(c, "/leaf.node/groupinfo");
	rename( s, c );
    } else {
	syslog( LOG_ERR, "error writing groupinfo file (disk full?)" );
	unlink(s);
    }
}

/*
 * fake an active file if the groupinfo file is corrupted
 */
void fakeactive( void ) {
    DIR * d;
    struct dirent * de;
    DIR * ng;
    struct dirent * nga;
    int i;

    int first, last;

    strcpy( s, spooldir );
    strcat( s, "/interesting.groups" );
    
    d = opendir( s );
    if ( !d ) {
	syslog( LOG_ERR, "cannot open directory %s: %m", s );
	return;
    }
    while ( (de = readdir(d)) ) {
	if ( isalnum( *(de->d_name) ) ) {
	    /* get first and last article from the directory. This is
	     * the most secure way to get to that information since the
	     * .overview files may be corrupt as well
	     */
	    chdirgroup( de->d_name );
	    first = INT_MAX;
	    last = 0;

	    ng = opendir( "." );
	    while ( ( nga = readdir( ng ) ) != NULL ) {
		if ( isdigit ( *(nga->d_name ) ) ) {
		    i = strtol( nga->d_name, NULL, 10 );
		    if ( i < first )
			first = i;
		    if ( i > last )
			last = i;
		}
	    }
	    if ( first > last ) {
		first = 0;
		last = 1;
	    }
	    closedir( ng );
	    syslog( LOG_DEBUG, "parsed directory %s: first %d, last %d",
		    de->d_name, first, last );
	    insertgroup( de->d_name, first, last, NULL, 0 );
	}
    }
}