summaryrefslogtreecommitdiff
path: root/src/order.c
blob: b8d4b82157aa032cb117ea40f7024a0731210084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
/*
 * Copyright (c) 2007-2015, SUSE LLC
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

/*
 * order.c
 *
 * Transaction ordering
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

#include "transaction.h"
#include "bitmap.h"
#include "pool.h"
#include "repo.h"
#include "util.h"

struct s_TransactionElement {
  Id p;		/* solvable id */
  Id edges;	/* pointer into edges data */
  Id mark;
};

struct s_TransactionOrderdata {
  struct s_TransactionElement *tes;
  int ntes;
  Id *invedgedata;
  int ninvedgedata;
  Queue *cycles;
};

#define TYPE_BROKEN	(1<<0)
#define TYPE_CON    	(1<<1)

#define TYPE_REQ_P    	(1<<2)
#define TYPE_PREREQ_P 	(1<<3)

#define TYPE_SUG	(1<<4)
#define TYPE_REC	(1<<5)

#define TYPE_REQ    	(1<<6)
#define TYPE_PREREQ 	(1<<7)

#define TYPE_CYCLETAIL  (1<<16)
#define TYPE_CYCLEHEAD  (1<<17)

#define EDGEDATA_BLOCK	127

void
transaction_clone_orderdata(Transaction *trans, Transaction *srctrans)
{
  struct s_TransactionOrderdata *od = srctrans->orderdata;
  if (!od)
    return;
  trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
  trans->orderdata->tes = solv_memdup2(od->tes, od->ntes, sizeof(*od->tes));
  trans->orderdata->ntes = od->ntes;
  trans->orderdata->invedgedata = solv_memdup2(od->invedgedata, od->ninvedgedata, sizeof(Id));
  trans->orderdata->ninvedgedata = od->ninvedgedata;
  if (od->cycles)
    {
      trans->orderdata->cycles = solv_calloc(1, sizeof(Queue));
      queue_init_clone(trans->orderdata->cycles, od->cycles);
    }
}

void
transaction_free_orderdata(Transaction *trans)
{
  if (trans->orderdata)
    {
      struct s_TransactionOrderdata *od = trans->orderdata;
      od->tes = solv_free(od->tes);
      od->invedgedata = solv_free(od->invedgedata);
      if (od->cycles)
	{
	  queue_free(od->cycles);
	  od->cycles = solv_free(od->cycles);
	}
      trans->orderdata = solv_free(trans->orderdata);
    }
}

struct orderdata {
  Transaction *trans;
  struct s_TransactionElement *tes;
  int ntes;
  Id *edgedata;
  int nedgedata;
  Id *invedgedata;

  Queue cycles;
  Queue cyclesdata;
  int ncycles;
};

static int
addteedge(struct orderdata *od, int from, int to, int type)
{
  int i;
  struct s_TransactionElement *te;

  if (from == to)
    return 0;

  /* 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); */

  te = od->tes + from;
  for (i = te->edges; od->edgedata[i]; i += 2)
    if (od->edgedata[i] == to)
      break;
  /* test of brokenness */
  if (type == TYPE_BROKEN)
    return od->edgedata[i] && (od->edgedata[i + 1] & TYPE_BROKEN) != 0 ? 1 : 0;
  if (od->edgedata[i])
    {
      od->edgedata[i + 1] |= type;
      return 0;
    }
  if (i + 1 == od->nedgedata)
    {
      /* printf("tail add %d\n", i - te->edges); */
      if (!i)
	te->edges = ++i;
      od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
    }
  else
    {
      /* printf("extend %d\n", i - te->edges); */
      od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
      if (i > te->edges)
	memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
      i = od->nedgedata + (i - te->edges);
      te->edges = od->nedgedata;
    }
  od->edgedata[i] = to;
  od->edgedata[i + 1] = type;
  od->edgedata[i + 2] = 0;	/* end marker */
  od->nedgedata = i + 3;
  return 0;
}

static int
addedge(struct orderdata *od, Id from, Id to, int type)
{
  Transaction *trans = od->trans;
  Pool *pool = trans->pool;
  Solvable *s;
  struct s_TransactionElement *te;
  int i;

  /* printf("addedge %d %d type %d\n", from, to, type); */
  s = pool->solvables + from;
  if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
    {
      /* obsolete, map to install */
      if (trans->transaction_installed[from - pool->installed->start] > 0)
	from = trans->transaction_installed[from - pool->installed->start];
      else
	{
	  int ret = 0;
	  Queue ti;
	  Id tibuf[5];

	  queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
	  transaction_all_obs_pkgs(trans, from, &ti);
	  for (i = 0; i < ti.count; i++)
	    ret |= addedge(od, ti.elements[i], to, type);
	  queue_free(&ti);
	  return ret;
	}
    }
  s = pool->solvables + to;
  if (s->repo == pool->installed && trans->transaction_installed[to - pool->installed->start])
    {
      /* obsolete, map to install */
      if (trans->transaction_installed[to - pool->installed->start] > 0)
	to = trans->transaction_installed[to - pool->installed->start];
      else
	{
	  int ret = 0;
	  Queue ti;
	  Id tibuf[5];

	  queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
	  transaction_all_obs_pkgs(trans, to, &ti);
	  for (i = 0; i < ti.count; i++)
	    ret |= addedge(od, from, ti.elements[i], type);
	  queue_free(&ti);
	  return ret;
	}
    }

  /* map from/to to te numbers */
  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
    if (te->p == to)
      break;
  if (i == od->ntes)
    return 0;
  to = i;

  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
    if (te->p == from)
      break;
  if (i == od->ntes)
    return 0;

  return addteedge(od, i, to, type);
}

