diff options
author | Dan McGee <dan@archlinux.org> | 2008-01-12 01:13:52 -0600 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2008-03-23 14:51:51 -0500 |
commit | bf8670036907b3ede18d37b7a3f0f7e14542a5ac (patch) | |
tree | 8352859ea5c9f2b40fe1c091f1f23c3651db153a | |
parent | 90a48c771d727e3bd231eeb1611dfcbecb88490c (diff) |
Switch pmgrp_t to dynamic allocation, general group cleanup
Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r-- | lib/libalpm/alpm.h | 2 | ||||
-rw-r--r-- | lib/libalpm/cache.c | 50 | ||||
-rw-r--r-- | lib/libalpm/group.c | 9 | ||||
-rw-r--r-- | lib/libalpm/group.h | 11 | ||||
-rw-r--r-- | src/pacman/query.c | 28 | ||||
-rw-r--r-- | src/pacman/sync.c | 37 |
6 files changed, 74 insertions, 63 deletions
diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h index 0a8812ad..25eb88c5 100644 --- a/lib/libalpm/alpm.h +++ b/lib/libalpm/alpm.h @@ -238,7 +238,7 @@ const char *alpm_delta_get_md5sum(pmdelta_t *delta); * Groups */ const char *alpm_grp_get_name(const pmgrp_t *grp); -const alpm_list_t *alpm_grp_get_pkgs(const pmgrp_t *grp); +alpm_list_t *alpm_grp_get_pkgs(const pmgrp_t *grp); /* * Sync diff --git a/lib/libalpm/cache.c b/lib/libalpm/cache.c index 09fa74c2..7fa24052 100644 --- a/lib/libalpm/cache.c +++ b/lib/libalpm/cache.c @@ -199,33 +199,29 @@ int _alpm_db_load_grpcache(pmdb_t *db) pmpkg_t *pkg = lp->data; for(i = alpm_pkg_get_groups(pkg); i; i = i->next) { - if(!alpm_list_find_str(db->grpcache, i->data)) { - pmgrp_t *grp = _alpm_grp_new(); - - strncpy(grp->name, i->data, GRP_NAME_LEN); - grp->name[GRP_NAME_LEN-1] = '\0'; - grp->packages = alpm_list_add_sorted(grp->packages, - /* gross signature forces us to - * discard const */ - (void*)alpm_pkg_get_name(pkg), - _alpm_str_cmp); - db->grpcache = alpm_list_add_sorted(db->grpcache, grp, _alpm_grp_cmp); - } else { - alpm_list_t *j; - - for(j = db->grpcache; j; j = j->next) { - pmgrp_t *grp = j->data; - - if(strcmp(grp->name, i->data) == 0) { - const char *pkgname = alpm_pkg_get_name(pkg); - if(!alpm_list_find_str(grp->packages, pkgname)) { - grp->packages = alpm_list_add_sorted(grp->packages, - (void*)pkgname, - _alpm_str_cmp); - } - } + const char *grpname = i->data; + alpm_list_t *j; + pmgrp_t *grp = NULL; + int found = 0; + + /* first look through the group cache for a group with this name */ + for(j = db->grpcache; j; j = j->next) { + grp = j->data; + + if(strcmp(grp->name, grpname) == 0 + && !alpm_list_find_ptr(grp->packages, pkg)) { + grp->packages = alpm_list_add(grp->packages, pkg); + found = 1; + break; } } + if(found) { + continue; + } + /* we didn't find the group, so create a new one with this name */ + grp = _alpm_grp_new(grpname); + grp->packages = alpm_list_add(grp->packages, pkg); + db->grpcache = alpm_list_add(db->grpcache, grp); } } @@ -243,10 +239,6 @@ void _alpm_db_free_grpcache(pmdb_t *db) } for(lg = db->grpcache; lg; lg = lg->next) { - pmgrp_t *grp = lg->data; - - alpm_list_free(grp->packages); - grp->packages = NULL; _alpm_grp_free(lg->data); lg->data = NULL; } diff --git a/lib/libalpm/group.c b/lib/libalpm/group.c index 050bcbd5..e9e7d008 100644 --- a/lib/libalpm/group.c +++ b/lib/libalpm/group.c @@ -31,13 +31,14 @@ #include "log.h" #include "alpm.h" -pmgrp_t *_alpm_grp_new() +pmgrp_t *_alpm_grp_new(const char *name) { pmgrp_t* grp; ALPM_LOG_FUNC; CALLOC(grp, 1, sizeof(pmgrp_t), RET_ERR(PM_ERR_MEMORY, NULL)); + STRDUP(grp->name, name, RET_ERR(PM_ERR_MEMORY, NULL)); return(grp); } @@ -50,7 +51,9 @@ void _alpm_grp_free(pmgrp_t *grp) return; } - FREELIST(grp->packages); + FREE(grp->name); + /* do NOT free the contents of the list, just the nodes */ + alpm_list_free(grp->packages); FREE(grp); } @@ -74,7 +77,7 @@ const char SYMEXPORT *alpm_grp_get_name(const pmgrp_t *grp) return grp->name; } -const alpm_list_t SYMEXPORT *alpm_grp_get_pkgs(const pmgrp_t *grp) +alpm_list_t SYMEXPORT *alpm_grp_get_pkgs(const pmgrp_t *grp) { ALPM_LOG_FUNC; diff --git a/lib/libalpm/group.h b/lib/libalpm/group.h index 88fc8b32..5f8fdec4 100644 --- a/lib/libalpm/group.h +++ b/lib/libalpm/group.h @@ -19,17 +19,16 @@ #ifndef _ALPM_GROUP_H #define _ALPM_GROUP_H -/* Groups */ -#define GRP_NAME_LEN 256 - #include "alpm.h" struct __pmgrp_t { - char name[GRP_NAME_LEN]; - alpm_list_t *packages; /* List of strings */ + /** group name */ + char *name; + /** list of pmpkg_t packages */ + alpm_list_t *packages; }; -pmgrp_t *_alpm_grp_new(void); +pmgrp_t *_alpm_grp_new(const char *name); void _alpm_grp_free(pmgrp_t *grp); int _alpm_grp_cmp(const void *g1, const void *g2); diff --git a/src/pacman/query.c b/src/pacman/query.c index e999a328..ca95aaa9 100644 --- a/src/pacman/query.c +++ b/src/pacman/query.c @@ -155,7 +155,6 @@ static int query_search(alpm_list_t *targets) } for(i = searchlist; i; i = alpm_list_next(i)) { - char *group = NULL; alpm_list_t *grp; pmpkg_t *pkg = alpm_list_getdata(i); @@ -176,8 +175,9 @@ static int query_search(alpm_list_t *targets) if (!config->quiet) { if((grp = alpm_pkg_get_groups(pkg)) != NULL) { - group = alpm_list_getdata(grp); - printf(" (%s)", (char *)alpm_list_getdata(grp)); + pmgrp_t *group = alpm_list_getdata(grp); + /* TODO handle multiple groups */ + printf(" (%s)", alpm_grp_get_name(group)); } /* we need a newline and initial indent first */ @@ -197,33 +197,33 @@ static int query_search(alpm_list_t *targets) static int query_group(alpm_list_t *targets) { alpm_list_t *i, *j; - char *package = NULL; + char *grpname = NULL; int ret = 0; if(targets == NULL) { for(j = alpm_db_getgrpcache(db_local); j; j = alpm_list_next(j)) { pmgrp_t *grp = alpm_list_getdata(j); - const alpm_list_t *p, *pkgnames; + const alpm_list_t *p, *packages; const char *grpname; grpname = alpm_grp_get_name(grp); - pkgnames = alpm_grp_get_pkgs(grp); + packages = alpm_grp_get_pkgs(grp); - for(p = pkgnames; p; p = alpm_list_next(p)) { - printf("%s %s\n", grpname, (char *)alpm_list_getdata(p)); + for(p = packages; p; p = alpm_list_next(p)) { + printf("%s %s\n", grpname, alpm_pkg_get_name(alpm_list_getdata(p))); } } } else { for(i = targets; i; i = alpm_list_next(i)) { pmgrp_t *grp; - package = alpm_list_getdata(i); - grp = alpm_db_readgrp(db_local, package); + grpname = alpm_list_getdata(i); + grp = alpm_db_readgrp(db_local, grpname); if(grp) { - const alpm_list_t *p, *pkgnames = alpm_grp_get_pkgs(grp); - for(p = pkgnames; p; p = alpm_list_next(p)) { - printf("%s %s\n", package, (char *)alpm_list_getdata(p)); + const alpm_list_t *p, *packages = alpm_grp_get_pkgs(grp); + for(p = packages; p; p = alpm_list_next(p)) { + printf("%s %s\n", grpname, alpm_pkg_get_name(alpm_list_getdata(p))); } } else { - fprintf(stderr, _("error: group \"%s\" was not found\n"), package); + fprintf(stderr, _("error: group \"%s\" was not found\n"), grpname); ret++; } } diff --git a/src/pacman/sync.c b/src/pacman/sync.c index 460933ae..9e02341d 100644 --- a/src/pacman/sync.c +++ b/src/pacman/sync.c @@ -353,9 +353,10 @@ static int sync_search(alpm_list_t *syncs, alpm_list_t *targets) } if (!config->quiet) { + /* TODO package in multiple groups needs to be handled, do a loop */ if((grp = alpm_pkg_get_groups(pkg)) != NULL) { group = alpm_list_getdata(grp); - printf(" (%s)", (char *)alpm_list_getdata(grp)); + printf(" (%s)", group); } /* we need a newline and initial indent first */ @@ -375,7 +376,8 @@ static int sync_search(alpm_list_t *syncs, alpm_list_t *targets) static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets) { - alpm_list_t *i, *j; + alpm_list_t *i, *j, *k; + alpm_list_t *pkgnames = NULL; if(targets) { for(i = targets; i; i = alpm_list_next(i)) { @@ -385,9 +387,14 @@ static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets) pmgrp_t *grp = alpm_db_readgrp(db, grpname); if(grp) { - /* TODO this should be a lot cleaner, why two outputs? */ printf("%s\n", (char *)alpm_grp_get_name(grp)); - list_display(" ", alpm_grp_get_pkgs(grp)); + /* get names of packages in group */ + for(k = alpm_grp_get_pkgs(grp); k; k = alpm_list_next(k)) { + pkgnames = alpm_list_add(pkgnames, + (char*)alpm_pkg_get_name(k->data)); + } + list_display(" ", pkgnames); + alpm_list_free(pkgnames); } } } @@ -400,7 +407,12 @@ static int sync_group(int level, alpm_list_t *syncs, alpm_list_t *targets) printf("%s\n", (char *)alpm_grp_get_name(grp)); if(grp && level > 1) { - list_display(" ", alpm_grp_get_pkgs(grp)); + for(k = alpm_grp_get_pkgs(grp); k; k = alpm_list_next(k)) { + pkgnames = alpm_list_add(pkgnames, + (char*)alpm_pkg_get_name(k->data)); + } + list_display(" ", pkgnames); + alpm_list_free(pkgnames); } } } @@ -622,26 +634,31 @@ static int sync_trans(alpm_list_t *targets) pmdb_t *db = alpm_list_getdata(j); grp = alpm_db_readgrp(db, targ); if(grp) { - alpm_list_t *k; + alpm_list_t *k, *pkgnames = NULL; found++; printf(_(":: group %s (including ignored packages):\n"), targ); /* remove dupe entries in case a package exists in multiple repos */ - const alpm_list_t *grppkgs = alpm_grp_get_pkgs(grp); + alpm_list_t *grppkgs = alpm_grp_get_pkgs(grp); alpm_list_t *pkgs = alpm_list_remove_dupes(grppkgs); - list_display(" ", pkgs); + for(k = pkgs; k; k = alpm_list_next(k)) { + pkgnames = alpm_list_add(pkgnames, + (char*)alpm_pkg_get_name(k->data)); + } + list_display(" ", pkgnames); if(yesno(1, _(":: Install whole content?"))) { - for(k = pkgs; k; k = alpm_list_next(k)) { + for(k = pkgnames; k; k = alpm_list_next(k)) { targets = alpm_list_add(targets, strdup(alpm_list_getdata(k))); } } else { - for(k = pkgs; k; k = alpm_list_next(k)) { + for(k = pkgnames; k; k = alpm_list_next(k)) { char *pkgname = alpm_list_getdata(k); if(yesno(1, _(":: Install %s from group %s?"), pkgname, targ)) { targets = alpm_list_add(targets, strdup(pkgname)); } } } + alpm_list_free(pkgnames); alpm_list_free(pkgs); } } |