From 024480aca02a5a81113cdf7a47aafa4c1c30de3d Mon Sep 17 00:00:00 2001 From: Timo Teras Date: Tue, 14 Apr 2009 09:35:02 +0300 Subject: [PATCH] info: option '-r' to print "required by" info --- src/info.c | 114 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 43 deletions(-) diff --git a/src/info.c b/src/info.c index 7bfc6b7..c7a05f8 100644 --- a/src/info.c +++ b/src/info.c @@ -17,10 +17,13 @@ #include "apk_state.h" struct info_ctx { - int (*action)(struct apk_database *db, int argc, char **argv); + int (*action)(struct info_ctx *ctx, struct apk_database *db, + int argc, char **argv); + void (*subaction)(struct apk_package *pkg); }; -static int info_list(struct apk_database *db, int argc, char **argv) +static int info_list(struct info_ctx *ctx, struct apk_database *db, + int argc, char **argv) { struct apk_package *pkg; @@ -35,7 +38,8 @@ static int info_list(struct apk_database *db, int argc, char **argv) return 0; } -static int info_exists(struct apk_database *db, int argc, char **argv) +static int info_exists(struct info_ctx *ctx, struct apk_database *db, + int argc, char **argv) { struct apk_name *name; int i, j; @@ -56,7 +60,8 @@ static int info_exists(struct apk_database *db, int argc, char **argv) return 0; } -static int info_who_owns(struct apk_database *db, int argc, char **argv) +static int info_who_owns(struct info_ctx *ctx, struct apk_database *db, + int argc, char **argv) { struct apk_package *pkg; struct apk_dependency_array *deps = NULL; @@ -88,6 +93,27 @@ static int info_who_owns(struct apk_database *db, int argc, char **argv) return 0; } +static int info_package(struct info_ctx *ctx, struct apk_database *db, + int argc, char **argv) +{ + struct apk_name *name; + int i, j; + + for (i = 0; i < argc; i++) { + name = apk_db_query_name(db, APK_BLOB_STR(argv[i])); + if (name == NULL) { + apk_error("Not found: %s", name); + return 1; + } + for (j = 0; j < name->pkgs->num; j++) { + struct apk_package *pkg = name->pkgs->item[j]; + if (apk_pkg_get_state(pkg) == APK_STATE_INSTALL) + ctx->subaction(pkg); + } + } + return 0; +} + static void info_print_contents(struct apk_package *pkg) { struct apk_db_dir_instance *diri; @@ -109,26 +135,6 @@ static void info_print_contents(struct apk_package *pkg) puts(""); } -static int info_contents(struct apk_database *db, int argc, char **argv) -{ - struct apk_name *name; - int i, j; - - for (i = 0; i < argc; i++) { - name = apk_db_query_name(db, APK_BLOB_STR(argv[i])); - if (name == NULL) { - apk_error("Not found: %s", name); - return 1; - } - for (j = 0; j < name->pkgs->num; j++) { - struct apk_package *pkg = name->pkgs->item[j]; - if (apk_pkg_get_state(pkg) == APK_STATE_INSTALL) - info_print_contents(pkg); - } - } - return 0; -} - static void info_print_depends(struct apk_package *pkg) { int i; @@ -145,27 +151,42 @@ static void info_print_depends(struct apk_package *pkg) puts(""); } -static int info_depends(struct apk_database *db, int argc, char **argv) +static void info_print_required_by(struct apk_package *pkg) { - struct apk_name *name; - int i, j; + int i, j, k; + char *separator = apk_verbosity > 1 ? " " : "\n"; - for (i = 0; i < argc; i++) { - name = apk_db_query_name(db, APK_BLOB_STR(argv[i])); - if (name == NULL) { - apk_error("Not found: %s", argv[i]); - return 1; - } - for (j = 0; j < name->pkgs->num; j++) { - struct apk_package *pkg = name->pkgs->item[j]; - if (apk_pkg_get_state(pkg) == APK_STATE_INSTALL) - info_print_depends(pkg); + if (apk_verbosity == 1) + printf("%s-%s is required by:\n", pkg->name->name, pkg->version); + if (pkg->name->rdepends == NULL) + return; + if (apk_verbosity > 1) + printf("%s: ", pkg->name->name); + for (i = 0; i < pkg->name->rdepends->num; i++) { + struct apk_name *name0 = pkg->name->rdepends->item[i]; + + /* Check only the package that is installed, and that + * it actually has this package as dependency. */ + if (name0->pkgs == NULL) + continue; + for (j = 0; j < name0->pkgs->num; j++) { + struct apk_package *pkg0 = name0->pkgs->item[j]; + + if (apk_pkg_get_state(pkg0) != APK_STATE_INSTALL || + pkg0->depends == NULL) + continue; + for (k = 0; k < pkg0->depends->num; k++) { + if (pkg0->depends->item[k].name != pkg->name) + continue; + printf("%s-%s%s", pkg0->name->name, + pkg0->version, separator); + break; + } } } - return 0; + puts(""); } - static int info_parse(void *ctx, int optch, int optindex, const char *optarg) { struct info_ctx *ictx = (struct info_ctx *) ctx; @@ -178,10 +199,16 @@ static int info_parse(void *ctx, int optch, int optindex, const char *optarg) ictx->action = info_who_owns; break; case 'L': - ictx->action = info_contents; + ictx->action = info_package; + ictx->subaction = info_print_contents; break; case 'R': - ictx->action = info_depends; + ictx->action = info_package; + ictx->subaction = info_print_depends; + break; + case 'r': + ictx->action = info_package; + ictx->subaction = info_print_required_by; break; default: return -1; @@ -199,9 +226,9 @@ static int info_main(void *ctx, int argc, char **argv) return -1; if (ictx->action != NULL) - r = ictx->action(&db, argc, argv); + r = ictx->action(ictx, &db, argc, argv); else - r = info_list(&db, argc, argv); + r = info_list(ictx, &db, argc, argv); apk_db_close(&db); return r; @@ -212,6 +239,7 @@ static struct option info_options[] = { { "installed", no_argument, NULL, 'e' }, { "who-owns", no_argument, NULL, 'W' }, { "depends", no_argument, NULL, 'R' }, + { "rdepends", no_argument, NULL, 'r' }, }; static struct apk_applet apk_info = {