static inline int
havescripts(Pool *pool, Id solvid)
{
  Solvable *s = pool->solvables + solvid;
  const char *dep;
  if (s->requires)
    {
      Id req, *reqp;
      int inpre = 0;
      reqp = s->repo->idarraydata + s->requires;
      while ((req = *reqp++) != 0)
	{
          if (req == SOLVABLE_PREREQMARKER)
	    {
	      inpre = 1;
	      continue;
	    }
	  if (!inpre)
	    continue;
	  dep = pool_id2str(pool, req);
	  if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
	    return 1;
	}
    }
  return 0;
}

static void
addsolvableedges(struct orderdata *od, Solvable *s)
{
  Transaction *trans = od->trans;
  Pool *pool = trans->pool;
  Id p, p2, pp2;
  int i, j, pre, numins;
  Repo *installed = pool->installed;
  Solvable *s2;
  Queue depq;
  int provbyinst;

#if 0
  printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
#endif
  p = s - pool->solvables;
  queue_init(&depq);
  if (s->requires)
    {
      Id req, *reqp;
      reqp = s->repo->idarraydata + s->requires;
      pre = TYPE_REQ;
      while ((req = *reqp++) != 0)
	{
	  if (req == SOLVABLE_PREREQMARKER)
	    {
	      pre = TYPE_PREREQ;
	      continue;
	    }
	  queue_empty(&depq);
	  numins = 0;	/* number of packages to be installed providing it */
	  provbyinst = 0;	/* provided by kept package */
	  FOR_PROVIDES(p2, pp2, req)
	    {
	      s2 = pool->solvables + p2;
	      if (p2 == p)
		{
		  depq.count = 0;	/* self provides */
		  break;
		}
	      if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
		{
		  provbyinst = 1;
		  continue;
		}
	      if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
		continue;		/* package stays uninstalled */

	      if (s->repo == installed)
		{
		  /* s gets uninstalled */
		  queue_pushunique(&depq, p2);
		  if (s2->repo != installed)
		    numins++;
		}
	      else
		{
		  if (s2->repo == installed)
		    continue;	/* s2 gets uninstalled */
		  queue_pushunique(&depq, p2);
		}
	    }
	  if (provbyinst)
	    {
	      /* prune to harmless ->inst edges */
	      for (i = j = 0; i < depq.count; i++)
		if (pool->solvables[depq.elements[i]].repo != installed)
		  depq.elements[j++] = depq.elements[i];
	      depq.count = j;
	    }

	  if (numins && depq.count)
	    {
	      if (s->repo == installed)
		{
		  for (i = 0; i < depq.count; i++)
		    {
		      if (pool->solvables[depq.elements[i]].repo == installed)
			{
			  for (j = 0; j < depq.count; j++)
			    {
			      if (pool->solvables[depq.elements[j]].repo != installed)
				{
				  if (trans->transaction_installed[depq.elements[i] - pool->installed->start] == depq.elements[j])
				    continue;	/* no self edge */
#if 0
				  printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, depq.elements[i]), pool_dep2str(pool, req), pool_solvid2str(pool, depq.elements[j]));
#endif
				  addedge(od, depq.elements[i], depq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
				}
			    }
			}
		    }
		}
	      /* no mixed types, remove all deps on uninstalls */
	      for (i = j = 0; i < depq.count; i++)
		if (pool->solvables[depq.elements[i]].repo != installed)
		  depq.elements[j++] = depq.elements[i];
	      depq.count = j;
	    }
          for (i = 0; i < depq.count; i++)
	    {
	      p2 = depq.elements[i];
	      if (pool->solvables[p2].repo != installed)
		{
		  /* all elements of depq are installs, thus have different TEs */
		  if (pool->solvables[p].repo != installed)
		    {
#if 0
		      printf("add inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
#endif
		      addedge(od, p, p2, pre);
		    }
		  else
		    {
#if 0
		      printf("add uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
#endif
		      addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
		    }
		}
	      else
		{
		  if (s->repo != installed)
		    continue;	/* no inst->uninst edges, please! */

		  /* uninst -> uninst edge. Those make trouble. Only add if we must */
		  if (trans->transaction_installed[p - installed->start] && !havescripts(pool, p))
		    {
		      /* p is obsoleted by another package and has no scripts */
		      /* we assume that the obsoletor is good enough to replace p */
		      continue;
		    }
#if 0
		  printf("add uninst->uninst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
#endif
	          addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
		}
	    }
	}
    }
  if (s->conflicts)
    {
      Id con, *conp;
      conp = s->repo->idarraydata + s->conflicts;
      while ((con = *conp++) != 0)
	{
	  FOR_PROVIDES(p2, pp2, con)
	    {
	      if (p2 == p)
		continue;
	      s2 = pool->solvables + p2;
	      if (!s2->repo)
		continue;
	      if (s->repo == installed)
		{
		  if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
		    {
		      /* deinstall p before installing p2 */
#if 0
		      printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
#endif
		      addedge(od, p2, p, TYPE_CON);
		    }
		}
	      else
		{
		  if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
		    {
		      /* deinstall p2 before installing p */
#if 0
		      printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
#endif
		      addedge(od, p, p2, TYPE_CON);
		    }
		}

	    }
	}
    }
  if (s->recommends && s->repo != installed)
    {
      Id rec, *recp;
      recp = s->repo->idarraydata + s->recommends;
      while ((rec = *recp++) != 0)
	{
	  queue_empty(&depq);
	  FOR_PROVIDES(p2, pp2, rec)
	    {
	      s2 = pool->solvables + p2;
	      if (p2 == p)
		{
		  depq.count = 0;	/* self provides */
		  break;
		}
	      if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
		continue;
	      if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
		continue;		/* package stays uninstalled */
	      if (s2->repo != installed)
	        queue_pushunique(&depq, p2);
	    }
          for (i = 0; i < depq.count; i++)
	    {
	      p2 = depq.elements[i];
	      if (pool->solvables[p2].repo != installed)
		{
#if 0
		  printf("add recommends inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, rec), pool_solvid2str(pool, p2));
#endif
		  addedge(od, p, p2, TYPE_REC);
		}
	    }
	}
    }
  if (s->suggests && s->repo != installed)
    {
      Id sug, *sugp;
      sugp = s->repo->idarraydata + s->suggests;
      while ((sug = *sugp++) != 0)
	{
	  queue_empty(&depq);
	  FOR_PROVIDES(p2, pp2, sug)
	    {
	      s2 = pool->solvables + p2;
	      if (p2 == p)
		{
		  depq.count = 0;	/* self provides */
		  break;
		}
	      if (s2->repo == installed && !MAPTST(&trans->transactsmap, p2))
		continue;
	      if (s2->repo != installed && !MAPTST(&trans->transactsmap, p2))
		continue;		/* package stays uninstalled */
	      if (s2->repo != installed)
	        queue_pushunique(&depq, p2);
	    }
          for (i = 0; i < depq.count; i++)
	    {
	      p2 = depq.elements[i];
	      if (pool->solvables[p2].repo != installed)
		{
#if 0
		  printf("add suggests inst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, sug), pool_solvid2str(pool, p2));
#endif
		  addedge(od, p, p2, TYPE_SUG);
		}
	    }
	}
    }
  if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &depq) && depq.count)
    {
      /* we're getting deinstalled/updated. Try to do this before our
       * triggers are hit */
      for (i = 0; i < depq.count; i++)
	{
	  Id tri = depq.elements[i];
	  FOR_PROVIDES(p2, pp2, tri)
	    {
	      if (p2 == p)
		continue;
	      s2 = pool->solvables + p2;
	      if (!s2->repo)
		continue;
	      if (s2->name == s->name)
		continue;	/* obsoleted anyway */
	      if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
		{
		  /* deinstall/update p before installing p2 */
#if 0
		  printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
#endif
		  addedge(od, p2, p, TYPE_CON);
		}
	    }
	}
    }
  queue_free(&depq);
}


/* break an edge in a cycle */
static void
breakcycle(struct orderdata *od, Id *cycle)
{
  Pool *pool = od->trans->pool;
  Id ddegmin, ddegmax, ddeg;
  int k, l;
  struct s_TransactionElement *te;

  l = 0;
  ddegmin = ddegmax = 0;
  for (k = 0; cycle[k + 1]; k += 2)
    {
      ddeg = od->edgedata[cycle[k + 1] + 1];
      if (ddeg > ddegmax)
	ddegmax = ddeg;
      if (!k || ddeg < ddegmin)
	{
	  l = k;
	  ddegmin = ddeg;
	  continue;
	}
      if (ddeg == ddegmin)
	{
	  if (havescripts(pool, od->tes[cycle[l]].p) && !havescripts(pool, od->tes[cycle[k]].p))
	    {
	      /* prefer k, as l comes from a package with contains scriptlets */
	      l = k;
	      continue;
	    }
	  /* same edge value, check for prereq */
	}
    }

  /* record brkoen cycle starting with the tail */
  queue_push(&od->cycles, od->cyclesdata.count);		/* offset into data */
  queue_push(&od->cycles, k / 2);				/* cycle elements */
  queue_push(&od->cycles, od->edgedata[cycle[l + 1] + 1]);	/* broken edge */
  queue_push(&od->cycles, (ddegmax << 16) | ddegmin);		/* max/min values */
  od->ncycles++;
  for (k = l;;)
    {
      k += 2;
      if (!cycle[k + 1])
	k = 0;
      queue_push(&od->cyclesdata, cycle[k]);
      if (k == l)
	break;
    }
  queue_push(&od->cyclesdata, 0);	/* mark end */

  /* break that edge */
  od->edgedata[cycle[l + 1] + 1] |= TYPE_BROKEN;

#if 1
  if (ddegmin < TYPE_REQ)
    return;
#endif

  /* cycle recorded, print it */
  if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
    POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
  POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
  for (k = 0; cycle[k + 1]; k += 2)
    {
      te = od->tes + cycle[k];
      if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
        POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
      else
        POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
    }
  POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
}

#if 0
static inline void
dump_tes(struct orderdata *od)
{
  Pool *pool = od->trans->pool;
  int i, j;
  Queue obsq;
  struct s_TransactionElement *te, *te2;

  queue_init(&obsq);
  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
    {
      Solvable *s = pool->solvables + te->p;
      POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
      if (s->repo != pool->installed)
        {
	  queue_empty(&obsq);
	  transaction_all_obs_pkgs(od->trans, te->p, &obsq);
	  for (j = 0; j < obsq.count; j++)
	    POOL_DEBUG(SOLV_DEBUG_RESULT, "         -%s\n", pool_solvid2str(pool, obsq.elements[j]));
	}
      for (j = te->edges; od->edgedata[j]; j += 2)
	{
	  te2 = od->tes + od->edgedata[j];
	  if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
	    POOL_DEBUG(SOLV_DEBUG_RESULT, "       --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
	  else
	    POOL_DEBUG(SOLV_DEBUG_RESULT, "       ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
	}
    }
}
#endif

static void
reachable(struct orderdata *od, Id i)
{
  struct s_TransactionElement *te = od->tes + i;
  int j, k;

  if (te->mark != 0)
    return;
  te->mark = 1;
  for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
    {
      if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
	continue;
      if (!od->tes[k].mark)
        reachable(od, k);
      if (od->tes[k].mark == 2)
	{
	  te->mark = 2;
	  return;
	}
    }
  te->mark = -1;
}

static void
addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
{
#if 0
  Transaction *trans = od->trans;
  Pool *pool = trans->pool;
#endif
  struct s_TransactionElement *te;
  int i, j, k, tail;
  int head;

#if 0
  printf("addcycleedges\n");
  for (i = 0; (j = cycle[i]) != 0; i++)
    printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
#endif

  /* first add all the tail cycle edges */

  /* see what we can reach from the cycle */
  queue_empty(todo);
  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
    te->mark = 0;
  for (i = 0; (j = cycle[i]) != 0; i++)
    {
      od->tes[j].mark = -1;
      queue_push(todo, j);
    }
  while (todo->count)
    {
      i = queue_pop(todo);
      te = od->tes + i;
      if (te->mark > 0)
	continue;
      te->mark = te->mark < 0 ? 2 : 1;
      for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
	{
	  if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
	    continue;
	  if (od->tes[k].mark > 0)
	    continue;	/* no need to visit again */
	  queue_push(todo, k);
	}
    }
  /* now all cycle TEs are marked with 2, all TEs reachable
   * from the cycle are marked with 1 */
  tail = cycle[0];
  od->tes[tail].mark = 1;	/* no need to add edges */

  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
    {
      if (te->mark)
	continue;	/* reachable from cycle */
      for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
	{
	  if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
	    continue;
	  if (od->tes[k].mark != 2)
	    continue;
	  /* We found an edge to the cycle. Add an extra edge to the tail */
	  /* the TE was not reachable, so we're not creating a new cycle! */
#if 0
	  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));
#endif
	  j -= te->edges;	/* in case we move */
	  addteedge(od, i, tail, TYPE_CYCLETAIL);
	  j += te->edges;
	  break;	/* one edge is enough */
	}
    }

  /* now add all head cycle edges */

  /* reset marks */
  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
    te->mark = 0;
  head = 0;
  for (i = 0; (j = cycle[i]) != 0; i++)
    {
      head = j;
      od->tes[j].mark = 2;
    }
  /* first the head to save some time */
  te = od->tes + head;
  for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
    {
      if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
	continue;
      if (!od->tes[k].mark)
	reachable(od, k);
      if (od->tes[k].mark == -1)
	od->tes[k].mark = -2;	/* no need for another edge */
    }
  for (i = 0; cycle[i] != 0; i++)
    {
      if (cycle[i] == head)
	break;
      te = od->tes + cycle[i];
      for (j = te->edges; (k = od->edgedata[j]) != 0; j += 2)
	{
	  if ((od->edgedata[j + 1] & TYPE_BROKEN) != 0)
	    continue;
	  /* see if we can reach a cycle TE from k */
	  if (!od->tes[k].mark)
	    reachable(od, k);
	  if (od->tes[k].mark == -1)
	    {
#if 0
	      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));
#endif
	      addteedge(od, head, k, TYPE_CYCLEHEAD);
	      od->tes[k].mark = -2;	/* no need to add that one again */
	    }
	}
    }
}

