/
Goal.cpp
1780 lines (1583 loc) · 57.7 KB
/
Goal.cpp
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
/*
* Copyright (C) 2018 Red Hat, Inc.
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <assert.h>
#include <map>
#include <vector>
#include <numeric>
extern "C" {
#include <solv/evr.h>
#include <solv/queue.h>
#include <solv/selection.h>
#include <solv/solver.h>
#include <solv/solverdebug.h>
#include <solv/testcase.h>
#include <solv/transaction.h>
#include <solv/rules.h>
}
#include "Goal-private.hpp"
#include "../hy-goal-private.hpp"
#include "../hy-iutil-private.hpp"
#include "../hy-package-private.hpp"
#include "../dnf-sack-private.hpp"
#include "../sack/packageset.hpp"
#include "../sack/query.hpp"
#include "../sack/selector.hpp"
#include "../utils/bgettext/bgettext-lib.h"
#include "../utils/tinyformat/tinyformat.hpp"
#include "IdQueue.hpp"
#include "../utils/filesystem.hpp"
namespace {
std::string pkgSolvid2str(Pool * pool, Id source)
{
return pool_solvid2str(pool, source);
}
std::string moduleSolvid2str(Pool * pool, Id source)
{
std::ostringstream ss;
auto * solvable = pool_id2solvable(pool, source);
// Add name:stream
ss << solvable_lookup_str(solvable, SOLVABLE_DESCRIPTION);
//Add version
ss << ":" << pool_id2str(pool, solvable->evr);
// Add original context
ss << ":" << solvable_lookup_str(solvable, SOLVABLE_SUMMARY);
ss << "." << pool_id2str(pool, solvable->arch);
return ss.str();
}
}
namespace libdnf {
enum {NO_MATCH=1, MULTIPLE_MATCH_OBJECTS, INCORECT_COMPARISON_TYPE};
static std::map<int, const char *> ERROR_DICT = {
{MULTIPLE_MATCH_OBJECTS, M_("Ill-formed Selector, presence of multiple match objects in the filter")},
{INCORECT_COMPARISON_TYPE, M_("Ill-formed Selector used for the operation, incorrect comparison type")}
};
enum {RULE_DISTUPGRADE=1, RULE_INFARCH, RULE_UPDATE, RULE_JOB, RULE_JOB_UNSUPPORTED, RULE_JOB_NOTHING_PROVIDES_DEP,
RULE_JOB_UNKNOWN_PACKAGE, RULE_JOB_PROVIDED_BY_SYSTEM, RULE_PKG, RULE_BEST_1, RULE_BEST_2,
RULE_PKG_NOT_INSTALLABLE_1, RULE_PKG_NOT_INSTALLABLE_2, RULE_PKG_NOT_INSTALLABLE_3, RULE_PKG_NOT_INSTALLABLE_4, RULE_PKG_NOTHING_PROVIDES_DEP,
RULE_PKG_SAME_NAME, RULE_PKG_CONFLICTS, RULE_PKG_OBSOLETES, RULE_PKG_INSTALLED_OBSOLETES, RULE_PKG_IMPLICIT_OBSOLETES,
RULE_PKG_REQUIRES, RULE_PKG_SELF_CONFLICT, RULE_YUMOBS
};
static const std::map<int, const char *> PKG_PROBLEMS_DICT = {
{RULE_DISTUPGRADE, M_("%s from %s does not belong to a distupgrade repository")},
{RULE_INFARCH, M_("%s from %s has inferior architecture")},
{RULE_UPDATE, M_("problem with installed package ")},
{RULE_JOB, M_("conflicting requests")},
{RULE_JOB_UNSUPPORTED, M_("unsupported request")},
{RULE_JOB_NOTHING_PROVIDES_DEP, M_("nothing provides requested ")},
{RULE_JOB_UNKNOWN_PACKAGE, M_("package %s does not exist")},
{RULE_JOB_PROVIDED_BY_SYSTEM, M_(" is provided by the system")},
{RULE_PKG, M_("some dependency problem")},
{RULE_BEST_1, M_("cannot install the best update candidate for package ")},
{RULE_BEST_2, M_("cannot install the best candidate for the job")},
{RULE_PKG_NOT_INSTALLABLE_1, M_("package %s from %s is filtered out by modular filtering")},
{RULE_PKG_NOT_INSTALLABLE_2, M_("package %s from %s does not have a compatible architecture")},
{RULE_PKG_NOT_INSTALLABLE_3, M_("package %s from %s is not installable")},
{RULE_PKG_NOT_INSTALLABLE_4, M_("package %s from %s is filtered out by exclude filtering")},
{RULE_PKG_NOTHING_PROVIDES_DEP, M_("nothing provides %s needed by %s from %s")},
{RULE_PKG_SAME_NAME, M_("cannot install both %s from %s and %s from %s")},
{RULE_PKG_CONFLICTS, M_("package %s from %s conflicts with %s provided by %s from %s")},
{RULE_PKG_OBSOLETES, M_("package %s from %s obsoletes %s provided by %s from %s")},
{RULE_PKG_INSTALLED_OBSOLETES, M_("installed package %s obsoletes %s provided by %s from %s")},
{RULE_PKG_IMPLICIT_OBSOLETES, M_("package %s from %s implicitly obsoletes %s provided by %s from %s")},
{RULE_PKG_REQUIRES, M_("package %s from %s requires %s, but none of the providers can be installed")},
{RULE_PKG_SELF_CONFLICT, M_("package %s from %s conflicts with %s provided by itself")},
{RULE_YUMOBS, M_("both package %s from %s and %s from %s obsolete %s")}
};
static const std::map<int, const char *> MODULE_PROBLEMS_DICT = {
{RULE_DISTUPGRADE, M_("%s from %s does not belong to a distupgrade repository")},
{RULE_INFARCH, M_("%s from %s has inferior architecture")},
{RULE_UPDATE, M_("problem with installed module ")},
{RULE_JOB, M_("conflicting requests")},
{RULE_JOB_UNSUPPORTED, M_("unsupported request")},
{RULE_JOB_NOTHING_PROVIDES_DEP, M_("nothing provides requested ")},
{RULE_JOB_UNKNOWN_PACKAGE, M_("module %s does not exist")},
{RULE_JOB_PROVIDED_BY_SYSTEM, M_(" is provided by the system")},
{RULE_PKG, M_("some dependency problem")},
{RULE_BEST_1, M_("cannot install the best update candidate for module ")},
{RULE_BEST_2, M_("cannot install the best candidate for the job")},
{RULE_PKG_NOT_INSTALLABLE_1, M_("module %s from %s is disabled")},
{RULE_PKG_NOT_INSTALLABLE_2, M_("module %s from %s does not have a compatible architecture")},
{RULE_PKG_NOT_INSTALLABLE_3, M_("module %s from %s is not installable")},
{RULE_PKG_NOT_INSTALLABLE_4, M_("module %s from %s is disabled")},
{RULE_PKG_NOTHING_PROVIDES_DEP, M_("nothing provides %s needed by module %s from %s")},
{RULE_PKG_SAME_NAME, M_("cannot install both modules %s from %s and %s from %s")},
{RULE_PKG_CONFLICTS, M_("module %s from %s conflicts with %s provided by %s from %s")},
{RULE_PKG_OBSOLETES, M_("module %s from %s obsoletes %s provided by %s from %s")},
{RULE_PKG_INSTALLED_OBSOLETES, M_("installed module %s obsoletes %s provided by %s from %s")},
{RULE_PKG_IMPLICIT_OBSOLETES, M_("module %s from %s implicitly obsoletes %s provided by %s from %s")},
{RULE_PKG_REQUIRES, M_("module %s from %s requires %s, but none of the providers can be installed")},
{RULE_PKG_SELF_CONFLICT, M_("module %s from %s conflicts with %s provided by itself")},
{RULE_YUMOBS, M_("both module %s from %s and %s from %s obsolete %s")}
};
static std::string
libdnf_problemruleinfo2str(libdnf::PackageSet * modularExclude, Solver *solv, SolverRuleinfo type, Id source, Id target,
Id dep, bool pkgs)
{
const std::map<int, const char *> & problemDict = pkgs ? PKG_PROBLEMS_DICT : MODULE_PROBLEMS_DICT;
const auto solvid2str = pkgs ? pkgSolvid2str : moduleSolvid2str;
Pool * const pool = solv->pool;
Solvable *ss;
switch (type) {
case SOLVER_RULE_DISTUPGRADE:
return tfm::format(TM_(problemDict.at(RULE_DISTUPGRADE), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name);
case SOLVER_RULE_INFARCH:
return tfm::format(TM_(problemDict.at(RULE_DISTUPGRADE), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name);
case SOLVER_RULE_UPDATE:
return std::string(TM_(problemDict.at(RULE_UPDATE), 1)) + solvid2str(pool, source);
case SOLVER_RULE_JOB:
return std::string(TM_(problemDict.at(RULE_JOB), 1));
case SOLVER_RULE_JOB_UNSUPPORTED:
return std::string(TM_(problemDict.at(RULE_JOB_UNSUPPORTED), 1));
case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
return std::string(TM_(problemDict.at(RULE_JOB_NOTHING_PROVIDES_DEP), 1)) + pool_dep2str(pool, dep);
case SOLVER_RULE_JOB_UNKNOWN_PACKAGE:
return tfm::format(TM_(problemDict.at(RULE_JOB_UNKNOWN_PACKAGE), 1), pool_dep2str(pool, dep));
case SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM:
return std::string(pool_dep2str(pool, dep)) + TM_(problemDict.at(RULE_JOB_PROVIDED_BY_SYSTEM), 1);
case SOLVER_RULE_PKG:
return std::string(TM_(problemDict.at(RULE_PKG), 1));
case SOLVER_RULE_BEST:
if (source > 0)
return std::string(TM_(problemDict.at(RULE_BEST_1), 1)) + solvid2str(pool, source);
return std::string(TM_(problemDict.at(RULE_BEST_2), 1));
case SOLVER_RULE_PKG_NOT_INSTALLABLE:
ss = pool->solvables + source;
if (pool_disabled_solvable(pool, ss)) {
if (modularExclude && modularExclude->has(source)) {
return tfm::format(TM_(problemDict.at(RULE_PKG_NOT_INSTALLABLE_1), 1),
solvid2str(pool, source).c_str(), pool_id2solvable(pool, source)->repo->name);
} else {
return tfm::format(TM_(problemDict.at(RULE_PKG_NOT_INSTALLABLE_4), 1),
solvid2str(pool, source).c_str(), pool_id2solvable(pool, source)->repo->name);
}
}
if (ss->arch && ss->arch != ARCH_SRC && ss->arch != ARCH_NOSRC &&
pool->id2arch && (ss->arch > pool->lastarch || !pool->id2arch[ss->arch]))
return tfm::format(TM_(problemDict.at(RULE_PKG_NOT_INSTALLABLE_2), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name);
return tfm::format(TM_(problemDict.at(RULE_PKG_NOT_INSTALLABLE_3), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name);
case SOLVER_RULE_PKG_NOTHING_PROVIDES_DEP:
return tfm::format(TM_(problemDict.at(RULE_PKG_NOTHING_PROVIDES_DEP), 1), pool_dep2str(pool, dep),
solvid2str(pool, source).c_str(), pool_id2solvable(pool, source)->repo->name);
case SOLVER_RULE_PKG_SAME_NAME:
return tfm::format(TM_(problemDict.at(RULE_PKG_SAME_NAME), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name, solvid2str(pool, target).c_str(),
pool_id2solvable(pool, target)->repo->name);
case SOLVER_RULE_PKG_CONFLICTS:
return tfm::format(TM_(problemDict.at(RULE_PKG_CONFLICTS), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name, pool_dep2str(pool, dep),
solvid2str(pool, target).c_str(), pool_id2solvable(pool, target)->repo->name);
case SOLVER_RULE_PKG_OBSOLETES:
return tfm::format(TM_(problemDict.at(RULE_PKG_OBSOLETES), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name, pool_dep2str(pool, dep),
solvid2str(pool, target).c_str(), pool_id2solvable(pool, target)->repo->name);
case SOLVER_RULE_PKG_INSTALLED_OBSOLETES:
return tfm::format(TM_(problemDict.at(RULE_PKG_INSTALLED_OBSOLETES), 1),
solvid2str(pool, source).c_str(), pool_dep2str(pool, dep),
solvid2str(pool, target).c_str(), pool_id2solvable(pool, target)->repo->name);
case SOLVER_RULE_PKG_IMPLICIT_OBSOLETES:
return tfm::format(TM_(problemDict.at(RULE_PKG_IMPLICIT_OBSOLETES), 1),
solvid2str(pool, source).c_str(), pool_dep2str(pool, dep),
pool_id2solvable(pool, source)->repo->name, solvid2str(pool, target).c_str(),
pool_id2solvable(pool, target)->repo->name);
case SOLVER_RULE_PKG_REQUIRES:
return tfm::format(TM_(problemDict.at(RULE_PKG_REQUIRES), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name, pool_dep2str(pool, dep));
case SOLVER_RULE_PKG_SELF_CONFLICT:
return tfm::format(TM_(problemDict.at(RULE_PKG_SELF_CONFLICT), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name, pool_dep2str(pool, dep));
case SOLVER_RULE_YUMOBS:
return tfm::format(TM_(problemDict.at(RULE_YUMOBS), 1), solvid2str(pool, source).c_str(),
pool_id2solvable(pool, source)->repo->name, solvid2str(pool, target).c_str(),
pool_id2solvable(pool, target)->repo->name, pool_dep2str(pool, dep));
default:
return solver_problemruleinfo2str(solv, type, source, target, dep);
}
}
static void
packageToJob(DnfPackage * package, Queue * job, int solver_action)
{
IdQueue pkgs;
Pool *pool = dnf_package_get_pool(package);
DnfSack *sack = dnf_package_get_sack(package);
dnf_sack_recompute_considered(sack);
dnf_sack_make_provides_ready(sack);
pkgs.pushBack(dnf_package_get_id(package));
Id what = pool_queuetowhatprovides(pool, pkgs.getQueue());
queue_push2(job, SOLVER_SOLVABLE_ONE_OF|SOLVER_SETARCH|SOLVER_SETEVR|solver_action, what);
}
static int
jobHas(Queue *job, Id what, Id id)
{
for (int i = 0; i < job->count; i += 2)
if (job->elements[i] == what && job->elements[i + 1] == id)
return 1;
return 0;
}
static int
filterArchToJob(DnfSack *sack, const Filter *f, Queue *job)
{
if (!f)
return 0;
auto matches = f->getMatches();
if (f->getCmpType() != HY_EQ) {
return INCORECT_COMPARISON_TYPE;
}
if (matches.size() != 1) {
return MULTIPLE_MATCH_OBJECTS;
}
Pool *pool = dnf_sack_get_pool(sack);
const char *arch = matches[0].str;
Id archid = str2archid(pool, arch);
if (archid == 0)
return NO_MATCH;
for (int i = 0; i < job->count; i += 2) {
Id dep;
assert((job->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_NAME);
dep = pool_rel2id(pool, job->elements[i + 1],
archid, REL_ARCH, 1);
job->elements[i] |= SOLVER_SETARCH;
job->elements[i + 1] = dep;
}
return 0;
}
static int
filterEvrToJob(DnfSack *sack, const Filter *f, Queue *job)
{
if (!f)
return 0;
auto matches = f->getMatches();
if (f->getCmpType() != HY_EQ) {
return INCORECT_COMPARISON_TYPE;
}
if (matches.size() != 1) {
return MULTIPLE_MATCH_OBJECTS;
}
Pool *pool = dnf_sack_get_pool(sack);
Id evr = pool_str2id(pool, matches[0].str, 1);
Id constr = f->getKeyname() == HY_PKG_VERSION ? SOLVER_SETEV : SOLVER_SETEVR;
for (int i = 0; i < job->count; i += 2) {
Id dep;
assert((job->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_NAME);
dep = pool_rel2id(pool, job->elements[i + 1],
evr, REL_EQ, 1);
job->elements[i] |= constr;
job->elements[i + 1] = dep;
}
return 0;
}
static int
filterFileToJob(DnfSack *sack, const Filter *f, Queue *job)
{
if (!f)
return 0;
auto matches = f->getMatches();
if (matches.size() != 1) {
return MULTIPLE_MATCH_OBJECTS;
}
const char *file = matches[0].str;
Pool *pool = dnf_sack_get_pool(sack);
int flags = f->getCmpType() & HY_GLOB ? SELECTION_GLOB : 0;
if (f->getCmpType() & HY_GLOB)
flags |= SELECTION_NOCASE;
if (selection_make(pool, job, file, flags | SELECTION_FILELIST) == 0)
return NO_MATCH;
return 0;
}
static int
filterPkgToJob(Id what, Queue *job)
{
if (!what)
return 0;
queue_push2(job, SOLVER_SOLVABLE_ONE_OF|SOLVER_SETARCH|SOLVER_SETEVR, what);
return 0;
}
static int
filterNameToJob(DnfSack *sack, const Filter *f, Queue *job)
{
if (!f)
return 0;
if (f->getMatches().size() != 1)
return MULTIPLE_MATCH_OBJECTS;
Pool *pool = dnf_sack_get_pool(sack);
const char *name = f->getMatches()[0].str;
Id id;
Dataiterator di;
switch (f->getCmpType()) {
case HY_EQ:
id = pool_str2id(pool, name, 0);
if (id)
queue_push2(job, SOLVER_SOLVABLE_NAME, id);
break;
case HY_GLOB:
dataiterator_init(&di, pool, 0, 0, SOLVABLE_NAME, name, SEARCH_GLOB);
while (dataiterator_step(&di)) {
if (!is_package(pool, pool_id2solvable(pool, di.solvid)))
continue;
assert(di.idp);
id = *di.idp;
if (jobHas(job, SOLVABLE_NAME, id))
continue;
queue_push2(job, SOLVER_SOLVABLE_NAME, id);
}
dataiterator_free(&di);
break;
default:
return INCORECT_COMPARISON_TYPE;
}
return 0;
}
static int
filterProvidesToJob(DnfSack *sack, const Filter *f, Queue *job)
{
if (!f)
return 0;
auto matches = f->getMatches();
if (f->getMatches().size() != 1)
return MULTIPLE_MATCH_OBJECTS;
const char *name;
Pool *pool = dnf_sack_get_pool(sack);
Id id;
Dataiterator di;
switch (f->getCmpType()) {
case HY_EQ:
id = matches[0].reldep;
queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
break;
case HY_GLOB:
name = matches[0].str;
dataiterator_init(&di, pool, 0, 0, SOLVABLE_PROVIDES, name, SEARCH_GLOB);
while (dataiterator_step(&di)) {
if (is_package(pool, pool_id2solvable(pool, di.solvid)))
break;
}
assert(di.idp);
id = *di.idp;
if (!jobHas(job, SOLVABLE_PROVIDES, id))
queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
dataiterator_free(&di);
break;
default:
return INCORECT_COMPARISON_TYPE;
}
return 0;
}
static int
filterReponameToJob(DnfSack *sack, const Filter *f, Queue *job)
{
Id i;
LibsolvRepo *repo;
if (!f)
return 0;
auto matches = f->getMatches();
if (f->getCmpType() != HY_EQ) {
return INCORECT_COMPARISON_TYPE;
}
if (matches.size() != 1) {
return MULTIPLE_MATCH_OBJECTS;
}
IdQueue repo_sel;
Pool *pool = dnf_sack_get_pool(sack);
FOR_REPOS(i, repo)
if (!strcmp(matches[0].str, repo->name)) {
repo_sel.pushBack(SOLVER_SOLVABLE_REPO | SOLVER_SETREPO, repo->repoid);
}
selection_filter(pool, job, repo_sel.getQueue());
return 0;
}
/**
* Build job queue from a Query.
*
* Returns an error code
*/
void
sltrToJob(const HySelector sltr, Queue *job, int solver_action)
{
DnfSack *sack = sltr->getSack();
int ret = 0;
int any_opt_filter = sltr->getFilterArch() || sltr->getFilterEvr()
|| sltr->getFilterReponame();
int any_req_filter = sltr->getFilterName() || sltr->getFilterProvides()
|| sltr->getFilterFile() || sltr->getPkgs();
IdQueue job_sltr;
if (!any_req_filter) {
if (any_opt_filter) {
// no name or provides or file in the selector is an error
throw Goal::Error("Ill-formed Selector. No name or"
"provides or file in the selector.", DNF_ERROR_BAD_SELECTOR);
}
goto finish;
}
dnf_sack_recompute_considered(sack);
dnf_sack_make_provides_ready(sack);
ret = filterPkgToJob(sltr->getPkgs(), job_sltr.getQueue());
if (ret)
goto finish;
ret = filterNameToJob(sack, sltr->getFilterName(), job_sltr.getQueue());
if (ret)
goto finish;
ret = filterFileToJob(sack, sltr->getFilterFile(), job_sltr.getQueue());
if (ret)
goto finish;
ret = filterProvidesToJob(sack, sltr->getFilterProvides(), job_sltr.getQueue());
if (ret)
goto finish;
ret = filterArchToJob(sack, sltr->getFilterArch(), job_sltr.getQueue());
if (ret)
goto finish;
ret = filterEvrToJob(sack, sltr->getFilterEvr(), job_sltr.getQueue());
if (ret)
goto finish;
ret = filterReponameToJob(sack, sltr->getFilterReponame(), job_sltr.getQueue());
if (ret)
goto finish;
for (int i = 0; i < job_sltr.size(); i += 2)
queue_push2(job, job_sltr[i] | solver_action, job_sltr[i + 1]);
finish:
if (ret > 1) {
throw Goal::Error(TM_(ERROR_DICT[ret], 1), DNF_ERROR_BAD_SELECTOR);
}
}
#define BLOCK_SIZE 15
struct InstallonliesSortCallback {
Pool *pool;
Id running_kernel;
};
static inline void
queue2pset(const IdQueue & queue, PackageSet * pset)
{
for (int i = 0; i < queue.size(); ++i)
pset->set(queue[i]);
}
static bool
/**
* @brief return false iff a does not depend on anything from b
*/
can_depend_on(Pool *pool, Solvable *sa, Id b)
{
IdQueue dep_requires;
solvable_lookup_idarray(sa, SOLVABLE_REQUIRES, dep_requires.getQueue());
for (int i = 0; i < dep_requires.size(); ++i) {
Id req_dep = dep_requires[i];
Id p, pp;
FOR_PROVIDES(p, pp, req_dep)
if (p == b)
return true;
}
return false;
}
static int
sort_packages(const void *ap, const void *bp, void *s_cb)
{
Id a = *(Id*)ap;
Id b = *(Id*)bp;
Pool *pool = ((struct InstallonliesSortCallback*) s_cb)->pool;
Id kernel = ((struct InstallonliesSortCallback*) s_cb)->running_kernel;
Solvable *sa = pool_id2solvable(pool, a);
Solvable *sb = pool_id2solvable(pool, b);
/* if the names are different sort them differently, particular order does
not matter as long as it's consistent. */
int name_diff = sa->name - sb->name;
if (name_diff)
return name_diff;
/* same name, if one is/depends on the running kernel put it last */
/* move available packages to end of the list */
if (pool->installed != sa->repo)
return 1;
if (pool->installed != sb->repo)
return -1;
if (kernel >= 0) {
if (a == kernel || can_depend_on(pool, sa, kernel))
return 1;
if (b == kernel || can_depend_on(pool, sb, kernel))
return -1;
// if package has same evr as kernel try them to keep (kernel-devel packages)
Solvable * kernelSolvable = pool_id2solvable(pool, kernel);
if (sa->evr == kernelSolvable->evr) {
return 1;
}
if (sb->evr == kernelSolvable->evr) {
return -1;
}
}
return pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
}
static void
same_name_subqueue(Pool *pool, Queue *in, Queue *out)
{
Id el = queue_pop(in);
Id name = pool_id2solvable(pool, el)->name;
queue_empty(out);
queue_push(out, el);
while (in->count &&
pool_id2solvable(pool, in->elements[in->count - 1])->name == name)
// reverses the order so packages are sorted by descending version
queue_push(out, queue_pop(in));
}
static std::unique_ptr<PackageSet>
remove_pkgs_with_same_nevra_from_pset(DnfPackageSet* pset, DnfPackageSet* remove_musters,
DnfSack* sack)
{
std::unique_ptr<PackageSet> final_pset(new PackageSet(sack));
Id id1 = -1;
while(true) {
id1 = pset->next(id1);
if (id1 == -1)
break;
DnfPackage *pkg1 = dnf_package_new(sack, id1);
Id id2 = -1;
bool found = false;
while(true) {
id2 = remove_musters->next(id2);
if (id2 == -1)
break;
DnfPackage *pkg2 = dnf_package_new(sack, id2);
if (!dnf_package_cmp(pkg1, pkg2)) {
found = true;
g_object_unref(pkg2);
break;
}
g_object_unref(pkg2);
}
if (!found) {
final_pset->set(pkg1);
}
g_object_unref(pkg1);
}
return final_pset;
}
static int
erase_flags2libsolv(int flags)
{
int ret = 0;
if (flags & HY_CLEAN_DEPS)
ret |= SOLVER_CLEANDEPS;
return ret;
}
static bool
NameSolvableComparator(const Solvable * first, const Solvable * second)
{
return first->name < second->name;
}
Goal::Goal(const Goal & goal_src) : pImpl(new Impl(*goal_src.pImpl)) {}
Goal::Impl::Impl(const Goal::Impl & goal_src)
: sack(goal_src.sack), exclude_from_weak(goal_src.exclude_from_weak)
{
queue_init_clone(&staging, const_cast<Queue *>(&goal_src.staging));
actions = goal_src.actions;
if (goal_src.protectedPkgs) {
protectedPkgs.reset(new PackageSet(*goal_src.protectedPkgs.get()));
}
if (goal_src.removalOfProtected) {
removalOfProtected.reset(new PackageSet(*goal_src.removalOfProtected.get()));
}
}
Goal::Impl::Impl(DnfSack *sack)
: sack(sack), exclude_from_weak(sack)
{
queue_init(&staging);
}
Goal::Goal(DnfSack *sack) : pImpl(new Impl(sack)) {}
Goal::~Goal() = default;
Goal::Impl::~Impl()
{
if (trans)
transaction_free(trans);
if (solv)
solver_free(solv);
queue_free(&staging);
}
DnfGoalActions Goal::getActions() { return pImpl->actions; }
DnfSack * Goal::getSack() { return pImpl->sack; }
int
Goal::getReason(DnfPackage *pkg)
{
//solver_get_recommendations
if (!pImpl->solv)
return HY_REASON_USER;
Id info;
const Id pkgID = dnf_package_get_id(pkg);
int reason = solver_describe_decision(pImpl->solv, pkgID, &info);
if ((reason == SOLVER_REASON_UNIT_RULE ||
reason == SOLVER_REASON_RESOLVE_JOB) &&
(solver_ruleclass(pImpl->solv, info) == SOLVER_RULE_JOB ||
solver_ruleclass(pImpl->solv, info) == SOLVER_RULE_BEST))
return HY_REASON_USER;
if (reason == SOLVER_REASON_CLEANDEPS_ERASE)
return HY_REASON_CLEAN;
if (reason == SOLVER_REASON_WEAKDEP)
return HY_REASON_WEAKDEP;
IdQueue cleanDepsQueue;
solver_get_cleandeps(pImpl->solv, cleanDepsQueue.getQueue());
for (int i = 0; i < cleanDepsQueue.size(); ++i) {
if (cleanDepsQueue[i] == pkgID) {
return HY_REASON_CLEAN;
}
}
return HY_REASON_DEP;
}
void
Goal::addProtected(PackageSet & pset)
{
if (!pImpl->protectedPkgs) {
pImpl->protectedPkgs.reset(new PackageSet(pset));
} else {
map_or(pImpl->protectedPkgs->getMap(), pset.getMap());
}
}
bool
Goal::get_protect_running_kernel() const noexcept
{
return pImpl->protect_running_kernel;
}
void
Goal::set_protect_running_kernel(bool value)
{
pImpl->protect_running_kernel = value;
}
void
Goal::setProtected(const PackageSet & pset)
{
pImpl->protectedPkgs.reset(new PackageSet(pset));
}
void
Goal::distupgrade()
{
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_DISTUPGRADE|DNF_ALLOW_DOWNGRADE);
DnfSack * sack = pImpl->sack;
Query query(sack);
query.available();
Selector selector(sack);
selector.set(query.runSet());
sltrToJob(&selector, &pImpl->staging, SOLVER_DISTUPGRADE);
}
void
Goal::distupgrade(DnfPackage *new_pkg)
{
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_DISTUPGRADE|DNF_ALLOW_DOWNGRADE);
packageToJob(new_pkg, &pImpl->staging, SOLVER_DISTUPGRADE);
}
void
Goal::distupgrade(HySelector sltr)
{
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_DISTUPGRADE|DNF_ALLOW_DOWNGRADE);
sltrToJob(sltr, &pImpl->staging, SOLVER_DISTUPGRADE);
}
void
Goal::erase(DnfPackage *pkg, int flags)
{
int additional = erase_flags2libsolv(flags);
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_ERASE);
queue_push2(&pImpl->staging, SOLVER_SOLVABLE|SOLVER_ERASE|additional, dnf_package_get_id(pkg));
}
void
Goal::erase(HySelector sltr, int flags)
{
int additional = erase_flags2libsolv(flags);
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_ERASE);
sltrToJob(sltr, &pImpl->staging, SOLVER_ERASE|additional);
}
void
Goal::install(DnfPackage *new_pkg, bool optional)
{
int solverActions = SOLVER_INSTALL;
if (optional) {
solverActions |= SOLVER_WEAK;
}
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_INSTALL|DNF_ALLOW_DOWNGRADE);
packageToJob(new_pkg, &pImpl->staging, solverActions);
}
void
Goal::lock(DnfPackage *pkg)
{
queue_push2(&pImpl->staging, SOLVER_SOLVABLE|SOLVER_LOCK, dnf_package_get_id(pkg));
}
void
Goal::favor(DnfPackage *pkg)
{
queue_push2(&pImpl->staging, SOLVER_SOLVABLE|SOLVER_FAVOR, dnf_package_get_id(pkg));
}
void
Goal::add_exclude_from_weak(const DnfPackageSet & pset)
{
pImpl->exclude_from_weak += pset;
}
void
Goal::add_exclude_from_weak(DnfPackage *pkg)
{
// ensure that the map has a corrent size before set to prevent memory corruption
map_grow(pImpl->exclude_from_weak.getMap(), dnf_sack_get_pool(pImpl->sack)->nsolvables);
pImpl->exclude_from_weak.set(pkg);
}
void
Goal::reset_exclude_from_weak()
{
pImpl->exclude_from_weak.clear();
}
void
Goal::exclude_from_weak_autodetect()
{
Query installed_query(pImpl->sack, Query::ExcludeFlags::IGNORE_EXCLUDES);
installed_query.installed();
if (installed_query.empty()) {
return;
}
Query base_query(pImpl->sack);
base_query.apply();
auto * installed_pset = installed_query.getResultPset();
Id installed_id = -1;
std::vector<const char *> installed_names;
installed_names.reserve(installed_pset->size() + 1);
// Iterate over installed packages to detect unmet weak deps
while ((installed_id = installed_pset->next(installed_id)) != -1) {
g_autoptr(DnfPackage) pkg = dnf_package_new(pImpl->sack, installed_id);
installed_names.push_back(dnf_package_get_name(pkg));
std::unique_ptr<libdnf::DependencyContainer> recommends(dnf_package_get_recommends(pkg));
for (int i = 0; i < recommends->count(); ++i) {
std::unique_ptr<libdnf::Dependency> dep(recommends->getPtr(i));
const char * dep_string = dep->toString();
if (dep_string[0] == '(') {
continue;
}
Query query(base_query);
const char * version = dep->getVersion();
// There can be installed provider in different version or upgraded packed can recommend a different version
// Ignore version and search only by reldep name
if (version && strlen(version) > 0) {
query.addFilter(HY_PKG_PROVIDES, HY_EQ, dep->getName());
} else {
query.addFilter(HY_PKG_PROVIDES, dep.get());
}
// No providers of recommend => continue
if (query.empty()) {
continue;
}
Query test_installed(query);
test_installed.installed();
// when there is not installed any provider of recommend, exclude it
if (test_installed.empty()) {
add_exclude_from_weak(*query.getResultPset());
}
}
}
// Investigate supplements of only available packages with a different name to installed packages
installed_names.push_back(nullptr);
base_query.addFilter(HY_PKG_NAME, HY_NEQ, installed_names.data());
auto * available_pset = base_query.getResultPset();
*available_pset -= *installed_pset;
Id available_id = -1;
while ((available_id = available_pset->next(available_id)) != -1) {
g_autoptr(DnfPackage) pkg = dnf_package_new(pImpl->sack, available_id);
std::unique_ptr<libdnf::DependencyContainer> supplements(dnf_package_get_supplements(pkg));
if (supplements->count() == 0) {
continue;
}
libdnf::DependencyContainer supplements_without_rich(getSack());
for (int i = 0; i < supplements->count(); ++i) {
std::unique_ptr<libdnf::Dependency> dep(supplements->getPtr(i));
const char * dep_string = dep->toString();
if (dep_string[0] == '(') {
continue;
}
supplements_without_rich.add(dep.get());
}
if (supplements_without_rich.count() == 0) {
continue;
}
Query query(installed_query);
query.addFilter(HY_PKG_PROVIDES, &supplements_without_rich);
// When supplemented package already installed, exclude_from_weak available package
if (!query.empty()) {
add_exclude_from_weak(pkg);
}
}
}
void
Goal::disfavor(DnfPackage *pkg)
{
queue_push2(&pImpl->staging, SOLVER_SOLVABLE|SOLVER_DISFAVOR, dnf_package_get_id(pkg));
}
void
Goal::install(HySelector sltr, bool optional)
{
int solverActions = SOLVER_INSTALL;
if (optional) {
solverActions |= SOLVER_WEAK;
}
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_INSTALL|DNF_ALLOW_DOWNGRADE);
sltrToJob(sltr, &pImpl->staging, solverActions);
}
void
Goal::upgrade()
{
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_UPGRADE_ALL);
queue_push2(&pImpl->staging, SOLVER_UPDATE|SOLVER_SOLVABLE_ALL, 0);
}
void
Goal::upgrade(DnfPackage *new_pkg)
{
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_UPGRADE);
packageToJob(new_pkg, &pImpl->staging, SOLVER_UPDATE);
}
void
Goal::upgrade(HySelector sltr)
{
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | DNF_UPGRADE);
auto flags = SOLVER_UPDATE;
if (sltr->getPkgs()) {
flags |= SOLVER_TARGETED;
}
sltrToJob(sltr, &pImpl->staging, flags);
}
void
Goal::userInstalled(DnfPackage *pkg)
{
queue_push2(&pImpl->staging, SOLVER_SOLVABLE|SOLVER_USERINSTALLED, dnf_package_get_id(pkg));
}
void
Goal::userInstalled(PackageSet & pset)
{
Id id = -1;
while (true) {
id = pset.next(id);
if (id == -1)
break;
queue_push2(&pImpl->staging, SOLVER_SOLVABLE|SOLVER_USERINSTALLED, id);
}
}
bool
Goal::hasActions(DnfGoalActions action)
{
return pImpl->actions & action;
}
int
Goal::jobLength()
{
return (&pImpl->staging)->count / 2;
}
bool
Goal::run(DnfGoalActions flags)
{
auto job = pImpl->constructJob(flags);
pImpl->actions = static_cast<DnfGoalActions>(pImpl->actions | flags);
int ret = pImpl->solve(job->getQueue(), flags);
return ret;
}
int
Goal::countProblems()
{
return pImpl->countProblems();
}