Factor out array code.
19 array_add(struct array *array, int size)
29 while (alloc < array->size + size)
32 if (array->alloc < alloc) {
33 data = realloc(array->data, alloc);
40 p = array->data + array->size;
47 write_to_fd(int fd, void *p, size_t size)
53 len = write(fd, p, rest);
63 write_to_file(const char *filename, void *p, size_t size)
67 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
70 err = write_to_fd(fd, p, size);
87 struct razor_set_header {
90 struct { unsigned int type, offset; } sections[0];
93 #define RAZOR_MAGIC 0x7a7a7a7a
94 #define RAZOR_VERSION 1
95 #define RAZOR_BUCKETS 1
96 #define RAZOR_STRINGS 2
97 #define RAZOR_PACKAGES 3
99 struct razor_package {
101 unsigned long version;
105 struct array buckets;
106 struct array string_pool;
107 struct array packages;
108 struct razor_set_header *header;
112 razor_set_create(void)
114 struct razor_set *set;
117 set = zalloc(sizeof(struct razor_set));
118 p = array_add(&set->string_pool, 1);
125 razor_set_open(const char *filename)
127 struct razor_set *set;
129 unsigned int size, offset;
132 set = zalloc(sizeof *set);
133 fd = open(filename, O_RDONLY);
134 if (fstat(fd, &stat) < 0)
136 set->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
137 if (set->header == MAP_FAILED) {
142 for (i = 0; i < set->header->sections[i].type; i++) {
143 offset = set->header->sections[i].offset;
144 size = set->header->sections[i + 1].offset - offset;
146 switch (set->header->sections[i].type) {
148 set->buckets.data = (void *) set->header + offset;
149 set->buckets.size = size;
150 set->buckets.alloc = size;
153 set->string_pool.data = (void *) set->header + offset;
154 set->string_pool.size = size;
155 set->string_pool.alloc = size;
158 set->packages.data = (void *) set->header + offset;
159 set->packages.size = size;
160 set->packages.size = size;
170 razor_set_destroy(struct razor_set *set)
176 for (i = 0; set->header->sections[i].type; i++)
178 size = set->header->sections[i].type;
179 munmap(set->header, size);
181 free(set->buckets.data);
182 free(set->string_pool.data);
183 free(set->packages.data);
190 razor_set_write(struct razor_set *set, const char *filename)
193 struct razor_set_header *header = (struct razor_set_header *) data;
194 int fd, pool_size, packages_size;
196 /* Align these to pages sizes */
197 pool_size = (set->string_pool.size + 4095) & ~4095;
198 packages_size = (set->packages.size + 4095) & ~4095;
200 memset(data, 0, sizeof data);
201 header->magic = RAZOR_MAGIC;
202 header->version = RAZOR_VERSION;
204 header->sections[0].type = RAZOR_BUCKETS;
205 header->sections[0].offset = sizeof data;
207 header->sections[1].type = RAZOR_STRINGS;
208 header->sections[1].offset =
209 header->sections[0].offset + set->buckets.alloc;
211 header->sections[2].type = RAZOR_PACKAGES;
212 header->sections[2].offset =
213 header->sections[1].offset + pool_size;
215 header->sections[3].type = 0;
216 header->sections[3].offset =
217 header->sections[2].offset + packages_size;
219 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
223 write_to_fd(fd, data, sizeof data);
224 write_to_fd(fd, set->buckets.data, set->buckets.alloc);
225 write_to_fd(fd, set->string_pool.data, pool_size);
226 write_to_fd(fd, set->packages.data, packages_size);
232 hash_string(const char *key)
235 unsigned int hash = 0;
237 for (p = key; *p; p++)
238 hash = (hash << 2) ^ *p;
244 razor_set_lookup(struct razor_set *set, const char *key)
246 unsigned int mask, start, i;
250 pool = set->string_pool.data;
251 mask = set->buckets.alloc - 1;
252 start = hash_string(key) * sizeof(unsigned long);
254 for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
255 b = set->buckets.data + ((start + i) & mask);
260 if (strcmp(key, &pool[*b]) == 0)
268 add_to_string_pool(struct razor_set *set, const char *key)
273 len = strlen(key) + 1;
274 p = array_add(&set->string_pool, len);
277 return p - (char *) set->string_pool.data;
281 do_insert(struct razor_set *set, unsigned long value)
283 unsigned int mask, start, i;
287 key = (char *) set->string_pool.data + value;
288 mask = set->buckets.alloc - 1;
289 start = hash_string(key) * sizeof(unsigned long);
291 for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
292 b = set->buckets.data + ((start + i) & mask);
301 razor_set_insert(struct razor_set *set, const char *key)
303 unsigned long value, *buckets, *b, *end;
306 alloc = set->buckets.alloc;
307 array_add(&set->buckets, 4 * sizeof *buckets);
308 if (alloc != set->buckets.alloc) {
309 end = set->buckets.data + alloc;
310 memset(end, 0, set->buckets.alloc - alloc);
311 for (b = set->buckets.data; b < end; b++) {
315 do_insert(set, value);
320 value = add_to_string_pool(set, key);
321 do_insert (set, value);
327 razor_set_add_package(struct razor_set *set,
328 unsigned long name, unsigned long version)
330 struct razor_package *p;
332 p = array_add(&set->packages, sizeof *p);
335 p->version = version;
337 return p - (struct razor_package *) set->packages.data;
341 razor_set_tokenize(struct razor_set *set, const char *string)
345 token = razor_set_lookup(set, string);
349 return razor_set_insert(set, string);
352 static struct razor_set *qsort_set;
355 compare_packages(const void *p1, const void *p2)
357 const struct razor_package *pkg1 = p1, *pkg2 = p2;
358 char *pool = qsort_set->string_pool.data;
360 return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
364 razor_set_sort(struct razor_set *set)
367 qsort(set->packages.data,
368 set->packages.size / sizeof(struct razor_package),
369 sizeof(struct razor_package), compare_packages);
372 struct parsing_context {
373 struct razor_set *set;
378 parse_package(struct parsing_context *ctx, const char **atts)
380 unsigned long name, version;
383 for (i = 0; atts[i]; i += 2) {
384 if (strcmp(atts[i], "name") == 0)
385 name = razor_set_tokenize(ctx->set, atts[i + 1]);
386 else if (strcmp(atts[i], "version") == 0)
387 version = razor_set_tokenize(ctx->set, atts[i + 1]);
390 if (name == 0 || version == 0) {
391 fprintf(stderr, "invalid package tag, "
392 "missing name or version attributes\n");
396 ctx->pkg_id = razor_set_add_package(ctx->set, name, version);
402 start_element(void *data, const char *name, const char **atts)
404 struct parsing_context *ctx = data;
407 if (strcmp(name, "package") == 0)
408 parse_package(ctx, atts);
410 for (i = 0; atts[i]; i += 2)
411 razor_set_tokenize(ctx->set, atts[i + 1]);
415 end_element (void *data, const char *name)
417 struct parsing_context *ctx = data;
419 if (strcmp(name, "package") == 0)
424 sha1_to_hex(const unsigned char *sha1)
427 static char hexbuffer[4][50];
428 static const char hex[] = "0123456789abcdef";
429 char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
432 for (i = 0; i < 20; i++) {
433 unsigned int val = *sha1++;
434 *buf++ = hex[val >> 4];
435 *buf++ = hex[val & 0xf];
443 razor_set_import(struct razor_set *set, const char *filename)
447 struct parsing_context ctx;
452 unsigned char hash[20];
454 fd = open(filename, O_RDONLY);
455 if (fstat(fd, &stat) < 0)
457 p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
461 parser = XML_ParserCreate(NULL);
463 XML_SetUserData(parser, &ctx);
464 XML_SetElementHandler(parser, start_element, end_element);
465 if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
467 "%s at line %d, %s\n",
468 XML_ErrorString(XML_GetErrorCode(parser)),
469 XML_GetCurrentLineNumber(parser),
474 XML_ParserFree(parser);
477 SHA1_Update(&sha1, p, stat.st_size);
478 SHA1_Final(hash, &sha1);
482 snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
483 if (write_to_file(buf, p, stat.st_size) < 0)
485 munmap(p, stat.st_size);
491 razor_set_list(struct razor_set *set)
493 struct razor_package *p, *end;
496 pool = set->string_pool.data;
497 end = set->packages.data + set->packages.size;
498 for (p = set->packages.data; p < end && p->name; p++)
499 printf("%s %s\n", &pool[p->name], &pool[p->version]);
503 razor_set_info(struct razor_set *set)
505 unsigned int offset, size;
508 for (i = 0; i < set->header->sections[i].type; i++) {
509 offset = set->header->sections[i].offset;
510 size = set->header->sections[i + 1].offset - offset;
512 switch (set->header->sections[i].type) {
514 printf("bucket section:\t\t%dkb\n", size / 1024);
517 printf("string pool:\t\t%dkb\n", size / 1024);
520 printf("package section:\t%dkb\n", size / 1024);
529 printf("usage: razor [ import FILES | lookup <key> | list | info ]\n");
533 static const char repo_filename[] = "system.repo";
536 main(int argc, char *argv[])
539 struct razor_set *set;
544 } else if (strcmp(argv[1], "import") == 0) {
545 if (stat("set", &statbuf) && mkdir("set", 0777)) {
546 fprintf(stderr, "could not create directory 'set'\n");
550 set = razor_set_create();
552 for (i = 2; i < argc; i++) {
553 if (razor_set_import(set, argv[i]) < 0) {
554 fprintf(stderr, "failed to import %s\n",
562 /* FIXME: We add a sentinel package here, but we
563 * should probably just have a size field in the
565 razor_set_add_package(set, 0, 0);
567 printf("bucket allocation: %d\n", set->buckets.alloc);
568 printf("pool size: %d\n", set->string_pool.size);
569 printf("pool allocation: %d\n", set->string_pool.alloc);
571 razor_set_write(set, repo_filename);
573 razor_set_destroy(set);
574 } else if (strcmp(argv[1], "lookup") == 0) {
575 set = razor_set_open(repo_filename);
576 printf("%s is %lu\n", argv[2],
577 razor_set_lookup(set, argv[2]));
578 razor_set_destroy(set);
579 } else if (strcmp(argv[1], "list") == 0) {
580 set = razor_set_open(repo_filename);
582 razor_set_destroy(set);
583 } else if (strcmp(argv[1], "info") == 0) {
584 set = razor_set_open(repo_filename);
586 razor_set_destroy(set);