librazor/iterator.c
changeset 367 e45f50e940b6
parent 308 f4761f529b9e
child 377 5549419824b4
     1.1 --- a/librazor/iterator.c	Tue Jul 01 09:44:46 2008 -0400
     1.2 +++ b/librazor/iterator.c	Thu May 14 05:55:19 2009 +0100
     1.3 @@ -1,6 +1,7 @@
     1.4  /*
     1.5   * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     1.6   * Copyright (C) 2008  Red Hat, Inc
     1.7 + * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     1.8   *
     1.9   * This program is free software; you can redistribute it and/or modify
    1.10   * it under the terms of the GNU General Public License as published by
    1.11 @@ -229,6 +230,83 @@
    1.12  	free(pi);
    1.13  }
    1.14  
    1.15 +RAZOR_EXPORT struct razor_file_iterator *
    1.16 +razor_file_iterator_create(struct razor_set *set, struct razor_package *package)
    1.17 +{
    1.18 +	struct razor_file_iterator *fi;
    1.19 +
    1.20 +	assert (set != NULL);
    1.21 +	assert (package != NULL);
    1.22 +
    1.23 +	fi = zalloc(sizeof *fi);
    1.24 +	fi->set = set;
    1.25 +	fi->index = list_first(&package->files, &set->file_pool);
    1.26 +	array_init(&fi->path);
    1.27 +
    1.28 +	return fi;
    1.29 +}
    1.30 +
    1.31 +RAZOR_EXPORT int
    1.32 +razor_file_iterator_next(struct razor_file_iterator *fi,
    1.33 +			 const char **name)
    1.34 +{
    1.35 +	struct razor_entry *e, *dir, *entries;
    1.36 +	char *pool, *s, *f;
    1.37 +
    1.38 +	assert (fi != NULL);
    1.39 +
    1.40 +	if (!fi->index) {
    1.41 +		*name = NULL;
    1.42 +		return 0;
    1.43 +	}
    1.44 +
    1.45 +	entries = (struct razor_entry *) fi->set->files.data;
    1.46 +	pool = fi->set->file_string_pool.data;
    1.47 +
    1.48 +	dir = entries;
    1.49 +	fi->path.size = 0;
    1.50 +	for(;;) {
    1.51 +		e = dir;
    1.52 +		do {
    1.53 +			if (entries + fi->index->data == e) {
    1.54 +				f = pool + e->name;
    1.55 +				s = array_add(&fi->path, strlen(f) + 1);
    1.56 +				strcpy(s, f);
    1.57 +				if (fi->path.size == 1) {
    1.58 +					array_add(&fi->path, 1);
    1.59 +					strcpy(fi->path.data, "/");
    1.60 +				}
    1.61 +				*name = fi->path.data;
    1.62 +				fi->index = list_next(fi->index);
    1.63 +				return 1;
    1.64 +			}
    1.65 +		} while (!((e++)->flags & RAZOR_ENTRY_LAST));
    1.66 +		for(e--; e >= dir; e--)
    1.67 +			if (e->start && fi->index->data >= e->start)
    1.68 +				break;
    1.69 +		if (e < dir)
    1.70 +			break;
    1.71 +		f = pool + e->name;
    1.72 +		s = array_add(&fi->path, strlen(f) + 1);
    1.73 +		strcpy(s, f);
    1.74 +		s += strlen(f);
    1.75 +		*s = '/';
    1.76 +		dir = entries + e->start;
    1.77 +	}
    1.78 +
    1.79 +	printf("file_iterator_next: Failed to find file %d\n",fi->index->data);
    1.80 +	*name = NULL;
    1.81 +	return 0;
    1.82 +}
    1.83 +
    1.84 +RAZOR_EXPORT void razor_file_iterator_destroy(struct razor_file_iterator *fi)
    1.85 +{
    1.86 +	assert (fi != NULL);
    1.87 +
    1.88 +	array_release(&fi->path);
    1.89 +	free(fi);
    1.90 +}
    1.91 +
    1.92  struct razor_package_query {
    1.93  	struct razor_set *set;
    1.94  	char *vector;