diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 9 | ||||
l--------- | src/apk-test.c | 1 | ||||
-rw-r--r-- | src/apk.c | 63 | ||||
-rw-r--r-- | src/test.c | 227 |
4 files changed, 70 insertions, 230 deletions
diff --git a/src/Makefile b/src/Makefile index 11aa305..9ec554f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -27,16 +27,18 @@ libapk.so-objs := common.o database.o package.o archive.o \ solver.o ifeq ($(TEST),y) -progs-y += apk_test -apk_test-objs := apk.o test.o $(libapk.so-objs) +progs-y += apk-test +apk-test-objs := apk-test.o $(filter-out apk.o, $(apk-objs)) endif ifeq ($(SHARED_LIBAPK),) apk-objs += $(libapk.so-objs) +apk-test-objs += $(libapk.so-objs) apk.so-objs += $(libapk.so-objs) else LIBAPK := YesPlease LIBS_apk := -lapk +LIBS_apk-test := -lapk LIBS_apk.so := -L$(obj) -lapk endif @@ -50,12 +52,13 @@ endif CFLAGS_ALL += -D_ATFILE_SOURCE CFLAGS_apk.o := -DAPK_VERSION=\"$(FULL_VERSION)\" CFLAGS_apk-static.o := -DAPK_VERSION=\"$(FULL_VERSION)\" -DOPENSSL_NO_ENGINE +CFLAGS_apk-test.o := -DAPK_VERSION=\"$(FULL_VERSION)\" -DOPENSSL_NO_ENGINE -DTEST_MODE progs-$(STATIC) += apk.static apk.static-objs := $(filter-out apk.o,$(apk-objs)) apk-static.o LDFLAGS_apk.static := -static LDFLAGS_apk += -nopie -L$(obj) -LDFLAGS_apk_test += -nopie -L$(obj) +LDFLAGS_apk-test += -nopie -L$(obj) CFLAGS_ALL += $(shell $(PKG_CONFIG) --cflags $(PKGDEPS)) LIBS := -Wl,--as-needed \ diff --git a/src/apk-test.c b/src/apk-test.c new file mode 120000 index 0000000..bf745af --- /dev/null +++ b/src/apk-test.c @@ -0,0 +1 @@ +apk.c
\ No newline at end of file @@ -31,6 +31,7 @@ #include "apk_applet.h" #include "apk_blob.h" #include "apk_print.h" +#include "apk_io.h" char **apk_argv; @@ -67,11 +68,19 @@ static struct apk_option generic_options[] = { { 0x111, "overlay-from-stdin", "Read list of overlay files from stdin" }, { 0x112, "arch", "Use architecture with --root", required_argument, "ARCH" }, +#ifdef TEST_MODE + { 0x200, "test-repo", "Repository", required_argument, "REPO" }, + { 0x201, "test-instdb", "Installed db", required_argument, "INSTALLED" }, + { 0x202, "test-world", "World", required_argument, "WORLD DEPS" }, +#endif }; static int version(void) { printf("apk-tools " APK_VERSION ", compiled for " APK_DEFAULT_ARCH ".\n"); +#ifdef TEST_MODE + printf("TEST MODE BUILD. NOT FOR PRODUCTION USE.\n"); +#endif return 0; } @@ -273,6 +282,14 @@ int main(int argc, char **argv) struct apk_repository_list *repo = NULL; struct apk_database db; struct apk_db_options dbopts; +#ifdef TEST_MODE + const char *test_installed_db = NULL; + const char *test_world = NULL; + struct apk_string_array *test_repos; + int i; + + apk_string_array_init(&test_repos); +#endif apk_argv = malloc(sizeof(char*[argc+2])); memcpy(apk_argv, argv, sizeof(char*[argc])); @@ -383,6 +400,17 @@ int main(int argc, char **argv) case 0x112: dbopts.arch = optarg; break; +#ifdef TEST_MODE + case 0x200: + *apk_string_array_add(&test_repos) = (char*) optarg; + break; + case 0x201: + test_installed_db = optarg; + break; + case 0x202: + test_world = optarg; + break; +#endif default: if (applet == NULL || applet->parse == NULL || applet->parse(ctx, &dbopts, r, @@ -407,6 +435,12 @@ int main(int argc, char **argv) argv++; } +#ifdef TEST_MODE + dbopts.open_flags &= ~(APK_OPENF_WRITE | APK_OPENF_CACHE_WRITE | APK_OPENF_CREATE); + dbopts.open_flags |= APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS; + apk_flags |= APK_SIMULATE; + apk_flags &= ~APK_INTERACTIVE; +#endif r = apk_db_open(&db, &dbopts); if (r != 0) { apk_error("Failed to open apk database: %s", @@ -414,6 +448,35 @@ int main(int argc, char **argv) goto err; } +#ifdef TEST_MODE + if (test_world != NULL) { + apk_blob_t b = APK_BLOB_STR(test_world); + apk_blob_pull_deps(&b, &db, &db.world); + } + if (test_installed_db != NULL) { + struct apk_bstream *bs = apk_bstream_from_file(AT_FDCWD, test_installed_db); + if (bs != NULL) { + apk_db_index_read(&db, bs, -1); + bs->close(bs, NULL); + } + } + for (i = 0; i < test_repos->num; i++) { + struct apk_bstream *bs; + char *fn = test_repos->item[i]; + int repo_tag = 0; + if (fn[0] == '+') { + repo_tag = apk_db_get_tag_id(&db, APK_BLOB_STR("testing")); + fn++; + } + bs = apk_bstream_from_file(AT_FDCWD, fn); + if (bs != NULL) { + apk_db_index_read(&db, bs, i); + db.repo_tags[repo_tag].allowed_repos |= BIT(i); + bs->close(bs, NULL); + } + } +#endif + r = applet->main(ctx, &db, argc, argv); apk_db_close(&db); diff --git a/src/test.c b/src/test.c deleted file mode 100644 index 8f34429..0000000 --- a/src/test.c +++ /dev/null @@ -1,227 +0,0 @@ -/* test.c - Alpine Package Keeper (APK) - * - * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi> - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 as published - * by the Free Software Foundation. See http://www.gnu.org/ for details. - */ - -#include <errno.h> -#include <fcntl.h> -#include "apk_applet.h" -#include "apk_database.h" -#include "apk_solver.h" -#include "apk_io.h" -#include "apk_print.h" - -struct test_ctx { - const char *installed_db; - struct apk_string_array *repos; - unsigned short solver_flags; -}; - -static int test_parse(void *pctx, struct apk_db_options *dbopts, - int optch, int optindex, const char *optarg) -{ - struct test_ctx *ctx = (struct test_ctx *) pctx; - - switch (optch) { - case 0x10000: - ctx->installed_db = optarg; - break; - case 0x10001: - if (ctx->repos == NULL) - apk_string_array_init(&ctx->repos); - *apk_string_array_add(&ctx->repos) = (char*) optarg; - break; - case 'u': - ctx->solver_flags |= APK_SOLVERF_UPGRADE; - break; - case 'a': - ctx->solver_flags |= APK_SOLVERF_AVAILABLE; - break; - default: - return -1; - } - return 0; -} - -static inline void print_change(struct apk_package *oldpkg, - struct apk_package *newpkg) -{ - const char *msg = NULL; - struct apk_name *name; - int r; - - if (oldpkg != NULL) - name = oldpkg->name; - else - name = newpkg->name; - - if (oldpkg == NULL) { - apk_message("Installing %s (" BLOB_FMT ")", - name->name, - BLOB_PRINTF(*newpkg->version)); - } else if (newpkg == NULL) { - apk_message("Purging %s (" BLOB_FMT ")", - name->name, - BLOB_PRINTF(*oldpkg->version)); - } else { - r = apk_pkg_version_compare(newpkg, oldpkg); - switch (r) { - case APK_VERSION_LESS: - msg = "Downgrading"; - break; - case APK_VERSION_EQUAL: - if (newpkg == oldpkg) - msg = "Re-installing"; - else - msg = "Replacing"; - break; - case APK_VERSION_GREATER: - msg = "Upgrading"; - break; - } - apk_message("%s %s (" BLOB_FMT " -> " BLOB_FMT ")", - msg, name->name, - BLOB_PRINTF(*oldpkg->version), - BLOB_PRINTF(*newpkg->version)); - } -} - -static void print_dep_errors(struct apk_database *db, char *label, struct apk_dependency_array *deps) -{ - int i, print_label = 1; - char buf[256]; - apk_blob_t p = APK_BLOB_BUF(buf); - - for (i = 0; i < deps->num; i++) { - struct apk_dependency *dep = &deps->item[i]; - struct apk_package *pkg = dep->name->state_ptr; - - if (apk_dep_is_satisfied(dep, pkg)) - continue; - - if (print_label) { - print_label = 0; - printf("%s: ", label); - } else { - printf(" "); - } - apk_blob_push_dep(&p, db, dep); - p = apk_blob_pushed(APK_BLOB_BUF(buf), p); - fwrite(p.ptr, p.len, 1, stdout); - } - if (!print_label) - printf("\n"); -} - -static void print_errors_in_solution(struct apk_database *db, int unsatisfiable, - struct apk_solution_array *solution) -{ - int i; - - printf("%d unsatisfiable dependencies (solution with %zu names)\n", - unsatisfiable, solution->num); - - for (i = 0; i < solution->num; i++) { - struct apk_package *pkg = solution->item[i].pkg; - pkg->name->state_ptr = pkg; - } - - print_dep_errors(db, "world", db->world); - for (i = 0; i < solution->num; i++) { - struct apk_package *pkg = solution->item[i].pkg; - char pkgtext[256]; - snprintf(pkgtext, sizeof(pkgtext), PKG_VER_FMT, PKG_VER_PRINTF(pkg)); - print_dep_errors(db, pkgtext, pkg->depends); - } - -} - -static int test_main(void *pctx, struct apk_database *db, int argc, char **argv) -{ - struct test_ctx *ctx = (struct test_ctx *) pctx; - struct apk_bstream *bs; - struct apk_solution_array *solution = NULL; - struct apk_changeset changeset = {}; - apk_blob_t b; - int i, r; - - if (argc != 1) - return -EINVAL; - - apk_db_get_tag_id(db, APK_BLOB_STR("testing")); - - /* load installed db */ - if (ctx->installed_db != NULL) { - bs = apk_bstream_from_file(AT_FDCWD, ctx->installed_db); - if (bs != NULL) { - apk_db_index_read(db, bs, -1); - bs->close(bs, NULL); - } - } - - /* load additional indexes */ - if (ctx->repos) { - for (i = 0; i < ctx->repos->num; i++) { - char *fn = ctx->repos->item[i]; - int repo = 0; - if (fn[0] == '+') { - fn++; - repo = 1; - } - bs = apk_bstream_from_file(AT_FDCWD, fn); - if (bs != NULL) { - apk_db_index_read(db, bs, i); - db->repo_tags[repo].allowed_repos |= BIT(i); - bs->close(bs, NULL); - } - } - } - - /* construct new world */ - b = APK_BLOB_STR(argv[0]); - apk_blob_pull_deps(&b, db, &db->world); - - /* run solver */ - r = apk_solver_solve(db, ctx->solver_flags, db->world, &solution, &changeset); - if (r == 0) { - /* dump changeset */ - for (i = 0; i < changeset.changes->num; i++) { - struct apk_change *c = &changeset.changes->item[i]; - print_change(c->oldpkg, c->newpkg); - } - } else { /* r >= 1*/ - print_errors_in_solution(db, r, solution); - } - - return 0; -} - -static struct apk_option test_options[] = { - { 0x10000, "installed", "Installed database", - required_argument, "DB" }, - { 0x10001, "raw-repository", "Add unsigned test repository index", - required_argument, "INDEX" }, - { 'u', "upgrade", "Prefer to upgrade package" }, - { 'a', "available", - "Re-install or downgrade if currently installed package is not " - "currently available from any repository" }, -}; - -static struct apk_applet test_applet = { - .name = "test", - .help = "Test dependency graph solver (uses simple repository and installed db)", - .arguments = "'WORLD'", - .open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS, - .context_size = sizeof(struct test_ctx), - .num_options = ARRAY_SIZE(test_options), - .options = test_options, - .parse = test_parse, - .main = test_main, -}; - -APK_DEFINE_APPLET(test_applet); |