2 * Copyright (c) 2007-2009, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Transaction handling
20 #include "transaction.h"
29 obsq_sortcmp(const void *ap, const void *bp, void *dp)
33 Solvable *s, *oas, *obs;
44 s = pool->solvables + a;
45 oas = pool->solvables + oa;
46 obs = pool->solvables + ob;
47 if (oas->name != obs->name)
49 if (oas->name == s->name)
51 if (obs->name == s->name)
53 return strcmp(pool_id2str(pool, oas->name), pool_id2str(pool, obs->name));
55 r = pool_evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
57 return -r; /* highest version first */
62 transaction_all_obs_pkgs(Transaction *trans, Id p, Queue *pkgs)
64 Pool *pool = trans->pool;
65 Solvable *s = pool->solvables + p;
66 Queue *ti = &trans->transaction_info;
71 if (p <= 0 || !s->repo)
73 if (s->repo == pool->installed)
75 q = trans->transaction_installed[p - pool->installed->start];
83 /* find which packages obsolete us */
84 for (i = 0; i < ti->count; i += 2)
85 if (ti->elements[i + 1] == p)
88 queue_push(pkgs, ti->elements[i]);
92 solv_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
93 for (i = 0; i < pkgs->count; i += 2)
94 pkgs->elements[i / 2] = pkgs->elements[i + 1];
99 /* find the packages we obsolete */
100 for (i = 0; i < ti->count; i += 2)
102 if (ti->elements[i] == p)
103 queue_push(pkgs, ti->elements[i + 1]);
104 else if (pkgs->count)
111 transaction_obs_pkg(Transaction *trans, Id p)
113 Pool *pool = trans->pool;
114 Solvable *s = pool->solvables + p;
118 if (p <= 0 || !s->repo)
120 if (s->repo == pool->installed)
122 p = trans->transaction_installed[p - pool->installed->start];
123 return p < 0 ? -p : p;
125 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
126 transaction_all_obs_pkgs(trans, p, &ti);
127 p = ti.count ? ti.elements[0] : 0;
134 * calculate base type of transaction element
138 transaction_base_type(Transaction *trans, Id p)
140 Pool *pool = trans->pool;
145 if (!MAPTST(&trans->transactsmap, p))
146 return SOLVER_TRANSACTION_IGNORE;
147 p2 = transaction_obs_pkg(trans, p);
148 if (pool->installed && pool->solvables[p].repo == pool->installed)
152 return SOLVER_TRANSACTION_ERASE;
153 s = pool->solvables + p;
154 s2 = pool->solvables + p2;
155 if (s->name == s2->name)
157 if (s->evr == s2->evr && solvable_identical(s, s2))
158 return SOLVER_TRANSACTION_REINSTALLED;
159 r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
161 return SOLVER_TRANSACTION_UPGRADED;
163 return SOLVER_TRANSACTION_DOWNGRADED;
164 return SOLVER_TRANSACTION_CHANGED;
166 return SOLVER_TRANSACTION_OBSOLETED;
170 int noobs = trans->noobsmap.size && MAPTST(&trans->noobsmap, p);
172 return p2 ? SOLVER_TRANSACTION_MULTIREINSTALL : SOLVER_TRANSACTION_MULTIINSTALL;
174 return SOLVER_TRANSACTION_INSTALL;
175 s = pool->solvables + p;
176 s2 = pool->solvables + p2;
177 if (s->name == s2->name)
179 if (s->evr == s2->evr && solvable_identical(s, s2))
180 return SOLVER_TRANSACTION_REINSTALL;
181 r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
183 return SOLVER_TRANSACTION_UPGRADE;
185 return SOLVER_TRANSACTION_DOWNGRADE;
187 return SOLVER_TRANSACTION_CHANGE;
189 return SOLVER_TRANSACTION_OBSOLETES;
194 * return type of transaction element
196 * filtering is needed if either not all packages are shown
197 * or replaces are not shown, as otherwise parts of the
198 * transaction might not be shown to the user */
201 transaction_type(Transaction *trans, Id p, int mode)
203 Pool *pool = trans->pool;
204 Solvable *s = pool->solvables + p;
211 return SOLVER_TRANSACTION_IGNORE;
213 n = pool_id2str(pool, s->name);
214 if (!strncmp(n, "patch:", 6))
215 return SOLVER_TRANSACTION_IGNORE;
216 if (!strncmp(n, "pattern:", 8))
217 return SOLVER_TRANSACTION_IGNORE;
219 type = transaction_base_type(trans, p);
221 if (type == SOLVER_TRANSACTION_IGNORE)
222 return SOLVER_TRANSACTION_IGNORE; /* not part of the transaction */
224 if ((mode & SOLVER_TRANSACTION_RPM_ONLY) != 0)
226 /* application wants to know what to feed to rpm */
227 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
229 if (s->repo == pool->installed)
230 return SOLVER_TRANSACTION_IGNORE; /* ignore as we're being obsoleted */
231 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
232 return SOLVER_TRANSACTION_MULTIINSTALL;
233 return SOLVER_TRANSACTION_INSTALL;
236 if ((mode & SOLVER_TRANSACTION_SHOW_MULTIINSTALL) == 0)
238 /* application wants to make no difference between install
239 * and multiinstall */
240 if (type == SOLVER_TRANSACTION_MULTIINSTALL)
241 type = SOLVER_TRANSACTION_INSTALL;
242 if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
243 type = SOLVER_TRANSACTION_REINSTALL;
246 if ((mode & SOLVER_TRANSACTION_CHANGE_IS_REINSTALL))
248 /* application wants to make no difference between change
250 if (type == SOLVER_TRANSACTION_CHANGED)
251 type = SOLVER_TRANSACTION_REINSTALLED;
252 else if (type == SOLVER_TRANSACTION_CHANGE)
253 type = SOLVER_TRANSACTION_REINSTALL;
256 if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
259 if (s->repo == pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
261 /* erase element and we're showing the passive side */
262 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0 && type == SOLVER_TRANSACTION_OBSOLETED)
263 type = SOLVER_TRANSACTION_ERASE;
266 if (s->repo != pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
268 /* install element and we're showing the active side */
269 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0 && type == SOLVER_TRANSACTION_OBSOLETES)
270 type = SOLVER_TRANSACTION_INSTALL;
274 /* the element doesn't match the show mode */
276 /* if we're showing all references, we can ignore this package */
277 if ((mode & (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES)) == (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES))
278 return SOLVER_TRANSACTION_IGNORE;
280 /* we're not showing all refs. check if some other package
281 * references us. If yes, it's safe to ignore this package,
282 * otherwise we need to map the type */
284 /* most of the time there's only one reference, so check it first */
285 q = transaction_obs_pkg(trans, p);
287 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
289 Solvable *sq = pool->solvables + q;
290 if (sq->name != s->name)
292 /* it's a replace but we're not showing replaces. map type. */
293 if (s->repo == pool->installed)
294 return SOLVER_TRANSACTION_ERASE;
295 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
296 return SOLVER_TRANSACTION_MULTIINSTALL;
298 return SOLVER_TRANSACTION_INSTALL;
302 /* if there's a match, p will be shown when q
304 if (transaction_obs_pkg(trans, q) == p)
305 return SOLVER_TRANSACTION_IGNORE;
307 /* too bad, a miss. check em all */
310 transaction_all_obs_pkgs(trans, p, &oq);
311 for (i = 0; i < oq.count; i++)
314 if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
316 Solvable *sq = pool->solvables + q;
317 if (sq->name != s->name)
320 /* check if we are referenced? */
321 if ((mode & SOLVER_TRANSACTION_SHOW_ALL) != 0)
323 transaction_all_obs_pkgs(trans, q, &rq);
324 for (j = 0; j < rq.count; j++)
325 if (rq.elements[j] == p)
333 else if (transaction_obs_pkg(trans, q) == p)
344 /* we're not referenced. map type */
345 if (s->repo == pool->installed)
346 return SOLVER_TRANSACTION_ERASE;
347 else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
348 return SOLVER_TRANSACTION_MULTIINSTALL;
350 return SOLVER_TRANSACTION_INSTALL;
352 /* there was a ref, so p is shown with some other package */
353 return SOLVER_TRANSACTION_IGNORE;
359 classify_cmp(const void *ap, const void *bp, void *dp)
361 Transaction *trans = dp;
362 Pool *pool = trans->pool;
372 return a[2] && b[2] ? strcmp(pool_id2str(pool, a[2]), pool_id2str(pool, b[2])) : r;
375 return a[3] && b[3] ? strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3])) : r;
380 classify_cmp_pkgs(const void *ap, const void *bp, void *dp)
382 Transaction *trans = dp;
383 Pool *pool = trans->pool;
388 sa = pool->solvables + a;
389 sb = pool->solvables + b;
390 if (sa->name != sb->name)
391 return strcmp(pool_id2str(pool, sa->name), pool_id2str(pool, sb->name));
392 if (sa->evr != sb->evr)
394 int r = pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
402 transaction_classify(Transaction *trans, int mode, Queue *classes)
404 Pool *pool = trans->pool;
405 int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
407 Id v, vq, type, p, q;
410 queue_empty(classes);
411 memset(ntypes, 0, sizeof(ntypes));
412 /* go through transaction and classify each step */
413 for (i = 0; i < trans->steps.count; i++)
415 p = trans->steps.elements[i];
416 s = pool->solvables + p;
417 type = transaction_type(trans, p, mode);
419 if (!pool->installed || s->repo != pool->installed)
421 /* look at arch/vendor changes */
422 q = transaction_obs_pkg(trans, p);
425 sq = pool->solvables + q;
431 if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 0)
433 for (j = 0; j < classes->count; j += 4)
434 if (classes->elements[j] == SOLVER_TRANSACTION_ARCHCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
436 if (j == classes->count)
438 queue_push(classes, SOLVER_TRANSACTION_ARCHCHANGE);
439 queue_push(classes, 1);
440 queue_push(classes, v);
441 queue_push(classes, vq);
444 classes->elements[j + 1]++;
447 v = s->vendor ? s->vendor : 1;
448 vq = sq->vendor ? sq->vendor : 1;
451 if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 0)
453 for (j = 0; j < classes->count; j += 4)
454 if (classes->elements[j] == SOLVER_TRANSACTION_VENDORCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
456 if (j == classes->count)
458 queue_push(classes, SOLVER_TRANSACTION_VENDORCHANGE);
459 queue_push(classes, 1);
460 queue_push(classes, v);
461 queue_push(classes, vq);
464 classes->elements[j + 1]++;
467 /* now sort all vendor/arch changes */
468 if (classes->count > 4)
469 solv_sort(classes->elements, classes->count / 4, 4 * sizeof(Id), classify_cmp, trans);
470 /* finally add all classes. put erases last */
471 i = SOLVER_TRANSACTION_ERASE;
474 queue_unshift(classes, 0);
475 queue_unshift(classes, 0);
476 queue_unshift(classes, ntypes[i]);
477 queue_unshift(classes, i);
479 for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
483 if (i == SOLVER_TRANSACTION_ERASE)
485 queue_unshift(classes, 0);
486 queue_unshift(classes, 0);
487 queue_unshift(classes, ntypes[i]);
488 queue_unshift(classes, i);
493 transaction_classify_pkgs(Transaction *trans, int mode, Id class, Id from, Id to, Queue *pkgs)
495 Pool *pool = trans->pool;
501 for (i = 0; i < trans->steps.count; i++)
503 p = trans->steps.elements[i];
504 s = pool->solvables + p;
505 if (class <= SOLVER_TRANSACTION_MAXTYPE)
507 type = transaction_type(trans, p, mode);
512 if (!pool->installed || s->repo != pool->installed)
514 q = transaction_obs_pkg(trans, p);
517 sq = pool->solvables + q;
518 if (class == SOLVER_TRANSACTION_ARCHCHANGE)
520 if ((!from && !to) || (s->arch == from && sq->arch == to))
524 if (class == SOLVER_TRANSACTION_VENDORCHANGE)
526 Id v = s->vendor ? s->vendor : 1;
527 Id vq = sq->vendor ? sq->vendor : 1;
528 if ((!from && !to) || (v == from && vq == to))
534 solv_sort(pkgs->elements, pkgs->count, sizeof(Id), classify_cmp_pkgs, trans);
538 create_transaction_info(Transaction *trans, Queue *decisionq)
540 Pool *pool = trans->pool;
541 Queue *ti = &trans->transaction_info;
542 Repo *installed = pool->installed;
548 trans->transaction_installed = solv_free(trans->transaction_installed);
550 return; /* no info needed */
551 for (i = 0; i < decisionq->count; i++)
553 p = decisionq->elements[i];
554 if (p <= 0 || p == SYSTEMSOLVABLE)
556 s = pool->solvables + p;
557 if (!s->repo || s->repo == installed)
559 noobs = trans->noobsmap.size && MAPTST(&trans->noobsmap, p);
560 FOR_PROVIDES(p2, pp2, s->name)
562 if (!MAPTST(&trans->transactsmap, p2))
564 s2 = pool->solvables + p2;
565 if (s2->repo != installed)
567 if (noobs && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
569 if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
571 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
576 if (s->obsoletes && !noobs)
578 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
579 while ((obs = *obsp++) != 0)
581 FOR_PROVIDES(p2, pp2, obs)
583 s2 = pool->solvables + p2;
584 if (s2->repo != installed)
586 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
588 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
596 solv_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
598 for (i = j = 0; i < ti->count; i += 2)
600 if (j && ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
602 ti->elements[j++] = ti->elements[i];
603 ti->elements[j++] = ti->elements[i + 1];
607 /* create transaction_installed helper */
608 trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
609 for (i = 0; i < ti->count; i += 2)
611 j = ti->elements[i + 1] - installed->start;
612 if (!trans->transaction_installed[j])
613 trans->transaction_installed[j] = ti->elements[i];
616 /* more than one package obsoletes us. compare */
618 if (trans->transaction_installed[j] > 0)
619 trans->transaction_installed[j] = -trans->transaction_installed[j];
620 q[0] = q[2] = ti->elements[i + 1];
621 q[1] = ti->elements[i];
622 q[3] = -trans->transaction_installed[j];
623 if (obsq_sortcmp(q, q + 2, pool) < 0)
624 trans->transaction_installed[j] = -ti->elements[i];
631 transaction_create_decisionq(Pool *pool, Queue *decisionq, Map *noobsmap)
633 Repo *installed = pool->installed;
639 trans = transaction_create(pool);
640 if (noobsmap && !noobsmap->size)
641 noobsmap = 0; /* ignore empty map */
642 queue_empty(&trans->steps);
643 map_init(&trans->transactsmap, pool->nsolvables);
645 for (i = 0; i < decisionq->count; i++)
647 p = decisionq->elements[i];
648 s = pool->solvables + (p > 0 ? p : -p);
651 if (installed && s->repo == installed && p < 0)
652 MAPSET(&trans->transactsmap, -p);
653 if ((!installed || s->repo != installed) && p > 0)
656 const char *n = pool_id2str(pool, s->name);
657 if (!strncmp(n, "patch:", 6))
659 if (!strncmp(n, "pattern:", 8))
662 MAPSET(&trans->transactsmap, p);
663 if (noobsmap && MAPTST(noobsmap, p))
667 MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
669 map_init_clone(&trans->noobsmap, noobsmap);
671 create_transaction_info(trans, decisionq);
675 FOR_REPO_SOLVABLES(installed, p, s)
677 if (MAPTST(&trans->transactsmap, p))
678 queue_push(&trans->steps, p);
681 for (i = 0; i < decisionq->count; i++)
683 p = decisionq->elements[i];
684 if (p > 0 && MAPTST(&trans->transactsmap, p))
685 queue_push(&trans->steps, p);
691 transaction_installedresult(Transaction *trans, Queue *installedq)
693 Pool *pool = trans->pool;
694 Repo *installed = pool->installed;
699 queue_empty(installedq);
700 /* first the new installs, than the kept packages */
701 for (i = 0; i < trans->steps.count; i++)
703 p = trans->steps.elements[i];
704 s = pool->solvables + p;
705 if (installed && s->repo == installed)
707 queue_push(installedq, p);
709 cutoff = installedq->count;
712 FOR_REPO_SOLVABLES(installed, p, s)
713 if (!MAPTST(&trans->transactsmap, p))
714 queue_push(installedq, p);
720 transaction_create_installedmap(Transaction *trans, Map *installedmap)
722 Pool *pool = trans->pool;
723 Repo *installed = pool->installed;
728 map_init(installedmap, pool->nsolvables);
729 for (i = 0; i < trans->steps.count; i++)
731 p = trans->steps.elements[i];
732 s = pool->solvables + p;
733 if (!installed || s->repo != installed)
734 MAPSET(installedmap, p);
738 FOR_REPO_SOLVABLES(installed, p, s)
739 if (!MAPTST(&trans->transactsmap, p))
740 MAPSET(installedmap, p);
745 transaction_calc_installsizechange(Transaction *trans)
750 transaction_create_installedmap(trans, &installedmap);
751 change = pool_calc_installsizechange(trans->pool, &installedmap);
752 map_free(&installedmap);
757 transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
761 transaction_create_installedmap(trans, &installedmap);
762 pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
763 map_free(&installedmap);
766 struct _TransactionElement {
767 Id p; /* solvable id */
768 Id edges; /* pointer into edges data */
772 struct _TransactionOrderdata {
773 struct _TransactionElement *tes;
779 #define TYPE_BROKEN (1<<0)
780 #define TYPE_CON (1<<1)
782 #define TYPE_REQ_P (1<<2)
783 #define TYPE_PREREQ_P (1<<3)
785 #define TYPE_REQ (1<<4)
786 #define TYPE_PREREQ (1<<5)
788 #define TYPE_CYCLETAIL (1<<16)
789 #define TYPE_CYCLEHEAD (1<<17)
791 #define EDGEDATA_BLOCK 127
794 transaction_create(Pool *pool)
796 Transaction *trans = solv_calloc(1, sizeof(*trans));
802 transaction_create_clone(Transaction *srctrans)
804 Transaction *trans = transaction_create(srctrans->pool);
805 queue_init_clone(&trans->steps, &srctrans->steps);
806 queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
807 if (srctrans->transaction_installed)
809 Repo *installed = srctrans->pool->installed;
810 trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
811 memcpy(trans->transaction_installed, srctrans->transaction_installed, (installed->end - installed->start) * sizeof(Id));
813 map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
814 map_init_clone(&trans->noobsmap, &srctrans->noobsmap);
815 if (srctrans->orderdata)
817 struct _TransactionOrderdata *od = srctrans->orderdata;
818 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
819 trans->orderdata->tes = solv_malloc2(od->ntes, sizeof(*od->tes));
820 memcpy(trans->orderdata->tes, od->tes, od->ntes * sizeof(*od->tes));
821 trans->orderdata->ntes = od->ntes;
822 trans->orderdata->invedgedata = solv_malloc2(od->ninvedgedata, sizeof(Id));
823 memcpy(trans->orderdata->invedgedata, od->invedgedata, od->ninvedgedata * sizeof(Id));
824 trans->orderdata->ninvedgedata = od->ninvedgedata;
830 transaction_free(Transaction *trans)
832 queue_free(&trans->steps);
833 queue_free(&trans->transaction_info);
834 trans->transaction_installed = solv_free(trans->transaction_installed);
835 map_free(&trans->transactsmap);
836 map_free(&trans->noobsmap);
837 transaction_free_orderdata(trans);
842 transaction_free_orderdata(Transaction *trans)
844 if (trans->orderdata)
846 struct _TransactionOrderdata *od = trans->orderdata;
847 od->tes = solv_free(od->tes);
848 od->invedgedata = solv_free(od->invedgedata);
849 trans->orderdata = solv_free(trans->orderdata);
855 struct _TransactionElement *tes;
867 addteedge(struct orderdata *od, int from, int to, int type)
870 struct _TransactionElement *te;
875 /* printf("edge %d(%s) -> %d(%s) type %x\n", from, pool_solvid2str(pool, od->tes[from].p), to, pool_solvid2str(pool, od->tes[to].p), type); */
878 for (i = te->edges; od->edgedata[i]; i += 2)
879 if (od->edgedata[i] == to)
881 /* test of brokenness */
882 if (type == TYPE_BROKEN)
883 return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
886 od->edgedata[i + 1] |= type;
889 if (i + 1 == od->nedgedata)
891 /* printf("tail add %d\n", i - te->edges); */
894 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
898 /* printf("extend %d\n", i - te->edges); */
899 od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
901 memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
902 i = od->nedgedata + (i - te->edges);
903 te->edges = od->nedgedata;
905 od->edgedata[i] = to;
906 od->edgedata[i + 1] = type;
907 od->edgedata[i + 2] = 0; /* end marker */
908 od->nedgedata = i + 3;
913 addedge(struct orderdata *od, Id from, Id to, int type)
915 Transaction *trans = od->trans;
916 Pool *pool = trans->pool;
918 struct _TransactionElement *te;
921 // printf("addedge %d %d type %d\n", from, to, type);
922 s = pool->solvables + from;
923 if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
925 /* obsolete, map to install */
926 if (trans->transaction_installed[from - pool->installed->start] > 0)
927 from = trans->transaction_installed[from - pool->installed->start];
934 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
935 transaction_all_obs_pkgs(trans, from, &ti);
936 for (i = 0; i < ti.count; i++)
937 ret |= addedge(od, ti.elements[i], to, type);
942 s = pool->solvables + to;
943 if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
945 /* obsolete, map to install */
946 if (trans->transaction_installed[to - pool->installed->start] > 0)
947 to = trans->transaction_installed[to - pool->installed->start];
954 queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
955 transaction_all_obs_pkgs(trans, to, &ti);
956 for (i = 0; i < ti.count; i++)
957 ret |= addedge(od, from, ti.elements[i], type);
963 /* map from/to to te numbers */
964 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
971 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
977 return addteedge(od, i, to, type);
982 havechoice(struct orderdata *od, Id p, Id q1, Id q2)
984 Transaction *trans = od->trans;
985 Pool *pool = trans->pool;
986 Id ti1buf[5], ti2buf[5];
990 /* both q1 and q2 are uninstalls. check if their TEs intersect */
991 /* common case: just one TE for both packages */
993 printf("havechoice %d %d %d\n", p, q1, q2);
995 if (trans->transaction_installed[q1 - pool->installed->start] == 0)
997 if (trans->transaction_installed[q2 - pool->installed->start] == 0)
999 if (trans->transaction_installed[q1 - pool->installed->start] == trans->transaction_installed[q2 - pool->installed->start])
1001 if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
1003 queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
1004 transaction_all_obs_pkgs(trans, q1, &ti1);
1005 queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
1006 transaction_all_obs_pkgs(trans, q2, &ti2);
1007 for (i = 0; i < ti1.count; i++)
1008 for (j = 0; j < ti2.count; j++)
1009 if (ti1.elements[i] == ti2.elements[j])
1011 /* found a common edge */
1023 havescripts(Pool *pool, Id solvid)
1025 Solvable *s = pool->solvables + solvid;
1031 reqp = s->repo->idarraydata + s->requires;
1032 while ((req = *reqp++) != 0)
1034 if (req == SOLVABLE_PREREQMARKER)
1041 dep = pool_id2str(pool, req);
1042 if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
1050 addsolvableedges(struct orderdata *od, Solvable *s)
1052 Transaction *trans = od->trans;
1053 Pool *pool = trans->pool;
1054 Id req, *reqp, con, *conp;
1056 int i, j, pre, numins;
1057 Repo *installed = pool->installed;
1063 printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
1065 p = s - pool->solvables;
1069 reqp = s->repo->idarraydata + s->requires;
1071 while ((req = *reqp++) != 0)
1073 if (req == SOLVABLE_PREREQMARKER)
1079 if (pre != TYPE_PREREQ && installed && s->repo == installed)
1081 /* ignore normal requires if we're getting obsoleted */
1082 if (trans->transaction_installed[p - pool->installed->start])
1087 numins = 0; /* number of packages to be installed providing it */
1088 provbyinst = 0; /* provided by kept package */
1089 FOR_PROVIDES(p2, pp2, req)
1091 s2 = pool->solvables + p2;
1094 reqq.count = 0; /* self provides */
1097 if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
1101 printf("IGNORE inst provides %s by %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s2));
1102 reqq.count = 0; /* provided by package that stays installed */
1108 if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
1109 continue; /* package stays uninstalled */
1111 if (s->repo == installed)
1113 /* s gets uninstalled */
1114 queue_pushunique(&reqq, p2);
1115 if (s2->repo != installed)
1120 if (s2->repo == installed)
1121 continue; /* s2 gets uninstalled */
1122 queue_pushunique(&reqq, p2);
1127 /* prune to harmless ->inst edges */
1128 for (i = j = 0; i < reqq.count; i++)
1129 if (pool->solvables[reqq.elements[i]].repo != installed)
1130 reqq.elements[j++] = reqq.elements[i];
1134 if (numins && reqq.count)
1136 if (s->repo == installed)
1138 for (i = 0; i < reqq.count; i++)
1140 if (pool->solvables[reqq.elements[i]].repo == installed)
1142 for (j = 0; j < reqq.count; j++)
1144 if (pool->solvables[reqq.elements[j]].repo != installed)
1146 if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
1147 continue; /* no self edge */
1149 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, reqq.elements[i]), pool_dep2str(pool, req), pool_solvid2str(pool, reqq.elements[j]));
1151 addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1157 /* no mixed types, remove all deps on uninstalls */
1158 for (i = j = 0; i < reqq.count; i++)
1159 if (pool->solvables[reqq.elements[i]].repo != installed)
1160 reqq.elements[j++] = reqq.elements[i];
1165 for (i = 0; i < reqq.count; i++)
1168 p2 = reqq.elements[i];
1169 if (pool->solvables[p2].repo != installed)
1171 /* all elements of reqq are installs, thus have different TEs */
1172 choice = reqq.count - 1;
1173 if (pool->solvables[p].repo != installed)
1176 printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1178 addedge(od, p, p2, pre);
1183 printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1185 addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1190 if (s->repo != installed)
1191 continue; /* no inst->uninst edges, please! */
1193 /* uninst -> uninst edge. Those make trouble. Only add if we must */
1194 if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
1196 /* p is obsoleted by another package and has no scripts */
1197 /* we assume that the obsoletor is good enough to replace p */
1202 for (j = 0; j < reqq.count; j++)
1206 if (havechoice(od, p, reqq.elements[i], reqq.elements[j]))
1211 printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
1213 addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
1220 conp = s->repo->idarraydata + s->conflicts;
1221 while ((con = *conp++) != 0)
1223 FOR_PROVIDES(p2, pp2, con)
1227 s2 = pool->solvables + p2;
1230 if (s->repo == installed)
1232 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1234 /* deinstall p before installing p2 */
1236 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
1238 addedge(od, p2, p, TYPE_CON);
1243 if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
1245 /* deinstall p2 before installing p */
1247 printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
1249 addedge(od, p, p2, TYPE_CON);
1256 if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
1258 /* we're getting deinstalled/updated. Try to do this before our
1259 * triggers are hit */
1260 for (i = 0; i < reqq.count; i++)
1262 Id tri = reqq.elements[i];
1263 FOR_PROVIDES(p2, pp2, tri)
1267 s2 = pool->solvables + p2;
1270 if (s2->name == s->name)
1271 continue; /* obsoleted anyway */
1272 if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
1274 /* deinstall/update p before installing p2 */
1276 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
1278 addedge(od, p2, p, TYPE_CON);
1287 /* break an edge in a cycle */
1289 breakcycle(struct orderdata *od, Id *cycle)
1291 Pool *pool = od->trans->pool;
1292 Id ddegmin, ddegmax, ddeg;
1294 struct _TransactionElement *te;
1297 ddegmin = ddegmax = 0;
1298 for (k = 0; cycle[k + 1]; k += 2)
1300 ddeg = od->edgedata[cycle[k + 1] + 1];
1303 if (!k || ddeg < ddegmin)
1309 if (ddeg == ddegmin)
1311 if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
1313 /* prefer k, as l comes from a package with contains scriptlets */
1318 /* same edge value, check for prereq */
1322 /* record brkoen cycle starting with the tail */
1323 queue_push(&od->cycles, od->cyclesdata.count); /* offset into data */
1324 queue_push(&od->cycles, k / 2); /* cycle elements */
1325 queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]); /* broken edge */
1332 queue_push(&od->cyclesdata, cycle[k]);
1336 queue_push(&od->cyclesdata, 0); /* mark end */
1338 /* break that edge */
1339 od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;
1342 if (ddegmin < TYPE_REQ)
1346 /* cycle recorded, print it */
1347 if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
1348 POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
1349 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
1350 for (k = 0; cycle[k + 1]; k += 2)
1352 te = od->tes + cycle[k];
1353 if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
1354 POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1356 POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
1358 POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
1362 dump_tes(struct orderdata *od)
1364 Pool *pool = od->trans->pool;
1367 struct _TransactionElement *te, *te2;
1370 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1372 Solvable *s = pool->solvables + te->p;
1373 POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
1374 if (s->repo != pool->installed)
1377 transaction_all_obs_pkgs(od->trans, te->p, &obsq);
1378 for (j = 0; j < obsq.count; j++)
1379 POOL_DEBUG(SOLV_DEBUG_RESULT, " -%s\n", pool_solvid2str(pool, obsq.elements[j]));
1381 for (j = te->edges; od->edgedata[j]; j += 2)
1383 te2 = od->tes + od->edgedata[j];
1384 if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
1385 POOL_DEBUG(SOLV_DEBUG_RESULT, " --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1387 POOL_DEBUG(SOLV_DEBUG_RESULT, " ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
1394 reachable(struct orderdata *od, Id i)
1396 struct _TransactionElement *te = od->tes + i;
1402 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1404 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1406 if (!od->tes[k].mark)
1408 if (od->tes[k].mark == 2)
1419 addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
1422 Transaction *trans = od->trans;
1423 Pool *pool = trans->pool;
1425 struct _TransactionElement *te;
1432 printf("addcycleedges\n");
1433 for (i = 0; (j = cycle[i]) != 0; i++)
1434 printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
1437 /* first add all the tail cycle edges */
1439 /* see what we can reach from the cycle */
1441 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1443 for (i = 0; (j = cycle[i]) != 0; i++)
1445 od->tes[j].mark = -1;
1446 queue_push(todo, j);
1450 i = queue_pop(todo);
1454 te->mark = te->mark < 0 ? 2 : 1;
1455 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1457 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1459 if (od->tes[k].mark > 0)
1460 continue; /* no need to visit again */
1461 queue_push(todo, k);
1464 /* now all cycle TEs are marked with 2, all TEs reachable
1465 * from the cycle are marked with 1 */
1467 od->tes[tail].mark = 1; /* no need to add edges */
1469 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1472 continue; /* reachable from cycle */
1473 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1475 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1477 if (od->tes[k].mark != 2)
1479 /* We found an edge to the cycle. Add an extra edge to the tail */
1480 /* the TE was not reachable, so we're not creating a new cycle! */
1482 printf("adding TO TAIL cycle edge %d->%d %s->%s!\n", i, tail, pool_solvid2str(pool, od->tes[i].p), pool_solvid2str(pool, od->tes[tail].p));
1484 j -= te->edges; /* in case we move */
1485 addteedge(od, i, tail, TYPE_CYCLETAIL);
1487 break; /* one edge is enough */
1492 /* now add all head cycle edges */
1495 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1498 for (i = 0; (j = cycle[i]) != 0; i++)
1501 od->tes[j].mark = 2;
1503 /* first the head to save some time */
1504 te = od->tes + head;
1505 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1507 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1509 if (!od->tes[k].mark)
1511 if (od->tes[k].mark == -1)
1512 od->tes[k].mark = -2; /* no need for another edge */
1514 for (i = 0; cycle[i] != 0; i++)
1516 if (cycle[i] == head)
1518 te = od->tes + cycle[i];
1519 for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
1521 if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
1523 /* see if we can reach a cycle TE from k */
1524 if (!od->tes[k].mark)
1526 if (od->tes[k].mark == -1)
1529 printf("adding FROM HEAD cycle edge %d->%d %s->%s [%s]!\n", head, k, pool_solvid2str(pool, od->tes[head].p), pool_solvid2str(pool, od->tes[k].p), pool_solvid2str(pool, od->tes[cycle[i]].p));
1531 addteedge(od, head, k, TYPE_CYCLEHEAD);
1532 od->tes[k].mark = -2; /* no need to add that one again */
1540 transaction_order(Transaction *trans, int flags)
1542 Pool *pool = trans->pool;
1543 Queue *tr = &trans->steps;
1544 Repo *installed = pool->installed;
1547 int i, j, k, numte, numedge;
1548 struct orderdata od;
1549 struct _TransactionElement *te;
1550 Queue todo, obsq, samerepoq, uninstq;
1551 int cycstart, cycel;
1559 start = now = solv_timems(0);
1560 POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
1561 /* free old data if present */
1562 if (trans->orderdata)
1564 struct _TransactionOrderdata *od = trans->orderdata;
1565 od->tes = solv_free(od->tes);
1566 od->invedgedata = solv_free(od->invedgedata);
1567 trans->orderdata = solv_free(trans->orderdata);
1570 /* create a transaction element for every active component */
1572 for (i = 0; i < tr->count; i++)
1574 p = tr->elements[i];
1575 s = pool->solvables + p;
1576 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1580 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
1582 return; /* nothing to do... */
1584 numte++; /* leave first one zero */
1585 memset(&od, 0, sizeof(od));
1588 od.tes = solv_calloc(numte, sizeof(*od.tes));
1589 od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
1592 queue_init(&od.cycles);
1594 /* initialize TEs */
1595 for (i = 0, te = od.tes + 1; i < tr->count; i++)
1597 p = tr->elements[i];
1598 s = pool->solvables + p;
1599 if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
1605 /* create dependency graph */
1606 for (i = 0; i < tr->count; i++)
1607 addsolvableedges(&od, pool->solvables + tr->elements[i]);
1611 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1612 for (j = te->edges; od.edgedata[j]; j += 2)
1614 POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
1615 POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
1621 now = solv_timems(0);
1622 /* kill all cycles */
1624 for (i = numte - 1; i > 0; i--)
1625 queue_push(&todo, i);
1629 i = queue_pop(&todo);
1630 /* printf("- look at TE %d\n", i); */
1634 od.tes[i].mark = 2; /* done with that one */
1639 continue; /* already finished before */
1642 int edgestovisit = 0;
1643 /* new node, visit edges */
1644 for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
1646 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1648 if (od.tes[k].mark == 2)
1649 continue; /* no need to visit again */
1650 if (!edgestovisit++)
1651 queue_push(&todo, -i); /* end of edges marker */
1652 queue_push(&todo, k);
1655 te->mark = 2; /* no edges, done with that one */
1657 te->mark = 1; /* under investigation */
1660 /* oh no, we found a cycle */
1661 /* find start of cycle node (<0) */
1662 for (j = todo.count - 1; j >= 0; j--)
1663 if (todo.elements[j] == -i)
1667 /* build te/edge chain */
1669 for (j = k; j < todo.count; j++)
1670 if (todo.elements[j] < 0)
1671 todo.elements[k++] = -todo.elements[j];
1672 cycel = k - cycstart;
1674 /* make room for edges, two extra element for cycle loop + terminating 0 */
1675 while (todo.count < cycstart + 2 * cycel + 2)
1676 queue_push(&todo, 0);
1677 cycle = todo.elements + cycstart;
1678 cycle[cycel] = i; /* close the loop */
1679 cycle[2 * cycel + 1] = 0; /* terminator */
1680 for (k = cycel; k > 0; k--)
1682 cycle[k * 2] = cycle[k];
1683 te = od.tes + cycle[k - 1];
1684 assert(te->mark == 1);
1685 te->mark = 0; /* reset investigation marker */
1686 /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
1687 for (j = te->edges; od.edgedata[j]; j += 2)
1688 if (od.edgedata[j] == cycle[k])
1690 assert(od.edgedata[j]);
1691 cycle[k * 2 - 1] = j;
1693 /* now cycle looks like this: */
1694 /* te1 edge te2 edge te3 ... teN edge te1 0 */
1695 breakcycle(&od, cycle);
1696 /* restart with start of cycle */
1697 todo.count = cycstart + 1;
1699 POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
1700 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
1702 now = solv_timems(0);
1703 /* now go through all broken cycles and create cycle edges to help
1705 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1707 if (od.cycles.elements[i + 2] >= TYPE_REQ)
1708 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1710 for (i = od.cycles.count - 3; i >= 0; i -= 3)
1712 if (od.cycles.elements[i + 2] < TYPE_REQ)
1713 addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
1715 POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
1720 /* all edges are finally set up and there are no cycles, now the easy part.
1721 * Create an ordered transaction */
1722 now = solv_timems(0);
1723 /* first invert all edges */
1724 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1725 te->mark = 1; /* term 0 */
1726 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1728 for (j = te->edges; od.edgedata[j]; j += 2)
1730 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1732 od.tes[od.edgedata[j]].mark++;
1736 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1741 POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
1742 od.invedgedata = solv_calloc(j + 1, sizeof(Id));
1743 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1745 for (j = te->edges; od.edgedata[j]; j += 2)
1747 if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
1749 od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
1752 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1753 te->edges = te->mark; /* edges now points into invedgedata */
1754 od.edgedata = solv_free(od.edgedata);
1755 od.nedgedata = j + 1;
1757 /* now the final ordering */
1758 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1760 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1761 for (j = te->edges; od.invedgedata[j]; j++)
1762 od.tes[od.invedgedata[j]].mark++;
1764 queue_init(&samerepoq);
1765 queue_init(&uninstq);
1767 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1770 if (installed && pool->solvables[te->p].repo == installed)
1771 queue_push(&uninstq, i);
1773 queue_push(&todo, i);
1775 assert(todo.count > 0 || uninstq.count > 0);
1776 oldcount = tr->count;
1783 temedianr = solv_calloc(numte, sizeof(Id));
1784 for (i = 1; i < numte; i++)
1786 Solvable *s = pool->solvables + od.tes[i].p;
1787 if (installed && s->repo == installed)
1790 j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
1795 /* select an TE i */
1797 i = queue_shift(&uninstq);
1798 else if (samerepoq.count)
1799 i = queue_shift(&samerepoq);
1800 else if (todo.count)
1802 /* find next repo/media */
1803 for (j = 0; j < todo.count; j++)
1805 if (!j || temedianr[todo.elements[j]] < lastmedia)
1808 lastmedia = temedianr[todo.elements[j]];
1811 lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
1813 /* move all matching TEs to samerepoq */
1814 for (i = j = 0; j < todo.count; j++)
1816 int k = todo.elements[j];
1817 if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
1818 queue_push(&samerepoq, k);
1820 todo.elements[i++] = k;
1824 assert(samerepoq.count);
1825 i = queue_shift(&samerepoq);
1831 queue_push(tr, te->p);
1833 printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
1835 s = pool->solvables + te->p;
1836 for (j = te->edges; od.invedgedata[j]; j++)
1838 struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
1839 assert(te2->mark > 0);
1840 if (--te2->mark == 0)
1842 Solvable *s = pool->solvables + te2->p;
1844 printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
1846 if (installed && s->repo == installed)
1847 queue_push(&uninstq, od.invedgedata[j]);
1848 else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
1849 queue_push(&samerepoq, od.invedgedata[j]);
1851 queue_push(&todo, od.invedgedata[j]);
1855 solv_free(temedianr);
1857 queue_free(&samerepoq);
1858 queue_free(&uninstq);
1860 for (i = 1, te = od.tes + i; i < numte; i++, te++)
1861 assert(te->mark == 0);
1863 /* add back obsoleted packages */
1864 transaction_add_obsoleted(trans);
1865 assert(tr->count == oldcount);
1867 POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
1868 POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
1870 if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
1872 trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
1873 trans->orderdata->tes = od.tes;
1874 trans->orderdata->ntes = numte;
1875 trans->orderdata->invedgedata = od.invedgedata;
1876 trans->orderdata->ninvedgedata = od.nedgedata;
1881 solv_free(od.invedgedata);
1887 transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
1890 struct _TransactionOrderdata *od = trans->orderdata;
1891 struct _TransactionElement *te;
1894 return choices->count;
1897 /* initialization step */
1898 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1900 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1902 for (j = te->edges; od->invedgedata[j]; j++)
1903 od->tes[od->invedgedata[j]].mark++;
1905 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1907 queue_push(choices, te->p);
1908 return choices->count;
1910 for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
1911 if (te->p == chosen)
1914 return choices->count;
1917 /* hey! out-of-order installation! */
1920 for (j = te->edges; od->invedgedata[j]; j++)
1922 te = od->tes + od->invedgedata[j];
1923 assert(te->mark > 0 || te->mark == -1);
1924 if (te->mark > 0 && --te->mark == 0)
1925 queue_push(choices, te->p);
1927 return choices->count;
1931 transaction_add_obsoleted(Transaction *trans)
1933 Pool *pool = trans->pool;
1934 Repo *installed = pool->installed;
1941 if (!installed || !trans->steps.count)
1943 /* calculate upper bound */
1945 FOR_REPO_SOLVABLES(installed, p, s)
1946 if (MAPTST(&trans->transactsmap, p))
1951 steps = &trans->steps;
1952 queue_insertn(steps, 0, max);
1955 map_init(&done, installed->end - installed->start);
1957 for (j = 0, i = max; i < steps->count; i++)
1959 p = trans->steps.elements[i];
1960 if (pool->solvables[p].repo == installed)
1962 if (!trans->transaction_installed[p - pool->installed->start])
1963 trans->steps.elements[j++] = p;
1966 trans->steps.elements[j++] = p;
1968 transaction_all_obs_pkgs(trans, p, &obsq);
1969 for (k = 0; k < obsq.count; k++)
1971 p = obsq.elements[k];
1972 assert(p >= installed->start && p < installed->end);
1973 if (MAPTST(&done, p - installed->start))
1975 MAPSET(&done, p - installed->start);
1976 trans->steps.elements[j++] = p;
1980 /* free unneeded space */
1981 queue_truncate(steps, j);
1987 transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
1989 Pool *pool = trans->pool;
1994 if (MAPTST(seen, pkg))
1997 s = pool->solvables + pkg;
1999 printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
2005 reqp = s->repo->idarraydata + s->requires;
2006 while ((req = *reqp++) != 0)
2008 if (req == SOLVABLE_PREREQMARKER)
2013 if (onlyprereq && !inpre)
2015 if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
2018 /* first check kept packages, then freshly installed, then not yet uninstalled */
2019 FOR_PROVIDES(p, pp, req)
2021 if (!MAPTST(ins, p))
2023 if (MAPTST(&trans->transactsmap, p))
2026 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2030 FOR_PROVIDES(p, pp, req)
2032 if (!MAPTST(ins, p))
2034 if (pool->solvables[p].repo == pool->installed)
2037 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2042 FOR_PROVIDES(p, pp, req)
2044 if (!MAPTST(ins, p))
2047 transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
2052 POOL_DEBUG(SOLV_DEBUG_RESULT, " %c%s: nothing provides %s needed by %c%s\n", pool->solvables[tepkg].repo == pool->installed ? '-' : '+', pool_solvid2str(pool, tepkg), pool_dep2str(pool, req), s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
2059 transaction_check_order(Transaction *trans)
2061 Pool *pool = trans->pool;
2067 POOL_DEBUG(SOLV_WARN, "\nchecking transaction order...\n");
2068 map_init(&ins, pool->nsolvables);
2069 map_init(&seen, pool->nsolvables);
2070 if (pool->installed)
2071 FOR_REPO_SOLVABLES(pool->installed, p, s)
2074 for (i = 0; i < trans->steps.count; i++)
2076 p = trans->steps.elements[i];
2077 s = pool->solvables + p;
2078 if (s->repo != pool->installed)
2080 if (s->repo != pool->installed)
2082 if (havescripts(pool, p))
2085 transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
2087 if (s->repo == pool->installed)
2092 POOL_DEBUG(SOLV_WARN, "transaction order check done.\n");