|
krh@15
|
1 |
#define _GNU_SOURCE
|
|
krh@15
|
2 |
|
|
krh@0
|
3 |
#include <stdlib.h>
|
|
krh@19
|
4 |
#include <stddef.h>
|
|
krh@0
|
5 |
#include <stdio.h>
|
|
krh@0
|
6 |
#include <string.h>
|
|
krh@0
|
7 |
#include <sys/types.h>
|
|
krh@0
|
8 |
#include <sys/stat.h>
|
|
krh@0
|
9 |
#include <sys/mman.h>
|
|
krh@0
|
10 |
#include <unistd.h>
|
|
krh@0
|
11 |
#include <fcntl.h>
|
|
krh@15
|
12 |
#include <errno.h>
|
|
krh@35
|
13 |
#include <ctype.h>
|
|
krh@0
|
14 |
|
|
krh@27
|
15 |
#include "razor.h"
|
|
krh@6
|
16 |
|
|
krh@30
|
17 |
struct array {
|
|
krh@30
|
18 |
void *data;
|
|
krh@30
|
19 |
int size, alloc;
|
|
krh@30
|
20 |
};
|
|
krh@30
|
21 |
|
|
krh@30
|
22 |
struct razor_set_section {
|
|
krh@30
|
23 |
unsigned int type;
|
|
krh@30
|
24 |
unsigned int offset;
|
|
krh@30
|
25 |
unsigned int size;
|
|
krh@30
|
26 |
};
|
|
krh@30
|
27 |
|
|
krh@30
|
28 |
struct razor_set_header {
|
|
krh@30
|
29 |
unsigned int magic;
|
|
krh@30
|
30 |
unsigned int version;
|
|
krh@30
|
31 |
struct razor_set_section sections[0];
|
|
krh@30
|
32 |
};
|
|
krh@30
|
33 |
|
|
krh@30
|
34 |
#define RAZOR_MAGIC 0x7a7a7a7a
|
|
krh@30
|
35 |
#define RAZOR_VERSION 1
|
|
krh@30
|
36 |
|
|
krh@30
|
37 |
#define RAZOR_PACKAGES 0
|
|
krh@30
|
38 |
#define RAZOR_REQUIRES 1
|
|
krh@30
|
39 |
#define RAZOR_PROVIDES 2
|
|
krh@30
|
40 |
#define RAZOR_STRING_POOL 3
|
|
krh@39
|
41 |
#define RAZOR_PACKAGE_POOL 4
|
|
krh@39
|
42 |
#define RAZOR_REQUIRES_POOL 5
|
|
krh@39
|
43 |
#define RAZOR_PROVIDES_POOL 6
|
|
krh@30
|
44 |
|
|
krh@30
|
45 |
struct razor_package {
|
|
krh@30
|
46 |
unsigned long name;
|
|
krh@30
|
47 |
unsigned long version;
|
|
krh@30
|
48 |
unsigned long requires;
|
|
krh@30
|
49 |
unsigned long provides;
|
|
krh@30
|
50 |
};
|
|
krh@30
|
51 |
|
|
krh@30
|
52 |
struct razor_property {
|
|
krh@30
|
53 |
unsigned long name;
|
|
krh@30
|
54 |
unsigned long version;
|
|
krh@30
|
55 |
unsigned long packages;
|
|
krh@30
|
56 |
};
|
|
krh@30
|
57 |
|
|
krh@30
|
58 |
struct razor_set {
|
|
krh@30
|
59 |
struct array string_pool;
|
|
krh@30
|
60 |
struct array packages;
|
|
krh@39
|
61 |
struct array package_pool;
|
|
krh@30
|
62 |
struct array requires;
|
|
krh@30
|
63 |
struct array provides;
|
|
krh@39
|
64 |
struct array requires_pool;
|
|
krh@39
|
65 |
struct array provides_pool;
|
|
krh@30
|
66 |
struct razor_set_header *header;
|
|
krh@30
|
67 |
};
|
|
krh@30
|
68 |
|
|
krh@30
|
69 |
struct import_property_context {
|
|
krh@30
|
70 |
struct array *all;
|
|
krh@30
|
71 |
struct array package;
|
|
krh@30
|
72 |
};
|
|
krh@30
|
73 |
|
|
krh@30
|
74 |
struct razor_importer {
|
|
krh@30
|
75 |
struct razor_set *set;
|
|
krh@32
|
76 |
struct array buckets;
|
|
krh@30
|
77 |
struct import_property_context requires;
|
|
krh@30
|
78 |
struct import_property_context provides;
|
|
krh@30
|
79 |
struct razor_package *package;
|
|
krh@30
|
80 |
};
|
|
krh@30
|
81 |
|
|
krh@13
|
82 |
static void
|
|
krh@13
|
83 |
array_init(struct array *array)
|
|
krh@13
|
84 |
{
|
|
krh@13
|
85 |
memset(array, 0, sizeof *array);
|
|
krh@13
|
86 |
}
|
|
krh@13
|
87 |
|
|
krh@13
|
88 |
static void
|
|
krh@13
|
89 |
array_release(struct array *array)
|
|
krh@13
|
90 |
{
|
|
krh@13
|
91 |
free(array->data);
|
|
krh@13
|
92 |
}
|
|
krh@13
|
93 |
|
|
krh@6
|
94 |
static void *
|
|
krh@6
|
95 |
array_add(struct array *array, int size)
|
|
krh@6
|
96 |
{
|
|
krh@6
|
97 |
int alloc;
|
|
krh@6
|
98 |
void *data, *p;
|
|
krh@6
|
99 |
|
|
krh@6
|
100 |
if (array->alloc > 0)
|
|
krh@6
|
101 |
alloc = array->alloc;
|
|
krh@6
|
102 |
else
|
|
krh@10
|
103 |
alloc = 16;
|
|
krh@6
|
104 |
|
|
krh@6
|
105 |
while (alloc < array->size + size)
|
|
krh@6
|
106 |
alloc *= 2;
|
|
krh@6
|
107 |
|
|
krh@6
|
108 |
if (array->alloc < alloc) {
|
|
krh@6
|
109 |
data = realloc(array->data, alloc);
|
|
krh@6
|
110 |
if (data == NULL)
|
|
krh@6
|
111 |
return 0;
|
|
krh@6
|
112 |
array->data = data;
|
|
krh@6
|
113 |
array->alloc = alloc;
|
|
krh@6
|
114 |
}
|
|
krh@6
|
115 |
|
|
krh@6
|
116 |
p = array->data + array->size;
|
|
krh@6
|
117 |
array->size += size;
|
|
krh@6
|
118 |
|
|
krh@6
|
119 |
return p;
|
|
krh@6
|
120 |
}
|
|
krh@6
|
121 |
|
|
krh@0
|
122 |
static int
|
|
krh@0
|
123 |
write_to_fd(int fd, void *p, size_t size)
|
|
krh@0
|
124 |
{
|
|
krh@0
|
125 |
int rest, len;
|
|
krh@0
|
126 |
|
|
krh@0
|
127 |
rest = size;
|
|
krh@0
|
128 |
while (rest > 0) {
|
|
krh@0
|
129 |
len = write(fd, p, rest);
|
|
krh@0
|
130 |
if (len < 0)
|
|
krh@0
|
131 |
return -1;
|
|
krh@0
|
132 |
rest -= len;
|
|
krh@0
|
133 |
}
|
|
krh@0
|
134 |
|
|
krh@0
|
135 |
return 0;
|
|
krh@0
|
136 |
}
|
|
krh@0
|
137 |
|
|
krh@0
|
138 |
static void *
|
|
krh@0
|
139 |
zalloc(size_t size)
|
|
krh@0
|
140 |
{
|
|
krh@0
|
141 |
void *p;
|
|
krh@0
|
142 |
|
|
krh@0
|
143 |
p = malloc(size);
|
|
krh@0
|
144 |
memset(p, 0, size);
|
|
krh@0
|
145 |
|
|
krh@0
|
146 |
return p;
|
|
krh@0
|
147 |
}
|
|
krh@0
|
148 |
|
|
krh@19
|
149 |
struct razor_set_section razor_sections[] = {
|
|
krh@19
|
150 |
{ RAZOR_PACKAGES, offsetof(struct razor_set, packages) },
|
|
krh@19
|
151 |
{ RAZOR_REQUIRES, offsetof(struct razor_set, requires) },
|
|
krh@19
|
152 |
{ RAZOR_PROVIDES, offsetof(struct razor_set, provides) },
|
|
krh@19
|
153 |
{ RAZOR_STRING_POOL, offsetof(struct razor_set, string_pool) },
|
|
krh@39
|
154 |
{ RAZOR_PACKAGE_POOL, offsetof(struct razor_set, package_pool) },
|
|
krh@39
|
155 |
{ RAZOR_REQUIRES_POOL, offsetof(struct razor_set, requires_pool) },
|
|
krh@39
|
156 |
{ RAZOR_PROVIDES_POOL, offsetof(struct razor_set, provides_pool) },
|
|
krh@19
|
157 |
};
|
|
krh@19
|
158 |
|
|
krh@4
|
159 |
struct razor_set *
|
|
krh@4
|
160 |
razor_set_create(void)
|
|
krh@0
|
161 |
{
|
|
krh@18
|
162 |
return zalloc(sizeof(struct razor_set));
|
|
krh@0
|
163 |
}
|
|
krh@0
|
164 |
|
|
krh@4
|
165 |
struct razor_set *
|
|
krh@4
|
166 |
razor_set_open(const char *filename)
|
|
krh@0
|
167 |
{
|
|
krh@4
|
168 |
struct razor_set *set;
|
|
krh@19
|
169 |
struct razor_set_section *s;
|
|
krh@0
|
170 |
struct stat stat;
|
|
krh@19
|
171 |
struct array *array;
|
|
krh@19
|
172 |
int fd;
|
|
krh@0
|
173 |
|
|
krh@4
|
174 |
set = zalloc(sizeof *set);
|
|
krh@0
|
175 |
fd = open(filename, O_RDONLY);
|
|
krh@0
|
176 |
if (fstat(fd, &stat) < 0)
|
|
krh@0
|
177 |
return NULL;
|
|
krh@4
|
178 |
set->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
krh@4
|
179 |
if (set->header == MAP_FAILED) {
|
|
krh@4
|
180 |
free(set);
|
|
krh@0
|
181 |
return NULL;
|
|
krh@0
|
182 |
}
|
|
krh@0
|
183 |
|
|
krh@19
|
184 |
for (s = set->header->sections; ~s->type; s++) {
|
|
krh@19
|
185 |
if (s->type >= ARRAY_SIZE(razor_sections))
|
|
krh@19
|
186 |
continue;
|
|
krh@19
|
187 |
if (s->type != razor_sections[s->type].type)
|
|
krh@19
|
188 |
continue;
|
|
krh@19
|
189 |
array = (void *) set + razor_sections[s->type].offset;
|
|
krh@19
|
190 |
array->data = (void *) set->header + s->offset;
|
|
krh@19
|
191 |
array->size = s->size;
|
|
krh@19
|
192 |
array->alloc = s->size;
|
|
krh@0
|
193 |
}
|
|
krh@0
|
194 |
close(fd);
|
|
krh@0
|
195 |
|
|
krh@4
|
196 |
return set;
|
|
krh@0
|
197 |
}
|
|
krh@0
|
198 |
|
|
krh@0
|
199 |
void
|
|
krh@4
|
200 |
razor_set_destroy(struct razor_set *set)
|
|
krh@0
|
201 |
{
|
|
krh@0
|
202 |
unsigned int size;
|
|
krh@19
|
203 |
struct array *a;
|
|
krh@0
|
204 |
int i;
|
|
krh@0
|
205 |
|
|
krh@4
|
206 |
if (set->header) {
|
|
krh@4
|
207 |
for (i = 0; set->header->sections[i].type; i++)
|
|
krh@0
|
208 |
;
|
|
krh@4
|
209 |
size = set->header->sections[i].type;
|
|
krh@4
|
210 |
munmap(set->header, size);
|
|
krh@0
|
211 |
} else {
|
|
krh@19
|
212 |
for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
|
|
krh@19
|
213 |
a = (void *) set + razor_sections[i].offset;
|
|
krh@19
|
214 |
free(a->data);
|
|
krh@19
|
215 |
}
|
|
krh@0
|
216 |
}
|
|
krh@0
|
217 |
|
|
krh@4
|
218 |
free(set);
|
|
krh@0
|
219 |
}
|
|
krh@0
|
220 |
|
|
krh@43
|
221 |
int
|
|
krh@4
|
222 |
razor_set_write(struct razor_set *set, const char *filename)
|
|
krh@0
|
223 |
{
|
|
krh@0
|
224 |
char data[4096];
|
|
krh@4
|
225 |
struct razor_set_header *header = (struct razor_set_header *) data;
|
|
krh@19
|
226 |
struct array *a;
|
|
krh@17
|
227 |
unsigned long offset;
|
|
krh@17
|
228 |
int i, fd;
|
|
krh@0
|
229 |
|
|
krh@0
|
230 |
memset(data, 0, sizeof data);
|
|
krh@4
|
231 |
header->magic = RAZOR_MAGIC;
|
|
krh@4
|
232 |
header->version = RAZOR_VERSION;
|
|
krh@17
|
233 |
offset = sizeof data;
|
|
krh@0
|
234 |
|
|
krh@19
|
235 |
for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
|
|
krh@19
|
236 |
if (razor_sections[i].type != i)
|
|
krh@19
|
237 |
continue;
|
|
krh@19
|
238 |
a = (void *) set + razor_sections[i].offset;
|
|
krh@19
|
239 |
header->sections[i].type = i;
|
|
krh@17
|
240 |
header->sections[i].offset = offset;
|
|
krh@19
|
241 |
header->sections[i].size = a->size;
|
|
krh@19
|
242 |
offset += (a->size + 4095) & ~4095;
|
|
krh@17
|
243 |
}
|
|
krh@0
|
244 |
|
|
krh@19
|
245 |
header->sections[i].type = ~0;
|
|
krh@17
|
246 |
header->sections[i].offset = 0;
|
|
krh@17
|
247 |
header->sections[i].size = 0;
|
|
krh@10
|
248 |
|
|
krh@0
|
249 |
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
|
|
krh@0
|
250 |
if (fd < 0)
|
|
krh@0
|
251 |
return -1;
|
|
krh@0
|
252 |
|
|
krh@0
|
253 |
write_to_fd(fd, data, sizeof data);
|
|
krh@19
|
254 |
for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
|
|
krh@19
|
255 |
if (razor_sections[i].type != i)
|
|
krh@19
|
256 |
continue;
|
|
krh@19
|
257 |
a = (void *) set + razor_sections[i].offset;
|
|
krh@19
|
258 |
write_to_fd(fd, a->data, (a->size + 4095) & ~4095);
|
|
krh@19
|
259 |
}
|
|
krh@17
|
260 |
|
|
krh@17
|
261 |
close(fd);
|
|
krh@0
|
262 |
|
|
krh@0
|
263 |
return 0;
|
|
krh@0
|
264 |
}
|
|
krh@0
|
265 |
|
|
krh@0
|
266 |
static unsigned int
|
|
krh@0
|
267 |
hash_string(const char *key)
|
|
krh@0
|
268 |
{
|
|
krh@0
|
269 |
const char *p;
|
|
krh@0
|
270 |
unsigned int hash = 0;
|
|
krh@0
|
271 |
|
|
krh@0
|
272 |
for (p = key; *p; p++)
|
|
krh@9
|
273 |
hash = (hash * 617) ^ *p;
|
|
krh@0
|
274 |
|
|
krh@0
|
275 |
return hash;
|
|
krh@0
|
276 |
}
|
|
krh@0
|
277 |
|
|
krh@32
|
278 |
static unsigned long
|
|
krh@32
|
279 |
razor_importer_lookup(struct razor_importer *importer, const char *key)
|
|
krh@0
|
280 |
{
|
|
krh@6
|
281 |
unsigned int mask, start, i;
|
|
krh@6
|
282 |
unsigned long *b;
|
|
krh@6
|
283 |
char *pool;
|
|
krh@0
|
284 |
|
|
krh@32
|
285 |
pool = importer->set->string_pool.data;
|
|
krh@32
|
286 |
mask = importer->buckets.alloc - 1;
|
|
krh@6
|
287 |
start = hash_string(key) * sizeof(unsigned long);
|
|
krh@0
|
288 |
|
|
krh@32
|
289 |
for (i = 0; i < importer->buckets.alloc; i += sizeof *b) {
|
|
krh@32
|
290 |
b = importer->buckets.data + ((start + i) & mask);
|
|
krh@6
|
291 |
|
|
krh@6
|
292 |
if (*b == 0)
|
|
krh@0
|
293 |
return 0;
|
|
krh@0
|
294 |
|
|
krh@6
|
295 |
if (strcmp(key, &pool[*b]) == 0)
|
|
krh@6
|
296 |
return *b;
|
|
krh@6
|
297 |
}
|
|
krh@0
|
298 |
|
|
krh@0
|
299 |
return 0;
|
|
krh@0
|
300 |
}
|
|
krh@0
|
301 |
|
|
krh@0
|
302 |
static unsigned long
|
|
krh@4
|
303 |
add_to_string_pool(struct razor_set *set, const char *key)
|
|
krh@0
|
304 |
{
|
|
krh@6
|
305 |
int len;
|
|
krh@6
|
306 |
char *p;
|
|
krh@0
|
307 |
|
|
krh@0
|
308 |
len = strlen(key) + 1;
|
|
krh@6
|
309 |
p = array_add(&set->string_pool, len);
|
|
krh@6
|
310 |
memcpy(p, key, len);
|
|
krh@0
|
311 |
|
|
krh@6
|
312 |
return p - (char *) set->string_pool.data;
|
|
krh@0
|
313 |
}
|
|
krh@0
|
314 |
|
|
krh@10
|
315 |
static unsigned long
|
|
krh@39
|
316 |
add_to_property_pool(struct array *pool, struct array *properties)
|
|
krh@10
|
317 |
{
|
|
krh@10
|
318 |
unsigned long *p;
|
|
krh@10
|
319 |
|
|
krh@10
|
320 |
p = array_add(properties, sizeof *p);
|
|
krh@18
|
321 |
*p = ~0ul;
|
|
krh@39
|
322 |
p = array_add(pool, properties->size);
|
|
krh@10
|
323 |
memcpy(p, properties->data, properties->size);
|
|
krh@10
|
324 |
|
|
krh@39
|
325 |
return p - (unsigned long *) pool->data;
|
|
krh@10
|
326 |
}
|
|
krh@10
|
327 |
|
|
krh@0
|
328 |
static void
|
|
krh@32
|
329 |
do_insert(struct razor_importer *importer, unsigned long value)
|
|
krh@0
|
330 |
{
|
|
krh@6
|
331 |
unsigned int mask, start, i;
|
|
krh@6
|
332 |
unsigned long *b;
|
|
krh@0
|
333 |
const char *key;
|
|
krh@0
|
334 |
|
|
krh@32
|
335 |
key = (char *) importer->set->string_pool.data + value;
|
|
krh@32
|
336 |
mask = importer->buckets.alloc - 1;
|
|
krh@6
|
337 |
start = hash_string(key) * sizeof(unsigned long);
|
|
krh@6
|
338 |
|
|
krh@32
|
339 |
for (i = 0; i < importer->buckets.alloc; i += sizeof *b) {
|
|
krh@32
|
340 |
b = importer->buckets.data + ((start + i) & mask);
|
|
krh@6
|
341 |
if (*b == 0) {
|
|
krh@6
|
342 |
*b = value;
|
|
krh@0
|
343 |
break;
|
|
krh@0
|
344 |
}
|
|
krh@6
|
345 |
}
|
|
krh@0
|
346 |
}
|
|
krh@0
|
347 |
|
|
krh@32
|
348 |
static unsigned long
|
|
krh@32
|
349 |
razor_importer_insert(struct razor_importer *importer, const char *key)
|
|
krh@0
|
350 |
{
|
|
krh@6
|
351 |
unsigned long value, *buckets, *b, *end;
|
|
krh@6
|
352 |
int alloc;
|
|
krh@0
|
353 |
|
|
krh@32
|
354 |
alloc = importer->buckets.alloc;
|
|
krh@32
|
355 |
array_add(&importer->buckets, 4 * sizeof *buckets);
|
|
krh@32
|
356 |
if (alloc != importer->buckets.alloc) {
|
|
krh@32
|
357 |
end = importer->buckets.data + alloc;
|
|
krh@32
|
358 |
memset(end, 0, importer->buckets.alloc - alloc);
|
|
krh@32
|
359 |
for (b = importer->buckets.data; b < end; b++) {
|
|
krh@6
|
360 |
value = *b;
|
|
krh@6
|
361 |
if (value != 0) {
|
|
krh@6
|
362 |
*b = 0;
|
|
krh@32
|
363 |
do_insert(importer, value);
|
|
krh@6
|
364 |
}
|
|
krh@0
|
365 |
}
|
|
krh@0
|
366 |
}
|
|
krh@0
|
367 |
|
|
krh@32
|
368 |
value = add_to_string_pool(importer->set, key);
|
|
krh@32
|
369 |
do_insert (importer, value);
|
|
krh@0
|
370 |
|
|
krh@0
|
371 |
return value;
|
|
krh@0
|
372 |
}
|
|
krh@0
|
373 |
|
|
krh@30
|
374 |
static unsigned long
|
|
krh@32
|
375 |
razor_importer_tokenize(struct razor_importer *importer, const char *string)
|
|
krh@0
|
376 |
{
|
|
krh@0
|
377 |
unsigned long token;
|
|
krh@0
|
378 |
|
|
krh@13
|
379 |
if (string == NULL)
|
|
krh@32
|
380 |
return razor_importer_tokenize(importer, "");
|
|
krh@13
|
381 |
|
|
krh@32
|
382 |
token = razor_importer_lookup(importer, string);
|
|
krh@0
|
383 |
if (token != 0)
|
|
krh@0
|
384 |
return token;
|
|
krh@0
|
385 |
|
|
krh@32
|
386 |
return razor_importer_insert(importer, string);
|
|
krh@0
|
387 |
}
|
|
krh@0
|
388 |
|
|
krh@27
|
389 |
void
|
|
krh@30
|
390 |
razor_importer_begin_package(struct razor_importer *importer,
|
|
krh@30
|
391 |
const char *name, const char *version)
|
|
krh@13
|
392 |
{
|
|
krh@25
|
393 |
struct razor_package *p;
|
|
krh@13
|
394 |
|
|
krh@30
|
395 |
p = array_add(&importer->set->packages, sizeof *p);
|
|
krh@32
|
396 |
p->name = razor_importer_tokenize(importer, name);
|
|
krh@32
|
397 |
p->version = razor_importer_tokenize(importer, version);
|
|
krh@13
|
398 |
|
|
krh@30
|
399 |
importer->package = p;
|
|
krh@30
|
400 |
array_init(&importer->requires.package);
|
|
krh@30
|
401 |
array_init(&importer->provides.package);
|
|
krh@13
|
402 |
}
|
|
krh@13
|
403 |
|
|
krh@13
|
404 |
void
|
|
krh@30
|
405 |
razor_importer_finish_package(struct razor_importer *importer)
|
|
krh@13
|
406 |
{
|
|
krh@25
|
407 |
struct razor_package *p;
|
|
krh@13
|
408 |
|
|
krh@30
|
409 |
p = importer->package;
|
|
krh@39
|
410 |
p->requires = add_to_property_pool(&importer->set->requires_pool,
|
|
krh@30
|
411 |
&importer->requires.package);
|
|
krh@39
|
412 |
p->provides = add_to_property_pool(&importer->set->provides_pool,
|
|
krh@30
|
413 |
&importer->provides.package);
|
|
krh@13
|
414 |
|
|
krh@30
|
415 |
array_release(&importer->requires.package);
|
|
krh@30
|
416 |
array_release(&importer->provides.package);
|
|
krh@13
|
417 |
}
|
|
krh@13
|
418 |
|
|
krh@30
|
419 |
static void
|
|
krh@30
|
420 |
razor_importer_add_property(struct razor_importer *importer,
|
|
krh@13
|
421 |
struct import_property_context *pctx,
|
|
krh@13
|
422 |
const char *name, const char *version)
|
|
krh@13
|
423 |
{
|
|
krh@25
|
424 |
struct razor_property *p;
|
|
krh@13
|
425 |
unsigned long *r;
|
|
krh@13
|
426 |
|
|
krh@25
|
427 |
p = array_add(pctx->all, sizeof *p);
|
|
krh@32
|
428 |
p->name = razor_importer_tokenize(importer, name);
|
|
krh@32
|
429 |
p->version = razor_importer_tokenize(importer, version);
|
|
krh@30
|
430 |
p->packages = importer->package -
|
|
krh@30
|
431 |
(struct razor_package *) importer->set->packages.data;
|
|
krh@13
|
432 |
|
|
krh@13
|
433 |
r = array_add(&pctx->package, sizeof *r);
|
|
krh@25
|
434 |
*r = p - (struct razor_property *) pctx->all->data;
|
|
krh@13
|
435 |
}
|
|
krh@13
|
436 |
|
|
krh@27
|
437 |
void
|
|
krh@30
|
438 |
razor_importer_add_requires(struct razor_importer *importer,
|
|
krh@30
|
439 |
const char *name, const char *version)
|
|
krh@9
|
440 |
{
|
|
krh@30
|
441 |
razor_importer_add_property(importer,
|
|
krh@30
|
442 |
&importer->requires, name, version);
|
|
krh@30
|
443 |
}
|
|
krh@30
|
444 |
|
|
krh@30
|
445 |
void
|
|
krh@30
|
446 |
razor_importer_add_provides(struct razor_importer *importer,
|
|
krh@30
|
447 |
const char *name, const char *version)
|
|
krh@30
|
448 |
{
|
|
krh@30
|
449 |
razor_importer_add_property(importer,
|
|
krh@30
|
450 |
&importer->provides, name, version);
|
|
krh@30
|
451 |
}
|
|
krh@30
|
452 |
|
|
krh@30
|
453 |
struct razor_importer *
|
|
krh@30
|
454 |
razor_importer_new(void)
|
|
krh@30
|
455 |
{
|
|
krh@30
|
456 |
struct razor_importer *importer;
|
|
krh@30
|
457 |
|
|
krh@30
|
458 |
importer = zalloc(sizeof *importer);
|
|
krh@30
|
459 |
importer->set = razor_set_create();
|
|
krh@30
|
460 |
importer->requires.all = &importer->set->requires;
|
|
krh@30
|
461 |
importer->provides.all = &importer->set->provides;
|
|
krh@30
|
462 |
|
|
krh@30
|
463 |
return importer;
|
|
krh@9
|
464 |
}
|
|
krh@9
|
465 |
|
|
krh@22
|
466 |
typedef int (*compare_with_data_func_t)(const void *p1,
|
|
krh@22
|
467 |
const void *p,
|
|
krh@22
|
468 |
void *data);
|
|
krh@22
|
469 |
|
|
krh@25
|
470 |
struct qsort_context {
|
|
krh@25
|
471 |
size_t size;
|
|
krh@25
|
472 |
compare_with_data_func_t compare;
|
|
krh@25
|
473 |
void *data;
|
|
krh@25
|
474 |
};
|
|
krh@25
|
475 |
|
|
krh@22
|
476 |
static void
|
|
krh@22
|
477 |
qsort_swap(void *p1, void *p2, size_t size)
|
|
krh@22
|
478 |
{
|
|
krh@22
|
479 |
char buffer[size];
|
|
krh@22
|
480 |
|
|
krh@22
|
481 |
memcpy(buffer, p1, size);
|
|
krh@22
|
482 |
memcpy(p1, p2, size);
|
|
krh@22
|
483 |
memcpy(p2, buffer, size);
|
|
krh@22
|
484 |
}
|
|
krh@22
|
485 |
|
|
krh@25
|
486 |
static void
|
|
krh@25
|
487 |
__qsort_with_data(void *base, size_t nelem, unsigned long *map,
|
|
krh@25
|
488 |
struct qsort_context *ctx)
|
|
krh@22
|
489 |
{
|
|
krh@22
|
490 |
void *p, *start, *end, *pivot;
|
|
krh@25
|
491 |
unsigned long *mp, *mstart, *mend, tmp;
|
|
krh@22
|
492 |
int left, right, result;
|
|
krh@25
|
493 |
size_t size = ctx->size;
|
|
krh@22
|
494 |
|
|
krh@22
|
495 |
p = base;
|
|
krh@22
|
496 |
start = base;
|
|
krh@22
|
497 |
end = base + nelem * size;
|
|
krh@25
|
498 |
mp = map;
|
|
krh@25
|
499 |
mstart = map;
|
|
krh@25
|
500 |
mend = map + nelem;
|
|
krh@22
|
501 |
pivot = base + (random() % nelem) * size;
|
|
krh@25
|
502 |
|
|
krh@22
|
503 |
while (p < end) {
|
|
krh@25
|
504 |
result = ctx->compare(p, pivot, ctx->data);
|
|
krh@22
|
505 |
if (result < 0) {
|
|
krh@22
|
506 |
qsort_swap(p, start, size);
|
|
krh@25
|
507 |
tmp = *mp;
|
|
krh@25
|
508 |
*mp = *mstart;
|
|
krh@25
|
509 |
*mstart = tmp;
|
|
krh@22
|
510 |
if (start == pivot)
|
|
krh@22
|
511 |
pivot = p;
|
|
krh@22
|
512 |
start += size;
|
|
krh@25
|
513 |
mstart++;
|
|
krh@22
|
514 |
p += size;
|
|
krh@29
|
515 |
mp++;
|
|
krh@22
|
516 |
} else if (result == 0) {
|
|
krh@22
|
517 |
p += size;
|
|
krh@25
|
518 |
mp++;
|
|
krh@22
|
519 |
} else {
|
|
krh@22
|
520 |
end -= size;
|
|
krh@25
|
521 |
mend--;
|
|
krh@22
|
522 |
qsort_swap(p, end, size);
|
|
krh@25
|
523 |
tmp = *mp;
|
|
krh@29
|
524 |
*mp = *mend;
|
|
krh@29
|
525 |
*mend = tmp;
|
|
krh@22
|
526 |
if (end == pivot)
|
|
krh@22
|
527 |
pivot = p;
|
|
krh@22
|
528 |
}
|
|
krh@22
|
529 |
}
|
|
krh@22
|
530 |
|
|
krh@22
|
531 |
left = (start - base) / size;
|
|
krh@22
|
532 |
right = (base + nelem * size - end) / size;
|
|
krh@22
|
533 |
if (left > 1)
|
|
krh@25
|
534 |
__qsort_with_data(base, left, map, ctx);
|
|
krh@22
|
535 |
if (right > 1)
|
|
krh@25
|
536 |
__qsort_with_data(end, right, mend, ctx);
|
|
krh@25
|
537 |
}
|
|
krh@25
|
538 |
|
|
krh@25
|
539 |
unsigned long *
|
|
krh@25
|
540 |
qsort_with_data(void *base, size_t nelem, size_t size,
|
|
krh@25
|
541 |
compare_with_data_func_t compare, void *data)
|
|
krh@25
|
542 |
{
|
|
krh@25
|
543 |
struct qsort_context ctx;
|
|
krh@25
|
544 |
unsigned long *map;
|
|
krh@25
|
545 |
int i;
|
|
krh@25
|
546 |
|
|
krh@25
|
547 |
ctx.size = size;
|
|
krh@25
|
548 |
ctx.compare = compare;
|
|
krh@25
|
549 |
ctx.data = data;
|
|
krh@25
|
550 |
|
|
krh@25
|
551 |
map = malloc(nelem * sizeof (unsigned long));
|
|
krh@25
|
552 |
for (i = 0; i < nelem; i++)
|
|
krh@25
|
553 |
map[i] = i;
|
|
krh@25
|
554 |
|
|
krh@25
|
555 |
__qsort_with_data(base, nelem, map, &ctx);
|
|
krh@25
|
556 |
|
|
krh@25
|
557 |
return map;
|
|
krh@22
|
558 |
}
|
|
krh@9
|
559 |
|
|
krh@9
|
560 |
static int
|
|
krh@35
|
561 |
versioncmp(const char *s1, const char *s2)
|
|
krh@35
|
562 |
{
|
|
krh@35
|
563 |
const char *p1, *p2;
|
|
krh@35
|
564 |
long n1, n2;
|
|
krh@35
|
565 |
int res;
|
|
krh@35
|
566 |
|
|
krh@35
|
567 |
n1 = strtol(s1, (char **) &p1, 0);
|
|
krh@35
|
568 |
n2 = strtol(s2, (char **) &p2, 0);
|
|
krh@35
|
569 |
|
|
krh@35
|
570 |
/* Epoch; if one but not the other has an epoch set, default
|
|
krh@35
|
571 |
* the epoch-less version to 0. */
|
|
krh@35
|
572 |
res = (*p1 == ':') - (*p2 == ':');
|
|
krh@35
|
573 |
if (res < 0) {
|
|
krh@35
|
574 |
n1 = 0;
|
|
krh@35
|
575 |
p1 = s1;
|
|
krh@35
|
576 |
p2++;
|
|
krh@35
|
577 |
} else if (res > 0) {
|
|
krh@35
|
578 |
p1++;
|
|
krh@35
|
579 |
n2 = 0;
|
|
krh@35
|
580 |
p2 = s2;
|
|
krh@35
|
581 |
}
|
|
krh@35
|
582 |
|
|
krh@35
|
583 |
if (n1 != n2)
|
|
krh@35
|
584 |
return n1 - n2;
|
|
krh@35
|
585 |
while (*p1 && *p2) {
|
|
krh@35
|
586 |
if (*p1 != *p2)
|
|
krh@35
|
587 |
return *p1 - *p2;
|
|
krh@35
|
588 |
p1++;
|
|
krh@35
|
589 |
p2++;
|
|
krh@35
|
590 |
if (isdigit(*p1) && isdigit(*p2))
|
|
krh@35
|
591 |
return versioncmp(p1, p2);
|
|
krh@35
|
592 |
}
|
|
krh@35
|
593 |
|
|
krh@35
|
594 |
return *p1 - *p2;
|
|
krh@35
|
595 |
}
|
|
krh@35
|
596 |
|
|
krh@35
|
597 |
|
|
krh@35
|
598 |
static int
|
|
krh@22
|
599 |
compare_packages(const void *p1, const void *p2, void *data)
|
|
krh@9
|
600 |
{
|
|
krh@25
|
601 |
const struct razor_package *pkg1 = p1, *pkg2 = p2;
|
|
krh@22
|
602 |
struct razor_set *set = data;
|
|
krh@22
|
603 |
char *pool = set->string_pool.data;
|
|
krh@9
|
604 |
|
|
krh@23
|
605 |
if (pkg1->name == pkg2->name)
|
|
krh@35
|
606 |
return versioncmp(&pool[pkg1->version], &pool[pkg2->version]);
|
|
krh@23
|
607 |
else
|
|
krh@23
|
608 |
return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
|
|
krh@9
|
609 |
}
|
|
krh@9
|
610 |
|
|
krh@9
|
611 |
static int
|
|
krh@22
|
612 |
compare_properties(const void *p1, const void *p2, void *data)
|
|
krh@9
|
613 |
{
|
|
krh@25
|
614 |
const struct razor_property *prop1 = p1, *prop2 = p2;
|
|
krh@22
|
615 |
struct razor_set *set = data;
|
|
krh@22
|
616 |
char *pool = set->string_pool.data;
|
|
krh@9
|
617 |
|
|
krh@23
|
618 |
if (prop1->name == prop2->name)
|
|
krh@35
|
619 |
return versioncmp(&pool[prop1->version], &pool[prop2->version]);
|
|
krh@12
|
620 |
else
|
|
krh@23
|
621 |
return strcmp(&pool[prop1->name], &pool[prop2->name]);
|
|
krh@9
|
622 |
}
|
|
krh@9
|
623 |
|
|
krh@10
|
624 |
static unsigned long *
|
|
krh@25
|
625 |
uniqueify_properties(struct razor_set *set, struct array *properties)
|
|
krh@9
|
626 |
{
|
|
krh@25
|
627 |
struct razor_property *rp, *up, *rp_end;
|
|
krh@18
|
628 |
struct array *pkgs, *p;
|
|
krh@25
|
629 |
unsigned long *map, *rmap, *r;
|
|
krh@18
|
630 |
int i, count, unique;
|
|
krh@9
|
631 |
|
|
krh@25
|
632 |
count = properties->size / sizeof(struct razor_property);
|
|
krh@25
|
633 |
map = qsort_with_data(properties->data,
|
|
krh@25
|
634 |
count,
|
|
krh@25
|
635 |
sizeof(struct razor_property),
|
|
krh@25
|
636 |
compare_properties,
|
|
krh@25
|
637 |
set);
|
|
krh@9
|
638 |
|
|
krh@25
|
639 |
rp_end = properties->data + properties->size;
|
|
krh@25
|
640 |
rmap = malloc(count * sizeof *map);
|
|
krh@25
|
641 |
pkgs = zalloc(count * sizeof *pkgs);
|
|
krh@25
|
642 |
for (rp = properties->data, up = rp, i = 0; rp < rp_end; rp++, i++) {
|
|
krh@25
|
643 |
if (rp->name != up->name || rp->version != up->version) {
|
|
krh@25
|
644 |
up++;
|
|
krh@25
|
645 |
up->name = rp->name;
|
|
krh@25
|
646 |
up->version = rp->version;
|
|
krh@10
|
647 |
}
|
|
krh@25
|
648 |
|
|
krh@25
|
649 |
unique = up - (struct razor_property *) properties->data;
|
|
krh@25
|
650 |
rmap[map[i]] = unique;
|
|
krh@25
|
651 |
r = array_add(&pkgs[unique], sizeof *r);
|
|
krh@25
|
652 |
*r = rp->packages;
|
|
krh@10
|
653 |
}
|
|
krh@25
|
654 |
free(map);
|
|
krh@9
|
655 |
|
|
krh@25
|
656 |
up++;
|
|
krh@25
|
657 |
properties->size = (void *) up - properties->data;
|
|
krh@25
|
658 |
rp_end = up;
|
|
krh@25
|
659 |
for (rp = properties->data, p = pkgs; rp < rp_end; rp++, p++) {
|
|
krh@39
|
660 |
rp->packages = add_to_property_pool(&set->package_pool, p);
|
|
krh@25
|
661 |
array_release(p);
|
|
krh@18
|
662 |
}
|
|
krh@18
|
663 |
|
|
krh@18
|
664 |
free(pkgs);
|
|
krh@18
|
665 |
|
|
krh@25
|
666 |
return rmap;
|
|
krh@10
|
667 |
}
|
|
krh@10
|
668 |
|
|
krh@10
|
669 |
static void
|
|
krh@39
|
670 |
remap_links(struct array *links, unsigned long *map)
|
|
krh@10
|
671 |
{
|
|
krh@39
|
672 |
unsigned long *p, *end;
|
|
krh@10
|
673 |
|
|
krh@39
|
674 |
end = links->data + links->size;
|
|
krh@39
|
675 |
for (p = links->data; p < end; p++)
|
|
krh@39
|
676 |
if (*p != ~0)
|
|
krh@39
|
677 |
*p = map[*p];
|
|
krh@9
|
678 |
}
|
|
krh@9
|
679 |
|
|
krh@27
|
680 |
struct razor_set *
|
|
krh@30
|
681 |
razor_importer_finish(struct razor_importer *importer)
|
|
krh@9
|
682 |
{
|
|
krh@30
|
683 |
struct razor_set *set;
|
|
krh@39
|
684 |
unsigned long *map, *rmap;
|
|
krh@39
|
685 |
int i, count;
|
|
krh@18
|
686 |
|
|
krh@39
|
687 |
map = uniqueify_properties(importer->set, &importer->set->requires);
|
|
krh@39
|
688 |
remap_links(&importer->set->requires_pool, map);
|
|
krh@39
|
689 |
free(map);
|
|
krh@39
|
690 |
|
|
krh@39
|
691 |
map = uniqueify_properties(importer->set, &importer->set->provides);
|
|
krh@39
|
692 |
remap_links(&importer->set->provides_pool, map);
|
|
krh@39
|
693 |
free(map);
|
|
krh@25
|
694 |
|
|
krh@30
|
695 |
count = importer->set->packages.size / sizeof(struct razor_package);
|
|
krh@30
|
696 |
map = qsort_with_data(importer->set->packages.data,
|
|
krh@25
|
697 |
count,
|
|
krh@25
|
698 |
sizeof(struct razor_package),
|
|
krh@25
|
699 |
compare_packages,
|
|
krh@30
|
700 |
importer->set);
|
|
krh@39
|
701 |
|
|
krh@39
|
702 |
rmap = malloc(count * sizeof *rmap);
|
|
krh@39
|
703 |
for (i = 0; i < count; i++)
|
|
krh@39
|
704 |
rmap[map[i]] = i;
|
|
krh@39
|
705 |
|
|
krh@39
|
706 |
remap_links(&importer->set->package_pool, rmap);
|
|
krh@25
|
707 |
free(map);
|
|
krh@39
|
708 |
free(rmap);
|
|
krh@13
|
709 |
|
|
krh@30
|
710 |
set = importer->set;
|
|
krh@32
|
711 |
array_release(&importer->buckets);
|
|
krh@30
|
712 |
free(importer);
|
|
krh@30
|
713 |
|
|
krh@30
|
714 |
return set;
|
|
krh@9
|
715 |
}
|
|
krh@9
|
716 |
|
|
krh@0
|
717 |
void
|
|
krh@4
|
718 |
razor_set_list(struct razor_set *set)
|
|
krh@3
|
719 |
{
|
|
krh@6
|
720 |
struct razor_package *p, *end;
|
|
krh@6
|
721 |
char *pool;
|
|
krh@3
|
722 |
|
|
krh@6
|
723 |
pool = set->string_pool.data;
|
|
krh@6
|
724 |
end = set->packages.data + set->packages.size;
|
|
krh@14
|
725 |
for (p = set->packages.data; p < end; p++)
|
|
krh@6
|
726 |
printf("%s %s\n", &pool[p->name], &pool[p->version]);
|
|
krh@3
|
727 |
}
|
|
krh@3
|
728 |
|
|
krh@16
|
729 |
struct razor_set *bsearch_set;
|
|
krh@16
|
730 |
|
|
krh@16
|
731 |
static int
|
|
krh@16
|
732 |
compare_package_name(const void *key, const void *data)
|
|
krh@16
|
733 |
{
|
|
krh@16
|
734 |
const struct razor_package *p = data;
|
|
krh@16
|
735 |
char *pool;
|
|
krh@16
|
736 |
|
|
krh@16
|
737 |
pool = bsearch_set->string_pool.data;
|
|
krh@16
|
738 |
|
|
krh@16
|
739 |
return strcmp(key, &pool[p->name]);
|
|
krh@16
|
740 |
}
|
|
krh@16
|
741 |
|
|
krh@10
|
742 |
struct razor_package *
|
|
krh@10
|
743 |
razor_set_get_package(struct razor_set *set, const char *package)
|
|
krh@10
|
744 |
{
|
|
krh@16
|
745 |
bsearch_set = set;
|
|
krh@16
|
746 |
return bsearch(package, set->packages.data,
|
|
krh@16
|
747 |
set->packages.size / sizeof(struct razor_package),
|
|
krh@16
|
748 |
sizeof(struct razor_package), compare_package_name);
|
|
krh@10
|
749 |
}
|
|
krh@10
|
750 |
|
|
krh@18
|
751 |
static int
|
|
krh@18
|
752 |
compare_property_name(const void *key, const void *data)
|
|
krh@18
|
753 |
{
|
|
krh@18
|
754 |
const struct razor_property *p = data;
|
|
krh@18
|
755 |
char *pool;
|
|
krh@18
|
756 |
|
|
krh@18
|
757 |
pool = bsearch_set->string_pool.data;
|
|
krh@18
|
758 |
|
|
krh@18
|
759 |
return strcmp(key, &pool[p->name]);
|
|
krh@18
|
760 |
}
|
|
krh@18
|
761 |
|
|
krh@18
|
762 |
struct razor_property *
|
|
krh@18
|
763 |
razor_set_get_property(struct razor_set *set,
|
|
krh@18
|
764 |
struct array *properties,
|
|
krh@18
|
765 |
const char *property)
|
|
krh@18
|
766 |
{
|
|
krh@18
|
767 |
struct razor_property *p, *start;
|
|
krh@18
|
768 |
|
|
krh@18
|
769 |
bsearch_set = set;
|
|
krh@18
|
770 |
p = bsearch(property, properties->data,
|
|
krh@18
|
771 |
properties->size / sizeof(struct razor_property),
|
|
krh@18
|
772 |
sizeof(struct razor_property), compare_property_name);
|
|
krh@18
|
773 |
|
|
krh@18
|
774 |
start = properties->data;
|
|
krh@18
|
775 |
while (p > start && (p - 1)->name == p->name)
|
|
krh@18
|
776 |
p--;
|
|
krh@18
|
777 |
|
|
krh@18
|
778 |
return p;
|
|
krh@18
|
779 |
}
|
|
krh@18
|
780 |
|
|
krh@10
|
781 |
static void
|
|
krh@10
|
782 |
razor_set_list_all_properties(struct razor_set *set, struct array *properties)
|
|
krh@8
|
783 |
{
|
|
krh@8
|
784 |
struct razor_property *p, *end;
|
|
krh@8
|
785 |
char *pool;
|
|
krh@8
|
786 |
|
|
krh@8
|
787 |
pool = set->string_pool.data;
|
|
krh@10
|
788 |
end = properties->data + properties->size;
|
|
krh@14
|
789 |
for (p = properties->data; p < end; p++)
|
|
krh@8
|
790 |
printf("%s %s\n", &pool[p->name], &pool[p->version]);
|
|
krh@8
|
791 |
}
|
|
krh@8
|
792 |
|
|
krh@8
|
793 |
void
|
|
krh@10
|
794 |
razor_set_list_requires(struct razor_set *set, const char *name)
|
|
krh@7
|
795 |
{
|
|
krh@10
|
796 |
struct razor_property *p, *requires;
|
|
krh@10
|
797 |
struct razor_package *package;
|
|
krh@10
|
798 |
unsigned long *r;
|
|
krh@7
|
799 |
char *pool;
|
|
krh@7
|
800 |
|
|
krh@10
|
801 |
if (name) {
|
|
krh@10
|
802 |
package = razor_set_get_package(set, name);
|
|
krh@39
|
803 |
r = (unsigned long *) set->requires_pool.data +
|
|
krh@10
|
804 |
package->requires;
|
|
krh@10
|
805 |
requires = set->requires.data;
|
|
krh@10
|
806 |
pool = set->string_pool.data;
|
|
krh@18
|
807 |
while (~*r) {
|
|
krh@10
|
808 |
p = &requires[*r++];
|
|
krh@10
|
809 |
printf("%s %s\n", &pool[p->name], &pool[p->version]);
|
|
krh@10
|
810 |
}
|
|
krh@10
|
811 |
} else
|
|
krh@10
|
812 |
razor_set_list_all_properties(set, &set->requires);
|
|
krh@10
|
813 |
}
|
|
krh@10
|
814 |
|
|
krh@10
|
815 |
void
|
|
krh@10
|
816 |
razor_set_list_provides(struct razor_set *set, const char *name)
|
|
krh@10
|
817 |
{
|
|
krh@10
|
818 |
struct razor_property *p, *provides;
|
|
krh@10
|
819 |
struct razor_package *package;
|
|
krh@10
|
820 |
unsigned long *r;
|
|
krh@10
|
821 |
char *pool;
|
|
krh@10
|
822 |
|
|
krh@10
|
823 |
if (name) {
|
|
krh@10
|
824 |
package = razor_set_get_package(set, name);
|
|
krh@39
|
825 |
r = (unsigned long *) set->provides_pool.data +
|
|
krh@10
|
826 |
package->provides;
|
|
krh@10
|
827 |
provides = set->provides.data;
|
|
krh@10
|
828 |
pool = set->string_pool.data;
|
|
krh@18
|
829 |
while (~*r) {
|
|
krh@10
|
830 |
p = &provides[*r++];
|
|
krh@10
|
831 |
printf("%s %s\n", &pool[p->name], &pool[p->version]);
|
|
krh@10
|
832 |
}
|
|
krh@10
|
833 |
} else
|
|
krh@10
|
834 |
razor_set_list_all_properties(set, &set->provides);
|
|
krh@7
|
835 |
}
|
|
krh@7
|
836 |
|
|
krh@43
|
837 |
static void
|
|
krh@18
|
838 |
razor_set_list_property_packages(struct razor_set *set,
|
|
krh@18
|
839 |
struct array *properties,
|
|
krh@20
|
840 |
const char *name,
|
|
krh@20
|
841 |
const char *version)
|
|
krh@18
|
842 |
{
|
|
krh@18
|
843 |
struct razor_property *property, *end;
|
|
krh@18
|
844 |
struct razor_package *p, *packages;
|
|
krh@18
|
845 |
unsigned long *r;
|
|
krh@18
|
846 |
char *pool;
|
|
krh@18
|
847 |
|
|
krh@18
|
848 |
if (name == NULL)
|
|
krh@18
|
849 |
return;
|
|
krh@18
|
850 |
|
|
krh@18
|
851 |
property = razor_set_get_property(set, properties, name);
|
|
krh@18
|
852 |
packages = set->packages.data;
|
|
krh@18
|
853 |
pool = set->string_pool.data;
|
|
krh@18
|
854 |
end = properties->data + properties->size;
|
|
krh@18
|
855 |
while (property < end && strcmp(name, &pool[property->name]) == 0) {
|
|
krh@35
|
856 |
if (version && versioncmp(version, &pool[property->version]) != 0)
|
|
krh@20
|
857 |
goto next;
|
|
krh@18
|
858 |
r = (unsigned long *)
|
|
krh@39
|
859 |
set->package_pool.data + property->packages;
|
|
krh@18
|
860 |
while (~*r) {
|
|
krh@18
|
861 |
p = &packages[*r++];
|
|
krh@18
|
862 |
printf("%s %s\n",
|
|
krh@18
|
863 |
&pool[p->name], &pool[p->version]);
|
|
krh@18
|
864 |
}
|
|
krh@20
|
865 |
next:
|
|
krh@18
|
866 |
property++;
|
|
krh@18
|
867 |
}
|
|
krh@18
|
868 |
}
|
|
krh@18
|
869 |
|
|
krh@18
|
870 |
void
|
|
krh@43
|
871 |
razor_set_list_requires_packages(struct razor_set *set,
|
|
krh@43
|
872 |
const char *name,
|
|
krh@43
|
873 |
const char *version)
|
|
krh@43
|
874 |
{
|
|
krh@43
|
875 |
razor_set_list_property_packages(set, &set->requires, name, version);
|
|
krh@43
|
876 |
}
|
|
krh@43
|
877 |
|
|
krh@43
|
878 |
void
|
|
krh@43
|
879 |
razor_set_list_provides_packages(struct razor_set *set,
|
|
krh@43
|
880 |
const char *name,
|
|
krh@43
|
881 |
const char *version)
|
|
krh@43
|
882 |
{
|
|
krh@43
|
883 |
razor_set_list_property_packages(set, &set->provides, name, version);
|
|
krh@43
|
884 |
}
|
|
krh@43
|
885 |
|
|
krh@43
|
886 |
static void
|
|
krh@21
|
887 |
razor_set_validate(struct razor_set *set, struct array *unsatisfied)
|
|
krh@21
|
888 |
{
|
|
krh@21
|
889 |
struct razor_property *r, *p, *rend, *pend;
|
|
krh@21
|
890 |
unsigned long *u;
|
|
krh@21
|
891 |
char *pool;
|
|
krh@21
|
892 |
|
|
krh@21
|
893 |
p = set->provides.data;
|
|
krh@21
|
894 |
rend = set->requires.data + set->requires.size;
|
|
krh@21
|
895 |
pend = set->provides.data + set->provides.size;
|
|
krh@21
|
896 |
pool = set->string_pool.data;
|
|
krh@21
|
897 |
|
|
krh@35
|
898 |
for (r = set->requires.data; r < rend; r++) {
|
|
krh@21
|
899 |
while (p < pend && strcmp(&pool[r->name], &pool[p->name]) > 0)
|
|
krh@21
|
900 |
p++;
|
|
krh@37
|
901 |
|
|
krh@35
|
902 |
/* If there is more than one version of a provides,
|
|
krh@35
|
903 |
* seek to the end for the highest version. */
|
|
krh@35
|
904 |
while (p + 1 < pend && p->name == (p + 1)->name)
|
|
krh@35
|
905 |
p++;
|
|
krh@37
|
906 |
|
|
krh@37
|
907 |
/* FIXME: We need to track property flags (<, <=, =
|
|
krh@37
|
908 |
* etc) to properly determine if a requires is
|
|
krh@37
|
909 |
* satisfied. The current code doesn't track that the
|
|
krh@37
|
910 |
* requires a = 1 isn't satisfied by a = 2 provides. */
|
|
krh@37
|
911 |
|
|
krh@35
|
912 |
if (p == pend || strcmp(&pool[r->name], &pool[p->name]) != 0 ||
|
|
krh@35
|
913 |
versioncmp(&pool[r->version], &pool[p->version]) > 0) {
|
|
krh@35
|
914 |
/* FIXME: We ignore file requires for now. */
|
|
krh@35
|
915 |
if (pool[r->name] == '/')
|
|
krh@35
|
916 |
continue;
|
|
krh@21
|
917 |
u = array_add(unsatisfied, sizeof *u);
|
|
krh@21
|
918 |
*u = r - (struct razor_property *) set->requires.data;
|
|
krh@21
|
919 |
}
|
|
krh@21
|
920 |
}
|
|
krh@21
|
921 |
}
|
|
krh@21
|
922 |
|
|
krh@21
|
923 |
void
|
|
krh@21
|
924 |
razor_set_list_unsatisfied(struct razor_set *set)
|
|
krh@21
|
925 |
{
|
|
krh@21
|
926 |
struct array unsatisfied;
|
|
krh@21
|
927 |
struct razor_property *requires, *r;
|
|
krh@21
|
928 |
unsigned long *u, *end;
|
|
krh@21
|
929 |
char *pool;
|
|
krh@21
|
930 |
|
|
krh@21
|
931 |
array_init(&unsatisfied);
|
|
krh@21
|
932 |
razor_set_validate(set, &unsatisfied);
|
|
krh@21
|
933 |
|
|
krh@21
|
934 |
end = unsatisfied.data + unsatisfied.size;
|
|
krh@21
|
935 |
requires = set->requires.data;
|
|
krh@21
|
936 |
pool = set->string_pool.data;
|
|
krh@21
|
937 |
|
|
krh@21
|
938 |
for (u = unsatisfied.data; u < end; u++) {
|
|
krh@21
|
939 |
r = requires + *u;
|
|
krh@21
|
940 |
printf("%s %s not satisfied\n",
|
|
krh@21
|
941 |
&pool[r->name], &pool[r->version]);
|
|
krh@21
|
942 |
}
|
|
krh@21
|
943 |
|
|
krh@21
|
944 |
array_release(&unsatisfied);
|
|
krh@21
|
945 |
}
|
|
krh@21
|
946 |
|
|
krh@41
|
947 |
#define UPSTREAM_SOURCE 0x80000000ul
|
|
krh@41
|
948 |
#define INDEX_MASK 0x00fffffful
|
|
krh@41
|
949 |
|
|
krh@41
|
950 |
struct source {
|
|
krh@41
|
951 |
struct razor_set *set;
|
|
krh@41
|
952 |
unsigned long *requires_map;
|
|
krh@41
|
953 |
unsigned long *provides_map;
|
|
krh@41
|
954 |
};
|
|
krh@41
|
955 |
|
|
krh@41
|
956 |
static void
|
|
krh@41
|
957 |
prepare_source(struct source *source, struct razor_set *set)
|
|
krh@41
|
958 |
{
|
|
krh@41
|
959 |
int count;
|
|
krh@41
|
960 |
size_t size;
|
|
krh@41
|
961 |
|
|
krh@41
|
962 |
source->set = set;
|
|
krh@41
|
963 |
|
|
krh@41
|
964 |
count = set->requires.size / sizeof (struct razor_property);
|
|
krh@41
|
965 |
size = count * sizeof *source->requires_map;
|
|
krh@41
|
966 |
source->requires_map = zalloc(size);
|
|
krh@41
|
967 |
|
|
krh@41
|
968 |
count = set->provides.size / sizeof (struct razor_property);
|
|
krh@41
|
969 |
size = count * sizeof *source->provides_map;
|
|
krh@41
|
970 |
source->provides_map = zalloc(size);
|
|
krh@41
|
971 |
}
|
|
krh@41
|
972 |
|
|
krh@33
|
973 |
static void
|
|
krh@33
|
974 |
add_package(struct razor_importer *importer,
|
|
krh@41
|
975 |
struct razor_package *package, struct source *source,
|
|
krh@41
|
976 |
unsigned long flags)
|
|
krh@33
|
977 |
{
|
|
krh@33
|
978 |
char *pool;
|
|
krh@33
|
979 |
unsigned long *r;
|
|
krh@41
|
980 |
struct razor_package *p;
|
|
krh@33
|
981 |
|
|
krh@41
|
982 |
pool = source->set->string_pool.data;
|
|
krh@41
|
983 |
p = array_add(&importer->set->packages, sizeof *p);
|
|
krh@41
|
984 |
p->name = razor_importer_tokenize(importer, &pool[package->name]);
|
|
krh@41
|
985 |
p->name |= flags;
|
|
krh@41
|
986 |
p->version = razor_importer_tokenize(importer,
|
|
krh@41
|
987 |
&pool[package->version]);
|
|
krh@41
|
988 |
p->requires = package->requires;
|
|
krh@41
|
989 |
p->provides = package->provides;
|
|
krh@33
|
990 |
|
|
krh@41
|
991 |
r = (unsigned long *)
|
|
krh@41
|
992 |
source->set->requires_pool.data + package->requires;
|
|
krh@41
|
993 |
while (*r != ~0)
|
|
krh@41
|
994 |
source->requires_map[*r++] = 1;
|
|
krh@41
|
995 |
|
|
krh@41
|
996 |
r = (unsigned long *)
|
|
krh@41
|
997 |
source->set->provides_pool.data + package->provides;
|
|
krh@41
|
998 |
while (*r != ~0)
|
|
krh@41
|
999 |
source->provides_map[*r++] = 1;
|
|
krh@41
|
1000 |
}
|
|
krh@41
|
1001 |
|
|
krh@41
|
1002 |
|
|
krh@41
|
1003 |
/* Build the new package list sorted by merging the two package lists.
|
|
krh@41
|
1004 |
* Build new string pool as we go. (for now we just re-use that part of
|
|
krh@41
|
1005 |
* the importer). */
|
|
krh@41
|
1006 |
static void
|
|
krh@41
|
1007 |
merge_packages(struct razor_importer *importer,
|
|
krh@41
|
1008 |
struct source *source1, struct source *source2,
|
|
krh@41
|
1009 |
struct array *packages)
|
|
krh@41
|
1010 |
{
|
|
krh@41
|
1011 |
struct razor_package *upstream_packages, *p, *s, *send;
|
|
krh@41
|
1012 |
char *spool, *upool;
|
|
krh@41
|
1013 |
unsigned long *u, *uend;
|
|
krh@41
|
1014 |
int cmp;
|
|
krh@41
|
1015 |
|
|
krh@41
|
1016 |
upstream_packages = source2->set->packages.data;
|
|
krh@41
|
1017 |
|
|
krh@41
|
1018 |
u = packages->data;
|
|
krh@41
|
1019 |
uend = packages->data + packages->size;
|
|
krh@41
|
1020 |
upool = source2->set->string_pool.data;
|
|
krh@41
|
1021 |
|
|
krh@41
|
1022 |
s = source1->set->packages.data;
|
|
krh@41
|
1023 |
send = source1->set->packages.data + source1->set->packages.size;
|
|
krh@41
|
1024 |
spool = source1->set->string_pool.data;
|
|
krh@41
|
1025 |
|
|
krh@41
|
1026 |
while (s < send) {
|
|
krh@41
|
1027 |
p = upstream_packages + *u;
|
|
krh@41
|
1028 |
|
|
krh@41
|
1029 |
if (u < uend)
|
|
krh@41
|
1030 |
cmp = strcmp(&spool[s->name], &upool[p->name]);
|
|
krh@41
|
1031 |
if (u >= uend || cmp < 0) {
|
|
krh@41
|
1032 |
add_package(importer, s, source1, 0);
|
|
krh@41
|
1033 |
s++;
|
|
krh@41
|
1034 |
} else if (cmp == 0) {
|
|
krh@41
|
1035 |
add_package(importer, p, source2, UPSTREAM_SOURCE);
|
|
krh@41
|
1036 |
s++;
|
|
krh@41
|
1037 |
u++;
|
|
krh@41
|
1038 |
} else {
|
|
krh@41
|
1039 |
add_package(importer, p, source2, UPSTREAM_SOURCE);
|
|
krh@41
|
1040 |
u++;
|
|
krh@41
|
1041 |
}
|
|
krh@41
|
1042 |
}
|
|
krh@41
|
1043 |
}
|
|
krh@41
|
1044 |
|
|
krh@41
|
1045 |
static unsigned long
|
|
krh@41
|
1046 |
add_property(struct razor_importer *importer, struct array *properties,
|
|
krh@41
|
1047 |
const char *name, const char *version)
|
|
krh@41
|
1048 |
{
|
|
krh@41
|
1049 |
struct razor_property *p;
|
|
krh@41
|
1050 |
|
|
krh@41
|
1051 |
p = array_add(properties, sizeof *p);
|
|
krh@41
|
1052 |
p->name = razor_importer_tokenize(importer, name);
|
|
krh@41
|
1053 |
p->version = razor_importer_tokenize(importer, version);
|
|
krh@41
|
1054 |
|
|
krh@41
|
1055 |
return p - (struct razor_property *) properties->data;
|
|
krh@41
|
1056 |
}
|
|
krh@41
|
1057 |
|
|
krh@41
|
1058 |
static void
|
|
krh@41
|
1059 |
merge_properties(struct array *properties,
|
|
krh@41
|
1060 |
struct razor_importer *importer,
|
|
krh@41
|
1061 |
struct razor_set *set1,
|
|
krh@41
|
1062 |
struct array *properties1,
|
|
krh@41
|
1063 |
unsigned long *map1,
|
|
krh@41
|
1064 |
struct razor_set *set2,
|
|
krh@41
|
1065 |
struct array *properties2,
|
|
krh@41
|
1066 |
unsigned long *map2)
|
|
krh@41
|
1067 |
{
|
|
krh@41
|
1068 |
struct razor_property *p1, *p2;
|
|
krh@41
|
1069 |
int i, j, cmp, count1, count2;
|
|
krh@41
|
1070 |
char *pool1, *pool2;
|
|
krh@41
|
1071 |
|
|
krh@41
|
1072 |
i = 0;
|
|
krh@41
|
1073 |
j = 0;
|
|
krh@41
|
1074 |
pool1 = set1->string_pool.data;
|
|
krh@41
|
1075 |
pool2 = set2->string_pool.data;
|
|
krh@41
|
1076 |
|
|
krh@41
|
1077 |
count1 = properties1->size / sizeof *p1;
|
|
krh@41
|
1078 |
count2 = properties2->size / sizeof *p2;
|
|
krh@41
|
1079 |
while (i < count1 || j < count2) {
|
|
krh@41
|
1080 |
if (i < count1 && map1[i] == 0) {
|
|
krh@41
|
1081 |
i++;
|
|
krh@41
|
1082 |
continue;
|
|
krh@41
|
1083 |
}
|
|
krh@41
|
1084 |
if (j < count2 && map2[j] == 0) {
|
|
krh@41
|
1085 |
j++;
|
|
krh@41
|
1086 |
continue;
|
|
krh@41
|
1087 |
}
|
|
krh@41
|
1088 |
p1 = (struct razor_property *) properties1->data + i;
|
|
krh@41
|
1089 |
p2 = (struct razor_property *) properties2->data + j;
|
|
krh@41
|
1090 |
if (i < count1 && j < count2)
|
|
krh@41
|
1091 |
cmp = strcmp(&pool1[p1->name], &pool2[p2->name]);
|
|
krh@41
|
1092 |
else if (i < count1)
|
|
krh@41
|
1093 |
cmp = -1;
|
|
krh@41
|
1094 |
else
|
|
krh@41
|
1095 |
cmp = 1;
|
|
krh@41
|
1096 |
if (cmp == 0)
|
|
krh@41
|
1097 |
cmp = versioncmp(&pool1[p1->version],
|
|
krh@41
|
1098 |
&pool2[p2->version]);
|
|
krh@41
|
1099 |
if (cmp < 0) {
|
|
krh@41
|
1100 |
map1[i++] = add_property(importer,
|
|
krh@41
|
1101 |
properties,
|
|
krh@41
|
1102 |
&pool1[p1->name],
|
|
krh@41
|
1103 |
&pool1[p1->version]);
|
|
krh@41
|
1104 |
} else if (cmp > 0) {
|
|
krh@41
|
1105 |
map2[j++] = add_property(importer,
|
|
krh@41
|
1106 |
properties,
|
|
krh@41
|
1107 |
&pool2[p2->name],
|
|
krh@41
|
1108 |
&pool2[p2->version]);
|
|
krh@41
|
1109 |
} else {
|
|
krh@41
|
1110 |
map1[i++] = map2[j++] = add_property(importer,
|
|
krh@41
|
1111 |
properties,
|
|
krh@41
|
1112 |
&pool1[p1->name],
|
|
krh@41
|
1113 |
&pool1[p1->version]);
|
|
krh@41
|
1114 |
}
|
|
krh@41
|
1115 |
}
|
|
krh@41
|
1116 |
}
|
|
krh@41
|
1117 |
|
|
krh@41
|
1118 |
static unsigned long
|
|
krh@41
|
1119 |
emit_properties(struct array *source_pool, unsigned long index,
|
|
krh@41
|
1120 |
unsigned long *map, struct array *pool)
|
|
krh@41
|
1121 |
{
|
|
krh@41
|
1122 |
unsigned long r, *p, *q;
|
|
krh@41
|
1123 |
|
|
krh@41
|
1124 |
r = pool->size / sizeof *q;
|
|
krh@41
|
1125 |
p = (unsigned long *) source_pool->data + index;
|
|
krh@41
|
1126 |
while (*p != ~0) {
|
|
krh@41
|
1127 |
q = array_add(pool, sizeof *q);
|
|
krh@41
|
1128 |
*q = map[*p++];
|
|
krh@33
|
1129 |
}
|
|
krh@33
|
1130 |
|
|
krh@41
|
1131 |
q = array_add(pool, sizeof *q);
|
|
krh@41
|
1132 |
*q = ~0;
|
|
krh@41
|
1133 |
|
|
krh@41
|
1134 |
return r;
|
|
krh@41
|
1135 |
}
|
|
krh@41
|
1136 |
|
|
krh@41
|
1137 |
/* Rebuild property->packages maps. We can't just remap these, as a
|
|
krh@41
|
1138 |
* property may have lost or gained a number of packages. Allocate an
|
|
krh@41
|
1139 |
* array per property and loop through the packages and add them to
|
|
krh@41
|
1140 |
* the arrays for their properties. */
|
|
krh@41
|
1141 |
static void
|
|
krh@41
|
1142 |
rebuild_package_lists(struct razor_set *set)
|
|
krh@41
|
1143 |
{
|
|
krh@41
|
1144 |
int requires_count, provides_count;
|
|
krh@41
|
1145 |
struct array *requires_pkgs, *provides_pkgs, *a;
|
|
krh@41
|
1146 |
struct razor_package *pkg, *pkg_end;
|
|
krh@41
|
1147 |
struct razor_property *prop, *prop_end;
|
|
krh@41
|
1148 |
unsigned long *r, *q, *rpool, *ppool;
|
|
krh@41
|
1149 |
|
|
krh@41
|
1150 |
requires_count = set->requires.size / sizeof (struct razor_property);
|
|
krh@41
|
1151 |
provides_count = set->provides.size / sizeof (struct razor_property);
|
|
krh@41
|
1152 |
requires_pkgs = zalloc(requires_count * sizeof *requires_pkgs);
|
|
krh@41
|
1153 |
provides_pkgs = zalloc(provides_count * sizeof *provides_pkgs);
|
|
krh@41
|
1154 |
pkg_end = set->packages.data + set->packages.size;
|
|
krh@41
|
1155 |
rpool = set->requires_pool.data;
|
|
krh@41
|
1156 |
ppool = set->provides_pool.data;
|
|
krh@41
|
1157 |
|
|
krh@41
|
1158 |
for (pkg = set->packages.data; pkg < pkg_end; pkg++) {
|
|
krh@41
|
1159 |
for (r = &rpool[pkg->requires]; *r != ~0; r++) {
|
|
krh@41
|
1160 |
q = array_add(&requires_pkgs[*r], sizeof *q);
|
|
krh@41
|
1161 |
*q = pkg - (struct razor_package *) set->packages.data;
|
|
krh@41
|
1162 |
}
|
|
krh@41
|
1163 |
for (r = &ppool[pkg->provides]; *r != ~0; r++) {
|
|
krh@41
|
1164 |
q = array_add(&provides_pkgs[*r], sizeof *q);
|
|
krh@41
|
1165 |
*q = pkg - (struct razor_package *) set->packages.data;
|
|
krh@41
|
1166 |
}
|
|
krh@33
|
1167 |
}
|
|
krh@33
|
1168 |
|
|
krh@41
|
1169 |
prop_end = set->requires.data + set->requires.size;
|
|
krh@41
|
1170 |
a = requires_pkgs;
|
|
krh@41
|
1171 |
for (prop = set->requires.data; prop < prop_end; prop++, a++) {
|
|
krh@41
|
1172 |
prop->packages = add_to_property_pool(&set->requires_pool, a);
|
|
krh@41
|
1173 |
array_release(a);
|
|
krh@41
|
1174 |
}
|
|
krh@41
|
1175 |
free(requires_pkgs);
|
|
krh@41
|
1176 |
|
|
krh@41
|
1177 |
prop_end = set->provides.data + set->provides.size;
|
|
krh@41
|
1178 |
a = provides_pkgs;
|
|
krh@41
|
1179 |
for (prop = set->provides.data; prop < prop_end; prop++, a++) {
|
|
krh@41
|
1180 |
prop->packages = add_to_property_pool(&set->provides_pool, a);
|
|
krh@41
|
1181 |
array_release(a);
|
|
krh@41
|
1182 |
}
|
|
krh@41
|
1183 |
free(provides_pkgs);
|
|
krh@33
|
1184 |
}
|
|
krh@33
|
1185 |
|
|
krh@33
|
1186 |
/* Add packages from 'upstream' to 'set'. The packages to add are
|
|
krh@33
|
1187 |
* specified by the 'packages' array, which is a sorted list of
|
|
krh@33
|
1188 |
* package indexes. Returns a newly allocated package set. Does not
|
|
krh@41
|
1189 |
* enforce validity of the resulting package set.
|
|
krh@41
|
1190 |
*
|
|
krh@41
|
1191 |
* This looks more complicated than it is. An easy way to merge two
|
|
krh@41
|
1192 |
* package sets would be to just use a razor_importer, but that
|
|
krh@41
|
1193 |
* requires resorting, and is thus O(n log n). We can do this in a
|
|
krh@41
|
1194 |
* linear sweep, but it gets a little more complicated.
|
|
krh@41
|
1195 |
*/
|
|
krh@33
|
1196 |
struct razor_set *
|
|
krh@33
|
1197 |
razor_set_add(struct razor_set *set, struct razor_set *upstream,
|
|
krh@33
|
1198 |
struct array *packages)
|
|
krh@33
|
1199 |
{
|
|
krh@41
|
1200 |
struct razor_set *result;
|
|
krh@33
|
1201 |
struct razor_importer *importer;
|
|
krh@41
|
1202 |
struct razor_package *p, *pend;
|
|
krh@41
|
1203 |
struct source source, upstream_source;
|
|
krh@33
|
1204 |
|
|
krh@33
|
1205 |
importer = razor_importer_new();
|
|
krh@33
|
1206 |
|
|
krh@41
|
1207 |
prepare_source(&upstream_source, upstream);
|
|
krh@41
|
1208 |
prepare_source(&source, set);
|
|
krh@41
|
1209 |
|
|
krh@41
|
1210 |
merge_packages(importer, &source, &upstream_source, packages);
|
|
krh@41
|
1211 |
|
|
krh@41
|
1212 |
/* As we built the package list, we filled out a bitvector of
|
|
krh@41
|
1213 |
* the properties that are referenced by the packages in the
|
|
krh@41
|
1214 |
* new set. Now we do a parallel loop through the properties
|
|
krh@41
|
1215 |
* and emit those marked in the bit vector to the new set. In
|
|
krh@41
|
1216 |
* the process, we update the bit vector to actually map from
|
|
krh@41
|
1217 |
* indices in the old property list to indices in the new
|
|
krh@41
|
1218 |
* property list for both sets. */
|
|
krh@41
|
1219 |
|
|
krh@41
|
1220 |
merge_properties(&importer->set->requires, importer,
|
|
krh@41
|
1221 |
set, &set->requires, source.requires_map,
|
|
krh@41
|
1222 |
upstream, &upstream->requires,
|
|
krh@41
|
1223 |
upstream_source.requires_map);
|
|
krh@41
|
1224 |
merge_properties(&importer->set->provides, importer,
|
|
krh@41
|
1225 |
set, &set->provides, source.provides_map,
|
|
krh@41
|
1226 |
upstream, &upstream->provides,
|
|
krh@41
|
1227 |
upstream_source.provides_map);
|
|
krh@41
|
1228 |
|
|
krh@41
|
1229 |
/* Now we loop through the packages again and emit the
|
|
krh@41
|
1230 |
* property lists, remapped to point to the new properties. */
|
|
krh@41
|
1231 |
|
|
krh@41
|
1232 |
pend = importer->set->packages.data + importer->set->packages.size;
|
|
krh@41
|
1233 |
for (p = importer->set->packages.data; p < pend; p++) {
|
|
krh@41
|
1234 |
struct source *src;
|
|
krh@41
|
1235 |
|
|
krh@41
|
1236 |
if (p->name & UPSTREAM_SOURCE)
|
|
krh@41
|
1237 |
src = &upstream_source;
|
|
krh@41
|
1238 |
else
|
|
krh@41
|
1239 |
src = &source;
|
|
krh@41
|
1240 |
|
|
krh@41
|
1241 |
p->requires = emit_properties(&src->set->requires_pool,
|
|
krh@41
|
1242 |
p->requires,
|
|
krh@41
|
1243 |
src->requires_map,
|
|
krh@41
|
1244 |
&importer->set->requires_pool);
|
|
krh@41
|
1245 |
p->provides = emit_properties(&src->set->provides_pool,
|
|
krh@41
|
1246 |
p->provides,
|
|
krh@41
|
1247 |
src->provides_map,
|
|
krh@41
|
1248 |
&importer->set->provides_pool);
|
|
krh@41
|
1249 |
p->name &= INDEX_MASK;
|
|
krh@33
|
1250 |
}
|
|
krh@33
|
1251 |
|
|
krh@41
|
1252 |
rebuild_package_lists(importer->set);
|
|
krh@41
|
1253 |
|
|
krh@41
|
1254 |
result = importer->set;
|
|
krh@41
|
1255 |
array_release(&importer->buckets);
|
|
krh@41
|
1256 |
free(importer);
|
|
krh@41
|
1257 |
|
|
krh@41
|
1258 |
return result;
|
|
krh@33
|
1259 |
}
|
|
krh@33
|
1260 |
|
|
krh@37
|
1261 |
void
|
|
krh@37
|
1262 |
razor_set_satisfy(struct razor_set *set, struct array *unsatisfied,
|
|
krh@37
|
1263 |
struct razor_set *upstream, struct array *list)
|
|
krh@37
|
1264 |
{
|
|
krh@37
|
1265 |
struct razor_property *requires, *r;
|
|
krh@37
|
1266 |
struct razor_property *p, *pend;
|
|
krh@39
|
1267 |
unsigned long *u, *end, *pkg, *package_pool;
|
|
krh@37
|
1268 |
char *pool, *upool;
|
|
krh@37
|
1269 |
|
|
krh@37
|
1270 |
end = unsatisfied->data + unsatisfied->size;
|
|
krh@37
|
1271 |
requires = set->requires.data;
|
|
krh@37
|
1272 |
pool = set->string_pool.data;
|
|
krh@37
|
1273 |
|
|
krh@37
|
1274 |
p = upstream->provides.data;
|
|
krh@37
|
1275 |
pend = upstream->provides.data + upstream->provides.size;
|
|
krh@37
|
1276 |
upool = upstream->string_pool.data;
|
|
krh@39
|
1277 |
package_pool = upstream->package_pool.data;
|
|
krh@37
|
1278 |
|
|
krh@37
|
1279 |
for (u = unsatisfied->data; u < end; u++) {
|
|
krh@37
|
1280 |
r = requires + *u;
|
|
krh@37
|
1281 |
|
|
krh@37
|
1282 |
while (p < pend && strcmp(&pool[r->name], &upool[p->name]) > 0)
|
|
krh@37
|
1283 |
p++;
|
|
krh@37
|
1284 |
/* If there is more than one version of a provides,
|
|
krh@37
|
1285 |
* seek to the end for the highest version. */
|
|
krh@37
|
1286 |
while (p + 1 < pend && p->name == (p + 1)->name)
|
|
krh@37
|
1287 |
p++;
|
|
krh@37
|
1288 |
|
|
krh@37
|
1289 |
if (p == pend ||
|
|
krh@37
|
1290 |
strcmp(&pool[r->name], &upool[p->name]) != 0 ||
|
|
krh@37
|
1291 |
versioncmp(&pool[r->version], &upool[p->version]) > 0) {
|
|
krh@37
|
1292 |
/* Do we need to track unsatisfiable requires
|
|
krh@37
|
1293 |
* as we go, or should we just do a
|
|
krh@37
|
1294 |
* razor_set_validate() at the end? */
|
|
krh@37
|
1295 |
} else {
|
|
krh@37
|
1296 |
pkg = array_add(list, sizeof *pkg);
|
|
krh@37
|
1297 |
/* We just pull in the first package that provides */
|
|
krh@39
|
1298 |
*pkg = package_pool[p->packages];
|
|
krh@37
|
1299 |
}
|
|
krh@37
|
1300 |
}
|
|
krh@37
|
1301 |
}
|
|
krh@37
|
1302 |
|
|
krh@38
|
1303 |
static void
|
|
krh@38
|
1304 |
find_packages(struct razor_set *set,
|
|
krh@38
|
1305 |
int count, const char **packages, struct array *list)
|
|
krh@38
|
1306 |
{
|
|
krh@38
|
1307 |
struct razor_package *p;
|
|
krh@38
|
1308 |
unsigned long *r;
|
|
krh@38
|
1309 |
int i;
|
|
krh@38
|
1310 |
|
|
krh@38
|
1311 |
/* FIXME: Sort the packages. */
|
|
krh@38
|
1312 |
for (i = 0; i < count; i++) {
|
|
krh@38
|
1313 |
p = razor_set_get_package(set, packages[i]);
|
|
krh@38
|
1314 |
r = array_add(list, sizeof *r);
|
|
krh@38
|
1315 |
*r = p - (struct razor_package *) set->packages.data;
|
|
krh@38
|
1316 |
}
|
|
krh@38
|
1317 |
}
|
|
krh@38
|
1318 |
|
|
krh@38
|
1319 |
static void
|
|
krh@38
|
1320 |
find_all_packages(struct razor_set *set,
|
|
krh@38
|
1321 |
struct razor_set *upstream, struct array *list)
|
|
krh@38
|
1322 |
{
|
|
krh@38
|
1323 |
struct razor_package *p, *u, *pend, *uend;
|
|
krh@38
|
1324 |
unsigned long *r;
|
|
krh@38
|
1325 |
char *pool, *upool;
|
|
krh@38
|
1326 |
|
|
krh@38
|
1327 |
pend = set->packages.data + set->packages.size;
|
|
krh@38
|
1328 |
pool = set->string_pool.data;
|
|
krh@38
|
1329 |
u = upstream->packages.data;
|
|
krh@38
|
1330 |
uend = upstream->packages.data + upstream->packages.size;
|
|
krh@38
|
1331 |
upool = upstream->string_pool.data;
|
|
krh@38
|
1332 |
|
|
krh@38
|
1333 |
for (p = set->packages.data; p < pend; p++) {
|
|
krh@38
|
1334 |
while (u < uend && strcmp(&pool[p->name], &upool[u->name]) > 0)
|
|
krh@38
|
1335 |
u++;
|
|
krh@38
|
1336 |
if (strcmp(&pool[p->name], &upool[u->name]) == 0) {
|
|
krh@38
|
1337 |
r = array_add(list, sizeof *r);
|
|
krh@38
|
1338 |
*r = u - (struct razor_package *) upstream->packages.data;
|
|
krh@38
|
1339 |
}
|
|
krh@38
|
1340 |
}
|
|
krh@38
|
1341 |
}
|
|
krh@38
|
1342 |
|
|
krh@33
|
1343 |
struct razor_set *
|
|
krh@33
|
1344 |
razor_set_update(struct razor_set *set, struct razor_set *upstream,
|
|
krh@33
|
1345 |
int count, const char **packages)
|
|
krh@33
|
1346 |
{
|
|
krh@37
|
1347 |
struct razor_set *new;
|
|
krh@43
|
1348 |
struct razor_package *upackages;
|
|
krh@37
|
1349 |
struct array list, unsatisfied;
|
|
krh@37
|
1350 |
char *pool;
|
|
krh@38
|
1351 |
unsigned long *u, *end;
|
|
krh@38
|
1352 |
int total = 0;
|
|
krh@33
|
1353 |
|
|
krh@33
|
1354 |
array_init(&list);
|
|
krh@38
|
1355 |
if (count > 0)
|
|
krh@38
|
1356 |
find_packages(upstream, count, packages, &list);
|
|
krh@38
|
1357 |
else
|
|
krh@38
|
1358 |
find_all_packages(set, upstream, &list);
|
|
krh@33
|
1359 |
|
|
krh@37
|
1360 |
end = list.data + list.size;
|
|
krh@37
|
1361 |
upackages = upstream->packages.data;
|
|
krh@37
|
1362 |
pool = upstream->string_pool.data;
|
|
krh@38
|
1363 |
total += list.size / sizeof *u;
|
|
krh@37
|
1364 |
|
|
krh@37
|
1365 |
while (list.size > 0) {
|
|
krh@37
|
1366 |
new = razor_set_add(set, upstream, &list);
|
|
krh@37
|
1367 |
array_release(&list);
|
|
krh@37
|
1368 |
razor_set_destroy(set);
|
|
krh@37
|
1369 |
set = new;
|
|
krh@37
|
1370 |
|
|
krh@37
|
1371 |
array_init(&unsatisfied);
|
|
krh@37
|
1372 |
razor_set_validate(new, &unsatisfied);
|
|
krh@37
|
1373 |
array_init(&list);
|
|
krh@37
|
1374 |
razor_set_satisfy(new, &unsatisfied, upstream, &list);
|
|
krh@37
|
1375 |
array_release(&unsatisfied);
|
|
krh@37
|
1376 |
|
|
krh@37
|
1377 |
end = list.data + list.size;
|
|
krh@37
|
1378 |
upackages = upstream->packages.data;
|
|
krh@37
|
1379 |
pool = upstream->string_pool.data;
|
|
krh@38
|
1380 |
total += list.size / sizeof *u;
|
|
krh@37
|
1381 |
}
|
|
krh@37
|
1382 |
|
|
krh@37
|
1383 |
array_release(&list);
|
|
krh@37
|
1384 |
|
|
krh@37
|
1385 |
return set;
|
|
krh@33
|
1386 |
}
|
|
krh@33
|
1387 |
|
|
krh@44
|
1388 |
/* The diff order matters. We should sort the packages so that a
|
|
krh@44
|
1389 |
* REMOVE of a package comes before the INSTALL, and so that all
|
|
krh@44
|
1390 |
* requires for a package have been installed before the package.
|
|
krh@44
|
1391 |
**/
|
|
krh@44
|
1392 |
|
|
krh@44
|
1393 |
void
|
|
krh@44
|
1394 |
razor_set_diff(struct razor_set *set, struct razor_set *upstream,
|
|
krh@44
|
1395 |
razor_package_callback_t callback, void *data)
|
|
krh@44
|
1396 |
{
|
|
krh@44
|
1397 |
struct razor_package *p, *pend, *u, *uend;
|
|
krh@44
|
1398 |
char *ppool, *upool;
|
|
krh@44
|
1399 |
int res = 0;
|
|
krh@44
|
1400 |
|
|
krh@44
|
1401 |
p = set->packages.data;
|
|
krh@44
|
1402 |
pend = set->packages.data + set->packages.size;
|
|
krh@44
|
1403 |
ppool = set->string_pool.data;
|
|
krh@44
|
1404 |
|
|
krh@44
|
1405 |
u = upstream->packages.data;
|
|
krh@44
|
1406 |
uend = upstream->packages.data + upstream->packages.size;
|
|
krh@44
|
1407 |
upool = upstream->string_pool.data;
|
|
krh@44
|
1408 |
|
|
krh@44
|
1409 |
while (p < pend || u < uend) {
|
|
krh@44
|
1410 |
if (p < pend && u < uend) {
|
|
krh@44
|
1411 |
res = strcmp(&ppool[p->name], &upool[u->name]);
|
|
krh@44
|
1412 |
if (res == 0)
|
|
krh@44
|
1413 |
res = versioncmp(&ppool[p->version],
|
|
krh@44
|
1414 |
&upool[u->version]);
|
|
krh@44
|
1415 |
}
|
|
krh@44
|
1416 |
|
|
krh@44
|
1417 |
if (u == uend || res < 0) {
|
|
krh@44
|
1418 |
callback(&ppool[p->name], &ppool[p->version],
|
|
krh@44
|
1419 |
NULL, data);
|
|
krh@44
|
1420 |
p++;
|
|
krh@44
|
1421 |
continue;
|
|
krh@44
|
1422 |
} else if (p == pend || res > 0) {
|
|
krh@44
|
1423 |
callback(&upool[u->name], NULL, &upool[u->version],
|
|
krh@44
|
1424 |
data);
|
|
krh@44
|
1425 |
u++;
|
|
krh@44
|
1426 |
continue;
|
|
krh@44
|
1427 |
} else {
|
|
krh@44
|
1428 |
p++;
|
|
krh@44
|
1429 |
u++;
|
|
krh@44
|
1430 |
}
|
|
krh@44
|
1431 |
}
|
|
krh@44
|
1432 |
}
|