File: xoverutil.c

package info (click to toggle)
leafnode 1.9.9-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,312 kB
  • ctags: 556
  • sloc: ansic: 9,893; sh: 817; makefile: 288; perl: 187
file content (432 lines) | stat: -rw-r--r-- 10,578 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
/*
libutil -- handling xover records

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 <Randolf.Skerka@gmx.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, 1999.

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

#include "leafnode.h"
#include <fcntl.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <netdb.h>
#include <signal.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>

#ifndef UIO_MAXIOV
#define UIO_MAXIOV 16
#endif

extern struct state _res;


void stripspace( char * p );
void stripspace( char * p ) {
    char * p1;
    char * p2;

    p1 = p2 = p;
    while ( *p1 ) {
	if ( isspace((unsigned char)*p1) ) {
	    *p2 = ' ';
	    do {
		p1++;
	    } while ( isspace((unsigned char)*p1) );
	    if ( *p1 )
		p2++;
	} else {
	    *p2++ = *p1++;
	}
    }
    *p2 = '\0';
}




char * getxoverline( const char * filename );
char * getxoverline( const char * filename ) {
    char * l;
    char * result;
    FILE * f;

    result = NULL;
    debug = 0;
    if ( (f=fopen( filename, "r" )) ) {
	char * from;
	char * subject;
	char * date;
	char * msgid;
	char * references;
	int bytes;
	int lines, linecount;
	char * xref;
	char ** h;
	int body;

	from = subject = date = msgid = references = xref = NULL;
	bytes = lines = linecount = 0;
	h = NULL;
	body = 0;

	while ( !feof( f ) && ( ( l = getaline( f )) != NULL ) ) {
	    if ( l ) {
		linecount++; /* most luxurious - but it doesn't happen often */
		bytes += strlen( l ) + 2;
	    }
	    if ( body || !l ) {
		/* nothing */
	    } else if ( !body && !*l ) {
		linecount = 0;
		body = 1;
	    } else if ( h && isspace((unsigned char)*l) ) {
		stripspace( l );
		if ( *l ) {
		    (*h) = critrealloc( *h, strlen( *h ) + strlen( l ) + 1,
					"extending header" );
		    strcat( *h, l );
		}
	    } else if ( !from && !strncasecmp( "From: ", l, 6 ) ) {
		l += 6;
		stripspace( l );
		if ( *l ) {
		    from = strdup( l );
		    h = &from;
		}
	    } else if ( !subject && !strncasecmp( "Subject: ", l, 9 ) ) {
		l += 9;
		stripspace( l );
		if ( *l ) {
		    subject = strdup( l );
		    h = &subject;
		}
	    } else if ( !date && !strncasecmp( "Date: ", l, 6 ) ) {
		l += 6;
		stripspace( l );
		if ( *l ) {
		    date = strdup( l );
		    h = &date;
		}
	    } else if ( !msgid && !strncasecmp( "Message-ID: ", l, 12 ) ) {
		l += 12;
		stripspace( l );
		if ( *l ) {
		    msgid = strdup( l );
		    h = &msgid;
		}
	    } else if ( !references &&
			!strncasecmp( "References: ", l, 12 ) ) {
		l += 12;
		stripspace( l );
		if ( *l ) {
		    references = strdup( l );
		    h = &references;
		}
	    } else if ( !lines && !strncasecmp( "Lines: ", l, 7 ) ) {
		l += 7;
		stripspace( l );
		if ( *l ) {
		    lines = strtol( l, NULL, 10 );
/* not strictly correct, but there are probably no multiline Line: headers
		    h = &lines;
*/
		}
	    } else if ( !xref && !strncasecmp( "Xref: ", l, 6 ) ) {
		l += 6;
		stripspace( l );
		if ( *l ) {
		    xref = strdup( l );
		    h = &xref;
		}
	    } else {
		h = NULL;
	    }
	}
	if ( from && date && subject && msgid && bytes ) {
	    result = critmalloc( strlen( from ) + strlen( date ) +
				 strlen( subject ) + strlen( msgid ) +
				 (references ? strlen( references ) : 0 ) +
				 100 + ( xref ? strlen( xref) : 0 ),
				 "computing overview line" );
	    sprintf( result, "%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d",
		     filename, subject, from, date, msgid,
		     references ? references : "" ,
		     bytes,
		     lines ? lines : linecount );
	    if ( xref ) {
		strcat( result, "\tXref: " );
		strcat( result, xref );
	    }
	}
	fclose( f );
	free ( from );
	free ( date );
	free ( subject );
	free ( msgid );
	free ( references );
	free ( xref );
    }
    debug = debugmode;
    return result;
}


