From: Kristian Høgsberg Date: Sat, 21 Jun 2008 01:56:43 +0000 (-0400) Subject: Introduce install/remove iterators. X-Git-Tag: 0.1~117 X-Git-Url: http://project.juiblex.co.uk/git/?a=commitdiff_plain;h=8a813368430e6773afbfd9c34a7da9454d4799ae;p=razor.git Introduce install/remove iterators. These iterator constructors lets you pass in two sets and creates an iterator for the packages to remove or the packages to install. The iterators will step through the packages in a sequence that respects the pre, post, preun and postun modifiers. Right now, the install order isn't actually implemented, this patch just implements the API changes and updates the applications. --- diff --git a/librazor/razor.c b/librazor/razor.c index b27f49d..bb05700 100644 --- a/librazor/razor.c +++ b/librazor/razor.c @@ -463,3 +463,55 @@ razor_set_diff(struct razor_set *set, struct razor_set *upstream, razor_package_iterator_destroy(pi1); razor_package_iterator_destroy(pi2); } + +static void +add_new_package(enum razor_diff_action action, + struct razor_package *package, + const char *name, + const char *version, + const char *arch, + void *data) +{ + if (action == RAZOR_DIFF_ACTION_ADD) + razor_package_query_add_package(data, package); +} + +struct razor_package_iterator * +razor_set_create_remove_iterator(struct razor_set *set, + struct razor_set *next) +{ + struct razor_package_query *query; + struct razor_package_iterator *pi; + + query = razor_package_query_create(set); + razor_set_diff(next, set, add_new_package, query); + + pi = razor_package_query_finish(query); + + /* FIXME: We need to figure out the right install order here, + * so the post and pre scripts can run. */ + + /* sort */ + + return pi; +} + +struct razor_package_iterator * +razor_set_create_install_iterator(struct razor_set *set, + struct razor_set *next) +{ + struct razor_package_query *query; + struct razor_package_iterator *pi; + + query = razor_package_query_create(next); + razor_set_diff(set, next, add_new_package, query); + + pi = razor_package_query_finish(query); + + /* FIXME: We need to figure out the right install order here, + * so the post and pre scripts can run. */ + + /* sort */ + + return pi; +} diff --git a/librazor/razor.h b/librazor/razor.h index dfe17d8..d8da00b 100644 --- a/librazor/razor.h +++ b/librazor/razor.h @@ -121,6 +121,12 @@ typedef void (*razor_diff_callback_t)(enum razor_diff_action action, void razor_set_diff(struct razor_set *set, struct razor_set *upstream, razor_diff_callback_t callback, void *data); +struct razor_package_iterator * +razor_set_create_remove_iterator(struct razor_set *set, + struct razor_set *next); +struct razor_package_iterator * +razor_set_create_install_iterator(struct razor_set *set, + struct razor_set *next); /* Package transactions */ diff --git a/src/main.c b/src/main.c index ec1820a..40e7674 100644 --- a/src/main.c +++ b/src/main.c @@ -578,74 +578,86 @@ command_import_rpms(int argc, const char *argv[]) return 0; } -static void -download_package(enum razor_diff_action action, - struct razor_package *package, - const char *name, - const char *version, - const char *arch, - void *data) +static const char * +rpm_filename(const char *name, const char *version, const char *arch) { - char file[PATH_MAX], url[256]; - const char *v; - int *errors = data; - - if (action != RAZOR_DIFF_ACTION_ADD) - return; - - /* Skip epoch */ + static char file[PATH_MAX]; + const char *v; + + /* Skip epoch */ v = strchr(version, ':'); - if (v != NULL) - v = v + 1; - else + if (v != NULL) + v = v + 1; + else v = version; - snprintf(url, sizeof url, - "%s/Packages/%s-%s.%s.rpm", yum_url, name, v, arch); - snprintf(file, sizeof file, - "rpms/%s-%s.%s.rpm", name, v, arch); - if (download_if_missing(url, file) < 0) - (*errors)++; -} + snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch); -static void -install_package(enum razor_diff_action action, - struct razor_package *package, - const char *name, - const char *version, - const char *arch, - void *data) -{ - const char *v, *root = data; - char file[PATH_MAX]; - struct razor_rpm *rpm; + return file; +} - if (action == RAZOR_DIFF_ACTION_REMOVE) { - printf("removing %s %s not handled\n", name, version); - return; +static int +download_packages(struct razor_set *system, struct razor_set *next) +{ + struct razor_package_iterator *pi; + struct razor_package *package; + const char *name, *version, *arch; + char file[PATH_MAX], url[256]; + int errors; + + pi = razor_set_create_install_iterator(system, next); + errors = 0; + while (razor_package_iterator_next(pi, &package, + &name, &version, &arch)) { + snprintf(url, sizeof url, + "%s/Packages/%s", + yum_url, rpm_filename(name, version, arch)); + snprintf(file, sizeof file, + "rpms/%s", rpm_filename(name, version, arch)); + if (download_if_missing(url, file) < 0) + errors++; } + razor_package_iterator_destroy(pi); - /* Skip epoch */ - v = strchr(version, ':'); - if (v != NULL) - v = v + 1; - else - v = version; + if (errors > 0) { + fprintf(stderr, "failed to download %d packages\n", errors); + return -1; + } - printf("install %s %s\n", name, v); - snprintf(file, sizeof file, "rpms/%s-%s.%s.rpm", name, v, arch); + return 0; +} - rpm = razor_rpm_open(file); - if (rpm == NULL) { - fprintf(stderr, "failed to open rpm %s\n", file); - return; - } - if (razor_rpm_install(rpm, root) < 0) { - fprintf(stderr, - "failed to install rpm %s\n", file); - return; +static int +install_packages(struct razor_set *system, struct razor_set *next) +{ + struct razor_package_iterator *pi; + struct razor_package *package; + struct razor_rpm *rpm; + const char *name, *version, *arch; + char file[PATH_MAX]; + + pi = razor_set_create_install_iterator(system, next); + while (razor_package_iterator_next(pi, &package, + &name, &version, &arch)) { + printf("install %s-%s\n", name, version); + + snprintf(file, sizeof file, + "rpms/%s", rpm_filename(name, version, arch)); + rpm = razor_rpm_open(file); + if (rpm == NULL) { + fprintf(stderr, "failed to open rpm %s\n", file); + return -1; + } + if (razor_rpm_install(rpm, install_root) < 0) { + fprintf(stderr, + "failed to install rpm %s\n", file); + return -1; + } + razor_rpm_close(rpm); } - razor_rpm_close(rpm); + razor_package_iterator_destroy(pi); + + return 0; } static int @@ -692,17 +704,12 @@ command_install(int argc, const char *argv[]) return 1; } - errors = 0; - razor_set_diff(system, next, download_package, &errors); - if (errors > 0) { - fprintf(stderr, "failed to download %d packages\n", errors); + if (download_packages(system, next) < 0) { razor_root_close(root); return 1; } - /* FIXME: We need to figure out the right install order here, - * so the post and pre scripts can run. */ - razor_set_diff(system, next, install_package, (void *) install_root); + install_packages(system, next); razor_set_destroy(next); razor_set_destroy(upstream);