void
transaction_order(Transaction *trans, int flags)
{
  Pool *pool = trans->pool;
  Queue *tr = &trans->steps;
  Repo *installed = pool->installed;
  Id p;
  Solvable *s;
  int i, j, k, numte, numedge;
  struct orderdata od;
  struct s_TransactionElement *te;
  Queue todo, obsq, samerepoq, uninstq;
  int cycstart, cycel;
  Id *cycle;
  int oldcount;
  int start, now;
  Repo *lastrepo;
  int lastmedia;
  Id *temedianr;

  start = now = solv_timems(0);
  POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
  /* free old data if present */
  if (trans->orderdata)
    {
      struct s_TransactionOrderdata *od = trans->orderdata;
      od->tes = solv_free(od->tes);
      od->invedgedata = solv_free(od->invedgedata);
      trans->orderdata = solv_free(trans->orderdata);
    }

  /* create a transaction element for every active component */
  numte = 0;
  for (i = 0; i < tr->count; i++)
    {
      p = tr->elements[i];
      s = pool->solvables + p;
      if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
	continue;
      numte++;
    }
  POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
  if (!numte)
    return;	/* nothing to do... */

  numte++;	/* leave first one zero */
  memset(&od, 0, sizeof(od));
  od.trans = trans;
  od.ntes = numte;
  od.tes = solv_calloc(numte, sizeof(*od.tes));
  od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
  od.edgedata[0] = 0;
  od.nedgedata = 1;
  queue_init(&od.cycles);

  /* initialize TEs */
  for (i = 0, te = od.tes + 1; i < tr->count; i++)
    {
      p = tr->elements[i];
      s = pool->solvables + p;
      if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
	continue;
      te->p = p;
      te++;
    }

  /* create dependency graph */
  for (i = 0; i < tr->count; i++)
    addsolvableedges(&od, pool->solvables + tr->elements[i]);

  /* count edges */
  numedge = 0;
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    for (j = te->edges; od.edgedata[j]; j += 2)
      numedge++;
  POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
  POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));

