File: filterlist.c

package info (click to toggle)
reprepro 4.2.0-2squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,016 kB
  • ctags: 3,674
  • sloc: ansic: 46,905; sh: 13,899; pascal: 160; makefile: 159; python: 138
file content (406 lines) | stat: -rw-r--r-- 9,962 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
/*  This file is part of "reprepro"
 *  Copyright (C) 2005 Bernhard R. Link
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
 */
#include <config.h>

#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#include "error.h"
#include "mprintf.h"
#include "strlist.h"
#include "names.h"
#include "configparser.h"
#include "filterlist.h"

struct filterlistfile {
	size_t reference_count;

	char *filename;
	size_t filename_len;

	/*@owned@*//*@null@*/
	struct filterlistitem *root;
	/*@dependent@*//*@null@*/
	const struct filterlistitem *last;

	/*@owned@*//*@null@*/
	struct filterlistfile *next;
} *listfiles = NULL;

struct filterlistitem {
	/*@owned@*//*@null@*/
	struct filterlistitem *next;
	char *packagename;
	enum filterlisttype what;
};

static void filterlistitems_free(/*@null@*//*@only@*/struct filterlistitem *list) {
	while( list != NULL ) {
		struct filterlistitem *next = list->next;
		free(list->packagename);
		free(list);
		list = next;
	}
}

static void filterlistfile_unlock(struct filterlistfile *list) {
	assert( list != NULL );

	if( list->reference_count <= 1 ) {
		struct filterlistfile **p = &listfiles;

		assert( list->reference_count == 1 );
		if( list->reference_count == 0 )
			return;

		while( *p != NULL && *p != list )
			p = &(*p)->next;
		assert( p != NULL );
		if( *p == list ) {
			*p = list->next;
			filterlistitems_free(list->root);
			free(list->filename);
			free(list);
		}
	} else
		list->reference_count--;
}

static inline retvalue filterlistfile_parse(struct filterlistfile *n, const char *filename, FILE *f) {
	char *lineend,*namestart,*nameend,*what;
	int cmp;
	enum filterlisttype type;
	struct filterlistitem *h;
	char line[1001];
	int lineno = 0;
	struct filterlistitem **last = &n->root;

	while( fgets(line,1000,f) != NULL ) {
		lineno++;
		lineend = strchr(line,'\n');
		if( lineend == NULL ) {
			fprintf(stderr,"Overlong line in '%s'!\n",filename);
			return RET_ERROR;
		}
		while( lineend >= line && xisspace(*lineend) )
			*(lineend--) = '\0';
		/* Ignore line only containing whitespace */
		if( line[0] == '\0' )
			continue;
		namestart = line;
		while( *namestart != '\0' && xisspace(*namestart) )
			namestart++;
		nameend=namestart;
		while( *nameend != '\0' && !xisspace(*nameend) )
			nameend++;
		what = nameend;
		while( *what != '\0' && xisspace(*what) )
			*(what++)='\0';
		if( *what == '\0' ) {
			fprintf(stderr,"Malformed line in '%s': %d!\n",filename,lineno);
			return RET_ERROR;
		}
		if( strcmp(what,"install") == 0 ) {
			type = flt_install;
		} else if( strcmp(what,"deinstall") == 0 ) {
			type = flt_deinstall;
		} else if( strcmp(what,"purge") == 0 ) {
			type = flt_purge;
		} else if( strcmp(what,"hold") == 0 ) {
			type = flt_hold;
		} else if( strcmp(what, "upgradeonly") == 0 ) {
			type = flt_upgradeonly;
		} else if( strcmp(what, "warning") == 0 ) {
			type = flt_warning;
		} else if( strcmp(what,"error") == 0 ) {
			type = flt_error;
		} else {
			fprintf(stderr,"Unknown status in '%s':%d: '%s'!\n",filename,lineno,what);
			return RET_ERROR;
		}
		if( *last == NULL || strcmp(namestart,(*last)->packagename) < 0 )
			last = &n->root;
		cmp = -1;
		while( *last != NULL && (cmp=strcmp(namestart,(*last)->packagename)) > 0 )
			last = &((*last)->next);
		if( cmp == 0 ) {
			fprintf(stderr,"Two lines describing '%s' in '%s'!\n",namestart,filename);
			return RET_ERROR;
		}
		h = calloc(1,sizeof(*h));
		if( h == NULL ) {
			return RET_ERROR_OOM;
		}
		h->next = *last;
		*last = h;
		h->what = type;
		h->packagename = strdup(namestart);
		if( h->packagename == NULL ) {
			return RET_ERROR_OOM;
		}
	}
	n->last = *last;
	return RET_OK;

}

static inline retvalue filterlistfile_read(struct filterlistfile *n, const char *filename) {
	FILE *f;
	retvalue r;

	f = fopen(filename,"r");
	if( f == NULL ) {
		fprintf(stderr, "Cannot open %s for reading: %s!\n",
				filename, strerror(errno));
		return RET_ERROR;
	}
	r = filterlistfile_parse(n, filename, f);

	// Can this return an yet unseen error? was read-only..
	(void)fclose(f);
	return r;
}

