X-Git-Url: https://git.librecmc.org/?p=oweals%2Fopkg-lede.git;a=blobdiff_plain;f=libopkg%2Fpkg_depends.c;h=998d08544300fcc69117d389587c6e8a77368762;hp=bbe786815b1ae9171dd490c8ce84803304931a6c;hb=195ca08fef4a70f499cf6d5dc331820f45c54990;hpb=733d8409723a397b4ced39ea7aaab8bc8b4af5d9 diff --git a/libopkg/pkg_depends.c b/libopkg/pkg_depends.c index bbe7868..998d085 100644 --- a/libopkg/pkg_depends.c +++ b/libopkg/pkg_depends.c @@ -25,10 +25,10 @@ #include "opkg_message.h" #include "pkg_parse.h" #include "hash_table.h" +#include "libbb/libbb.h" static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str); static depend_t * depend_init(void); -static void depend_deinit(depend_t *d); static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx); static char ** merge_unresolved(char ** oldstuff, char ** newstuff); static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg); @@ -56,7 +56,7 @@ int pkg_hash_fetch_unsatisfied_dependencies(opkg_conf_t *conf, pkg_t * pkg, pkg_vec_t *unsatisfied, char *** unresolved) { pkg_t * satisfier_entry_pkg; - register int i, j, k; + int i, j, k; int count, found; char ** the_lost; abstract_pkg_t * ab_pkg; @@ -125,10 +125,10 @@ int pkg_hash_fetch_unsatisfied_dependencies(opkg_conf_t *conf, pkg_t * pkg, tmp_vec, &newstuff); if (newstuff == NULL) { - int i; + int m; int ok = 1; - for (i = 0; i < rc; i++) { - pkg_t *p = tmp_vec->pkgs[i]; + for (m = 0; m < rc; m++) { + pkg_t *p = tmp_vec->pkgs[m]; if (p->state_want == SW_INSTALL) continue; opkg_message(conf, OPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name); @@ -280,7 +280,7 @@ pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg) compound_depend_t * conflicts; depend_t ** possible_satisfiers; depend_t * possible_satisfier; - register int i, j, k; + int i, j, k; int count; abstract_pkg_t * ab_pkg; pkg_t **pkg_scouts; @@ -356,7 +356,7 @@ int version_constraints_satisfied(depend_t * depends, pkg_t * pkg) temp = pkg_new(); - parseVersion(temp, depends->version); + parse_version(temp, depends->version); comparison = pkg_compare_versions(pkg, temp); @@ -437,7 +437,7 @@ int pkg_dependence_satisfied(opkg_conf_t *conf, depend_t *depend) static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg) { - register int i; + int i; pkg_t ** pkgs = vec->pkgs; for(i = 0; i < vec->len; i++) @@ -448,47 +448,6 @@ static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg) return 0; } - -#ifdef DeadCode -/** - * pkg_has_common_provides returns 1 if pkg and replacee both provide - * the same abstract package and 0 otherwise. - */ -int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee) -{ - abstract_pkg_t **provides = pkg->provides; - int provides_count = pkg->provides_count; - abstract_pkg_t **replacee_provides = replacee->provides; - int replacee_provides_count = replacee->provides_count; - int i, j; - for (i = 0; i < provides_count; i++) { - abstract_pkg_t *apkg = provides[i]; - for (j = 0; j < replacee_provides_count; j++) { - abstract_pkg_t *replacee_apkg = replacee_provides[i]; - if (apkg == replacee_apkg) - return 1; - } - } - return 0; -} -#endif - -/** - * pkg_provides_abstract returns 1 if pkg->provides contains providee - * and 0 otherwise. - */ -int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee) -{ - abstract_pkg_t **provides = pkg->provides; - int provides_count = pkg->provides_count; - int i; - for (i = 0; i < provides_count; i++) { - if (provides[i] == providee) - return 1; - } - return 0; -} - /** * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0 * otherwise. @@ -497,14 +456,11 @@ int pkg_replaces(pkg_t *pkg, pkg_t *replacee) { abstract_pkg_t **replaces = pkg->replaces; int replaces_count = pkg->replaces_count; - /* abstract_pkg_t **replacee_provides = pkg->provides; - int replacee_provides_count = pkg->provides_count; */ + int replacee_provides_count = replacee->provides_count; int i, j; for (i = 0; i < replaces_count; i++) { abstract_pkg_t *abstract_replacee = replaces[i]; - for (j = 0; j < replaces_count; j++) { - /* opkg_message(NULL, OPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n", - pkg->name,replacee->provides[j]->name, abstract_replacee->name); */ + for (j = 0; j < replacee_provides_count; j++) { if (replacee->provides[j] == abstract_replacee) return 1; } @@ -568,28 +524,24 @@ static char ** merge_unresolved(char ** oldstuff, char ** newstuff) { int oldlen = 0, newlen = 0; char ** result; - register int i, j; + int i, j; if(!newstuff) return oldstuff; - + while(oldstuff && oldstuff[oldlen]) oldlen++; while(newstuff && newstuff[newlen]) newlen++; - - result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1)); - if (result == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } - + + result = xrealloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1)); + for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++) *(result + i) = *(newstuff + j); - + *(result + i) = NULL; return result; } - + /* * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre) * this is null terminated, no count is carried around @@ -598,264 +550,232 @@ char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx) { int count; char ** resized; - char *depend_str = pkg_depend_str(pkg, ref_ndx); count = 0; while(the_lost && the_lost[count]) count++; count++; /* need one to hold the null */ - resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1)); - if (resized == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } - resized[count - 1] = strdup(depend_str); + resized = xrealloc(the_lost, sizeof(char *) * (count + 1)); + resized[count - 1] = pkg_depend_str(pkg, ref_ndx); resized[count] = NULL; return resized; } - -void printDepends(pkg_t * pkg) -{ - register int i, j; - compound_depend_t * depend; - int count; - - count = pkg->pre_depends_count + pkg->depends_count; - - depend = pkg->depends; - if(!depend){ - fprintf(stderr, "Depends pointer is NULL\n"); - return; - } - for(i = 0; i < count; i++){ - fprintf(stderr, "%s has %d possibilities:\n", - (depend->type == GREEDY_DEPEND) ? "Greedy-Depend" : ((depend->type == DEPEND) ? "Depend" : "Pre-Depend"), - depend->possibility_count); - for(j = 0; j < depend->possibility_count; j++) - fprintf(stderr, "\t%s version %s (%d)\n", - depend->possibilities[j]->pkg->name, - depend->possibilities[j]->version, - depend->possibilities[j]->constraint); - depend++; - } -} -int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) +void buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) { - register int i, j; + int i; /* every pkg provides itself */ + pkg->provides_count++; abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg); - - if (!pkg->provides_count) - return 0; - if (pkg->provides) - return 0; - - pkg->provides = (abstract_pkg_t **)calloc((pkg->provides_count + 1), sizeof(abstract_pkg_t *)); - if (pkg->provides == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return -1 ; - } + pkg->provides = xcalloc(pkg->provides_count, sizeof(abstract_pkg_t *)); pkg->provides[0] = ab_pkg; - // if (strcmp(ab_pkg->name, pkg->name)) - // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name); - - for(i = 0; i < pkg->provides_count; i++){ - abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]); + for (i=1; iprovides_count; i++) { + abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, + pkg->provides_str[i-1]); + free(pkg->provides_str[i-1]); - pkg->provides[i+1] = provided_abpkg; + pkg->provides[i] = provided_abpkg; - j = 0; abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg); } - return 0; + if (pkg->provides_str) + free(pkg->provides_str); } /* Abhaya: added conflicts support */ -int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) +void buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) { - register int i; + int i; compound_depend_t * conflicts; if (!pkg->conflicts_count) - return 0; + return; - conflicts = pkg->conflicts = calloc(pkg->conflicts_count, sizeof(compound_depend_t)); - if (conflicts == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return -1; - } + conflicts = pkg->conflicts = xcalloc(pkg->conflicts_count, sizeof(compound_depend_t)); for (i = 0; i < pkg->conflicts_count; i++) { conflicts->type = CONFLICTS; parseDepends(conflicts, hash, pkg->conflicts_str[i]); + free(pkg->conflicts_str[i]); conflicts++; } - return 0; + if (pkg->conflicts_str) + free(pkg->conflicts_str); } -int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) +void buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) { - register int i, j; + int i; if (!pkg->replaces_count) - return 0; + return; - pkg->replaces = (abstract_pkg_t **)calloc(pkg->replaces_count, sizeof(abstract_pkg_t *)); - if (pkg->replaces == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return -1; - } + pkg->replaces = xcalloc(pkg->replaces_count, sizeof(abstract_pkg_t *)); for(i = 0; i < pkg->replaces_count; i++){ abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]); pkg->replaces[i] = old_abpkg; + free(pkg->replaces_str[i]); - j = 0; if (!old_abpkg->replaced_by) old_abpkg->replaced_by = abstract_pkg_vec_alloc(); - if ( old_abpkg->replaced_by == NULL ){ - return -1; - } /* if a package pkg both replaces and conflicts old_abpkg, * then add it to the replaced_by vector so that old_abpkg * will be upgraded to ab_pkg automatically */ if (pkg_conflicts_abstract(pkg, old_abpkg)) abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg); } - return 0; + + if (pkg->replaces_str) + free(pkg->replaces_str); } -int buildDepends(hash_table_t * hash, pkg_t * pkg) +void buildDepends(hash_table_t * hash, pkg_t * pkg) { int count; - register int i; + int i; compound_depend_t * depends; if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count)) - return 0; + return; - if (0 && pkg->pre_depends_count) - fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n", - pkg->name, pkg->pre_depends_count, pkg->depends_count); - depends = pkg->depends = calloc(count, sizeof(compound_depend_t)); - if (depends == NULL) { - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return -1; - } - + depends = pkg->depends = xcalloc(count, sizeof(compound_depend_t)); for(i = 0; i < pkg->pre_depends_count; i++){ parseDepends(depends, hash, pkg->pre_depends_str[i]); - if (0 && pkg->pre_depends_count) - fprintf(stderr, " pre_depends_str=%s depends=%p possibility_count=%x\n", - pkg->pre_depends_str[i], depends, depends->possibility_count); + free(pkg->pre_depends_str[i]); depends->type = PREDEPEND; depends++; } + if (pkg->pre_depends_str) + free(pkg->pre_depends_str); + + for(i = 0; i < pkg->depends_count; i++){ + parseDepends(depends, hash, pkg->depends_str[i]); + free(pkg->depends_str[i]); + depends++; + } + if (pkg->depends_str) + free(pkg->depends_str); for(i = 0; i < pkg->recommends_count; i++){ parseDepends(depends, hash, pkg->recommends_str[i]); - if (0 && pkg->recommends_count) - fprintf(stderr, " recommends_str=%s depends=%p possibility_count=%x\n", - pkg->recommends_str[i], depends, depends->possibility_count); + free(pkg->recommends_str[i]); depends->type = RECOMMEND; depends++; } + if(pkg->recommends_str) + free(pkg->recommends_str); for(i = 0; i < pkg->suggests_count; i++){ parseDepends(depends, hash, pkg->suggests_str[i]); - if (0 && pkg->suggests_count) - fprintf(stderr, " suggests_str=%s depends=%p possibility_count=%x\n", - pkg->suggests_str[i], depends, depends->possibility_count); + free(pkg->suggests_str[i]); depends->type = SUGGEST; depends++; } + if(pkg->suggests_str) + free(pkg->suggests_str); +} - for(i = 0; i < pkg->depends_count; i++){ - parseDepends(depends, hash, pkg->depends_str[i]); - if (0 && pkg->depends_count) - fprintf(stderr, " depends_str=%s depends=%p possibility_count=%x\n", - pkg->depends_str[i], depends, depends->possibility_count); - depends++; - } - return 0; -} +const char* +constraint_to_str(enum version_constraint c) +{ + switch (c) { + case NONE: + return ""; + case EARLIER: + return "< "; + case EARLIER_EQUAL: + return "<= "; + case EQUAL: + return "= "; + case LATER_EQUAL: + return ">= "; + case LATER: + return "> "; + } + + return ""; +} /* - * pkg_depend_string: returns the depends string specified by index. - * All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0. - * [0,npredepends) -> returns pre_depends_str[index] - * [npredepends,npredepends+nrecommends) -> returns recommends_str[index] - * [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index] - * [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index] + * Returns a printable string for pkg's dependency at the specified index. The + * resultant string must be passed to free() by the caller. */ -char *pkg_depend_str(pkg_t *pkg, int index) +char * +pkg_depend_str(pkg_t *pkg, int index) { - if (index < pkg->pre_depends_count) { - return pkg->pre_depends_str[index]; - } - index -= pkg->pre_depends_count; + int i, len; + char *str; + compound_depend_t *cdep; + depend_t *dep; - if (index < pkg->recommends_count) { - return pkg->recommends_str[index]; - } - index -= pkg->recommends_count; + len = 0; + cdep = &pkg->depends[index]; - if (index < pkg->suggests_count) { - return pkg->suggests_str[index]; - } - index -= pkg->suggests_count; + /* calculate string length */ + for (i=0; ipossibility_count; i++) { + dep = cdep->possibilities[i]; - if (index < pkg->depends_count) { - return pkg->depends_str[index]; - } - fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name); - return NULL; -} + if (i != 0) + len += 3; /* space, pipe, space */ -void freeDepends(pkg_t *pkg) -{ - int i; + len += strlen(dep->pkg->name); - if (pkg == NULL || pkg->depends == NULL) { - return; - } + if (dep->version) { + len += 2; /* space, left parenthesis */ + len += 3; /* constraint string (<=, >=, etc), space */ + len += strlen(dep->version); + len += 1; /* right parenthesis */ + } + } - fprintf(stderr, "Freeing depends=%p\n", pkg->depends); - for (i=0; i < pkg->depends->possibility_count; i++) { - depend_deinit(pkg->depends->possibilities[i]); - } - free(pkg->depends->possibilities); - free(pkg->depends); - pkg->depends = NULL; + str = xmalloc(len + 1); /* +1 for the NULL terminator */ + str[0] = '\0'; + + for (i=0; ipossibility_count; i++) { + dep = cdep->possibilities[i]; + + if (i != 0) + strncat(str, " | ", len); + + strncat(str, dep->pkg->name, len); + + if (dep->version) { + strncat(str, " (", len); + strncat(str, constraint_to_str(dep->constraint), len); + strncat(str, dep->version, len); + strncat(str, ")", len); + } + } + + return str; } +/* + * WARNING: This function assumes pre_depends and depends are at the + * start of the pkg->depends array. + */ void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) { compound_depend_t * depends; int count, othercount; - register int i, j; + int i, j; abstract_pkg_t * ab_depend; abstract_pkg_t ** temp; count = pkg->pre_depends_count + pkg->depends_count; depends = pkg->depends; - if (0 && pkg->pre_depends_count) - fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n", - pkg->name, pkg->pre_depends_count, pkg->depends_count); for (i = 0; i < count; i++) { - if (0 && pkg->pre_depends_count) - fprintf(stderr, " i=%d possibility_count=%x depends=%p\n", i, depends->possibility_count, depends); for (j = 0; j < depends->possibility_count; j++){ ab_depend = depends->possibilities[j]->pkg; if(!ab_depend->depended_upon_by) - ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *)); + ab_depend->depended_upon_by = xcalloc(1, sizeof(abstract_pkg_t *)); temp = ab_depend->depended_upon_by; othercount = 1; @@ -865,7 +785,7 @@ void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) } *temp = ab_pkg; - ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by, + ab_depend->depended_upon_by = xrealloc(ab_depend->depended_upon_by, (othercount + 1) * sizeof(abstract_pkg_t *)); /* the array may have moved */ temp = ab_depend->depended_upon_by + othercount; @@ -877,11 +797,7 @@ void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) static depend_t * depend_init(void) { - depend_t * d = (depend_t *)calloc(1, sizeof(depend_t)); - if ( d==NULL ){ - fprintf(stderr, "%s: out of memory\n", __FUNCTION__); - return NULL; - } + depend_t * d = xcalloc(1, sizeof(depend_t)); d->constraint = NONE; d->version = NULL; d->pkg = NULL; @@ -889,18 +805,13 @@ static depend_t * depend_init(void) return d; } -static void depend_deinit(depend_t *d) -{ - free(d); -} - static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str) { char * pkg_name, buffer[2048]; int num_of_ors = 0; - register int i; - register char * src, * dest; + int i; + char * src, * dest; depend_t ** possibilities; /* first count the number of ored possibilities for satisfying dependency */ @@ -912,16 +823,12 @@ static int parseDepends(compound_depend_t *compound_depend, compound_depend->type = DEPEND; compound_depend->possibility_count = num_of_ors + 1; - possibilities = (depend_t **)calloc((num_of_ors + 1), sizeof(depend_t *) ); - if (!possibilities) - return -ENOMEM; + possibilities = xcalloc((num_of_ors + 1), sizeof(depend_t *) ); compound_depend->possibilities = possibilities; src = depend_str; for(i = 0; i < num_of_ors + 1; i++){ possibilities[i] = depend_init(); - if (!possibilities[i]) - return -ENOMEM; /* gobble up just the name first */ dest = buffer; while(*src && @@ -931,7 +838,7 @@ static int parseDepends(compound_depend_t *compound_depend, (*src != '|')) *dest++ = *src++; *dest = '\0'; - pkg_name = trim_alloc(buffer); + pkg_name = trim_xstrdup(buffer); if (pkg_name == NULL ) return -ENOMEM; @@ -983,7 +890,7 @@ static int parseDepends(compound_depend_t *compound_depend, *dest++ = *src++; *dest = '\0'; - possibilities[i]->version = trim_alloc(buffer); + possibilities[i]->version = trim_xstrdup(buffer); /* fprintf(stderr, "let's print the depends version string:"); fprintf(stderr, "version %s\n", possibilities[i]->version);*/ if (possibilities[i]->version == NULL )