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