|
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@0
|
13 |
|
|
krh@27
|
14 |
#include "razor.h"
|
|
krh@6
|
15 |
|
|
krh@30
|
16 |
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
|
krh@30
|
17 |
|
|
krh@30
|
18 |
struct array {
|
|
krh@30
|
19 |
void *data;
|
|
krh@30
|
20 |
int size, alloc;
|
|
krh@30
|
21 |
};
|
|
krh@30
|
22 |
|
|
krh@30
|
23 |
struct razor_set_section {
|
|
krh@30
|
24 |
unsigned int type;
|
|
krh@30
|
25 |
unsigned int offset;
|
|
krh@30
|
26 |
unsigned int size;
|
|
krh@30
|
27 |
};
|
|
krh@30
|
28 |
|
|
krh@30
|
29 |
struct razor_set_header {
|
|
krh@30
|
30 |
unsigned int magic;
|
|
krh@30
|
31 |
unsigned int version;
|
|
krh@30
|
32 |
struct razor_set_section sections[0];
|
|
krh@30
|
33 |
};
|
|
krh@30
|
34 |
|
|
krh@30
|
35 |
#define RAZOR_MAGIC 0x7a7a7a7a
|
|
krh@30
|
36 |
#define RAZOR_VERSION 1
|
|
krh@30
|
37 |
|
|
krh@30
|
38 |
#define RAZOR_PACKAGES 0
|
|
krh@30
|
39 |
#define RAZOR_REQUIRES 1
|
|
krh@30
|
40 |
#define RAZOR_PROVIDES 2
|
|
krh@30
|
41 |
#define RAZOR_STRING_POOL 3
|
|
krh@30
|
42 |
#define RAZOR_PROPERTY_POOL 4
|
|
krh@30
|
43 |
|
|
krh@30
|
44 |
struct razor_package {
|
|
krh@30
|
45 |
unsigned long name;
|
|
krh@30
|
46 |
unsigned long version;
|
|
krh@30
|
47 |
unsigned long requires;
|
|
krh@30
|
48 |
unsigned long provides;
|
|
krh@30
|
49 |
};
|
|
krh@30
|
50 |
|
|
krh@30
|
51 |
struct razor_property {
|
|
krh@30
|
52 |
unsigned long name;
|
|
krh@30
|
53 |
unsigned long version;
|
|
krh@30
|
54 |
unsigned long packages;
|
|
krh@30
|
55 |
};
|
|
krh@30
|
56 |
|
|
krh@30
|
57 |
struct razor_set {
|
|
krh@30
|
58 |
struct array string_pool;
|
|
krh@30
|
59 |
struct array property_pool;
|
|
krh@30
|
60 |
struct array packages;
|
|
krh@30
|
61 |
struct array requires;
|
|
krh@30
|
62 |
struct array provides;
|
|
krh@30
|
63 |
struct razor_set_header *header;
|
|
krh@30
|
64 |
};
|
|
krh@30
|
65 |
|
|
krh@30
|
66 |
struct import_property_context {
|
|
krh@30
|
67 |
struct array *all;
|
|
krh@30
|
68 |
struct array package;
|
|
krh@30
|
69 |
};
|
|
krh@30
|
70 |
|
|
krh@30
|
71 |
struct razor_importer {
|
|
krh@30
|
72 |
struct razor_set *set;
|
|
krh@32
|
73 |
struct array buckets;
|
|
krh@30
|
74 |
struct import_property_context requires;
|
|
krh@30
|
75 |
struct import_property_context provides;
|
|
krh@30
|
76 |
struct razor_package *package;
|
|
krh@30
|
77 |
unsigned long *requires_map;
|
|
krh@30
|
78 |
unsigned long *provides_map;
|
|
krh@30
|
79 |
};
|
|
krh@30
|
80 |
|
|
krh@13
|
81 |
static void
|
|
krh@13
|
82 |
array_init(struct array *array)
|
|
krh@13
|
83 |
{
|
|
krh@13
|
84 |
memset(array, 0, sizeof *array);
|
|
krh@13
|
85 |
}
|
|
krh@13
|
86 |
|
|
krh@13
|
87 |
static void
|
|
krh@13
|
88 |
array_release(struct array *array)
|
|
krh@13
|
89 |
{
|
|
krh@13
|
90 |
free(array->data);
|
|
krh@13
|
91 |
}
|
|
krh@13
|
92 |
|
|
krh@6
|
93 |
static void *
|
|
krh@6
|
94 |
array_add(struct array *array, int size)
|
|
krh@6
|
95 |
{
|
|
krh@6
|
96 |
int alloc;
|
|
krh@6
|
97 |
void *data, *p;
|
|
krh@6
|
98 |
|
|
krh@6
|
99 |
if (array->alloc > 0)
|
|
krh@6
|
100 |
alloc = array->alloc;
|
|
krh@6
|
101 |
else
|
|
krh@10
|
102 |
alloc = 16;
|
|
krh@6
|
103 |
|
|
krh@6
|
104 |
while (alloc < array->size + size)
|
|
krh@6
|
105 |
alloc *= 2;
|
|
krh@6
|
106 |
|
|
krh@6
|
107 |
if (array->alloc < alloc) {
|
|
krh@6
|
108 |
data = realloc(array->data, alloc);
|
|
krh@6
|
109 |
if (data == NULL)
|
|
krh@6
|
110 |
return 0;
|
|
krh@6
|
111 |
array->data = data;
|
|
krh@6
|
112 |
array->alloc = alloc;
|
|
krh@6
|
113 |
}
|
|
krh@6
|
114 |
|
|
krh@6
|
115 |
p = array->data + array->size;
|
|
krh@6
|
116 |
array->size += size;
|
|
krh@6
|
117 |
|
|
krh@6
|
118 |
return p;
|
|
krh@6
|
119 |
}
|
|
krh@6
|
120 |
|
|
krh@0
|
121 |
static int
|
|
krh@0
|
122 |
write_to_fd(int fd, void *p, size_t size)
|
|
krh@0
|
123 |
{
|
|
krh@0
|
124 |
int rest, len;
|
|
krh@0
|
125 |
|
|
krh@0
|
126 |
rest = size;
|
|
krh@0
|
127 |
while (rest > 0) {
|
|
krh@0
|
128 |
len = write(fd, p, rest);
|
|
krh@0
|
129 |
if (len < 0)
|
|
krh@0
|
130 |
return -1;
|
|
krh@0
|
131 |
rest -= len;
|
|
krh@0
|
132 |
}
|
|
krh@0
|
133 |
|
|
krh@0
|
134 |
return 0;
|
|
krh@0
|
135 |
}
|
|
krh@0
|
136 |
|
|
krh@0
|
137 |
static void *
|
|
krh@0
|
138 |
zalloc(size_t size)
|
|
krh@0
|
139 |
{
|
|
krh@0
|
140 |
void *p;
|
|
krh@0
|
141 |
|
|
krh@0
|
142 |
p = malloc(size);
|
|
krh@0
|
143 |
memset(p, 0, size);
|
|
krh@0
|
144 |
|
|
krh@0
|
145 |
return p;
|
|
krh@0
|
146 |
}
|
|
krh@0
|
147 |
|
|
krh@19
|
148 |
struct razor_set_section razor_sections[] = {
|
|
krh@19
|
149 |
{ RAZOR_PACKAGES, offsetof(struct razor_set, packages) },
|
|
krh@19
|
150 |
{ RAZOR_REQUIRES, offsetof(struct razor_set, requires) },
|
|
krh@19
|
151 |
{ RAZOR_PROVIDES, offsetof(struct razor_set, provides) },
|
|
krh@19
|
152 |
{ RAZOR_STRING_POOL, offsetof(struct razor_set, string_pool) },
|
|
krh@19
|
153 |
{ RAZOR_PROPERTY_POOL, offsetof(struct razor_set, property_pool) },
|
|
krh@19
|
154 |
};
|
|
krh@19
|
155 |
|
|
krh@4
|
156 |
struct razor_set *
|
|
krh@4
|
157 |
razor_set_create(void)
|
|
krh@0
|
158 |
{
|
|
krh@18
|
159 |
return zalloc(sizeof(struct razor_set));
|
|
krh@0
|
160 |
}
|
|
krh@0
|
161 |
|
|
krh@4
|
162 |
struct razor_set *
|
|
krh@4
|
163 |
razor_set_open(const char *filename)
|
|
krh@0
|
164 |
{
|
|
krh@4
|
165 |
struct razor_set *set;
|
|
krh@19
|
166 |
struct razor_set_section *s;
|
|
krh@0
|
167 |
struct stat stat;
|
|
krh@19
|
168 |
struct array *array;
|
|
krh@19
|
169 |
int fd;
|
|
krh@0
|
170 |
|
|
krh@4
|
171 |
set = zalloc(sizeof *set);
|
|
krh@0
|
172 |
fd = open(filename, O_RDONLY);
|
|
krh@0
|
173 |
if (fstat(fd, &stat) < 0)
|
|
krh@0
|
174 |
return NULL;
|
|
krh@4
|
175 |
set->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
krh@4
|
176 |
if (set->header == MAP_FAILED) {
|
|
krh@4
|
177 |
free(set);
|
|
krh@0
|
178 |
return NULL;
|
|
krh@0
|
179 |
}
|
|
krh@0
|
180 |
|
|
krh@19
|
181 |
for (s = set->header->sections; ~s->type; s++) {
|
|
krh@19
|
182 |
if (s->type >= ARRAY_SIZE(razor_sections))
|
|
krh@19
|
183 |
continue;
|
|
krh@19
|
184 |
if (s->type != razor_sections[s->type].type)
|
|
krh@19
|
185 |
continue;
|
|
krh@19
|
186 |
array = (void *) set + razor_sections[s->type].offset;
|
|
krh@19
|
187 |
array->data = (void *) set->header + s->offset;
|
|
krh@19
|
188 |
array->size = s->size;
|
|
krh@19
|
189 |
array->alloc = s->size;
|
|
krh@0
|
190 |
}
|
|
krh@0
|
191 |
close(fd);
|
|
krh@0
|
192 |
|
|
krh@4
|
193 |
return set;
|
|
krh@0
|
194 |
}
|
|
krh@0
|
195 |
|
|
krh@0
|
196 |
void
|
|
krh@4
|
197 |
razor_set_destroy(struct razor_set *set)
|
|
krh@0
|
198 |
{
|
|
krh@0
|
199 |
unsigned int size;
|
|
krh@19
|
200 |
struct array *a;
|
|
krh@0
|
201 |
int i;
|
|
krh@0
|
202 |
|
|
krh@4
|
203 |
if (set->header) {
|
|
krh@4
|
204 |
for (i = 0; set->header->sections[i].type; i++)
|
|
krh@0
|
205 |
;
|
|
krh@4
|
206 |
size = set->header->sections[i].type;
|
|
krh@4
|
207 |
munmap(set->header, size);
|
|
krh@0
|
208 |
} else {
|
|
krh@19
|
209 |
for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
|
|
krh@19
|
210 |
a = (void *) set + razor_sections[i].offset;
|
|
krh@19
|
211 |
free(a->data);
|
|
krh@19
|
212 |
}
|
|
krh@0
|
213 |
}
|
|
krh@0
|
214 |
|
|
krh@4
|
215 |
free(set);
|
|
krh@0
|
216 |
}
|
|
krh@0
|
217 |
|
|
krh@0
|
218 |
static int
|
|
krh@4
|
219 |
razor_set_write(struct razor_set *set, const char *filename)
|
|
krh@0
|
220 |
{
|
|
krh@0
|
221 |
char data[4096];
|
|
krh@4
|
222 |
struct razor_set_header *header = (struct razor_set_header *) data;
|
|
krh@19
|
223 |
struct array *a;
|
|
krh@17
|
224 |
unsigned long offset;
|
|
krh@17
|
225 |
int i, fd;
|
|
krh@0
|
226 |
|
|
krh@0
|
227 |
memset(data, 0, sizeof data);
|
|
krh@4
|
228 |
header->magic = RAZOR_MAGIC;
|
|
krh@4
|
229 |
header->version = RAZOR_VERSION;
|
|
krh@17
|
230 |
offset = sizeof data;
|
|
krh@0
|
231 |
|
|
krh@19
|
232 |
for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
|
|
krh@19
|
233 |
if (razor_sections[i].type != i)
|
|
krh@19
|
234 |
continue;
|
|
krh@19
|
235 |
a = (void *) set + razor_sections[i].offset;
|
|
krh@19
|
236 |
header->sections[i].type = i;
|
|
krh@17
|
237 |
header->sections[i].offset = offset;
|
|
krh@19
|
238 |
header->sections[i].size = a->size;
|
|
krh@19
|
239 |
offset += (a->size + 4095) & ~4095;
|
|
krh@17
|
240 |
}
|
|
krh@0
|
241 |
|
|
krh@19
|
242 |
header->sections[i].type = ~0;
|
|
krh@17
|
243 |
header->sections[i].offset = 0;
|
|
krh@17
|
244 |
header->sections[i].size = 0;
|
|
krh@10
|
245 |
|
|
krh@0
|
246 |
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
|
|
krh@0
|
247 |
if (fd < 0)
|
|
krh@0
|
248 |
return -1;
|
|
krh@0
|
249 |
|
|
krh@0
|
250 |
write_to_fd(fd, data, sizeof data);
|
|
krh@19
|
251 |
for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
|
|
krh@19
|
252 |
if (razor_sections[i].type != i)
|
|
krh@19
|
253 |
continue;
|
|
krh@19
|
254 |
a = (void *) set + razor_sections[i].offset;
|
|
krh@19
|
255 |
write_to_fd(fd, a->data, (a->size + 4095) & ~4095);
|
|
krh@19
|
256 |
}
|
|
krh@17
|
257 |
|
|
krh@17
|
258 |
close(fd);
|
|
krh@0
|
259 |
|
|
krh@0
|
260 |
return 0;
|
|
krh@0
|
261 |
}
|
|
krh@0
|
262 |
|
|
krh@0
|
263 |
static unsigned int
|
|
krh@0
|
264 |
hash_string(const char *key)
|
|
krh@0
|
265 |
{
|
|
krh@0
|
266 |
const char *p;
|
|
krh@0
|
267 |
unsigned int hash = 0;
|
|
krh@0
|
268 |
|
|
krh@0
|
269 |
for (p = key; *p; p++)
|
|
krh@9
|
270 |
hash = (hash * 617) ^ *p;
|
|
krh@0
|
271 |
|
|
krh@0
|
272 |
return hash;
|
|
krh@0
|
273 |
}
|
|
krh@0
|
274 |
|
|
krh@32
|
275 |
static unsigned long
|
|
krh@32
|
276 |
razor_importer_lookup(struct razor_importer *importer, const char *key)
|
|
krh@0
|
277 |
{
|
|
krh@6
|
278 |
unsigned int mask, start, i;
|
|
krh@6
|
279 |
unsigned long *b;
|
|
krh@6
|
280 |
char *pool;
|
|
krh@0
|
281 |
|
|
krh@32
|
282 |
pool = importer->set->string_pool.data;
|
|
krh@32
|
283 |
mask = importer->buckets.alloc - 1;
|
|
krh@6
|
284 |
start = hash_string(key) * sizeof(unsigned long);
|
|
krh@0
|
285 |
|
|
krh@32
|
286 |
for (i = 0; i < importer->buckets.alloc; i += sizeof *b) {
|
|
krh@32
|
287 |
b = importer->buckets.data + ((start + i) & mask);
|
|
krh@6
|
288 |
|
|
krh@6
|
289 |
if (*b == 0)
|
|
krh@0
|
290 |
return 0;
|
|
krh@0
|
291 |
|
|
krh@6
|
292 |
if (strcmp(key, &pool[*b]) == 0)
|
|
krh@6
|
293 |
return *b;
|
|
krh@6
|
294 |
}
|
|
krh@0
|
295 |
|
|
krh@0
|
296 |
return 0;
|
|
krh@0
|
297 |
}
|
|
krh@0
|
298 |
|
|
krh@0
|
299 |
static unsigned long
|
|
krh@4
|
300 |
add_to_string_pool(struct razor_set *set, const char *key)
|
|
krh@0
|
301 |
{
|
|
krh@6
|
302 |
int len;
|
|
krh@6
|
303 |
char *p;
|
|
krh@0
|
304 |
|
|
krh@0
|
305 |
len = strlen(key) + 1;
|
|
krh@6
|
306 |
p = array_add(&set->string_pool, len);
|
|
krh@6
|
307 |
memcpy(p, key, len);
|
|
krh@0
|
308 |
|
|
krh@6
|
309 |
return p - (char *) set->string_pool.data;
|
|
krh@0
|
310 |
}
|
|
krh@0
|
311 |
|
|
krh@10
|
312 |
static unsigned long
|
|
krh@10
|
313 |
add_to_property_pool(struct razor_set *set, struct array *properties)
|
|
krh@10
|
314 |
{
|
|
krh@10
|
315 |
unsigned long *p;
|
|
krh@10
|
316 |
|
|
krh@10
|
317 |
p = array_add(properties, sizeof *p);
|
|
krh@18
|
318 |
*p = ~0ul;
|
|
krh@10
|
319 |
p = array_add(&set->property_pool, properties->size);
|
|
krh@10
|
320 |
memcpy(p, properties->data, properties->size);
|
|
krh@10
|
321 |
|
|
krh@10
|
322 |
return p - (unsigned long *) set->property_pool.data;
|
|
krh@10
|
323 |
}
|
|
krh@10
|
324 |
|
|
krh@0
|
325 |
static void
|
|
krh@32
|
326 |
do_insert(struct razor_importer *importer, unsigned long value)
|
|
krh@0
|
327 |
{
|
|
krh@6
|
328 |
unsigned int mask, start, i;
|
|
krh@6
|
329 |
unsigned long *b;
|
|
krh@0
|
330 |
const char *key;
|
|
krh@0
|
331 |
|
|
krh@32
|
332 |
key = (char *) importer->set->string_pool.data + value;
|
|
krh@32
|
333 |
mask = importer->buckets.alloc - 1;
|
|
krh@6
|
334 |
start = hash_string(key) * sizeof(unsigned long);
|
|
krh@6
|
335 |
|
|
krh@32
|
336 |
for (i = 0; i < importer->buckets.alloc; i += sizeof *b) {
|
|
krh@32
|
337 |
b = importer->buckets.data + ((start + i) & mask);
|
|
krh@6
|
338 |
if (*b == 0) {
|
|
krh@6
|
339 |
*b = value;
|
|
krh@0
|
340 |
break;
|
|
krh@0
|
341 |
}
|
|
krh@6
|
342 |
}
|
|
krh@0
|
343 |
}
|
|
krh@0
|
344 |
|
|
krh@32
|
345 |
static unsigned long
|
|
krh@32
|
346 |
razor_importer_insert(struct razor_importer *importer, const char *key)
|
|
krh@0
|
347 |
{
|
|
krh@6
|
348 |
unsigned long value, *buckets, *b, *end;
|
|
krh@6
|
349 |
int alloc;
|
|
krh@0
|
350 |
|
|
krh@32
|
351 |
alloc = importer->buckets.alloc;
|
|
krh@32
|
352 |
array_add(&importer->buckets, 4 * sizeof *buckets);
|
|
krh@32
|
353 |
if (alloc != importer->buckets.alloc) {
|
|
krh@32
|
354 |
end = importer->buckets.data + alloc;
|
|
krh@32
|
355 |
memset(end, 0, importer->buckets.alloc - alloc);
|
|
krh@32
|
356 |
for (b = importer->buckets.data; b < end; b++) {
|
|
krh@6
|
357 |
value = *b;
|
|
krh@6
|
358 |
if (value != 0) {
|
|
krh@6
|
359 |
*b = 0;
|
|
krh@32
|
360 |
do_insert(importer, value);
|
|
krh@6
|
361 |
}
|
|
krh@0
|
362 |
}
|
|
krh@0
|
363 |
}
|
|
krh@0
|
364 |
|
|
krh@32
|
365 |
value = add_to_string_pool(importer->set, key);
|
|
krh@32
|
366 |
do_insert (importer, value);
|
|
krh@0
|
367 |
|
|
krh@0
|
368 |
return value;
|
|
krh@0
|
369 |
}
|
|
krh@0
|
370 |
|
|
krh@30
|
371 |
static unsigned long
|
|
krh@32
|
372 |
razor_importer_tokenize(struct razor_importer *importer, const char *string)
|
|
krh@0
|
373 |
{
|
|
krh@0
|
374 |
unsigned long token;
|
|
krh@0
|
375 |
|
|
krh@13
|
376 |
if (string == NULL)
|
|
krh@32
|
377 |
return razor_importer_tokenize(importer, "");
|
|
krh@13
|
378 |
|
|
krh@32
|
379 |
token = razor_importer_lookup(importer, string);
|
|
krh@0
|
380 |
if (token != 0)
|
|
krh@0
|
381 |
return token;
|
|
krh@0
|
382 |
|
|
krh@32
|
383 |
return razor_importer_insert(importer, string);
|
|
krh@0
|
384 |
}
|
|
krh@0
|
385 |
|
|
krh@27
|
386 |
void
|
|
krh@30
|
387 |
razor_importer_begin_package(struct razor_importer *importer,
|
|
krh@30
|
388 |
const char *name, const char *version)
|
|
krh@13
|
389 |
{
|
|
krh@25
|
390 |
struct razor_package *p;
|
|
krh@13
|
391 |
|
|
krh@30
|
392 |
p = array_add(&importer->set->packages, sizeof *p);
|
|
krh@32
|
393 |
p->name = razor_importer_tokenize(importer, name);
|
|
krh@32
|
394 |
p->version = razor_importer_tokenize(importer, version);
|
|
krh@13
|
395 |
|
|
krh@30
|
396 |
importer->package = p;
|
|
krh@30
|
397 |
array_init(&importer->requires.package);
|
|
krh@30
|
398 |
array_init(&importer->provides.package);
|
|
krh@13
|
399 |
}
|
|
krh@13
|
400 |
|
|
krh@13
|
401 |
void
|
|
krh@30
|
402 |
razor_importer_finish_package(struct razor_importer *importer)
|
|
krh@13
|
403 |
{
|
|
krh@25
|
404 |
struct razor_package *p;
|
|
krh@13
|
405 |
|
|
krh@30
|
406 |
p = importer->package;
|
|
krh@30
|
407 |
p->requires = add_to_property_pool(importer->set,
|
|
krh@30
|
408 |
&importer->requires.package);
|
|
krh@30
|
409 |
p->provides = add_to_property_pool(importer->set,
|
|
krh@30
|
410 |
&importer->provides.package);
|
|
krh@13
|
411 |
|
|
krh@30
|
412 |
array_release(&importer->requires.package);
|
|
krh@30
|
413 |
array_release(&importer->provides.package);
|
|
krh@13
|
414 |
}
|
|
krh@13
|
415 |
|
|
krh@30
|
416 |
static void
|
|
krh@30
|
417 |
razor_importer_add_property(struct razor_importer *importer,
|
|
krh@13
|
418 |
struct import_property_context *pctx,
|
|
krh@13
|
419 |
const char *name, const char *version)
|
|
krh@13
|
420 |
{
|
|
krh@25
|
421 |
struct razor_property *p;
|
|
krh@13
|
422 |
unsigned long *r;
|
|
krh@13
|
423 |
|
|
krh@25
|
424 |
p = array_add(pctx->all, sizeof *p);
|
|
krh@32
|
425 |
p->name = razor_importer_tokenize(importer, name);
|
|
krh@32
|
426 |
p->version = razor_importer_tokenize(importer, version);
|
|
krh@30
|
427 |
p->packages = importer->package -
|
|
krh@30
|
428 |
(struct razor_package *) importer->set->packages.data;
|
|
krh@13
|
429 |
|
|
krh@13
|
430 |
r = array_add(&pctx->package, sizeof *r);
|
|
krh@25
|
431 |
*r = p - (struct razor_property *) pctx->all->data;
|
|
krh@13
|
432 |
}
|
|
krh@13
|
433 |
|
|
krh@27
|
434 |
void
|
|
krh@30
|
435 |
razor_importer_add_requires(struct razor_importer *importer,
|
|
krh@30
|
436 |
const char *name, const char *version)
|
|
krh@9
|
437 |
{
|
|
krh@30
|
438 |
razor_importer_add_property(importer,
|
|
krh@30
|
439 |
&importer->requires, name, version);
|
|
krh@30
|
440 |
}
|
|
krh@30
|
441 |
|
|
krh@30
|
442 |
void
|
|
krh@30
|
443 |
razor_importer_add_provides(struct razor_importer *importer,
|
|
krh@30
|
444 |
const char *name, const char *version)
|
|
krh@30
|
445 |
{
|
|
krh@30
|
446 |
razor_importer_add_property(importer,
|
|
krh@30
|
447 |
&importer->provides, name, version);
|
|
krh@30
|
448 |
}
|
|
krh@30
|
449 |
|
|
krh@30
|
450 |
struct razor_importer *
|
|
krh@30
|
451 |
razor_importer_new(void)
|
|
krh@30
|
452 |
{
|
|
krh@30
|
453 |
struct razor_importer *importer;
|
|
krh@30
|
454 |
|
|
krh@30
|
455 |
importer = zalloc(sizeof *importer);
|
|
krh@30
|
456 |
importer->set = razor_set_create();
|
|
krh@30
|
457 |
importer->requires.all = &importer->set->requires;
|
|
krh@30
|
458 |
importer->provides.all = &importer->set->provides;
|
|
krh@30
|
459 |
|
|
krh@30
|
460 |
return importer;
|
|
krh@9
|
461 |
}
|
|
krh@9
|
462 |
|
|
krh@22
|
463 |
typedef int (*compare_with_data_func_t)(const void *p1,
|
|
krh@22
|
464 |
const void *p,
|
|
krh@22
|
465 |
void *data);
|
|
krh@22
|
466 |
|
|
krh@25
|
467 |
struct qsort_context {
|
|
krh@25
|
468 |
size_t size;
|
|
krh@25
|
469 |
compare_with_data_func_t compare;
|
|
krh@25
|
470 |
void *data;
|
|
krh@25
|
471 |
};
|
|
krh@25
|
472 |
|
|
krh@22
|
473 |
static void
|
|
krh@22
|
474 |
qsort_swap(void *p1, void *p2, size_t size)
|
|
krh@22
|
475 |
{
|
|
krh@22
|
476 |
char buffer[size];
|
|
krh@22
|
477 |
|
|
krh@22
|
478 |
memcpy(buffer, p1, size);
|
|
krh@22
|
479 |
memcpy(p1, p2, size);
|
|
krh@22
|
480 |
memcpy(p2, buffer, size);
|
|
krh@22
|
481 |
}
|
|
krh@22
|
482 |
|
|
krh@25
|
483 |
static void
|
|
krh@25
|
484 |
__qsort_with_data(void *base, size_t nelem, unsigned long *map,
|
|
krh@25
|
485 |
struct qsort_context *ctx)
|
|
krh@22
|
486 |
{
|
|
krh@22
|
487 |
void *p, *start, *end, *pivot;
|
|
krh@25
|
488 |
unsigned long *mp, *mstart, *mend, tmp;
|
|
krh@22
|
489 |
int left, right, result;
|
|
krh@25
|
490 |
size_t size = ctx->size;
|
|
krh@22
|
491 |
|
|
krh@22
|
492 |
p = base;
|
|
krh@22
|
493 |
start = base;
|
|
krh@22
|
494 |
end = base + nelem * size;
|
|
krh@25
|
495 |
mp = map;
|
|
krh@25
|
496 |
mstart = map;
|
|
krh@25
|
497 |
mend = map + nelem;
|
|
krh@22
|
498 |
pivot = base + (random() % nelem) * size;
|
|
krh@25
|
499 |
|
|
krh@22
|
500 |
while (p < end) {
|
|
krh@25
|
501 |
result = ctx->compare(p, pivot, ctx->data);
|
|
krh@22
|
502 |
if (result < 0) {
|
|
krh@22
|
503 |
qsort_swap(p, start, size);
|
|
krh@25
|
504 |
tmp = *mp;
|
|
krh@25
|
505 |
*mp = *mstart;
|
|
krh@25
|
506 |
*mstart = tmp;
|
|
krh@22
|
507 |
if (start == pivot)
|
|
krh@22
|
508 |
pivot = p;
|
|
krh@22
|
509 |
start += size;
|
|
krh@25
|
510 |
mstart++;
|
|
krh@22
|
511 |
p += size;
|
|
krh@29
|
512 |
mp++;
|
|
krh@22
|
513 |
} else if (result == 0) {
|
|
krh@22
|
514 |
p += size;
|
|
krh@25
|
515 |
mp++;
|
|
krh@22
|
516 |
} else {
|
|
krh@22
|
517 |
end -= size;
|
|
krh@25
|
518 |
mend--;
|
|
krh@22
|
519 |
qsort_swap(p, end, size);
|
|
krh@25
|
520 |
tmp = *mp;
|
|
krh@29
|
521 |
*mp = *mend;
|
|
krh@29
|
522 |
*mend = tmp;
|
|
krh@22
|
523 |
if (end == pivot)
|
|
krh@22
|
524 |
pivot = p;
|
|
krh@22
|
525 |
}
|
|
krh@22
|
526 |
}
|
|
krh@22
|
527 |
|
|
krh@22
|
528 |
left = (start - base) / size;
|
|
krh@22
|
529 |
right = (base + nelem * size - end) / size;
|
|
krh@22
|
530 |
if (left > 1)
|
|
krh@25
|
531 |
__qsort_with_data(base, left, map, ctx);
|
|
krh@22
|
532 |
if (right > 1)
|
|
krh@25
|
533 |
__qsort_with_data(end, right, mend, ctx);
|
|
krh@25
|
534 |
}
|
|
krh@25
|
535 |
|
|
krh@25
|
536 |
unsigned long *
|
|
krh@25
|
537 |
qsort_with_data(void *base, size_t nelem, size_t size,
|
|
krh@25
|
538 |
compare_with_data_func_t compare, void *data)
|
|
krh@25
|
539 |
{
|
|
krh@25
|
540 |
struct qsort_context ctx;
|
|
krh@25
|
541 |
unsigned long *map;
|
|
krh@25
|
542 |
int i;
|
|
krh@25
|
543 |
|
|
krh@25
|
544 |
ctx.size = size;
|
|
krh@25
|
545 |
ctx.compare = compare;
|
|
krh@25
|
546 |
ctx.data = data;
|
|
krh@25
|
547 |
|
|
krh@25
|
548 |
map = malloc(nelem * sizeof (unsigned long));
|
|
krh@25
|
549 |
for (i = 0; i < nelem; i++)
|
|
krh@25
|
550 |
map[i] = i;
|
|
krh@25
|
551 |
|
|
krh@25
|
552 |
__qsort_with_data(base, nelem, map, &ctx);
|
|
krh@25
|
553 |
|
|
krh@25
|
554 |
return map;
|
|
krh@22
|
555 |
}
|
|
krh@9
|
556 |
|
|
krh@9
|
557 |
static int
|
|
krh@22
|
558 |
compare_packages(const void *p1, const void *p2, void *data)
|
|
krh@9
|
559 |
{
|
|
krh@25
|
560 |
const struct razor_package *pkg1 = p1, *pkg2 = p2;
|
|
krh@22
|
561 |
struct razor_set *set = data;
|
|
krh@22
|
562 |
char *pool = set->string_pool.data;
|
|
krh@9
|
563 |
|
|
krh@23
|
564 |
if (pkg1->name == pkg2->name)
|
|
krh@23
|
565 |
return 0;
|
|
krh@23
|
566 |
else
|
|
krh@23
|
567 |
return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
|
|
krh@9
|
568 |
}
|
|
krh@9
|
569 |
|
|
krh@9
|
570 |
static int
|
|
krh@22
|
571 |
compare_properties(const void *p1, const void *p2, void *data)
|
|
krh@9
|
572 |
{
|
|
krh@25
|
573 |
const struct razor_property *prop1 = p1, *prop2 = p2;
|
|
krh@22
|
574 |
struct razor_set *set = data;
|
|
krh@22
|
575 |
char *pool = set->string_pool.data;
|
|
krh@9
|
576 |
|
|
krh@23
|
577 |
if (prop1->name == prop2->name)
|
|
krh@12
|
578 |
return strcmp(&pool[prop1->version], &pool[prop2->version]);
|
|
krh@12
|
579 |
else
|
|
krh@23
|
580 |
return strcmp(&pool[prop1->name], &pool[prop2->name]);
|
|
krh@9
|
581 |
}
|
|
krh@9
|
582 |
|
|
krh@10
|
583 |
static unsigned long *
|
|
krh@25
|
584 |
uniqueify_properties(struct razor_set *set, struct array *properties)
|
|
krh@9
|
585 |
{
|
|
krh@25
|
586 |
struct razor_property *rp, *up, *rp_end;
|
|
krh@18
|
587 |
struct array *pkgs, *p;
|
|
krh@25
|
588 |
unsigned long *map, *rmap, *r;
|
|
krh@18
|
589 |
int i, count, unique;
|
|
krh@9
|
590 |
|
|
krh@25
|
591 |
count = properties->size / sizeof(struct razor_property);
|
|
krh@25
|
592 |
map = qsort_with_data(properties->data,
|
|
krh@25
|
593 |
count,
|
|
krh@25
|
594 |
sizeof(struct razor_property),
|
|
krh@25
|
595 |
compare_properties,
|
|
krh@25
|
596 |
set);
|
|
krh@9
|
597 |
|
|
krh@25
|
598 |
rp_end = properties->data + properties->size;
|
|
krh@25
|
599 |
rmap = malloc(count * sizeof *map);
|
|
krh@25
|
600 |
pkgs = zalloc(count * sizeof *pkgs);
|
|
krh@25
|
601 |
for (rp = properties->data, up = rp, i = 0; rp < rp_end; rp++, i++) {
|
|
krh@25
|
602 |
if (rp->name != up->name || rp->version != up->version) {
|
|
krh@25
|
603 |
up++;
|
|
krh@25
|
604 |
up->name = rp->name;
|
|
krh@25
|
605 |
up->version = rp->version;
|
|
krh@10
|
606 |
}
|
|
krh@25
|
607 |
|
|
krh@25
|
608 |
unique = up - (struct razor_property *) properties->data;
|
|
krh@25
|
609 |
rmap[map[i]] = unique;
|
|
krh@25
|
610 |
r = array_add(&pkgs[unique], sizeof *r);
|
|
krh@25
|
611 |
*r = rp->packages;
|
|
krh@10
|
612 |
}
|
|
krh@25
|
613 |
free(map);
|
|
krh@9
|
614 |
|
|
krh@25
|
615 |
up++;
|
|
krh@25
|
616 |
properties->size = (void *) up - properties->data;
|
|
krh@25
|
617 |
rp_end = up;
|
|
krh@25
|
618 |
for (rp = properties->data, p = pkgs; rp < rp_end; rp++, p++) {
|
|
krh@25
|
619 |
rp->packages = add_to_property_pool(set, p);
|
|
krh@25
|
620 |
array_release(p);
|
|
krh@18
|
621 |
}
|
|
krh@18
|
622 |
|
|
krh@18
|
623 |
free(pkgs);
|
|
krh@18
|
624 |
|
|
krh@25
|
625 |
return rmap;
|
|
krh@10
|
626 |
}
|
|
krh@10
|
627 |
|
|
krh@10
|
628 |
static void
|
|
krh@30
|
629 |
remap_package_links(struct razor_importer *importer)
|
|
krh@10
|
630 |
{
|
|
krh@25
|
631 |
struct razor_package *p, *end;
|
|
krh@10
|
632 |
unsigned long *pool, *r;
|
|
krh@10
|
633 |
|
|
krh@30
|
634 |
pool = importer->set->property_pool.data;
|
|
krh@30
|
635 |
end = importer->set->packages.data + importer->set->packages.size;
|
|
krh@30
|
636 |
for (p = importer->set->packages.data; p < end; p++) {
|
|
krh@18
|
637 |
for (r = &pool[p->requires]; ~*r; r++)
|
|
krh@30
|
638 |
*r = importer->requires_map[*r];
|
|
krh@18
|
639 |
for (r = &pool[p->provides]; ~*r; r++)
|
|
krh@30
|
640 |
*r = importer->provides_map[*r];
|
|
krh@9
|
641 |
}
|
|
krh@18
|
642 |
}
|
|
krh@10
|
643 |
|
|
krh@18
|
644 |
static void
|
|
krh@30
|
645 |
remap_property_links(struct razor_importer *importer, unsigned long *map)
|
|
krh@18
|
646 |
{
|
|
krh@18
|
647 |
struct razor_property *p, *end;
|
|
krh@25
|
648 |
struct razor_package *rp;
|
|
krh@25
|
649 |
unsigned long *pool, *r, *rmap;
|
|
krh@18
|
650 |
int i, count;
|
|
krh@18
|
651 |
|
|
krh@30
|
652 |
pool = importer->set->property_pool.data;
|
|
krh@30
|
653 |
count = importer->set->packages.size / sizeof(struct razor_package);
|
|
krh@25
|
654 |
rmap = malloc(count * sizeof *map);
|
|
krh@30
|
655 |
rp = importer->set->packages.data;
|
|
krh@18
|
656 |
for (i = 0; i < count; i++)
|
|
krh@25
|
657 |
rmap[map[i]] = i;
|
|
krh@18
|
658 |
|
|
krh@18
|
659 |
/* FIXME: This will break if we implement package list sharing
|
|
krh@18
|
660 |
* for all properties, since we'll remap those lists more than
|
|
krh@18
|
661 |
* once. We should just have a separate pool for property
|
|
krh@18
|
662 |
* lists and a separate pool for package lists and remap it as
|
|
krh@18
|
663 |
* a flat pool. Right now, as property lists and package
|
|
krh@18
|
664 |
* lists are mixed, we can't do that. */
|
|
krh@18
|
665 |
|
|
krh@30
|
666 |
end = importer->set->requires.data + importer->set->requires.size;
|
|
krh@30
|
667 |
for (p = importer->set->requires.data; p < end; p++)
|
|
krh@18
|
668 |
for (r = &pool[p->packages]; ~*r; r++)
|
|
krh@25
|
669 |
*r = rmap[*r];
|
|
krh@18
|
670 |
|
|
krh@30
|
671 |
end = importer->set->provides.data + importer->set->provides.size;
|
|
krh@30
|
672 |
for (p = importer->set->provides.data; p < end; p++)
|
|
krh@18
|
673 |
for (r = &pool[p->packages]; ~*r; r++)
|
|
krh@25
|
674 |
*r = rmap[*r];
|
|
krh@18
|
675 |
|
|
krh@25
|
676 |
free(rmap);
|
|
krh@9
|
677 |
}
|
|
krh@9
|
678 |
|
|
krh@27
|
679 |
struct razor_set *
|
|
krh@30
|
680 |
razor_importer_finish(struct razor_importer *importer)
|
|
krh@9
|
681 |
{
|
|
krh@30
|
682 |
struct razor_set *set;
|
|
krh@25
|
683 |
unsigned long *map;
|
|
krh@25
|
684 |
int count;
|
|
krh@18
|
685 |
|
|
krh@30
|
686 |
importer->requires_map = uniqueify_properties(importer->set,
|
|
krh@30
|
687 |
importer->requires.all);
|
|
krh@30
|
688 |
importer->provides_map = uniqueify_properties(importer->set,
|
|
krh@30
|
689 |
importer->provides.all);
|
|
krh@30
|
690 |
remap_package_links(importer);
|
|
krh@30
|
691 |
free(importer->requires_map);
|
|
krh@30
|
692 |
free(importer->provides_map);
|
|
krh@25
|
693 |
|
|
krh@30
|
694 |
count = importer->set->packages.size / sizeof(struct razor_package);
|
|
krh@30
|
695 |
map = qsort_with_data(importer->set->packages.data,
|
|
krh@25
|
696 |
count,
|
|
krh@25
|
697 |
sizeof(struct razor_package),
|
|
krh@25
|
698 |
compare_packages,
|
|
krh@30
|
699 |
importer->set);
|
|
krh@30
|
700 |
remap_property_links(importer, map);
|
|
krh@25
|
701 |
free(map);
|
|
krh@13
|
702 |
|
|
krh@30
|
703 |
set = importer->set;
|
|
krh@32
|
704 |
array_release(&importer->buckets);
|
|
krh@30
|
705 |
free(importer);
|
|
krh@30
|
706 |
|
|
krh@30
|
707 |
return set;
|
|
krh@9
|
708 |
}
|
|
krh@9
|
709 |
|
|
krh@0
|
710 |
void
|
|
krh@4
|
711 |
razor_set_list(struct razor_set *set)
|
|
krh@3
|
712 |
{
|
|
krh@6
|
713 |
struct razor_package *p, *end;
|
|
krh@6
|
714 |
char *pool;
|
|
krh@3
|
715 |
|
|
krh@6
|
716 |
pool = set->string_pool.data;
|
|
krh@6
|
717 |
end = set->packages.data + set->packages.size;
|
|
krh@14
|
718 |
for (p = set->packages.data; p < end; p++)
|
|
krh@6
|
719 |
printf("%s %s\n", &pool[p->name], &pool[p->version]);
|
|
krh@3
|
720 |
}
|
|
krh@3
|
721 |
|
|
krh@16
|
722 |
struct razor_set *bsearch_set;
|
|
krh@16
|
723 |
|
|
krh@16
|
724 |
static int
|
|
krh@16
|
725 |
compare_package_name(const void *key, const void *data)
|
|
krh@16
|
726 |
{
|
|
krh@16
|
727 |
const struct razor_package *p = data;
|
|
krh@16
|
728 |
char *pool;
|
|
krh@16
|
729 |
|
|
krh@16
|
730 |
pool = bsearch_set->string_pool.data;
|
|
krh@16
|
731 |
|
|
krh@16
|
732 |
return strcmp(key, &pool[p->name]);
|
|
krh@16
|
733 |
}
|
|
krh@16
|
734 |
|
|
krh@10
|
735 |
struct razor_package *
|
|
krh@10
|
736 |
razor_set_get_package(struct razor_set *set, const char *package)
|
|
krh@10
|
737 |
{
|
|
krh@16
|
738 |
bsearch_set = set;
|
|
krh@16
|
739 |
return bsearch(package, set->packages.data,
|
|
krh@16
|
740 |
set->packages.size / sizeof(struct razor_package),
|
|
krh@16
|
741 |
sizeof(struct razor_package), compare_package_name);
|
|
krh@10
|
742 |
}
|
|
krh@10
|
743 |
|
|
krh@18
|
744 |
static int
|
|
krh@18
|
745 |
compare_property_name(const void *key, const void *data)
|
|
krh@18
|
746 |
{
|
|
krh@18
|
747 |
const struct razor_property *p = data;
|
|
krh@18
|
748 |
char *pool;
|
|
krh@18
|
749 |
|
|
krh@18
|
750 |
pool = bsearch_set->string_pool.data;
|
|
krh@18
|
751 |
|
|
krh@18
|
752 |
return strcmp(key, &pool[p->name]);
|
|
krh@18
|
753 |
}
|
|
krh@18
|
754 |
|
|
krh@18
|
755 |
struct razor_property *
|
|
krh@18
|
756 |
razor_set_get_property(struct razor_set *set,
|
|
krh@18
|
757 |
struct array *properties,
|
|
krh@18
|
758 |
const char *property)
|
|
krh@18
|
759 |
{
|
|
krh@18
|
760 |
struct razor_property *p, *start;
|
|
krh@18
|
761 |
|
|
krh@18
|
762 |
bsearch_set = set;
|
|
krh@18
|
763 |
p = bsearch(property, properties->data,
|
|
krh@18
|
764 |
properties->size / sizeof(struct razor_property),
|
|
krh@18
|
765 |
sizeof(struct razor_property), compare_property_name);
|
|
krh@18
|
766 |
|
|
krh@18
|
767 |
start = properties->data;
|
|
krh@18
|
768 |
while (p > start && (p - 1)->name == p->name)
|
|
krh@18
|
769 |
p--;
|
|
krh@18
|
770 |
|
|
krh@18
|
771 |
return p;
|
|
krh@18
|
772 |
}
|
|
krh@18
|
773 |
|
|
krh@10
|
774 |
static void
|
|
krh@10
|
775 |
razor_set_list_all_properties(struct razor_set *set, struct array *properties)
|
|
krh@8
|
776 |
{
|
|
krh@8
|
777 |
struct razor_property *p, *end;
|
|
krh@8
|
778 |
char *pool;
|
|
krh@8
|
779 |
|
|
krh@8
|
780 |
pool = set->string_pool.data;
|
|
krh@10
|
781 |
end = properties->data + properties->size;
|
|
krh@14
|
782 |
for (p = properties->data; p < end; p++)
|
|
krh@8
|
783 |
printf("%s %s\n", &pool[p->name], &pool[p->version]);
|
|
krh@8
|
784 |
}
|
|
krh@8
|
785 |
|
|
krh@8
|
786 |
void
|
|
krh@10
|
787 |
razor_set_list_requires(struct razor_set *set, const char *name)
|
|
krh@7
|
788 |
{
|
|
krh@10
|
789 |
struct razor_property *p, *requires;
|
|
krh@10
|
790 |
struct razor_package *package;
|
|
krh@10
|
791 |
unsigned long *r;
|
|
krh@7
|
792 |
char *pool;
|
|
krh@7
|
793 |
|
|
krh@10
|
794 |
if (name) {
|
|
krh@10
|
795 |
package = razor_set_get_package(set, name);
|
|
krh@10
|
796 |
r = (unsigned long *) set->property_pool.data +
|
|
krh@10
|
797 |
package->requires;
|
|
krh@10
|
798 |
requires = set->requires.data;
|
|
krh@10
|
799 |
pool = set->string_pool.data;
|
|
krh@18
|
800 |
while (~*r) {
|
|
krh@10
|
801 |
p = &requires[*r++];
|
|
krh@10
|
802 |
printf("%s %s\n", &pool[p->name], &pool[p->version]);
|
|
krh@10
|
803 |
}
|
|
krh@10
|
804 |
} else
|
|
krh@10
|
805 |
razor_set_list_all_properties(set, &set->requires);
|
|
krh@10
|
806 |
}
|
|
krh@10
|
807 |
|
|
krh@10
|
808 |
void
|
|
krh@10
|
809 |
razor_set_list_provides(struct razor_set *set, const char *name)
|
|
krh@10
|
810 |
{
|
|
krh@10
|
811 |
struct razor_property *p, *provides;
|
|
krh@10
|
812 |
struct razor_package *package;
|
|
krh@10
|
813 |
unsigned long *r;
|
|
krh@10
|
814 |
char *pool;
|
|
krh@10
|
815 |
|
|
krh@10
|
816 |
if (name) {
|
|
krh@10
|
817 |
package = razor_set_get_package(set, name);
|
|
krh@10
|
818 |
r = (unsigned long *) set->property_pool.data +
|
|
krh@10
|
819 |
package->provides;
|
|
krh@10
|
820 |
provides = set->provides.data;
|
|
krh@10
|
821 |
pool = set->string_pool.data;
|
|
krh@18
|
822 |
while (~*r) {
|
|
krh@10
|
823 |
p = &provides[*r++];
|
|
krh@10
|
824 |
printf("%s %s\n", &pool[p->name], &pool[p->version]);
|
|
krh@10
|
825 |
}
|
|
krh@10
|
826 |
} else
|
|
krh@10
|
827 |
razor_set_list_all_properties(set, &set->provides);
|
|
krh@7
|
828 |
}
|
|
krh@7
|
829 |
|
|
krh@7
|
830 |
void
|
|
krh@18
|
831 |
razor_set_list_property_packages(struct razor_set *set,
|
|
krh@18
|
832 |
struct array *properties,
|
|
krh@20
|
833 |
const char *name,
|
|
krh@20
|
834 |
const char *version)
|
|
krh@18
|
835 |
{
|
|
krh@18
|
836 |
struct razor_property *property, *end;
|
|
krh@18
|
837 |
struct razor_package *p, *packages;
|
|
krh@18
|
838 |
unsigned long *r;
|
|
krh@18
|
839 |
char *pool;
|
|
krh@18
|
840 |
|
|
krh@18
|
841 |
if (name == NULL)
|
|
krh@18
|
842 |
return;
|
|
krh@18
|
843 |
|
|
krh@18
|
844 |
property = razor_set_get_property(set, properties, name);
|
|
krh@18
|
845 |
packages = set->packages.data;
|
|
krh@18
|
846 |
pool = set->string_pool.data;
|
|
krh@18
|
847 |
end = properties->data + properties->size;
|
|
krh@18
|
848 |
while (property < end && strcmp(name, &pool[property->name]) == 0) {
|
|
krh@20
|
849 |
if (version && strcmp(version, &pool[property->version]) != 0)
|
|
krh@20
|
850 |
goto next;
|
|
krh@18
|
851 |
r = (unsigned long *)
|
|
krh@18
|
852 |
set->property_pool.data + property->packages;
|
|
krh@18
|
853 |
while (~*r) {
|
|
krh@18
|
854 |
p = &packages[*r++];
|
|
krh@18
|
855 |
printf("%s %s\n",
|
|
krh@18
|
856 |
&pool[p->name], &pool[p->version]);
|
|
krh@18
|
857 |
}
|
|
krh@20
|
858 |
next:
|
|
krh@18
|
859 |
property++;
|
|
krh@18
|
860 |
}
|
|
krh@18
|
861 |
}
|
|
krh@18
|
862 |
|
|
krh@18
|
863 |
void
|
|
krh@21
|
864 |
razor_set_validate(struct razor_set *set, struct array *unsatisfied)
|
|
krh@21
|
865 |
{
|
|
krh@21
|
866 |
struct razor_property *r, *p, *rend, *pend;
|
|
krh@21
|
867 |
unsigned long *u;
|
|
krh@21
|
868 |
char *pool;
|
|
krh@21
|
869 |
|
|
krh@21
|
870 |
r = set->requires.data;
|
|
krh@21
|
871 |
p = set->provides.data;
|
|
krh@21
|
872 |
rend = set->requires.data + set->requires.size;
|
|
krh@21
|
873 |
pend = set->provides.data + set->provides.size;
|
|
krh@21
|
874 |
pool = set->string_pool.data;
|
|
krh@21
|
875 |
|
|
krh@21
|
876 |
while (r < rend) {
|
|
krh@21
|
877 |
while (p < pend && strcmp(&pool[r->name], &pool[p->name]) > 0)
|
|
krh@21
|
878 |
p++;
|
|
krh@21
|
879 |
if (p == pend || strcmp(&pool[r->name], &pool[p->name]) != 0) {
|
|
krh@21
|
880 |
u = array_add(unsatisfied, sizeof *u);
|
|
krh@21
|
881 |
*u = r - (struct razor_property *) set->requires.data;
|
|
krh@21
|
882 |
}
|
|
krh@21
|
883 |
r++;
|
|
krh@21
|
884 |
}
|
|
krh@21
|
885 |
}
|
|
krh@21
|
886 |
|
|
krh@21
|
887 |
void
|
|
krh@21
|
888 |
razor_set_list_unsatisfied(struct razor_set *set)
|
|
krh@21
|
889 |
{
|
|
krh@21
|
890 |
struct array unsatisfied;
|
|
krh@21
|
891 |
struct razor_property *requires, *r;
|
|
krh@21
|
892 |
unsigned long *u, *end;
|
|
krh@21
|
893 |
char *pool;
|
|
krh@21
|
894 |
|
|
krh@21
|
895 |
array_init(&unsatisfied);
|
|
krh@21
|
896 |
razor_set_validate(set, &unsatisfied);
|
|
krh@21
|
897 |
|
|
krh@21
|
898 |
end = unsatisfied.data + unsatisfied.size;
|
|
krh@21
|
899 |
requires = set->requires.data;
|
|
krh@21
|
900 |
pool = set->string_pool.data;
|
|
krh@21
|
901 |
|
|
krh@21
|
902 |
for (u = unsatisfied.data; u < end; u++) {
|
|
krh@21
|
903 |
r = requires + *u;
|
|
krh@21
|
904 |
printf("%s %s not satisfied\n",
|
|
krh@21
|
905 |
&pool[r->name], &pool[r->version]);
|
|
krh@21
|
906 |
}
|
|
krh@21
|
907 |
|
|
krh@21
|
908 |
array_release(&unsatisfied);
|
|
krh@21
|
909 |
}
|
|
krh@21
|
910 |
|
|
krh@21
|
911 |
void
|
|
krh@4
|
912 |
razor_set_info(struct razor_set *set)
|
|
krh@3
|
913 |
{
|
|
krh@3
|
914 |
unsigned int offset, size;
|
|
krh@3
|
915 |
int i;
|
|
krh@3
|
916 |
|
|
krh@4
|
917 |
for (i = 0; i < set->header->sections[i].type; i++) {
|
|
krh@4
|
918 |
offset = set->header->sections[i].offset;
|
|
krh@18
|
919 |
size = set->header->sections[i].size;
|
|
krh@3
|
920 |
|
|
krh@4
|
921 |
switch (set->header->sections[i].type) {
|
|
krh@4
|
922 |
case RAZOR_PACKAGES:
|
|
krh@3
|
923 |
printf("package section:\t%dkb\n", size / 1024);
|
|
krh@3
|
924 |
break;
|
|
krh@8
|
925 |
case RAZOR_REQUIRES:
|
|
krh@8
|
926 |
printf("requires section:\t%dkb\n", size / 1024);
|
|
krh@8
|
927 |
break;
|
|
krh@7
|
928 |
case RAZOR_PROVIDES:
|
|
krh@7
|
929 |
printf("provides section:\t%dkb\n", size / 1024);
|
|
krh@7
|
930 |
break;
|
|
krh@19
|
931 |
case RAZOR_STRING_POOL:
|
|
krh@19
|
932 |
printf("string pool:\t\t%dkb\n", size / 1024);
|
|
krh@19
|
933 |
break;
|
|
krh@19
|
934 |
case RAZOR_PROPERTY_POOL:
|
|
krh@18
|
935 |
printf("properties section:\t%dkb\n", size / 1024);
|
|
krh@18
|
936 |
break;
|
|
krh@3
|
937 |
}
|
|
krh@3
|
938 |
}
|
|
krh@0
|
939 |
}
|
|
krh@0
|
940 |
|
|
krh@0
|
941 |
static int
|
|
krh@0
|
942 |
usage(void)
|
|
krh@0
|
943 |
{
|
|
krh@7
|
944 |
printf("usage: razor [ import FILES | lookup <key> | "
|
|
krh@15
|
945 |
"list | list-requires | list-provides | eat-yum | info ]\n");
|
|
krh@0
|
946 |
exit(1);
|
|
krh@0
|
947 |
}
|
|
krh@0
|
948 |
|
|
krh@15
|
949 |
static const char *repo_filename = "system.repo";
|
|
krh@15
|
950 |
static const char rawhide_repo_filename[] = "rawhide.repo";
|
|
krh@0
|
951 |
|
|
krh@0
|
952 |
int
|
|
krh@27
|
953 |
main(int argc, const char *argv[])
|
|
krh@0
|
954 |
{
|
|
krh@4
|
955 |
struct razor_set *set;
|
|
krh@0
|
956 |
struct stat statbuf;
|
|
krh@15
|
957 |
char *repo;
|
|
krh@15
|
958 |
|
|
krh@15
|
959 |
repo = getenv("RAZOR_REPO");
|
|
krh@15
|
960 |
if (repo != NULL)
|
|
krh@15
|
961 |
repo_filename = repo;
|
|
krh@0
|
962 |
|
|
krh@3
|
963 |
if (argc < 2) {
|
|
krh@0
|
964 |
usage();
|
|
krh@0
|
965 |
} else if (strcmp(argv[1], "import") == 0) {
|
|
krh@0
|
966 |
if (stat("set", &statbuf) && mkdir("set", 0777)) {
|
|
krh@0
|
967 |
fprintf(stderr, "could not create directory 'set'\n");
|
|
krh@0
|
968 |
exit(-1);
|
|
krh@0
|
969 |
}
|
|
krh@0
|
970 |
|
|
krh@27
|
971 |
set = razor_import_rzr_files(argc - 2, argv + 2);
|
|
krh@6
|
972 |
|
|
krh@6
|
973 |
printf("pool size: %d\n", set->string_pool.size);
|
|
krh@6
|
974 |
printf("pool allocation: %d\n", set->string_pool.alloc);
|
|
krh@7
|
975 |
printf("packages: %d\n",
|
|
krh@7
|
976 |
set->packages.size / sizeof(struct razor_package));
|
|
krh@8
|
977 |
printf("requires: %d\n",
|
|
krh@8
|
978 |
set->requires.size / sizeof(struct razor_property));
|
|
krh@7
|
979 |
printf("provides: %d\n",
|
|
krh@8
|
980 |
set->provides.size / sizeof(struct razor_property));
|
|
krh@0
|
981 |
|
|
krh@4
|
982 |
razor_set_write(set, repo_filename);
|
|
krh@0
|
983 |
|
|
krh@4
|
984 |
razor_set_destroy(set);
|
|
krh@3
|
985 |
} else if (strcmp(argv[1], "list") == 0) {
|
|
krh@4
|
986 |
set = razor_set_open(repo_filename);
|
|
krh@4
|
987 |
razor_set_list(set);
|
|
krh@4
|
988 |
razor_set_destroy(set);
|
|
krh@8
|
989 |
} else if (strcmp(argv[1], "list-requires") == 0) {
|
|
krh@8
|
990 |
set = razor_set_open(repo_filename);
|
|
krh@10
|
991 |
razor_set_list_requires(set, argv[2]);
|
|
krh@8
|
992 |
razor_set_destroy(set);
|
|
krh@7
|
993 |
} else if (strcmp(argv[1], "list-provides") == 0) {
|
|
krh@7
|
994 |
set = razor_set_open(repo_filename);
|
|
krh@10
|
995 |
razor_set_list_provides(set, argv[2]);
|
|
krh@7
|
996 |
razor_set_destroy(set);
|
|
krh@18
|
997 |
} else if (strcmp(argv[1], "what-requires") == 0) {
|
|
krh@18
|
998 |
set = razor_set_open(repo_filename);
|
|
krh@20
|
999 |
razor_set_list_property_packages(set, &set->requires,
|
|
krh@20
|
1000 |
argv[2], argv[3]);
|
|
krh@18
|
1001 |
razor_set_destroy(set);
|
|
krh@18
|
1002 |
} else if (strcmp(argv[1], "what-provides") == 0) {
|
|
krh@18
|
1003 |
set = razor_set_open(repo_filename);
|
|
krh@20
|
1004 |
razor_set_list_property_packages(set, &set->provides,
|
|
krh@20
|
1005 |
argv[2], argv[3]);
|
|
krh@18
|
1006 |
razor_set_destroy(set);
|
|
krh@3
|
1007 |
} else if (strcmp(argv[1], "info") == 0) {
|
|
krh@4
|
1008 |
set = razor_set_open(repo_filename);
|
|
krh@4
|
1009 |
razor_set_info(set);
|
|
krh@4
|
1010 |
razor_set_destroy(set);
|
|
krh@15
|
1011 |
} else if (strcmp(argv[1], "eat-yum") == 0) {
|
|
krh@15
|
1012 |
set = razor_set_create_from_yum_filelist(STDIN_FILENO);
|
|
krh@15
|
1013 |
if (set == NULL)
|
|
krh@15
|
1014 |
return 1;
|
|
krh@15
|
1015 |
razor_set_write(set, rawhide_repo_filename);
|
|
krh@15
|
1016 |
razor_set_destroy(set);
|
|
krh@18
|
1017 |
printf("wrote %s\n", rawhide_repo_filename);
|
|
krh@28
|
1018 |
} else if (strcmp(argv[1], "import-rpmdb") == 0) {
|
|
krh@28
|
1019 |
set = razor_set_create_from_rpmdb();
|
|
krh@28
|
1020 |
if (set == NULL)
|
|
krh@28
|
1021 |
return 1;
|
|
krh@28
|
1022 |
razor_set_write(set, repo_filename);
|
|
krh@28
|
1023 |
razor_set_destroy(set);
|
|
krh@28
|
1024 |
printf("wrote %s\n", repo_filename);
|
|
krh@21
|
1025 |
} else if (strcmp(argv[1], "validate") == 0) {
|
|
krh@21
|
1026 |
set = razor_set_open(repo_filename);
|
|
krh@21
|
1027 |
if (set == NULL)
|
|
krh@21
|
1028 |
return 1;
|
|
krh@21
|
1029 |
razor_set_list_unsatisfied(set);
|
|
krh@21
|
1030 |
razor_set_destroy(set);
|
|
krh@0
|
1031 |
} else {
|
|
krh@0
|
1032 |
usage();
|
|
krh@0
|
1033 |
}
|
|
krh@0
|
1034 |
|
|
krh@0
|
1035 |
return 0;
|
|
krh@0
|
1036 |
}
|