1 /* pkg_depends.c - the opkg package management system
5 Copyright (C) 2002 Compaq Computer Corporation
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
22 #include "opkg_utils.h"
24 #include "opkg_message.h"
25 #include "pkg_parse.h"
26 #include "hash_table.h"
27 #include "libbb/libbb.h"
29 static int parseDepends(compound_depend_t
* compound_depend
, char *depend_str
, enum depend_type type
);
30 static depend_t
*depend_init(void);
31 static char **add_unresolved_dep(pkg_t
* pkg
, char **the_lost
, int ref_ndx
);
32 static char **merge_unresolved(char **oldstuff
, char **newstuff
);
33 static int is_pkg_in_pkg_vec(pkg_vec_t
* vec
, pkg_t
* pkg
);
35 static int pkg_installed_and_constraint_satisfied(pkg_t
* pkg
, void *cdata
)
37 depend_t
*depend
= (depend_t
*) cdata
;
38 if ((pkg
->state_status
== SS_INSTALLED
39 || pkg
->state_status
== SS_UNPACKED
)
40 && version_constraints_satisfied(depend
, pkg
))
46 static int pkg_constraint_satisfied(pkg_t
* pkg
, void *cdata
)
48 depend_t
*depend
= (depend_t
*) cdata
;
49 if (version_constraints_satisfied(depend
, pkg
))
55 /* returns ndependencies or negative error value */
57 pkg_hash_fetch_unsatisfied_dependencies(pkg_t
* pkg
, pkg_vec_t
* unsatisfied
,
60 pkg_t
*satisfier_entry_pkg
;
64 abstract_pkg_t
*ab_pkg
;
65 compound_depend_t
*compound_depend
;
68 * this is a setup to check for redundant/cyclic dependency checks,
69 * which are marked at the abstract_pkg level
71 if (!(ab_pkg
= pkg
->parent
)) {
72 opkg_msg(ERROR
, "Internal error, with pkg %s.\n", pkg
->name
);
76 if (ab_pkg
->dependencies_checked
) { /* avoid duplicate or cyclic checks */
80 ab_pkg
->dependencies_checked
= 1; /* mark it for subsequent visits */
83 compound_depend
= pkg_get_ptr(pkg
, PKG_DEPENDS
);
85 if (!compound_depend
|| !compound_depend
->type
) {
92 /* foreach dependency */
93 for (i
= 0; compound_depend
&& compound_depend
->type
; compound_depend
++, i
++) {
94 depend_t
**possible_satisfiers
=
95 compound_depend
->possibilities
;;
97 satisfier_entry_pkg
= NULL
;
99 if (compound_depend
->type
== GREEDY_DEPEND
) {
100 /* foreach possible satisfier */
101 for (j
= 0; j
< compound_depend
->possibility_count
; j
++) {
102 /* foreach provided_by, which includes the abstract_pkg itself */
103 abstract_pkg_t
*abpkg
= possible_satisfiers
[j
]->pkg
;
104 abstract_pkg_vec_t
*ab_provider_vec
= abpkg
->provided_by
;
105 int nposs
= ab_provider_vec
->len
;
106 abstract_pkg_t
**ab_providers
= ab_provider_vec
->pkgs
;
108 for (l
= 0; l
< nposs
; l
++) {
109 pkg_vec_t
*test_vec
= ab_providers
[l
]->pkgs
;
110 /* if no depends on this one, try the first package that Provides this one */
111 if (!test_vec
) { /* no pkg_vec hooked up to the abstract_pkg! (need another feed?) */
115 /* cruise this possiblity's pkg_vec looking for an installed version */
116 for (k
= 0; k
< test_vec
->len
; k
++) {
117 pkg_t
*pkg_scout
= test_vec
->pkgs
[k
];
118 /* not installed, and not already known about? */
119 if ((pkg_scout
->state_want
!= SW_INSTALL
)
120 && !pkg_scout
->parent
->dependencies_checked
121 && !is_pkg_in_pkg_vec(unsatisfied
, pkg_scout
)) {
122 char **newstuff
= NULL
;
124 pkg_vec_t
*tmp_vec
= pkg_vec_alloc();
125 /* check for not-already-installed dependencies */
126 rc
= pkg_hash_fetch_unsatisfied_dependencies(
127 pkg_scout
, tmp_vec
, &newstuff
);
128 if (newstuff
== NULL
) {
131 for (m
= 0; m
< rc
; m
++) {
132 pkg_t
*p
= tmp_vec
->pkgs
[m
];
133 if (p
->state_want
== SW_INSTALL
)
136 "Not installing %s due"
137 " to requirement for %s.\n",
138 pkg_scout
->name
, p
->name
);
142 pkg_vec_free(tmp_vec
);
144 /* mark this one for installation */
146 "Adding satisfier for greedy"
149 pkg_vec_insert(unsatisfied
, pkg_scout
);
153 "Not installing %s due to "
166 /* foreach possible satisfier, look for installed package */
167 for (j
= 0; j
< compound_depend
->possibility_count
; j
++) {
168 /* foreach provided_by, which includes the abstract_pkg itself */
169 depend_t
*dependence_to_satisfy
= possible_satisfiers
[j
];
170 abstract_pkg_t
*satisfying_apkg
= possible_satisfiers
[j
]->pkg
;
171 pkg_t
*satisfying_pkg
=
172 pkg_hash_fetch_best_installation_candidate(satisfying_apkg
,
173 pkg_installed_and_constraint_satisfied
,
174 dependence_to_satisfy
, 1);
175 /* Being that I can't test constraing in pkg_hash, I will test it here */
176 if (satisfying_pkg
!= NULL
) {
177 if (!pkg_installed_and_constraint_satisfied
178 (satisfying_pkg
, dependence_to_satisfy
)) {
179 satisfying_pkg
= NULL
;
182 opkg_msg(DEBUG
, "satisfying_pkg=%p\n", satisfying_pkg
);
183 if (satisfying_pkg
!= NULL
) {
189 /* if nothing installed matches, then look for uninstalled satisfier */
191 /* foreach possible satisfier, look for installed package */
192 for (j
= 0; j
< compound_depend
->possibility_count
; j
++) {
193 /* foreach provided_by, which includes the abstract_pkg itself */
194 depend_t
*dependence_to_satisfy
= possible_satisfiers
[j
];
195 abstract_pkg_t
*satisfying_apkg
= possible_satisfiers
[j
]->pkg
;
196 pkg_t
*satisfying_pkg
=
197 pkg_hash_fetch_best_installation_candidate(satisfying_apkg
,
198 pkg_constraint_satisfied
, dependence_to_satisfy
, 1);
199 /* Being that I can't test constraing in pkg_hash, I will test it here too */
200 if (satisfying_pkg
!= NULL
) {
201 if (!pkg_constraint_satisfied(satisfying_pkg
,
202 dependence_to_satisfy
)) {
203 satisfying_pkg
= NULL
;
207 /* user request overrides package recommendation */
208 if (satisfying_pkg
!= NULL
209 && (compound_depend
->type
== RECOMMEND
210 || compound_depend
->type
== SUGGEST
)
211 && (satisfying_pkg
->state_want
== SW_DEINSTALL
212 || satisfying_pkg
->state_want
== SW_PURGE
)) {
214 "%s: ignoring recommendation for "
215 "%s at user request\n",
216 pkg
->name
, satisfying_pkg
->name
);
220 opkg_msg(DEBUG
, "satisfying_pkg=%p\n",
222 if (satisfying_pkg
!= NULL
) {
223 satisfier_entry_pkg
= satisfying_pkg
;
229 /* we didn't find one, add something to the unsatisfied vector */
231 if (!satisfier_entry_pkg
) {
232 /* failure to meet recommendations is not an error */
233 if (compound_depend
->type
!= RECOMMEND
234 && compound_depend
->type
!= SUGGEST
)
235 the_lost
= add_unresolved_dep(pkg
, the_lost
, i
);
238 "%s: unsatisfied recommendation for %s\n",
240 compound_depend
->possibilities
[0]->pkg
->name
);
242 if (compound_depend
->type
== SUGGEST
) {
243 /* just mention it politely */
245 "package %s suggests installing %s\n",
246 pkg
->name
, satisfier_entry_pkg
->name
);
248 char **newstuff
= NULL
;
250 if (satisfier_entry_pkg
!= pkg
&&
251 !is_pkg_in_pkg_vec(unsatisfied
, satisfier_entry_pkg
))
253 pkg_hash_fetch_unsatisfied_dependencies(
254 satisfier_entry_pkg
, unsatisfied
, &newstuff
);
255 pkg_vec_insert(unsatisfied
, satisfier_entry_pkg
);
256 the_lost
= merge_unresolved(the_lost
, newstuff
);
264 *unresolved
= the_lost
;
266 return unsatisfied
->len
;
269 /*checking for conflicts !in replaces
270 If a packages conflicts with another but is also replacing it, I should not consider it a
272 returns 0 if conflicts <> replaces or 1 if conflicts == replaces
274 static int is_pkg_a_replaces(pkg_t
* pkg_scout
, pkg_t
* pkg
)
276 abstract_pkg_t
**replaces
= pkg_get_ptr(pkg
, PKG_REPLACES
);
278 if (!replaces
|| !*replaces
)
282 if (strcmp(pkg_scout
->name
, (*replaces
)->name
) == 0) { // Found
283 opkg_msg(DEBUG2
, "Seems I've found a replace %s %s\n",
284 pkg_scout
->name
, (*replaces
)->name
);
293 pkg_vec_t
*pkg_hash_fetch_conflicts(pkg_t
* pkg
)
295 pkg_vec_t
*installed_conflicts
, *test_vec
;
296 compound_depend_t
*conflicts
, *conflict
;
297 depend_t
**possible_satisfiers
;
298 depend_t
*possible_satisfier
;
300 abstract_pkg_t
*ab_pkg
;
305 * this is a setup to check for redundant/cyclic dependency checks,
306 * which are marked at the abstract_pkg level
308 if (!(ab_pkg
= pkg
->parent
)) {
309 opkg_msg(ERROR
, "Internal error: %s not in hash table\n",
311 return (pkg_vec_t
*) NULL
;
314 conflicts
= pkg_get_ptr(pkg
, PKG_CONFLICTS
);
316 return (pkg_vec_t
*) NULL
;
318 installed_conflicts
= pkg_vec_alloc();
320 /* foreach conflict */
321 for (conflict
= conflicts
; conflict
->type
; conflict
++ ) {
322 possible_satisfiers
= conflicts
->possibilities
;
324 /* foreach possible satisfier */
325 for (j
= 0; j
< conflicts
->possibility_count
; j
++) {
326 possible_satisfier
= possible_satisfiers
[j
];
327 if (!possible_satisfier
)
329 "Internal error: possible_satisfier=NULL\n");
330 if (!possible_satisfier
->pkg
)
332 "Internal error: possible_satisfier->pkg=NULL\n");
333 test_vec
= possible_satisfier
->pkg
->pkgs
;
335 /* pkg_vec found, it is an actual package conflict
336 * cruise this possiblity's pkg_vec looking for an installed version */
337 pkg_scouts
= test_vec
->pkgs
;
338 for (k
= 0; k
< test_vec
->len
; k
++) {
339 pkg_scout
= pkg_scouts
[k
];
342 "Internal error: pkg_scout=NULL\n");
345 if ((pkg_scout
->state_status
== SS_INSTALLED
346 || pkg_scout
->state_want
== SW_INSTALL
)
347 && version_constraints_satisfied(possible_satisfier
,
349 && !is_pkg_a_replaces(pkg_scout
, pkg
)) {
350 if (!is_pkg_in_pkg_vec(installed_conflicts
,
352 pkg_vec_insert(installed_conflicts
, pkg_scout
);
361 if (installed_conflicts
->len
)
362 return installed_conflicts
;
363 pkg_vec_free(installed_conflicts
);
364 return (pkg_vec_t
*) NULL
;
367 int version_constraints_satisfied(depend_t
* depends
, pkg_t
* pkg
)
372 if (depends
->constraint
== NONE
)
377 parse_version(temp
, depends
->version
);
379 comparison
= pkg_compare_versions(pkg
, temp
);
384 if ((depends
->constraint
== EARLIER
) && (comparison
< 0))
386 else if ((depends
->constraint
== LATER
) && (comparison
> 0))
388 else if (comparison
== 0)
390 else if ((depends
->constraint
== LATER_EQUAL
) && (comparison
>= 0))
392 else if ((depends
->constraint
== EARLIER_EQUAL
) && (comparison
<= 0))
398 int pkg_dependence_satisfiable(depend_t
* depend
)
400 abstract_pkg_t
*apkg
= depend
->pkg
;
401 abstract_pkg_vec_t
*provider_apkgs
= apkg
->provided_by
;
402 int n_providers
= provider_apkgs
->len
;
403 abstract_pkg_t
**apkgs
= provider_apkgs
->pkgs
;
409 for (i
= 0; i
< n_providers
; i
++) {
410 abstract_pkg_t
*papkg
= apkgs
[i
];
411 pkg_vec
= papkg
->pkgs
;
413 n_pkgs
= pkg_vec
->len
;
414 for (j
= 0; j
< n_pkgs
; j
++) {
415 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
416 if (version_constraints_satisfied(depend
, pkg
)) {
425 static int is_pkg_in_pkg_vec(pkg_vec_t
* vec
, pkg_t
* pkg
)
429 pkg_t
**pkgs
= vec
->pkgs
;
430 arch1
= pkg_get_architecture(pkg
);
432 for (i
= 0; i
< vec
->len
; i
++) {
433 arch2
= pkg_get_architecture(*(pkgs
+ i
));
435 if ((strcmp(pkg
->name
, (*(pkgs
+ i
))->name
) == 0)
436 && (pkg_compare_versions(pkg
, *(pkgs
+ i
)) == 0)
437 && (strcmp(arch1
, arch2
) == 0))
444 * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
447 int pkg_replaces(pkg_t
* pkg
, pkg_t
* replacee
)
449 abstract_pkg_t
**replaces
= pkg_get_ptr(pkg
, PKG_REPLACES
);
450 abstract_pkg_t
**provides
= pkg_get_ptr(replacee
, PKG_PROVIDES
);
451 abstract_pkg_t
**r
, **p
;
453 for (r
= replaces
; r
&& *r
; r
++)
454 for (p
= provides
; p
&& *p
; p
++)
462 * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
465 int pkg_conflicts_abstract(pkg_t
* pkg
, abstract_pkg_t
* conflictee
)
467 compound_depend_t
*conflicts
, *conflict
;
469 conflicts
= pkg_get_ptr(pkg
, PKG_CONFLICTS
);
472 for (conflict
= conflicts
; conflict
->type
; conflict
++) {
473 int possibility_count
= conflict
->possibility_count
;
474 struct depend
**possibilities
= conflict
->possibilities
;
475 for (j
= 0; j
< possibility_count
; j
++) {
476 if (possibilities
[j
]->pkg
== conflictee
) {
485 * pkg_conflicts returns 1 if pkg->conflicts contains one of
486 * conflictee's provides and 0 otherwise.
488 int pkg_conflicts(pkg_t
* pkg
, pkg_t
* conflictee
)
491 int possibility_count
;
492 struct depend
**possibilities
;
493 compound_depend_t
*conflicts
, *conflict
;
494 abstract_pkg_t
**conflictee_provides
, **provider
, *possibility
;
496 conflicts
= pkg_get_ptr(pkg
, PKG_CONFLICTS
);
497 conflictee_provides
= pkg_get_ptr(conflictee
, PKG_PROVIDES
);
499 for (conflict
= conflicts
; conflict
->type
; conflict
++) {
500 possibility_count
= conflict
->possibility_count
;
501 possibilities
= conflict
->possibilities
;
502 for (j
= 0; j
< possibility_count
; j
++) {
503 possibility
= possibilities
[j
]->pkg
;
504 for (provider
= conflictee_provides
; provider
&& *provider
; provider
++) {
505 if (possibility
== *provider
) {
514 static char **merge_unresolved(char **oldstuff
, char **newstuff
)
516 int oldlen
= 0, newlen
= 0;
523 while (oldstuff
&& oldstuff
[oldlen
])
525 while (newstuff
&& newstuff
[newlen
])
528 result
= xrealloc(oldstuff
, sizeof(char *) * (oldlen
+ newlen
+ 1));
530 for (i
= oldlen
, j
= 0; i
< (oldlen
+ newlen
); i
++, j
++)
531 *(result
+ i
) = *(newstuff
+ j
);
533 *(result
+ i
) = NULL
;
539 * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
540 * this is null terminated, no count is carried around
542 char **add_unresolved_dep(pkg_t
* pkg
, char **the_lost
, int ref_ndx
)
548 while (the_lost
&& the_lost
[count
])
551 count
++; /* need one to hold the null */
552 resized
= xrealloc(the_lost
, sizeof(char *) * (count
+ 1));
553 resized
[count
- 1] = pkg_depend_str(pkg
, ref_ndx
);
554 resized
[count
] = NULL
;
559 static void flag_related_packages(pkg_t
*pkg
, int state_flags
)
562 compound_depend_t
*deps
;
564 for (deps
= pkg_get_ptr(pkg
, PKG_DEPENDS
), i
= 0; deps
&& deps
[i
].type
; i
++)
565 for (j
= 0; j
< deps
[i
].possibility_count
; j
++) {
566 if ((deps
[i
].possibilities
[j
]->pkg
->state_flag
& state_flags
) != state_flags
) {
567 opkg_msg(DEBUG
, "propagating pkg flag to dependent abpkg %s\n",
568 deps
[i
].possibilities
[j
]->pkg
->name
);
569 deps
[i
].possibilities
[j
]->pkg
->state_flag
|= state_flags
;
573 for (deps
= pkg_get_ptr(pkg
, PKG_CONFLICTS
), i
= 0; deps
&& deps
[i
].type
; i
++)
574 for (j
= 0; j
< deps
[i
].possibility_count
; j
++) {
575 if ((deps
[i
].possibilities
[j
]->pkg
->state_flag
& state_flags
) != state_flags
) {
576 opkg_msg(DEBUG
, "propagating pkg flag to conflicting abpkg %s\n",
577 deps
[i
].possibilities
[j
]->pkg
->name
);
578 deps
[i
].possibilities
[j
]->pkg
->state_flag
|= state_flags
;
583 abstract_pkg_t
**init_providelist(pkg_t
*pkg
, int *count
)
585 abstract_pkg_t
*ab_pkg
;
586 abstract_pkg_t
**provides
= pkg_get_ptr(pkg
, PKG_PROVIDES
);
589 provides
= calloc(2, sizeof(abstract_pkg_t
*));
598 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
601 ab_pkg
->pkgs
= pkg_vec_alloc();
603 if (!abstract_pkg_vec_contains(ab_pkg
->provided_by
, ab_pkg
))
604 abstract_pkg_vec_insert(ab_pkg
->provided_by
, ab_pkg
);
606 provides
[0] = ab_pkg
;
612 pkg_set_ptr(pkg
, PKG_PROVIDES
, provides
);
615 for (*count
= 1; *provides
; provides
++) {
616 if (pkg
->state_flag
& SF_NEED_DETAIL
) {
617 if (!((*provides
)->state_flag
& SF_NEED_DETAIL
)) {
618 opkg_msg(DEBUG
, "propagating pkg flag to provided abpkg %s\n",
620 (*provides
)->state_flag
|= SF_NEED_DETAIL
;
627 flag_related_packages(pkg
, SF_NEED_DETAIL
);
632 void parse_providelist(pkg_t
*pkg
, char *list
)
636 abstract_pkg_t
*ab_pkg
, *provided_abpkg
, **tmp
, **provides
;
638 provides
= init_providelist(pkg
, &count
);
639 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
641 if (!provides
|| !ab_pkg
)
644 for (item
= strtok_r(list
, ", ", &tok
); item
;
645 count
++, item
= strtok_r(NULL
, ", ", &tok
)) {
646 tmp
= realloc(provides
, sizeof(abstract_pkg_t
*) * (count
+ 1));
651 provided_abpkg
= ensure_abstract_pkg_by_name(item
);
653 if (provided_abpkg
->state_flag
& SF_NEED_DETAIL
) {
654 if (!(ab_pkg
->state_flag
& SF_NEED_DETAIL
)) {
655 opkg_msg(DEBUG
, "propagating provided abpkg flag to "
656 "provider abpkg %s\n", ab_pkg
->name
);
657 ab_pkg
->state_flag
|= SF_NEED_DETAIL
;
661 if (!abstract_pkg_vec_contains(provided_abpkg
->provided_by
, ab_pkg
))
662 abstract_pkg_vec_insert(provided_abpkg
->provided_by
, ab_pkg
);
665 provides
[count
- 1] = provided_abpkg
;
668 provides
[count
- 1] = NULL
;
670 pkg_set_ptr(pkg
, PKG_PROVIDES
, provides
);
673 void parse_replacelist(pkg_t
*pkg
, char *list
)
677 abstract_pkg_t
*ab_pkg
, *old_abpkg
, **tmp
, **replaces
= NULL
;
679 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
682 ab_pkg
->pkgs
= pkg_vec_alloc();
684 abstract_pkg_vec_insert(ab_pkg
->provided_by
, ab_pkg
);
686 for (count
= 1, item
= strtok_r(list
, ", ", &tok
);
688 count
++, item
= strtok_r(NULL
, ", ", &tok
), count
++) {
689 tmp
= realloc(replaces
, sizeof(abstract_pkg_t
*) * (count
+ 1));
694 old_abpkg
= ensure_abstract_pkg_by_name(item
);
696 if (pkg
->state_flag
& SF_NEED_DETAIL
) {
697 if (!(old_abpkg
->state_flag
& SF_NEED_DETAIL
)) {
698 opkg_msg(DEBUG
, "propagating pkg flag to replaced abpkg %s\n",
700 old_abpkg
->state_flag
|= SF_NEED_DETAIL
;
704 if (!old_abpkg
->replaced_by
)
705 old_abpkg
->replaced_by
= abstract_pkg_vec_alloc();
707 /* if a package pkg both replaces and conflicts old_abpkg,
708 * then add it to the replaced_by vector so that old_abpkg
709 * will be upgraded to ab_pkg automatically */
710 if (pkg_conflicts_abstract(pkg
, old_abpkg
)) {
711 if (!abstract_pkg_vec_contains(old_abpkg
->replaced_by
, ab_pkg
))
712 abstract_pkg_vec_insert(old_abpkg
->replaced_by
, ab_pkg
);
716 replaces
[count
- 1] = old_abpkg
;
722 replaces
[count
- 1] = NULL
;
724 pkg_set_ptr(pkg
, PKG_REPLACES
, replaces
);
727 void buildProvides(abstract_pkg_t
* ab_pkg
, pkg_t
* pkg
)
732 /* every pkg provides itself */
733 pkg
->provides_count
++;
734 abstract_pkg_vec_insert(ab_pkg
->provided_by
, ab_pkg
);
735 pkg
->provides
= xcalloc(pkg
->provides_count
, sizeof(abstract_pkg_t
*));
736 pkg
->provides
[0] = ab_pkg
;
737 for (i
= 1; i
< pkg
->provides_count
; i
++) {
738 abstract_pkg_t
*provided_abpkg
=
739 ensure_abstract_pkg_by_name(pkg
->provides_str
[i
- 1]);
740 free(pkg
->provides_str
[i
- 1]);
742 pkg
->provides
[i
] = provided_abpkg
;
744 abstract_pkg_vec_insert(provided_abpkg
->provided_by
, ab_pkg
);
746 if (pkg
->provides_str
)
747 free(pkg
->provides_str
);
751 void buildConflicts(pkg_t
* pkg
)
755 compound_depend_t *conflicts, *conflict;
757 if (!pkg->conflicts_count)
760 conflicts = pkg->conflicts =
761 xcalloc(pkg->conflicts_count, sizeof(compound_depend_t));
762 for (i = 0; i < pkg->conflicts_count; i++) {
763 conflicts->type = CONFLICTS;
764 parseDepends(conflicts, pkg->conflicts_str[i]);
765 free(pkg->conflicts_str[i]);
768 if (pkg->conflicts_str)
769 free(pkg->conflicts_str);
773 void buildReplaces(abstract_pkg_t
* ab_pkg
, pkg_t
* pkg
)
778 if (!pkg
->replaces_count
)
781 pkg
->replaces
= xcalloc(pkg
->replaces_count
, sizeof(abstract_pkg_t
*));
783 for (i
= 0; i
< pkg
->replaces_count
; i
++) {
784 abstract_pkg_t
*old_abpkg
=
785 ensure_abstract_pkg_by_name(pkg
->replaces_str
[i
]);
787 pkg
->replaces
[i
] = old_abpkg
;
788 free(pkg
->replaces_str
[i
]);
790 if (!old_abpkg
->replaced_by
)
791 old_abpkg
->replaced_by
= abstract_pkg_vec_alloc();
792 /* if a package pkg both replaces and conflicts old_abpkg,
793 * then add it to the replaced_by vector so that old_abpkg
794 * will be upgraded to ab_pkg automatically */
795 if (pkg_conflicts_abstract(pkg
, old_abpkg
))
796 abstract_pkg_vec_insert(old_abpkg
->replaced_by
, ab_pkg
);
799 if (pkg
->replaces_str
)
800 free(pkg
->replaces_str
);
804 void parse_deplist(pkg_t
*pkg
, enum depend_type type
, char *list
)
808 compound_depend_t
*tmp
, *deps
;
828 deps
= pkg_get_ptr(pkg
, id
);
830 for (tmp
= deps
, count
= 1; tmp
&& tmp
->type
; tmp
++)
833 for (item
= strtok_r(list
, ",", &tok
); item
; item
= strtok_r(NULL
, ",", &tok
), count
++) {
834 tmp
= realloc(deps
, sizeof(compound_depend_t
) * (count
+ 1));
841 memset(deps
+ count
- 1, 0, sizeof(compound_depend_t
));
842 parseDepends(deps
+ count
- 1, item
, type
);
848 memset(deps
+ count
- 1, 0, sizeof(compound_depend_t
));
849 pkg_set_ptr(pkg
, id
, deps
);
852 void buildDepends(pkg_t
* pkg
)
857 compound_depend_t
*depends
;
861 pkg
->pre_depends_count
+ pkg
->depends_count
+
862 pkg
->recommends_count
+ pkg
->suggests_count
))
865 depends
= pkg
->depends
= xcalloc(count
, sizeof(compound_depend_t
));
867 for (i
= 0; i
< pkg
->pre_depends_count
; i
++) {
868 parseDepends(depends
, pkg
->pre_depends_str
[i
]);
869 free(pkg
->pre_depends_str
[i
]);
870 depends
->type
= PREDEPEND
;
873 if (pkg
->pre_depends_str
)
874 free(pkg
->pre_depends_str
);
876 for (i
= 0; i
< pkg
->depends_count
; i
++) {
877 parseDepends(depends
, pkg
->depends_str
[i
]);
878 free(pkg
->depends_str
[i
]);
881 if (pkg
->depends_str
)
882 free(pkg
->depends_str
);
884 for (i
= 0; i
< pkg
->recommends_count
; i
++) {
885 parseDepends(depends
, pkg
->recommends_str
[i
]);
886 free(pkg
->recommends_str
[i
]);
887 depends
->type
= RECOMMEND
;
890 if (pkg
->recommends_str
)
891 free(pkg
->recommends_str
);
893 for (i
= 0; i
< pkg
->suggests_count
; i
++) {
894 parseDepends(depends
, pkg
->suggests_str
[i
]);
895 free(pkg
->suggests_str
[i
]);
896 depends
->type
= SUGGEST
;
899 if (pkg
->suggests_str
)
900 free(pkg
->suggests_str
);
905 const char *constraint_to_str(enum version_constraint c
)
926 * Returns a printable string for pkg's dependency at the specified idx. The
927 * resultant string must be passed to free() by the caller.
929 char *pkg_depend_str(pkg_t
* pkg
, int idx
)
934 compound_depend_t
*cdep
= NULL
, *p
;
937 for (i
= 0, p
= pkg_get_ptr(pkg
, PKG_DEPENDS
); p
&& p
->type
; i
++, p
++)
948 /* calculate string length */
949 for (i
= 0; i
< cdep
->possibility_count
; i
++) {
950 dep
= cdep
->possibilities
[i
];
953 len
+= 3; /* space, pipe, space */
955 len
+= strlen(dep
->pkg
->name
);
958 len
+= 2; /* space, left parenthesis */
959 len
+= 3; /* constraint string (<=, >=, etc), space */
960 len
+= strlen(dep
->version
);
961 len
+= 1; /* right parenthesis */
965 str
= xmalloc(len
+ 1); /* +1 for the NULL terminator */
968 for (i
= 0; i
< cdep
->possibility_count
; i
++) {
969 dep
= cdep
->possibilities
[i
];
972 strncat(str
, " | ", len
);
974 strncat(str
, dep
->pkg
->name
, len
);
977 strncat(str
, " (", len
);
978 strncat(str
, constraint_to_str(dep
->constraint
), len
);
979 strncat(str
, dep
->version
, len
);
980 strncat(str
, ")", len
);
987 void buildDependedUponBy(pkg_t
* pkg
, abstract_pkg_t
* ab_pkg
)
989 compound_depend_t
*depends
;
992 abstract_pkg_t
*ab_depend
;
993 abstract_pkg_t
**temp
;
995 for (depends
= pkg_get_ptr(pkg
, PKG_DEPENDS
); depends
&& depends
->type
; depends
++) {
996 if (depends
->type
!= PREDEPEND
997 && depends
->type
!= DEPEND
&& depends
->type
!= RECOMMEND
)
999 for (j
= 0; j
< depends
->possibility_count
; j
++) {
1000 ab_depend
= depends
->possibilities
[j
]->pkg
;
1001 if (!ab_depend
->depended_upon_by
) {
1002 ab_depend
->depended_upon_by
=
1003 xcalloc(1, sizeof(abstract_pkg_t
*));
1006 temp
= ab_depend
->depended_upon_by
;
1014 ab_depend
->depended_upon_by
=
1015 xrealloc(ab_depend
->depended_upon_by
,
1017 1) * sizeof(abstract_pkg_t
*));
1019 /* the array may have been moved by realloc */
1020 temp
= ab_depend
->depended_upon_by
+ othercount
;
1026 static depend_t
*depend_init(void)
1028 depend_t
*d
= xcalloc(1, sizeof(depend_t
));
1029 d
->constraint
= NONE
;
1036 static int parseDepends(compound_depend_t
* compound_depend
, char *depend_str
, enum depend_type type
)
1039 char *depend
, *name
, *vstr
, *rest
, *tok
= NULL
;
1040 depend_t
**possibilities
= NULL
, **tmp
;
1042 compound_depend
->type
= type
;
1044 for (i
= 0, depend
= strtok_r(depend_str
, "|", &tok
); depend
; i
++, depend
= strtok_r(NULL
, "|", &tok
)) {
1045 name
= strtok(depend
, " ");
1046 rest
= strtok(NULL
, "\n");
1048 tmp
= realloc(possibilities
, sizeof(tmp
) * (i
+ 1));
1053 possibilities
= tmp
;
1054 possibilities
[i
] = depend_init();
1055 possibilities
[i
]->pkg
= ensure_abstract_pkg_by_name(name
);
1057 if (rest
&& *rest
== '(') {
1058 vstr
= strtok(rest
+ 1, ")");
1060 if (!strncmp(vstr
, "<<", 2)) {
1061 possibilities
[i
]->constraint
= EARLIER
;
1063 } else if (!strncmp(vstr
, "<=", 2)) {
1064 possibilities
[i
]->constraint
= EARLIER_EQUAL
;
1066 } else if (!strncmp(vstr
, ">=", 2)) {
1067 possibilities
[i
]->constraint
= LATER_EQUAL
;
1069 } else if (!strncmp(vstr
, ">>", 2)) {
1070 possibilities
[i
]->constraint
= LATER
;
1072 } else if (!strncmp(vstr
, "=", 1)) {
1073 possibilities
[i
]->constraint
= EQUAL
;
1076 /* should these be here to support deprecated designations; dpkg does */
1077 else if (!strncmp(vstr
, "<", 1)) {
1078 possibilities
[i
]->constraint
= EARLIER_EQUAL
;
1080 } else if (!strncmp(vstr
, ">", 1)) {
1081 possibilities
[i
]->constraint
= LATER_EQUAL
;
1085 possibilities
[i
]->version
= trim_xstrdup(vstr
);
1086 rest
= strtok(NULL
, " ");
1089 rest
= strtok(rest
, " ");
1092 if (rest
&& *rest
== '*')
1093 compound_depend
->type
= GREEDY_DEPEND
;
1096 compound_depend
->possibility_count
= i
;
1097 compound_depend
->possibilities
= possibilities
;
1102 compound_depend_t
*pkg_get_depends(pkg_t
*pkg
, enum depend_type type
)
1104 compound_depend_t
*dep
;
1106 for (dep
= pkg_get_ptr(pkg
, PKG_DEPENDS
); dep
&& dep
->type
; dep
++)
1107 if (type
== UNSPEC
|| dep
->type
== type
)