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
)
41 && !(pkg
->state_flag
& SF_IGNORE
))
47 static int pkg_constraint_satisfied(pkg_t
* pkg
, void *cdata
)
49 depend_t
*depend
= (depend_t
*) cdata
;
50 if (version_constraints_satisfied(depend
, pkg
)
51 && !(pkg
->state_flag
& SF_IGNORE
))
57 /* returns ndependencies or negative error value */
59 pkg_hash_fetch_unsatisfied_dependencies(pkg_t
* pkg
, pkg_vec_t
* unsatisfied
,
62 pkg_t
*satisfier_entry_pkg
;
66 abstract_pkg_t
*ab_pkg
;
67 compound_depend_t
*compound_depend
;
70 * this is a setup to check for redundant/cyclic dependency checks,
71 * which are marked at the abstract_pkg level
73 if (!(ab_pkg
= pkg
->parent
)) {
74 opkg_msg(ERROR
, "Internal error, with pkg %s.\n", pkg
->name
);
78 if (ab_pkg
->dependencies_checked
) { /* avoid duplicate or cyclic checks */
82 ab_pkg
->dependencies_checked
= 1; /* mark it for subsequent visits */
85 compound_depend
= pkg_get_ptr(pkg
, PKG_DEPENDS
);
87 if (!compound_depend
|| !compound_depend
->type
) {
94 /* foreach dependency */
95 for (i
= 0; compound_depend
&& compound_depend
->type
; compound_depend
++, i
++) {
96 depend_t
**possible_satisfiers
=
97 compound_depend
->possibilities
;;
99 satisfier_entry_pkg
= NULL
;
101 if (compound_depend
->type
== GREEDY_DEPEND
) {
102 /* foreach possible satisfier */
103 for (j
= 0; j
< compound_depend
->possibility_count
; j
++) {
104 /* foreach provided_by, which includes the abstract_pkg itself */
105 abstract_pkg_t
*abpkg
= possible_satisfiers
[j
]->pkg
;
106 abstract_pkg_vec_t
*ab_provider_vec
= abpkg
->provided_by
;
107 int nposs
= ab_provider_vec
->len
;
108 abstract_pkg_t
**ab_providers
= ab_provider_vec
->pkgs
;
110 for (l
= 0; l
< nposs
; l
++) {
111 pkg_vec_t
*test_vec
= ab_providers
[l
]->pkgs
;
112 /* if no depends on this one, try the first package that Provides this one */
113 if (!test_vec
) { /* no pkg_vec hooked up to the abstract_pkg! (need another feed?) */
117 /* cruise this possiblity's pkg_vec looking for an installed version */
118 for (k
= 0; k
< test_vec
->len
; k
++) {
119 pkg_t
*pkg_scout
= test_vec
->pkgs
[k
];
120 /* not installed, and not already known about? */
121 if ((pkg_scout
->state_want
!= SW_INSTALL
)
122 && !pkg_scout
->parent
->dependencies_checked
123 && !is_pkg_in_pkg_vec(unsatisfied
, pkg_scout
)) {
124 char **newstuff
= NULL
;
126 pkg_vec_t
*tmp_vec
= pkg_vec_alloc();
127 /* check for not-already-installed dependencies */
128 rc
= pkg_hash_fetch_unsatisfied_dependencies(
129 pkg_scout
, tmp_vec
, &newstuff
);
130 if (newstuff
== NULL
) {
133 for (m
= 0; m
< rc
; m
++) {
134 pkg_t
*p
= tmp_vec
->pkgs
[m
];
135 if (p
->state_want
== SW_INSTALL
)
138 "Not installing %s due"
139 " to requirement for %s.\n",
140 pkg_scout
->name
, p
->name
);
144 pkg_vec_free(tmp_vec
);
146 /* mark this one for installation */
148 "Adding satisfier for greedy"
151 pkg_vec_insert(unsatisfied
, pkg_scout
);
155 "Not installing %s due to "
168 /* foreach possible satisfier, look for installed package */
169 for (j
= 0; j
< compound_depend
->possibility_count
; j
++) {
170 /* foreach provided_by, which includes the abstract_pkg itself */
171 depend_t
*dependence_to_satisfy
= possible_satisfiers
[j
];
172 abstract_pkg_t
*satisfying_apkg
= possible_satisfiers
[j
]->pkg
;
173 pkg_t
*satisfying_pkg
=
174 pkg_hash_fetch_best_installation_candidate(satisfying_apkg
,
175 pkg_installed_and_constraint_satisfied
,
176 dependence_to_satisfy
, 1);
177 /* Being that I can't test constraing in pkg_hash, I will test it here */
178 if (satisfying_pkg
!= NULL
) {
179 if (!pkg_installed_and_constraint_satisfied
180 (satisfying_pkg
, dependence_to_satisfy
)) {
181 satisfying_pkg
= NULL
;
184 opkg_msg(DEBUG
, "satisfying_pkg=%p\n", satisfying_pkg
);
185 if (satisfying_pkg
!= NULL
) {
191 /* if nothing installed matches, then look for uninstalled satisfier */
193 /* foreach possible satisfier, look for installed package */
194 for (j
= 0; j
< compound_depend
->possibility_count
; j
++) {
195 /* foreach provided_by, which includes the abstract_pkg itself */
196 depend_t
*dependence_to_satisfy
= possible_satisfiers
[j
];
197 abstract_pkg_t
*satisfying_apkg
= possible_satisfiers
[j
]->pkg
;
198 pkg_t
*satisfying_pkg
=
199 pkg_hash_fetch_best_installation_candidate(satisfying_apkg
,
200 pkg_constraint_satisfied
, dependence_to_satisfy
, 1);
201 /* Being that I can't test constraing in pkg_hash, I will test it here too */
202 if (satisfying_pkg
!= NULL
) {
203 if (!pkg_constraint_satisfied(satisfying_pkg
,
204 dependence_to_satisfy
)) {
205 satisfying_pkg
= NULL
;
209 /* user request overrides package recommendation */
210 if (satisfying_pkg
!= NULL
211 && (compound_depend
->type
== RECOMMEND
212 || compound_depend
->type
== SUGGEST
)
213 && (satisfying_pkg
->state_want
== SW_DEINSTALL
214 || satisfying_pkg
->state_want
== SW_PURGE
)) {
216 "%s: ignoring recommendation for "
217 "%s at user request\n",
218 pkg
->name
, satisfying_pkg
->name
);
222 opkg_msg(DEBUG
, "satisfying_pkg=%p\n",
224 if (satisfying_pkg
!= NULL
) {
225 satisfier_entry_pkg
= satisfying_pkg
;
231 /* we didn't find one, add something to the unsatisfied vector */
233 if (!satisfier_entry_pkg
) {
234 /* failure to meet recommendations is not an error */
235 if (compound_depend
->type
!= RECOMMEND
236 && compound_depend
->type
!= SUGGEST
)
237 the_lost
= add_unresolved_dep(pkg
, the_lost
, i
);
240 "%s: unsatisfied recommendation for %s\n",
242 compound_depend
->possibilities
[0]->pkg
->name
);
244 if (compound_depend
->type
== SUGGEST
) {
245 /* just mention it politely */
247 "package %s suggests installing %s\n",
248 pkg
->name
, satisfier_entry_pkg
->name
);
250 char **newstuff
= NULL
;
252 if (satisfier_entry_pkg
!= pkg
&&
253 !is_pkg_in_pkg_vec(unsatisfied
, satisfier_entry_pkg
))
255 pkg_hash_fetch_unsatisfied_dependencies(
256 satisfier_entry_pkg
, unsatisfied
, &newstuff
);
257 pkg_vec_insert(unsatisfied
, satisfier_entry_pkg
);
258 the_lost
= merge_unresolved(the_lost
, newstuff
);
266 *unresolved
= the_lost
;
268 return unsatisfied
->len
;
271 /*checking for conflicts !in replaces
272 If a packages conflicts with another but is also replacing it, I should not consider it a
274 returns 0 if conflicts <> replaces or 1 if conflicts == replaces
276 static int is_pkg_a_replaces(pkg_t
* pkg_scout
, pkg_t
* pkg
)
278 abstract_pkg_t
**replaces
= pkg_get_ptr(pkg
, PKG_REPLACES
);
280 if (!replaces
|| !*replaces
)
284 if (strcmp(pkg_scout
->name
, (*replaces
)->name
) == 0) { // Found
285 opkg_msg(DEBUG2
, "Seems I've found a replace %s %s\n",
286 pkg_scout
->name
, (*replaces
)->name
);
295 pkg_vec_t
*pkg_hash_fetch_conflicts(pkg_t
* pkg
)
297 pkg_vec_t
*installed_conflicts
, *test_vec
;
298 compound_depend_t
*conflicts
, *conflict
;
299 depend_t
**possible_satisfiers
;
300 depend_t
*possible_satisfier
;
302 abstract_pkg_t
*ab_pkg
;
307 * this is a setup to check for redundant/cyclic dependency checks,
308 * which are marked at the abstract_pkg level
310 if (!(ab_pkg
= pkg
->parent
)) {
311 opkg_msg(ERROR
, "Internal error: %s not in hash table\n",
313 return (pkg_vec_t
*) NULL
;
316 conflicts
= pkg_get_ptr(pkg
, PKG_CONFLICTS
);
318 return (pkg_vec_t
*) NULL
;
320 installed_conflicts
= pkg_vec_alloc();
322 /* foreach conflict */
323 for (conflict
= conflicts
; conflict
->type
; conflict
++ ) {
324 possible_satisfiers
= conflicts
->possibilities
;
326 /* foreach possible satisfier */
327 for (j
= 0; j
< conflicts
->possibility_count
; j
++) {
328 possible_satisfier
= possible_satisfiers
[j
];
329 if (!possible_satisfier
)
331 "Internal error: possible_satisfier=NULL\n");
332 if (!possible_satisfier
->pkg
)
334 "Internal error: possible_satisfier->pkg=NULL\n");
335 test_vec
= possible_satisfier
->pkg
->pkgs
;
337 /* pkg_vec found, it is an actual package conflict
338 * cruise this possiblity's pkg_vec looking for an installed version */
339 pkg_scouts
= test_vec
->pkgs
;
340 for (k
= 0; k
< test_vec
->len
; k
++) {
341 pkg_scout
= pkg_scouts
[k
];
344 "Internal error: pkg_scout=NULL\n");
347 if ((pkg_scout
->state_status
== SS_INSTALLED
348 || pkg_scout
->state_want
== SW_INSTALL
)
349 && version_constraints_satisfied(possible_satisfier
,
351 && !is_pkg_a_replaces(pkg_scout
, pkg
)) {
352 if (!is_pkg_in_pkg_vec(installed_conflicts
,
354 pkg_vec_insert(installed_conflicts
, pkg_scout
);
363 if (installed_conflicts
->len
)
364 return installed_conflicts
;
365 pkg_vec_free(installed_conflicts
);
366 return (pkg_vec_t
*) NULL
;
369 int version_constraints_satisfied(depend_t
* depends
, pkg_t
* pkg
)
374 if (depends
->constraint
== NONE
)
379 parse_version(temp
, depends
->version
);
381 comparison
= pkg_compare_versions(pkg
, temp
);
386 if ((depends
->constraint
== EARLIER
) && (comparison
< 0))
388 else if ((depends
->constraint
== LATER
) && (comparison
> 0))
390 else if (comparison
== 0)
392 else if ((depends
->constraint
== LATER_EQUAL
) && (comparison
>= 0))
394 else if ((depends
->constraint
== EARLIER_EQUAL
) && (comparison
<= 0))
400 int pkg_dependence_satisfiable(depend_t
* depend
)
402 abstract_pkg_t
*apkg
= depend
->pkg
;
403 abstract_pkg_vec_t
*provider_apkgs
= apkg
->provided_by
;
404 int n_providers
= provider_apkgs
->len
;
405 abstract_pkg_t
**apkgs
= provider_apkgs
->pkgs
;
411 for (i
= 0; i
< n_providers
; i
++) {
412 abstract_pkg_t
*papkg
= apkgs
[i
];
413 pkg_vec
= papkg
->pkgs
;
415 n_pkgs
= pkg_vec
->len
;
416 for (j
= 0; j
< n_pkgs
; j
++) {
417 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
418 if (version_constraints_satisfied(depend
, pkg
)) {
427 static int is_pkg_in_pkg_vec(pkg_vec_t
* vec
, pkg_t
* pkg
)
431 pkg_t
**pkgs
= vec
->pkgs
;
432 arch1
= pkg_get_architecture(pkg
);
434 for (i
= 0; i
< vec
->len
; i
++) {
435 arch2
= pkg_get_architecture(*(pkgs
+ i
));
437 if ((strcmp(pkg
->name
, (*(pkgs
+ i
))->name
) == 0)
438 && (pkg_compare_versions(pkg
, *(pkgs
+ i
)) == 0)
439 && (strcmp(arch1
, arch2
) == 0))
446 * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
449 int pkg_replaces(pkg_t
* pkg
, pkg_t
* replacee
)
451 abstract_pkg_t
**replaces
= pkg_get_ptr(pkg
, PKG_REPLACES
);
452 abstract_pkg_t
**provides
= pkg_get_ptr(replacee
, PKG_PROVIDES
);
453 abstract_pkg_t
**r
, **p
;
455 for (r
= replaces
; r
&& *r
; r
++)
456 for (p
= provides
; p
&& *p
; p
++)
464 * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
467 int pkg_conflicts_abstract(pkg_t
* pkg
, abstract_pkg_t
* conflictee
)
470 compound_depend_t
*conflicts
;
472 for (conflicts
= pkg_get_ptr(pkg
, PKG_CONFLICTS
), i
= 0; conflicts
&& conflicts
[i
].type
; i
++)
473 for (j
= 0; j
< conflicts
[i
].possibility_count
; j
++)
474 if (conflicts
[i
].possibilities
[j
]->pkg
== conflictee
)
481 * pkg_conflicts returns 1 if pkg->conflicts contains one of
482 * conflictee's provides and 0 otherwise.
484 int pkg_conflicts(pkg_t
* pkg
, pkg_t
* conflictee
)
487 compound_depend_t
*conflicts
;
488 abstract_pkg_t
**provider
;
490 for (conflicts
= pkg_get_ptr(pkg
, PKG_CONFLICTS
), i
= 0; conflicts
&& conflicts
[i
].type
; i
++)
491 for (j
= 0; j
< conflicts
[i
].possibility_count
; j
++)
492 for (provider
= pkg_get_ptr(conflictee
, PKG_PROVIDES
); provider
&& *provider
; provider
++)
493 if (conflicts
[i
].possibilities
[j
]->pkg
== *provider
)
499 static char **merge_unresolved(char **oldstuff
, char **newstuff
)
501 int oldlen
= 0, newlen
= 0;
508 while (oldstuff
&& oldstuff
[oldlen
])
510 while (newstuff
&& newstuff
[newlen
])
513 result
= xrealloc(oldstuff
, sizeof(char *) * (oldlen
+ newlen
+ 1));
515 for (i
= oldlen
, j
= 0; i
< (oldlen
+ newlen
); i
++, j
++)
516 *(result
+ i
) = *(newstuff
+ j
);
518 *(result
+ i
) = NULL
;
524 * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
525 * this is null terminated, no count is carried around
527 char **add_unresolved_dep(pkg_t
* pkg
, char **the_lost
, int ref_ndx
)
533 while (the_lost
&& the_lost
[count
])
536 count
++; /* need one to hold the null */
537 resized
= xrealloc(the_lost
, sizeof(char *) * (count
+ 1));
538 resized
[count
- 1] = pkg_depend_str(pkg
, ref_ndx
);
539 resized
[count
] = NULL
;
544 static void flag_related_packages(pkg_t
*pkg
, int state_flags
)
547 compound_depend_t
*deps
;
549 for (deps
= pkg_get_ptr(pkg
, PKG_DEPENDS
), i
= 0; deps
&& deps
[i
].type
; i
++)
550 for (j
= 0; j
< deps
[i
].possibility_count
; j
++) {
551 if ((deps
[i
].possibilities
[j
]->pkg
->state_flag
& state_flags
) != state_flags
) {
552 opkg_msg(DEBUG
, "propagating pkg flag to dependent abpkg %s\n",
553 deps
[i
].possibilities
[j
]->pkg
->name
);
554 deps
[i
].possibilities
[j
]->pkg
->state_flag
|= state_flags
;
558 for (deps
= pkg_get_ptr(pkg
, PKG_CONFLICTS
), i
= 0; deps
&& deps
[i
].type
; i
++)
559 for (j
= 0; j
< deps
[i
].possibility_count
; j
++) {
560 if ((deps
[i
].possibilities
[j
]->pkg
->state_flag
& state_flags
) != state_flags
) {
561 opkg_msg(DEBUG
, "propagating pkg flag to conflicting abpkg %s\n",
562 deps
[i
].possibilities
[j
]->pkg
->name
);
563 deps
[i
].possibilities
[j
]->pkg
->state_flag
|= state_flags
;
568 abstract_pkg_t
**init_providelist(pkg_t
*pkg
, int *count
)
570 abstract_pkg_t
*ab_pkg
;
571 abstract_pkg_t
**provides
= pkg_get_ptr(pkg
, PKG_PROVIDES
);
574 provides
= calloc(2, sizeof(abstract_pkg_t
*));
583 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
586 ab_pkg
->pkgs
= pkg_vec_alloc();
588 if (!abstract_pkg_vec_contains(ab_pkg
->provided_by
, ab_pkg
))
589 abstract_pkg_vec_insert(ab_pkg
->provided_by
, ab_pkg
);
591 provides
[0] = ab_pkg
;
597 pkg_set_ptr(pkg
, PKG_PROVIDES
, provides
);
600 for (*count
= 1; *provides
; provides
++) {
601 if (pkg
->state_flag
& SF_NEED_DETAIL
) {
602 if (!((*provides
)->state_flag
& SF_NEED_DETAIL
)) {
603 opkg_msg(DEBUG
, "propagating pkg flag to provided abpkg %s\n",
605 (*provides
)->state_flag
|= SF_NEED_DETAIL
;
612 flag_related_packages(pkg
, SF_NEED_DETAIL
);
617 void parse_providelist(pkg_t
*pkg
, char *list
)
621 abstract_pkg_t
*ab_pkg
, *provided_abpkg
, **tmp
, **provides
;
623 provides
= init_providelist(pkg
, &count
);
624 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
626 if (!provides
|| !ab_pkg
)
629 for (item
= strtok_r(list
, ", ", &tok
); item
;
630 count
++, item
= strtok_r(NULL
, ", ", &tok
)) {
631 tmp
= realloc(provides
, sizeof(abstract_pkg_t
*) * (count
+ 1));
636 provided_abpkg
= ensure_abstract_pkg_by_name(item
);
638 if (provided_abpkg
->state_flag
& SF_NEED_DETAIL
) {
639 if (!(ab_pkg
->state_flag
& SF_NEED_DETAIL
)) {
640 opkg_msg(DEBUG
, "propagating provided abpkg flag to "
641 "provider abpkg %s\n", ab_pkg
->name
);
642 ab_pkg
->state_flag
|= SF_NEED_DETAIL
;
646 if (!abstract_pkg_vec_contains(provided_abpkg
->provided_by
, ab_pkg
))
647 abstract_pkg_vec_insert(provided_abpkg
->provided_by
, ab_pkg
);
650 provides
[count
- 1] = provided_abpkg
;
653 provides
[count
- 1] = NULL
;
655 pkg_set_ptr(pkg
, PKG_PROVIDES
, provides
);
658 void parse_replacelist(pkg_t
*pkg
, char *list
)
662 abstract_pkg_t
*ab_pkg
, *old_abpkg
, **tmp
, **replaces
= NULL
;
664 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
667 ab_pkg
->pkgs
= pkg_vec_alloc();
669 abstract_pkg_vec_insert(ab_pkg
->provided_by
, ab_pkg
);
671 for (count
= 1, item
= strtok_r(list
, ", ", &tok
);
673 count
++, item
= strtok_r(NULL
, ", ", &tok
), count
++) {
674 tmp
= realloc(replaces
, sizeof(abstract_pkg_t
*) * (count
+ 1));
679 old_abpkg
= ensure_abstract_pkg_by_name(item
);
681 if (pkg
->state_flag
& SF_NEED_DETAIL
) {
682 if (!(old_abpkg
->state_flag
& SF_NEED_DETAIL
)) {
683 opkg_msg(DEBUG
, "propagating pkg flag to replaced abpkg %s\n",
685 old_abpkg
->state_flag
|= SF_NEED_DETAIL
;
689 if (!old_abpkg
->replaced_by
)
690 old_abpkg
->replaced_by
= abstract_pkg_vec_alloc();
692 /* if a package pkg both replaces and conflicts old_abpkg,
693 * then add it to the replaced_by vector so that old_abpkg
694 * will be upgraded to ab_pkg automatically */
695 if (pkg_conflicts_abstract(pkg
, old_abpkg
)) {
696 if (!abstract_pkg_vec_contains(old_abpkg
->replaced_by
, ab_pkg
))
697 abstract_pkg_vec_insert(old_abpkg
->replaced_by
, ab_pkg
);
701 replaces
[count
- 1] = old_abpkg
;
707 replaces
[count
- 1] = NULL
;
709 pkg_set_ptr(pkg
, PKG_REPLACES
, replaces
);
712 void buildProvides(abstract_pkg_t
* ab_pkg
, pkg_t
* pkg
)
717 /* every pkg provides itself */
718 pkg
->provides_count
++;
719 abstract_pkg_vec_insert(ab_pkg
->provided_by
, ab_pkg
);
720 pkg
->provides
= xcalloc(pkg
->provides_count
, sizeof(abstract_pkg_t
*));
721 pkg
->provides
[0] = ab_pkg
;
722 for (i
= 1; i
< pkg
->provides_count
; i
++) {
723 abstract_pkg_t
*provided_abpkg
=
724 ensure_abstract_pkg_by_name(pkg
->provides_str
[i
- 1]);
725 free(pkg
->provides_str
[i
- 1]);
727 pkg
->provides
[i
] = provided_abpkg
;
729 abstract_pkg_vec_insert(provided_abpkg
->provided_by
, ab_pkg
);
731 if (pkg
->provides_str
)
732 free(pkg
->provides_str
);
736 void buildConflicts(pkg_t
* pkg
)
740 compound_depend_t *conflicts, *conflict;
742 if (!pkg->conflicts_count)
745 conflicts = pkg->conflicts =
746 xcalloc(pkg->conflicts_count, sizeof(compound_depend_t));
747 for (i = 0; i < pkg->conflicts_count; i++) {
748 conflicts->type = CONFLICTS;
749 parseDepends(conflicts, pkg->conflicts_str[i]);
750 free(pkg->conflicts_str[i]);
753 if (pkg->conflicts_str)
754 free(pkg->conflicts_str);
758 void buildReplaces(abstract_pkg_t
* ab_pkg
, pkg_t
* pkg
)
763 if (!pkg
->replaces_count
)
766 pkg
->replaces
= xcalloc(pkg
->replaces_count
, sizeof(abstract_pkg_t
*));
768 for (i
= 0; i
< pkg
->replaces_count
; i
++) {
769 abstract_pkg_t
*old_abpkg
=
770 ensure_abstract_pkg_by_name(pkg
->replaces_str
[i
]);
772 pkg
->replaces
[i
] = old_abpkg
;
773 free(pkg
->replaces_str
[i
]);
775 if (!old_abpkg
->replaced_by
)
776 old_abpkg
->replaced_by
= abstract_pkg_vec_alloc();
777 /* if a package pkg both replaces and conflicts old_abpkg,
778 * then add it to the replaced_by vector so that old_abpkg
779 * will be upgraded to ab_pkg automatically */
780 if (pkg_conflicts_abstract(pkg
, old_abpkg
))
781 abstract_pkg_vec_insert(old_abpkg
->replaced_by
, ab_pkg
);
784 if (pkg
->replaces_str
)
785 free(pkg
->replaces_str
);
789 void parse_deplist(pkg_t
*pkg
, enum depend_type type
, char *list
)
793 compound_depend_t
*tmp
, *deps
;
813 deps
= pkg_get_ptr(pkg
, id
);
815 for (tmp
= deps
, count
= 1; tmp
&& tmp
->type
; tmp
++)
818 for (item
= strtok_r(list
, ",", &tok
); item
; item
= strtok_r(NULL
, ",", &tok
), count
++) {
819 tmp
= realloc(deps
, sizeof(compound_depend_t
) * (count
+ 1));
826 memset(deps
+ count
- 1, 0, sizeof(compound_depend_t
));
827 parseDepends(deps
+ count
- 1, item
, type
);
833 memset(deps
+ count
- 1, 0, sizeof(compound_depend_t
));
834 pkg_set_ptr(pkg
, id
, deps
);
837 void buildDepends(pkg_t
* pkg
)
842 compound_depend_t
*depends
;
846 pkg
->pre_depends_count
+ pkg
->depends_count
+
847 pkg
->recommends_count
+ pkg
->suggests_count
))
850 depends
= pkg
->depends
= xcalloc(count
, sizeof(compound_depend_t
));
852 for (i
= 0; i
< pkg
->pre_depends_count
; i
++) {
853 parseDepends(depends
, pkg
->pre_depends_str
[i
]);
854 free(pkg
->pre_depends_str
[i
]);
855 depends
->type
= PREDEPEND
;
858 if (pkg
->pre_depends_str
)
859 free(pkg
->pre_depends_str
);
861 for (i
= 0; i
< pkg
->depends_count
; i
++) {
862 parseDepends(depends
, pkg
->depends_str
[i
]);
863 free(pkg
->depends_str
[i
]);
866 if (pkg
->depends_str
)
867 free(pkg
->depends_str
);
869 for (i
= 0; i
< pkg
->recommends_count
; i
++) {
870 parseDepends(depends
, pkg
->recommends_str
[i
]);
871 free(pkg
->recommends_str
[i
]);
872 depends
->type
= RECOMMEND
;
875 if (pkg
->recommends_str
)
876 free(pkg
->recommends_str
);
878 for (i
= 0; i
< pkg
->suggests_count
; i
++) {
879 parseDepends(depends
, pkg
->suggests_str
[i
]);
880 free(pkg
->suggests_str
[i
]);
881 depends
->type
= SUGGEST
;
884 if (pkg
->suggests_str
)
885 free(pkg
->suggests_str
);
890 const char *constraint_to_str(enum version_constraint c
)
911 * Returns a printable string for pkg's dependency at the specified idx. The
912 * resultant string must be passed to free() by the caller.
914 char *pkg_depend_str(pkg_t
* pkg
, int idx
)
919 compound_depend_t
*cdep
= NULL
, *p
;
922 for (i
= 0, p
= pkg_get_ptr(pkg
, PKG_DEPENDS
); p
&& p
->type
; i
++, p
++)
933 /* calculate string length */
934 for (i
= 0; i
< cdep
->possibility_count
; i
++) {
935 dep
= cdep
->possibilities
[i
];
938 len
+= 3; /* space, pipe, space */
940 len
+= strlen(dep
->pkg
->name
);
943 len
+= 2; /* space, left parenthesis */
944 len
+= 3; /* constraint string (<=, >=, etc), space */
945 len
+= strlen(dep
->version
);
946 len
+= 1; /* right parenthesis */
950 str
= xmalloc(len
+ 1); /* +1 for the NULL terminator */
953 for (i
= 0; i
< cdep
->possibility_count
; i
++) {
954 dep
= cdep
->possibilities
[i
];
957 strncat(str
, " | ", len
);
959 strncat(str
, dep
->pkg
->name
, len
);
962 strncat(str
, " (", len
);
963 strncat(str
, constraint_to_str(dep
->constraint
), len
);
964 strncat(str
, dep
->version
, len
);
965 strncat(str
, ")", len
);
972 void buildDependedUponBy(pkg_t
* pkg
, abstract_pkg_t
* ab_pkg
)
974 compound_depend_t
*depends
;
977 abstract_pkg_t
*ab_depend
;
978 abstract_pkg_t
**temp
;
980 for (depends
= pkg_get_ptr(pkg
, PKG_DEPENDS
); depends
&& depends
->type
; depends
++) {
981 if (depends
->type
!= PREDEPEND
982 && depends
->type
!= DEPEND
&& depends
->type
!= RECOMMEND
)
984 for (j
= 0; j
< depends
->possibility_count
; j
++) {
985 ab_depend
= depends
->possibilities
[j
]->pkg
;
986 if (!ab_depend
->depended_upon_by
) {
987 ab_depend
->depended_upon_by
=
988 xcalloc(1, sizeof(abstract_pkg_t
*));
991 temp
= ab_depend
->depended_upon_by
;
999 ab_depend
->depended_upon_by
=
1000 xrealloc(ab_depend
->depended_upon_by
,
1002 1) * sizeof(abstract_pkg_t
*));
1004 /* the array may have been moved by realloc */
1005 temp
= ab_depend
->depended_upon_by
+ othercount
;
1011 static depend_t
*depend_init(void)
1013 depend_t
*d
= xcalloc(1, sizeof(depend_t
));
1014 d
->constraint
= NONE
;
1021 static int parseDepends(compound_depend_t
* compound_depend
, char *depend_str
, enum depend_type type
)
1024 char *depend
, *name
, *vstr
, *rest
, *tok
= NULL
;
1025 depend_t
**possibilities
= NULL
, **tmp
;
1027 compound_depend
->type
= type
;
1029 for (i
= 0, depend
= strtok_r(depend_str
, "|", &tok
); depend
; i
++, depend
= strtok_r(NULL
, "|", &tok
)) {
1030 name
= strtok(depend
, " ");
1031 rest
= strtok(NULL
, "\n");
1033 tmp
= realloc(possibilities
, sizeof(tmp
) * (i
+ 1));
1038 possibilities
= tmp
;
1039 possibilities
[i
] = depend_init();
1040 possibilities
[i
]->pkg
= ensure_abstract_pkg_by_name(name
);
1042 if (rest
&& *rest
== '(') {
1043 vstr
= strtok(rest
+ 1, ")");
1045 if (!strncmp(vstr
, "<<", 2)) {
1046 possibilities
[i
]->constraint
= EARLIER
;
1048 } else if (!strncmp(vstr
, "<=", 2)) {
1049 possibilities
[i
]->constraint
= EARLIER_EQUAL
;
1051 } else if (!strncmp(vstr
, ">=", 2)) {
1052 possibilities
[i
]->constraint
= LATER_EQUAL
;
1054 } else if (!strncmp(vstr
, ">>", 2)) {
1055 possibilities
[i
]->constraint
= LATER
;
1057 } else if (!strncmp(vstr
, "=", 1)) {
1058 possibilities
[i
]->constraint
= EQUAL
;
1061 /* should these be here to support deprecated designations; dpkg does */
1062 else if (!strncmp(vstr
, "<", 1)) {
1063 possibilities
[i
]->constraint
= EARLIER_EQUAL
;
1065 } else if (!strncmp(vstr
, ">", 1)) {
1066 possibilities
[i
]->constraint
= LATER_EQUAL
;
1070 possibilities
[i
]->version
= trim_xstrdup(vstr
);
1071 rest
= strtok(NULL
, " ");
1074 rest
= strtok(rest
, " ");
1077 if (rest
&& *rest
== '*')
1078 compound_depend
->type
= GREEDY_DEPEND
;
1081 compound_depend
->possibility_count
= i
;
1082 compound_depend
->possibilities
= possibilities
;
1087 compound_depend_t
*pkg_get_depends(pkg_t
*pkg
, enum depend_type type
)
1089 compound_depend_t
*dep
;
1091 for (dep
= pkg_get_ptr(pkg
, PKG_DEPENDS
); dep
&& dep
->type
; dep
++)
1092 if (type
== UNSPEC
|| dep
->type
== type
)