File: filterlist.c

package info (click to toggle)
reprepro 1.3.1%2B1-1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 1,660 kB
  • ctags: 1,621
  • sloc: ansic: 22,424; sh: 2,032; python: 138; makefile: 127
file content (358 lines) | stat: -rw-r--r-- 9,217 bytes parent folder | download | duplicates (2)
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
/*  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 "chunks.h"
#include "filterlist.h"

extern int verbose;

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,"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: %m!\n",filename);
		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_get(const char *confdir, 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_dirconcat(confdir, 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 );
	}
}

retvalue filterlist_load(struct filterlist *list, const char *confdir,const char *configline) {
	const char *filename;
	enum filterlisttype defaulttype;
	size_t count;
	struct filterlistfile **files;


	if( strncmp(configline,"install",7) == 0 && xisspace(configline[7]) ) {
		defaulttype = flt_install; filename = configline + 7;
	} else if( strncmp(configline,"hold",4) == 0 && xisspace(configline[4]) ) {
		defaulttype = flt_hold; filename = configline + 4;
	} else if( strncmp(configline,"deinstall",9) == 0 && xisspace(configline[9]) ) {
		defaulttype = flt_deinstall; filename = configline + 9;
	} else if( strncmp(configline,"purge",5) == 0 && xisspace(configline[5]) ) {
		defaulttype = flt_purge; filename = configline + 5;
	} else if( strncmp(configline,"error",5) == 0 && xisspace(configline[5]) ) {
		defaulttype = flt_error; filename = configline + 5;
	} else {
		fprintf(stderr,"Cannot parse '%s' into the format 'install|deinstall|purge|hold <filename>'\n",configline);
		return RET_ERROR;
	}
	count = 0;
	files = NULL;
	while( *filename != '\0' && xisspace(*filename) )
		filename++;
	while( *filename != '\0' ) {
		const char *filenameend = filename;
		struct filterlistfile **n;
		retvalue r;

		while( *filenameend != '\0' && !xisspace(*filenameend) )
			filenameend++;

		n = realloc(files, (count+1)*
				sizeof(struct filterlistfile *));
		if( n == NULL ) {
			r = RET_ERROR_OOM;
		} else {
			n[count] = NULL;
			files = n;
			r = filterlistfile_get(confdir, filename, filenameend-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;
		}
		filename = filenameend;
		while( *filename != '\0' && xisspace(*filename) )
			filename++;
	}
	list->count = count;
	list->files = files;
	list->defaulttype = defaulttype;
	return RET_OK;
}

void filterlist_empty(struct filterlist *list, enum filterlisttype defaulttype) {
	list->files = NULL;
	list->count = 0;
	list->defaulttype = defaulttype;
}

static inline bool_t 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,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;
}