#if 0
  dump_tes(&od);
#endif

  now = solv_timems(0);
  /* kill all cycles */
  queue_init(&todo);
  for (i = numte - 1; i > 0; i--)
    queue_push(&todo, i);

  while (todo.count)
    {
      i = queue_pop(&todo);
      /* printf("- look at TE %d\n", i); */
      if (i < 0)
	{
	  i = -i;
	  od.tes[i].mark = 2;	/* done with that one */
	  continue;
	}
      te = od.tes + i;
      if (te->mark == 2)
	continue;		/* already finished before */
      if (te->mark == 0)
	{
	  int edgestovisit = 0;
	  /* new node, visit edges */
	  for (j = te->edges; (k = od.edgedata[j]) != 0; j += 2)
	    {
	      if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
		continue;
	      if (od.tes[k].mark == 2)
		continue;	/* no need to visit again */
	      if (!edgestovisit++)
	        queue_push(&todo, -i);	/* end of edges marker */
	      queue_push(&todo, k);
	    }
	  if (!edgestovisit)
	    te->mark = 2;	/* no edges, done with that one */
	  else
	    te->mark = 1;	/* under investigation */
	  continue;
	}
      /* oh no, we found a cycle */
      /* find start of cycle node (<0) */
      for (j = todo.count - 1; j >= 0; j--)
	if (todo.elements[j] == -i)
	  break;
      assert(j >= 0);
      cycstart = j;
      /* build te/edge chain */
      k = cycstart;
      for (j = k; j < todo.count; j++)
	if (todo.elements[j] < 0)
	  todo.elements[k++] = -todo.elements[j];
      cycel = k - cycstart;
      assert(cycel > 1);
      /* make room for edges, two extra element for cycle loop + terminating 0 */
      while (todo.count < cycstart + 2 * cycel + 2)
	queue_push(&todo, 0);
      cycle = todo.elements + cycstart;
      cycle[cycel] = i;		/* close the loop */
      cycle[2 * cycel + 1] = 0;	/* terminator */
      for (k = cycel; k > 0; k--)
	{
	  cycle[k * 2] = cycle[k];
	  te = od.tes + cycle[k - 1];
	  assert(te->mark == 1);
	  te->mark = 0;	/* reset investigation marker */
	  /* printf("searching for edge from %d to %d\n", cycle[k - 1], cycle[k]); */
	  for (j = te->edges; od.edgedata[j]; j += 2)
	    if (od.edgedata[j] == cycle[k])
	      break;
	  assert(od.edgedata[j]);
	  cycle[k * 2 - 1] = j;
	}
      /* now cycle looks like this: */
      /* te1 edge te2 edge te3 ... teN edge te1 0 */
      breakcycle(&od, cycle);
      /* restart with start of cycle */
      todo.count = cycstart + 1;
    }
  POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
  POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));

  now = solv_timems(0);
  /* now go through all broken cycles and create cycle edges to help
     the ordering */
   for (i = od.cycles.count - 4; i >= 0; i -= 4)
     {
       if (od.cycles.elements[i + 2] >= TYPE_REQ)
         addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
     }
   for (i = od.cycles.count - 4; i >= 0; i -= 4)
     {
       if (od.cycles.elements[i + 2] < TYPE_REQ)
         addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
     }
  POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));