static inline retvalue filterlistfile_getl(const char *filename, size_t len, struct filterlistfile **list) {
	struct filterlistfile *p;
	retvalue r;

	for( p = listfiles ; p != NULL ; p = p->next ) {
		if( p->filename_len == len &&
				strncmp(p->filename, filename, len) == 0 ) {
			p->reference_count++;
			*list = p;
			return RET_OK;
		}
	}
	p = calloc(1,sizeof(struct filterlistfile));
	if( p == NULL )
		return RET_ERROR_OOM;
	p->reference_count = 1;
	p->filename = strndup(filename, len);
	p->filename_len = len;
	if( p->filename == NULL ) {
		free(p);
		return RET_ERROR_OOM;
	}
	if( p->filename[0] != '/' ) {
		char *fullfilename = calc_conffile(p->filename);
		if( fullfilename == NULL )
			r = RET_ERROR_OOM;
		else {
			r = filterlistfile_read(p, fullfilename);
			free(fullfilename);
		}
	} else
		r = filterlistfile_read(p, p->filename);

	if( RET_IS_OK(r) ) {
		p->next = listfiles;
		listfiles = p;
		*list = p;
	} else {
		filterlistitems_free(p->root);
		free(p->filename);
		free(p);
	}
	return r;
}

static inline retvalue filterlistfile_get(/*@only@*/char *filename, /*@out@*/struct filterlistfile **list) {
	struct filterlistfile *p;
	retvalue r;
	size_t len = strlen(filename);

	for( p = listfiles ; p != NULL ; p = p->next ) {
		if( p->filename_len == len &&
				strncmp(p->filename, filename, len) == 0 ) {
			p->reference_count++;
			*list = p;
			free(filename);
			return RET_OK;
		}
	}
	p = calloc(1,sizeof(struct filterlistfile));
	if( p == NULL ) {
		free(filename);
		return RET_ERROR_OOM;
	}
	p->reference_count = 1;
	p->filename = filename;
	p->filename_len = len;
	if( p->filename == NULL ) {
		free(p);
		return RET_ERROR_OOM;
	}
	if( p->filename[0] != '/' ) {
		char *fullfilename = calc_conffile(p->filename);
		if( fullfilename == NULL )
			r = RET_ERROR_OOM;
		else {
			r = filterlistfile_read(p, fullfilename);
			free(fullfilename);
		}
	} else
		r = filterlistfile_read(p, p->filename);

	if( RET_IS_OK(r) ) {
		p->next = listfiles;
		listfiles = p;
		*list = p;
	} else {
		filterlistitems_free(p->root);
		free(p->filename);
		free(p);
	}
	return r;
}

void filterlist_release(struct filterlist *list) {
	size_t i;

	assert(list != NULL);

	if( list->files != NULL ) {
		for( i = 0 ; i < list->count ; i++ )
			filterlistfile_unlock(list->files[i]);
		free(list->files);
		list->files = NULL;
	} else {
		assert( list->count == 0 );
	}
}

static const struct constant filterlisttype_listtypes[] = {
	{"install",	(int)flt_install},
	{"hold",	(int)flt_hold},
	{"deinstall",	(int)flt_deinstall},
	{"purge",	(int)flt_purge},
	{"upgradeonly",	(int)flt_upgradeonly},
	{"warning",	(int)flt_warning},
	{"error",	(int)flt_error},
	{NULL, 0}
};

retvalue filterlist_load(struct filterlist *list, struct configiterator *iter) {
	enum filterlisttype defaulttype;
	size_t count;
	struct filterlistfile **files;
	retvalue r;
	char *filename;

	r = config_getenum(iter, filterlisttype, listtypes, &defaulttype);
	if( r == RET_NOTHING || r == RET_ERROR_UNKNOWNFIELD ) {
		fprintf(stderr,
"Error parsing %s, line %u, column %u: Expected default action as first argument to FilterList: (one of install, purge, hold, ...)\n",
				config_filename(iter),
				config_markerline(iter),
				config_markercolumn(iter));
		return RET_ERROR;
	}
	if( RET_WAS_ERROR(r) )
		return r;

	count = 0;
	files = NULL;
	while( (r = config_getword(iter, &filename)) != RET_NOTHING ) {
		struct filterlistfile **n;

		n = realloc(files, (count+1)*
				sizeof(struct filterlistfile *));
		if( n == NULL ) {
			free(filename);
			r = RET_ERROR_OOM;
		} else {
			n[count] = NULL;
			files = n;
			// TODO: make filename only
			r = filterlistfile_get(filename, &files[count]);
			if( RET_IS_OK(r) )
				count++;
		}
		if( RET_WAS_ERROR(r) ) {
			while( count > 0 ) {
				count--;
				filterlistfile_unlock(files[count]);
			}
			free(files);
			return r;
		}
	}
	list->count = count;
	list->files = files;
	list->defaulttype = defaulttype;
	list->set = true;
	return RET_OK;
}

static inline bool find(const char *name, /*@null@*/struct filterlistfile *list) {
	int cmp;
	/*@dependent@*/const struct filterlistitem *last = list->last;

	assert( last != NULL );

	if( last->next != NULL ) {
		cmp = strcmp(name,last->next->packagename);
		if( cmp == 0 ) {
			list->last = last->next;
			return true;
		}
	}
	if( last->next == NULL || cmp < 0 ) {
		cmp = strcmp(name,last->packagename);
		if( cmp == 0 ) {
			return true;
		} else if( cmp > 0 )
			return false;
		last = list->root;
		cmp = strcmp(name,last->packagename);
		if( cmp == 0 ) {
			list->last = list->root;
			return true;
		} else if( cmp < 0 )
			return false;
	}
	/* now we are after last */
	while( last->next != NULL ) {
		cmp = strcmp(name,last->next->packagename);
		if( cmp == 0 ) {
			list->last = last->next;
			return true;
		}
		if( cmp < 0 ) {
			list->last = last;
			return false;
		}
		last = last->next;
	}
	list->last = last;
	return false;
}

enum filterlisttype filterlist_find(const char *name, const struct filterlist *list) {
	size_t i;
	for( i = 0 ; i < list->count ; i++ ) {
		if( list->files[i]->root != NULL && find(name,list->files[i]) )
			return list->files[i]->last->what;
	}
	return list->defaulttype;
}