Include winsock.h rather than arpa/inet.h on mingw platforms.
Also fix a trivial int/long mismatch.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/types.h>
37 #include "razor-internal.h"
51 struct razor_set_section_index {
56 struct razor_set_section_index razor_sections[] = {
57 { RAZOR_STRING_POOL, offsetof(struct razor_set, string_pool) },
58 { RAZOR_PACKAGES, offsetof(struct razor_set, packages) },
59 { RAZOR_PROPERTIES, offsetof(struct razor_set, properties) },
60 { RAZOR_PACKAGE_POOL, offsetof(struct razor_set, package_pool) },
61 { RAZOR_PROPERTY_POOL, offsetof(struct razor_set, property_pool) },
64 struct razor_set_section_index razor_files_sections[] = {
65 { RAZOR_FILES, offsetof(struct razor_set, files) },
66 { RAZOR_FILE_POOL, offsetof(struct razor_set, file_pool) },
67 { RAZOR_FILE_STRING_POOL, offsetof(struct razor_set, file_string_pool) },
70 struct razor_set_section_index razor_details_sections[] = {
71 { RAZOR_DETAILS_STRING_POOL, offsetof(struct razor_set, details_string_pool) },
74 RAZOR_EXPORT struct razor_set *
75 razor_set_create(void)
77 struct razor_set *set;
78 struct razor_entry *e;
81 set = zalloc(sizeof *set);
83 e = array_add(&set->files, sizeof *e);
84 empty = array_add(&set->string_pool, 1);
87 e->flags = RAZOR_ENTRY_LAST;
89 list_set_empty(&e->packages);
95 razor_set_bind_sections(struct razor_set *set,
96 struct razor_set_header **header,
98 struct razor_set_section_index section_index[],
99 int section_index_size,
100 const char *filename)
102 struct razor_set_section *s, *sections;
107 *header = razor_file_get_contents(filename, header_size);
111 sections = (void *) *header + sizeof **header;
112 pool = (void *) sections + (*header)->num_sections * sizeof *sections;
114 for (i = 0; i < (*header)->num_sections; i++) {
117 for (j = 0; j < section_index_size; j++)
118 if (!strcmp(section_index[j].name,
121 if (j == section_index_size)
123 array = (void *) set + section_index[j].offset;
124 array->data = (void *) *header + s->offset;
125 array->size = s->size;
126 array->alloc = s->size;
132 RAZOR_EXPORT struct razor_set *
133 razor_set_open(const char *filename)
135 struct razor_set *set;
137 set = zalloc(sizeof *set);
138 if (razor_set_bind_sections(set, &set->header, &set->header_size,
139 razor_sections, ARRAY_SIZE(razor_sections),
148 razor_set_open_details(struct razor_set *set, const char *filename)
150 return razor_set_bind_sections(set, &set->details_header,
151 &set->details_header_size,
152 razor_details_sections,
153 ARRAY_SIZE(razor_details_sections),
158 razor_set_open_files(struct razor_set *set, const char *filename)
160 return razor_set_bind_sections(set, &set->files_header,
161 &set->files_header_size,
162 razor_files_sections,
163 ARRAY_SIZE(razor_files_sections),
168 razor_set_destroy(struct razor_set *set)
173 assert (set != NULL);
176 razor_file_free_contents(set->header, set->header_size);
178 for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
179 a = (void *) set + razor_sections[i].offset;
184 if (set->details_header) {
185 razor_file_free_contents(set->details_header,
186 set->details_header_size);
188 for (i = 0; i < ARRAY_SIZE(razor_details_sections); i++) {
189 a = (void *) set + razor_details_sections[i].offset;
194 if (set->files_header) {
195 razor_file_free_contents(set->files_header,
196 set->files_header_size);
198 for (i = 0; i < ARRAY_SIZE(razor_files_sections); i++) {
199 a = (void *) set + razor_files_sections[i].offset;
208 razor_set_write_sections_to_fd(struct razor_set *set, int fd,
209 struct razor_set_section_index *sections,
212 struct razor_set_header header;
213 struct razor_set_section *out_sections =
214 malloc(array_size * sizeof *out_sections);
215 struct hashtable table;
216 struct array *a, pool;
220 header.magic = RAZOR_MAGIC;
221 header.version = RAZOR_VERSION;
222 header.num_sections = array_size;
223 offset = sizeof header + array_size * sizeof *out_sections;
226 hashtable_init(&table, &pool);
228 for (i = 0; i < array_size; i++)
229 out_sections[i].name =
230 hashtable_tokenize(&table, sections[i].name);
234 for (i = 0; i < array_size; i++) {
235 a = (void *) set + sections[i].offset;
236 out_sections[i].offset = offset;
237 out_sections[i].size = a->size;
241 razor_write(fd, &header, sizeof header);
242 razor_write(fd, out_sections, array_size * sizeof *out_sections);
243 razor_write(fd, pool.data, pool.size);
245 for (i = 0; i < array_size; i++) {
246 a = (void *) set + sections[i].offset;
247 razor_write(fd, a->data, a->size);
256 razor_set_write_to_fd(struct razor_set *set, int fd,
257 enum razor_repo_file_type type)
260 case RAZOR_REPO_FILE_MAIN:
261 return razor_set_write_sections_to_fd(set, fd,
263 ARRAY_SIZE(razor_sections));
265 case RAZOR_REPO_FILE_DETAILS:
266 return razor_set_write_sections_to_fd(set, fd,
267 razor_details_sections,
268 ARRAY_SIZE(razor_details_sections));
269 case RAZOR_REPO_FILE_FILES:
270 return razor_set_write_sections_to_fd(set, fd,
271 razor_files_sections,
272 ARRAY_SIZE(razor_files_sections));
279 razor_set_write(struct razor_set *set, const char *filename,
280 enum razor_repo_file_type type)
284 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
288 status = razor_set_write_to_fd(set, fd, type);
298 razor_build_evr(char *evr_buf, int size, const char *epoch,
299 const char *version, const char *release)
303 if (!version || !*version) {
308 if (epoch && *epoch && strcmp(epoch, "0") != 0) {
309 len = snprintf(evr_buf, size, "%s:", epoch);
313 len = snprintf(evr_buf, size, "%s", version);
316 if (release && *release)
317 snprintf(evr_buf, size, "-%s", release);
321 razor_versioncmp(const char *s1, const char *s2)
330 n1 = strtol(s1, (char **) &p1, 10);
331 n2 = strtol(s2, (char **) &p2, 10);
333 /* Epoch; if one but not the other has an epoch set, default
334 * the epoch-less version to 0. */
335 res = (*p1 == ':') - (*p2 == ':');
340 } else if (res > 0) {
353 if (isdigit(*p1) && isdigit(*p2))
354 return razor_versioncmp(p1, p2);
361 razor_package_get_details_type(struct razor_set *set,
362 struct razor_package *package,
363 enum razor_detail_type type)
368 case RAZOR_DETAIL_NAME:
369 pool = set->string_pool.data;
370 return &pool[package->name];
372 case RAZOR_DETAIL_VERSION:
373 pool = set->string_pool.data;
374 return &pool[package->version];
376 case RAZOR_DETAIL_ARCH:
377 pool = set->string_pool.data;
378 return &pool[package->arch];
380 case RAZOR_DETAIL_SUMMARY:
381 pool = set->details_string_pool.data;
382 return &pool[package->summary];
384 case RAZOR_DETAIL_DESCRIPTION:
385 pool = set->details_string_pool.data;
386 return &pool[package->description];
388 case RAZOR_DETAIL_URL:
389 pool = set->details_string_pool.data;
390 return &pool[package->url];
392 case RAZOR_DETAIL_LICENSE:
393 pool = set->details_string_pool.data;
394 return &pool[package->license];
397 fprintf(stderr, "type %u not found\n", type);
403 * razor_package_get_details_varg:
405 * @package: a %razor_package
406 * @args: a va_list of arguments to set
409 razor_package_get_details_varg(struct razor_set *set,
410 struct razor_package *package,
414 enum razor_detail_type type;
417 for (i = 0;; i += 2) {
418 type = va_arg(args, enum razor_detail_type);
419 if (type == RAZOR_DETAIL_LAST)
421 data = va_arg(args, const char **);
422 *data = razor_package_get_details_type(set, package, type);
428 * razor_package_get_details:
430 * @package: a %razor_package
432 * Gets details about a package using a varg interface
433 * The vararg must be terminated with %RAZOR_DETAIL_LAST.
435 * Example: razor_package_get_details (set, package,
436 * RAZOR_DETAIL_URL, &url,
437 * RAZOR_DETAIL_LAST);
440 razor_package_get_details(struct razor_set *set, struct razor_package *package, ...)
444 assert (set != NULL);
445 assert (package != NULL);
447 va_start(args, NULL);
448 razor_package_get_details_varg (set, package, args);
452 RAZOR_EXPORT const char *
453 razor_property_relation_to_string(struct razor_property *p)
457 switch (p->flags & RAZOR_PROPERTY_RELATION_MASK) {
458 case RAZOR_PROPERTY_LESS:
461 case RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL:
464 case RAZOR_PROPERTY_EQUAL:
467 case RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL:
470 case RAZOR_PROPERTY_GREATER:
478 RAZOR_EXPORT const char *
479 razor_property_type_to_string(struct razor_property *p)
483 switch (p->flags & RAZOR_PROPERTY_TYPE_MASK) {
484 case RAZOR_PROPERTY_REQUIRES:
486 case RAZOR_PROPERTY_PROVIDES:
488 case RAZOR_PROPERTY_CONFLICTS:
490 case RAZOR_PROPERTY_OBSOLETES:
497 RAZOR_EXPORT struct razor_entry *
498 razor_set_find_entry(struct razor_set *set,
499 struct razor_entry *dir, const char *pattern)
501 struct razor_entry *e;
502 const char *n, *pool = set->file_string_pool.data;
505 assert (set != NULL);
506 assert (dir != NULL);
507 assert (pattern != NULL);
509 e = (struct razor_entry *) set->files.data + dir->start;
512 if (strcmp(pattern + 1, n) == 0)
515 if (e->start != 0 && strncmp(pattern + 1, n, len) == 0 &&
516 pattern[len + 1] == '/') {
517 return razor_set_find_entry(set, e, pattern + len + 1);
519 } while (!((e++)->flags & RAZOR_ENTRY_LAST));
525 list_dir(struct razor_set *set, struct razor_entry *dir,
526 char *prefix, const char *pattern)
528 struct razor_entry *e;
529 const char *n, *pool = set->file_string_pool.data;
531 e = (struct razor_entry *) set->files.data + dir->start;
534 if (pattern && pattern[0] && fnmatch(pattern, n, 0) != 0)
536 printf("%s/%s\n", prefix, n);
538 char *sub = prefix + strlen (prefix);
541 list_dir(set, e, prefix, pattern);
544 } while (!((e++)->flags & RAZOR_ENTRY_LAST));
548 razor_set_list_files(struct razor_set *set, const char *pattern)
550 struct razor_entry *e;
551 char buffer[512], *p, *base;
553 assert (set != NULL);
555 if (pattern == NULL || !strcmp (pattern, "/")) {
557 list_dir(set, set->files.data, buffer, NULL);
561 strcpy(buffer, pattern);
562 e = razor_set_find_entry(set, set->files.data, buffer);
563 if (e && e->start > 0) {
566 p = strrchr(buffer, '/');
574 e = razor_set_find_entry(set, set->files.data, buffer);
575 if (e && e->start != 0)
576 list_dir(set, e, buffer, base);
580 list_package_files(struct razor_set *set, struct list *r,
581 struct razor_entry *dir, uint32_t end,
584 struct razor_entry *e, *f, *entries;
589 entries = (struct razor_entry *) set->files.data;
590 pool = set->file_string_pool.data;
592 e = entries + dir->start;
594 if (entries + r->data == e) {
595 printf("%s/%s\n", prefix, pool + e->name);
602 } while (!((e++)->flags & RAZOR_ENTRY_LAST));
604 e = entries + dir->start;
609 if (e->flags & RAZOR_ENTRY_LAST)
613 while (f->start == 0 && !(f->flags & RAZOR_ENTRY_LAST))
622 if (e->start <= file && file < next) {
623 len = strlen(prefix);
625 strcpy(prefix + len + 1, pool + e->name);
626 r = list_package_files(set, r, e, next, prefix);
629 } while (!((e++)->flags & RAZOR_ENTRY_LAST) && r != NULL);
635 razor_set_list_package_files(struct razor_set *set,
636 struct razor_package *package)
642 assert (set != NULL);
643 assert (package != NULL);
645 r = list_first(&package->files, &set->file_pool);
646 end = set->files.size / sizeof (struct razor_entry);
648 list_package_files(set, r, set->files.data, end, buffer);
651 /* The diff order matters. We should sort the packages so that a
652 * REMOVE of a package comes before the INSTALL, and so that all
653 * requires for a package have been installed before the package.
657 razor_set_diff(struct razor_set *set, struct razor_set *upstream,
658 razor_diff_callback_t callback, void *data)
660 struct razor_package_iterator *pi1, *pi2;
661 struct razor_package *p1, *p2;
662 const char *name1, *name2, *version1, *version2, *arch1, *arch2;
665 assert (set != NULL);
666 assert (upstream != NULL);
668 pi1 = razor_package_iterator_create(set);
669 pi2 = razor_package_iterator_create(upstream);
671 razor_package_iterator_next(pi1, &p1,
672 RAZOR_DETAIL_NAME, &name1,
673 RAZOR_DETAIL_VERSION, &version1,
674 RAZOR_DETAIL_ARCH, &arch1,
676 razor_package_iterator_next(pi2, &p2,
677 RAZOR_DETAIL_NAME, &name2,
678 RAZOR_DETAIL_VERSION, &version2,
679 RAZOR_DETAIL_ARCH, &arch2,
684 res = strcmp(name1, name2);
686 res = razor_versioncmp(version1, version2);
691 if (p2 == NULL || res < 0)
692 callback(RAZOR_DIFF_ACTION_REMOVE,
693 p1, name1, version1, arch1, data);
694 else if (p1 == NULL || res > 0)
695 callback(RAZOR_DIFF_ACTION_ADD,
696 p2, name2, version2, arch2, data);
698 if (p1 != NULL && res <= 0)
699 razor_package_iterator_next(pi1, &p1,
700 RAZOR_DETAIL_NAME, &name1,
701 RAZOR_DETAIL_VERSION, &version1,
702 RAZOR_DETAIL_ARCH, &arch1,
704 if (p2 != NULL && res >= 0)
705 razor_package_iterator_next(pi2, &p2,
706 RAZOR_DETAIL_NAME, &name2,
707 RAZOR_DETAIL_VERSION, &version2,
708 RAZOR_DETAIL_ARCH, &arch2,
712 razor_package_iterator_destroy(pi1);
713 razor_package_iterator_destroy(pi2);
716 struct install_action {
717 enum razor_install_action action;
718 struct razor_package *package;
721 struct razor_install_iterator {
722 struct razor_set *set;
723 struct razor_set *next;
724 struct array actions;
725 struct install_action *a, *end;
729 add_action(enum razor_diff_action action,
730 struct razor_package *package,
736 struct razor_install_iterator *ii = data;
737 struct install_action *a;
739 a = array_add(&ii->actions, sizeof *a);
740 a->package = package;
743 case RAZOR_DIFF_ACTION_ADD:
744 a->action = RAZOR_INSTALL_ACTION_ADD;
746 case RAZOR_DIFF_ACTION_REMOVE:
747 a->action = RAZOR_INSTALL_ACTION_REMOVE;
752 RAZOR_EXPORT struct razor_install_iterator *
753 razor_set_create_install_iterator(struct razor_set *set,
754 struct razor_set *next)
756 struct razor_install_iterator *ii;
758 assert (set != NULL);
759 assert (next != NULL);
761 ii = zalloc(sizeof *ii);
765 razor_set_diff(set, next, add_action, ii);
767 ii->a = ii->actions.data;
768 ii->end = ii->actions.data + ii->actions.size;
770 /* FIXME: We need to figure out the right install order here,
771 * so the post and pre scripts can run. */
777 razor_install_iterator_next(struct razor_install_iterator *ii,
778 struct razor_set **set,
779 struct razor_package **package,
780 enum razor_install_action *action,
783 if (ii->a == ii->end)
786 switch (ii->a->action) {
787 case RAZOR_INSTALL_ACTION_ADD:
790 case RAZOR_INSTALL_ACTION_REMOVE:
795 *package = ii->a->package;
796 *action = ii->a->action;
804 razor_install_iterator_destroy(struct razor_install_iterator *ii)
806 array_release(&ii->actions);