#if 0
  dump_tes(&od);
#endif
  /* all edges are finally set up and there are no cycles, now the easy part.
   * Create an ordered transaction */
  now = solv_timems(0);
  /* first invert all edges */
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    te->mark = 1;	/* term 0 */
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    {
      for (j = te->edges; od.edgedata[j]; j += 2)
        {
	  if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
	    continue;
	  od.tes[od.edgedata[j]].mark++;
	}
    }
  j = 1;
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    {
      te->mark += j;
      j = te->mark;
    }
  POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
  od.invedgedata = solv_calloc(j + 1, sizeof(Id));
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    {
      for (j = te->edges; od.edgedata[j]; j += 2)
        {
	  if ((od.edgedata[j + 1] & TYPE_BROKEN) != 0)
	    continue;
	  od.invedgedata[--od.tes[od.edgedata[j]].mark] = i;
	}
    }
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    te->edges = te->mark;	/* edges now points into invedgedata */
  od.edgedata = solv_free(od.edgedata);
  od.nedgedata = j + 1;

  /* now the final ordering */
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    te->mark = 0;
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    for (j = te->edges; od.invedgedata[j]; j++)
      od.tes[od.invedgedata[j]].mark++;

  queue_init(&samerepoq);
  queue_init(&uninstq);
  queue_empty(&todo);
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    if (te->mark == 0)
      {
	if (installed && pool->solvables[te->p].repo == installed)
          queue_push(&uninstq, i);
	else
          queue_push(&todo, i);
      }
  assert(todo.count > 0 || uninstq.count > 0);
  oldcount = tr->count;
  queue_empty(tr);

  queue_init(&obsq);

  lastrepo = 0;
  lastmedia = 0;
  temedianr = solv_calloc(numte, sizeof(Id));
  for (i = 1; i < numte; i++)
    {
      Solvable *s = pool->solvables + od.tes[i].p;
      if (installed && s->repo == installed)
	j = 1;
      else
        j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
      temedianr[i] = j;
    }
  for (;;)
    {
      /* select an TE i */
      if (uninstq.count)
	i = queue_shift(&uninstq);
      else if (samerepoq.count)
	i = queue_shift(&samerepoq);
      else if (todo.count)
	{
	  /* find next repo/media */
	  for (j = 0; j < todo.count; j++)
	    {
	      if (!j || temedianr[todo.elements[j]] < lastmedia)
		{
		  i = j;
		  lastmedia = temedianr[todo.elements[j]];
		}
	    }
	  lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;

	  /* move all matching TEs to samerepoq */
	  for (i = j = 0; j < todo.count; j++)
	    {
	      int k = todo.elements[j];
	      if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
		queue_push(&samerepoq, k);
	      else
		todo.elements[i++] = k;
	    }
	  todo.count = i;

	  assert(samerepoq.count);
	  i = queue_shift(&samerepoq);
	}
      else
	break;

      te = od.tes + i;
      queue_push(tr, te->p);
#if 0
printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
#endif
      for (j = te->edges; od.invedgedata[j]; j++)
	{
	  struct s_TransactionElement *te2 = od.tes + od.invedgedata[j];
	  assert(te2->mark > 0);
	  if (--te2->mark == 0)
	    {
	      Solvable *s = pool->solvables + te2->p;
#if 0
printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
#endif
	      if (installed && s->repo == installed)
	        queue_push(&uninstq, od.invedgedata[j]);
	      else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
	        queue_push(&samerepoq, od.invedgedata[j]);
	      else
	        queue_push(&todo, od.invedgedata[j]);
	    }
	}
    }
  solv_free(temedianr);
  queue_free(&todo);
  queue_free(&samerepoq);
  queue_free(&uninstq);
  queue_free(&obsq);
  for (i = 1, te = od.tes + i; i < numte; i++, te++)
    assert(te->mark == 0);

  /* add back obsoleted packages */
  transaction_add_obsoleted(trans);
  assert(tr->count == oldcount);

  POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
  POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));

  if ((flags & (SOLVER_TRANSACTION_KEEP_ORDERDATA | SOLVER_TRANSACTION_KEEP_ORDERCYCLES)) != 0)
    {
      struct s_TransactionOrderdata *tod;
      trans->orderdata = tod = solv_calloc(1, sizeof(*trans->orderdata));
      if ((flags & SOLVER_TRANSACTION_KEEP_ORDERCYCLES) != 0)
	{
	  Queue *cycles = tod->cycles = solv_calloc(1, sizeof(Queue));
	  queue_init_clone(cycles, &od.cyclesdata);
	  /* map from tes to packages */
	  for (i = 0; i < cycles->count; i++)
	    if (cycles->elements[i])
	      cycles->elements[i] = od.tes[cycles->elements[i]].p;
	  queue_insertn(cycles, cycles->count, od.cycles.count, od.cycles.elements);
	  queue_push(cycles, od.cycles.count / 4);
	}
      if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
	{
	  tod->tes = od.tes;
	  tod->ntes = numte;
	  tod->invedgedata = od.invedgedata;
	  tod->ninvedgedata = od.nedgedata;
	  od.tes = 0;
	  od.invedgedata = 0;
	}
    }
  solv_free(od.tes);
  solv_free(od.invedgedata);
  queue_free(&od.cycles);
  queue_free(&od.cyclesdata);
}


