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