/* utility routine to pull the xover info into memory
   returns 0 if there's some error, non-zero else */
int getxover( void ) {
    DIR *d;
    struct dirent * de;
    int fd;
    struct stat st;
    long art;
    static char * overview;
    int error;
    char * p, * q;

    /* free any memory left over from last time */
    error = 0;

    if ( xoverinfo ) {
	for( art=xfirst; art<=xlast; art++ ) {
	    if ( xoverinfo[art-xfirst].mallocd &&
		 xoverinfo[art-xfirst].text ) {
		free( xoverinfo[art-xfirst].text );
		xoverinfo[art-xfirst].text = NULL;
	    }
	}
    }

    if ( overview ) {
	free( overview );
	overview = NULL;
    }

    /* find article range, without locking problems */

    d = opendir( "." );
    if ( !d ) {
	syslog( LOG_ERR, "opendir: %m" );
	return 0;
    }

    xfirst = INT_MAX;
    xlast = 0;
    while ( (de=readdir(d)) ) {
	art = strtol( de->d_name, &p, 10 );
	if ( art && p && !*p ) {
	    if ( art < xfirst )
		xfirst = art;
	    if ( art > xlast )
		xlast = art;
	}
    }

    if ( xlast < xfirst ) {
	closedir( d );
	return 0;
    }

    /* next, read .overview, correct it if it seems too different from
       what the directory implies, and write the result back */

    rewinddir( d );

    xoverinfo = (struct xoverinfo *)
		critrealloc( (char*)xoverinfo,
			     sizeof(struct xoverinfo) * (xlast+1-xfirst),
			     "allocating overview array" );
    memset( xoverinfo, 0, sizeof(struct xoverinfo) * (xlast+1-xfirst) );

    if ( (fd=open( ".overview", O_RDONLY)) >= 0 &&
	  fstat( fd, &st )==0 &&
	  (overview=(char*)malloc( st.st_size + 1 )) != NULL &&
	  read( fd, overview, st.st_size ) == st.st_size ) {

	close( fd );
	overview[st.st_size] = '\0';

	/* okay, we have the content, so let's parse it roughly */

	p = overview;
	while ( p && *p ) {
	    while ( p && isspace((unsigned char)*p) )
		p++;
	    q = strchr( p, '\n' );
	    if ( q )
		*q++ = '\0';

	    art = strtol( p, NULL, 10 );
	    if ( art > xlast || art < xfirst ) {
		error++;
	    } else if ( xoverinfo[art-xfirst].text ) {
		error++;
		xoverinfo[art-xfirst].text = NULL;
	    } else {
		xoverinfo[art-xfirst].text = p;
	    }

	    p = q;
	}
    }

    /* so, what was missing? */

    while ( (de=readdir(d)) ) {
      if (!strcmp(".", de->d_name) || !strcmp("..", de->d_name)) continue;
	art = strtol( de->d_name, &p, 10 );
	if (p && !*p && art>=xfirst && art<=xlast) {
	    if ( !xoverinfo[art-xfirst].text ) {
		if ( debugmode )
		    syslog( LOG_DEBUG, "reading XOVER info from %s/%s",
			    getcwd( s, 1024 ), de->d_name );
		error++;
		if ( ( xoverinfo[art-xfirst].text =
		      getxoverline( de->d_name )) != NULL ) {
		    xoverinfo[art-xfirst].mallocd = 1;
		} else if ( delaybody ) {
		    xoverinfo[art-xfirst].mallocd = 1;
		} else {
		    getcwd( s, 1024 );
		    syslog( LOG_NOTICE, "illegal article: %s/%s: %m",
			    s, de->d_name );
		    if ( lstat( de->d_name, &st ) && S_ISREG( st.st_mode ) ) {
			if ( unlink( de->d_name ) )
			    syslog( LOG_WARNING, "failed to remove %s/%s: %m",
				    s, de->d_name );
		    } else {
			syslog( LOG_WARNING, "%s/%s is not a regular file",
				s, de->d_name );
		    }
		}
	    }
	}
	if ( art >= xfirst && art <= xlast && xoverinfo[art-xfirst].text ) {
	    xoverinfo[art-xfirst].exists = 1;
	} else if ( verbose ) {
	    getcwd( s, 1024 );
	    if ( art == 0 && strcmp( de->d_name, ".overview" ) &&
		 strcmp( de->d_name, "." ) && strcmp( de->d_name, ".." ) ) {
		if ( verbose && unlink( de->d_name ) &&
		     errno != EISDIR && errno != EPERM )
		    printf( "attempted to delete %s/%s in case it's junk: %s\n",
			    s, de->d_name, errno ? strerror(errno) : "OK" );
	    } else if ( p && !*p ) {
	    	if ( art > 0 && art < xfirst )
		    printf("article %s/%ld is below the low-water mark (%ld)\n",
			   s, art, xfirst );
		else if ( art > xlast )
		    printf("article %s/%ld is above the high-water mark (%ld)\n",
			   s, art, xlast );
		else {
		    printf( "article %s/%ld contained illegal headers\n",
			    s, art );
		}
	    }
	}
    }

    /* if something had to be fixed, write a better file to disk for
       next time - race conditions here, but none dangerous */

    if ( error ) {
	int wfd;
	char newfile[20];

	if ( verbose )
	    syslog( LOG_DEBUG, "updated %d lines in %s/.overview\n",
		    error, getcwd( s, 1024 ) );

	strcpy( newfile, ".overview.XXXXXX" );
#ifdef HAVE_MKSTEMP
	if ( (wfd=mkstemp(newfile)) != -1) {
#else
	if ( (wfd=open( mktemp(newfile), O_WRONLY|O_CREAT|O_EXCL, 0664 )) ) {
#endif
	    struct iovec oooh[UIO_MAXIOV];
	    int vi, vc, va;

	    vi = vc = va = 0;
	    for ( art=xfirst; art<=xlast; art++ ) {
		if ( xoverinfo[art-xfirst].exists &&
		     xoverinfo[art-xfirst].text ) {
		    oooh[vi].iov_base = xoverinfo[art-xfirst].text;
		    oooh[vi].iov_len = strlen( xoverinfo[art-xfirst].text );
		    vc += oooh[vi++].iov_len + 1;
		    oooh[vi].iov_base = "\n";
		    oooh[vi++].iov_len = 1;
		    if ( vi >= (UIO_MAXIOV - 1) ) {
			if ( writev( wfd, oooh, vi ) != vc ) {
			    syslog( LOG_ERR,
				    "writev() for .overview failed: %m" );
			    art = xlast+1; /* so the loop will stop */
			}
			vi = vc = 0;
			va = 1;
		    }
		}
	    }
	    if ( vi ) {
		if ( writev( wfd, oooh, vi ) != vc ) {
		    syslog( LOG_ERR, "writev() for .overview failed: %m" );
		} else {
		    va = 1;
		}
	    }
	    fchmod( wfd, 0664 );
	    close( wfd );
	    if ( va ) {
		if ( rename( newfile, ".overview" ) ) {
		    if ( unlink( newfile ) )
			syslog( LOG_ERR,
				"rename() and unlink() both failed: %m" );
		    else
			syslog( LOG_ERR,
				"rename(%s/%s, .overview) failed: %m",
				getcwd(s, 1024), newfile );
		} else {
		    if ( debugmode )
			syslog( LOG_DEBUG, "wrote %s/.overview\n",
				getcwd( s, 1024 ) );
		}
	    } else {
		unlink( newfile );
		/* the group must be newly empty: I want to keep the old
		   .overview file I think */
	    }
	} else {
#ifdef HAVE_MKSTEMP
	    syslog( LOG_ERR, "mkstemp of new .overview failed: %m" );
#else
	    syslog( LOG_ERR, "open(O_WRONLY|O_CREAT|O_EXCL) of new "
		    ".overview failed: %m" );
#endif
	}
    }
    return 1;
}