int
transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
{
  int i, j;
  struct s_TransactionOrderdata *od = trans->orderdata;
  struct s_TransactionElement *te;

  if (!od)
     return choices->count;
  if (!chosen)
    {
      /* initialization step */
      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
	te->mark = 0;
      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
	{
	  for (j = te->edges; od->invedgedata[j]; j++)
	    od->tes[od->invedgedata[j]].mark++;
	}
      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
	if (!te->mark)
	  queue_push(choices, te->p);
      return choices->count;
    }
  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
    if (te->p == chosen)
      break;
  if (i == od->ntes)
    return choices->count;
  if (te->mark > 0)
    {
      /* hey! out-of-order installation! */
      te->mark = -1;
    }
  for (j = te->edges; od->invedgedata[j]; j++)
    {
      te = od->tes + od->invedgedata[j];
      assert(te->mark > 0 || te->mark == -1);
      if (te->mark > 0 && --te->mark == 0)
	queue_push(choices, te->p);
    }
  return choices->count;
}

void
transaction_add_obsoleted(Transaction *trans)
{
  Pool *pool = trans->pool;
  Repo *installed = pool->installed;
  Id p;
  Solvable *s;
  int i, j, k, max;
  Map done;
  Queue obsq, *steps;

  if (!installed || !trans->steps.count)
    return;
  /* calculate upper bound */
  max = 0;
  FOR_REPO_SOLVABLES(installed, p, s)
    if (MAPTST(&trans->transactsmap, p))
      max++;
  if (!max)
    return;
  /* make room */
  steps = &trans->steps;
  queue_insertn(steps, 0, max, 0);

  /* now add em */
  map_init(&done, installed->end - installed->start);
  queue_init(&obsq);
  for (j = 0, i = max; i < steps->count; i++)
    {
      p = trans->steps.elements[i];
      if (pool->solvables[p].repo == installed)
	{
	  if (!trans->transaction_installed[p - pool->installed->start])
	    trans->steps.elements[j++] = p;
	  continue;
	}
      trans->steps.elements[j++] = p;
      queue_empty(&obsq);
      transaction_all_obs_pkgs(trans, p, &obsq);
      for (k = 0; k < obsq.count; k++)
	{
	  p = obsq.elements[k];
	  assert(p >= installed->start && p < installed->end);
	  if (!MAPTST(&trans->transactsmap, p))	/* just in case */
	    continue;
	  if (MAPTST(&done, p - installed->start))
	    continue;
	  MAPSET(&done, p - installed->start);
	  trans->steps.elements[j++] = p;
	}
    }

  /* free unneeded space */
  queue_truncate(steps, j);
  map_free(&done);
  queue_free(&obsq);
}

static void
transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen, int onlyprereq, Id noconfpkg, int depth)
{
  Pool *pool = trans->pool;
  Id p, pp;
  Solvable *s;
  int good;

  if (MAPTST(seen, pkg))
    return;
  MAPSET(seen, pkg);
  s = pool->solvables + pkg;
#if 0
  printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
#endif
  if (s->requires)
    {
      Id req, *reqp;
      int inpre = 0;
      reqp = s->repo->idarraydata + s->requires;
      while ((req = *reqp++) != 0)
	{
          if (req == SOLVABLE_PREREQMARKER)
	    {
	      inpre = 1;
	      continue;
	    }
	  if (onlyprereq && !inpre)
	    continue;
	  if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
	    continue;
	  good = 0;
	  /* first check kept packages, then freshly installed, then not yet uninstalled */
	  FOR_PROVIDES(p, pp, req)
	    {
	      if (!MAPTST(ins, p))
		continue;
	      if (MAPTST(&trans->transactsmap, p))
		continue;
	      good++;
	      transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
	    }
	  if (!good)
	    {
	      FOR_PROVIDES(p, pp, req)
		{
		  if (!MAPTST(ins, p))
		    continue;
		  if (pool->solvables[p].repo == pool->installed)
		    continue;
		  good++;
		  transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
		}
	    }
	  if (!good)
	    {
	      FOR_PROVIDES(p, pp, req)
		{
		  if (!MAPTST(ins, p))
		    continue;
		  good++;
		  transaction_check_pkg(trans, tepkg, p, ins, seen, 0, noconfpkg, depth + 1);
		}
	    }
	  if (!good)
	    {
	      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));
	    }
	}
    }
}

void
transaction_check_order(Transaction *trans)
{
  Pool *pool = trans->pool;
  Solvable *s;
  Id p, lastins;
  Map ins, seen;
  int i;

  POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
  map_init(&ins, pool->nsolvables);
  map_init(&seen, pool->nsolvables);
  if (pool->installed)
    {
      FOR_REPO_SOLVABLES(pool->installed, p, s)
        MAPSET(&ins, p);
    }
  lastins = 0;
  for (i = 0; i < trans->steps.count; i++)
    {
      p = trans->steps.elements[i];
      s = pool->solvables + p;
      if (s->repo != pool->installed)
	lastins = p;
      if (s->repo != pool->installed)
	MAPSET(&ins, p);
      if (havescripts(pool, p))
	{
	  MAPZERO(&seen);
	  transaction_check_pkg(trans, p, p, &ins, &seen, 1, lastins, 0);
	}
      if (s->repo == pool->installed)
	MAPCLR(&ins, p);
    }
  map_free(&seen);
  map_free(&ins);
  POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
}

void
transaction_order_get_cycleids(Transaction *trans, Queue *q, int minseverity)
{
  struct s_TransactionOrderdata *od = trans->orderdata;
  Queue *cq;
  int i, cid, ncycles;

  queue_empty(q);
  if (!od || !od->cycles || !od->cycles->count)
    return;
  cq = od->cycles;
  ncycles = cq->elements[cq->count - 1];
  i = cq->count - 1 - ncycles * 4;
  for (cid = 1; cid <= ncycles; cid++, i += 4)
    {
      if (minseverity)
	{
	  int cmin = cq->elements[i + 3] & 0xffff;
	  int cmax = (cq->elements[i + 3] >> 16) & 0xffff;
	  if (minseverity >= SOLVER_ORDERCYCLE_NORMAL && cmin < TYPE_REQ)
	    continue;
	  if (minseverity >= SOLVER_ORDERCYCLE_CRITICAL && (cmax & TYPE_PREREQ) == 0)
	    continue;
	}
      queue_push(q, cid);
    }
}

int
transaction_order_get_cycle(Transaction *trans, Id cid, Queue *q)
{
  struct s_TransactionOrderdata *od = trans->orderdata;
  Queue *cq;
  int cmin, cmax, severity;
  int ncycles;

  queue_empty(q);
  if (!od || !od->cycles || !od->cycles->count)
    return SOLVER_ORDERCYCLE_HARMLESS;
  cq = od->cycles;
  ncycles = cq->elements[cq->count - 1];
  if (cid < 1 || cid > ncycles)
    return SOLVER_ORDERCYCLE_HARMLESS;
  cid =  cq->count - 1 - 4 * (ncycles - cid + 1);
  cmin = cq->elements[cid + 3] & 0xffff;
  cmax = (cq->elements[cid + 3] >> 16) & 0xffff;
  if (cmin < TYPE_REQ)
    severity = SOLVER_ORDERCYCLE_HARMLESS;
  else if ((cmax & TYPE_PREREQ) == 0)
    severity = SOLVER_ORDERCYCLE_NORMAL;
  else
    severity = SOLVER_ORDERCYCLE_CRITICAL;
  if (q)
    queue_insertn(q, 0, cq->elements[cid + 1], cq->elements + cq->elements[cid]);
  return severity;
}