You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1139 lines
35 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Order List - Represents and Manipulates an ordered list of packages.
  5. A list of packages can be ordered by a number of conflicting criteria
  6. each given a specific priority. Each package also has a set of flags
  7. indicating some useful things about it that are derived in the
  8. course of sorting. The pkgPackageManager class uses this class for
  9. all of it's installation ordering needs.
  10. This is a modified version of Manoj's Routine B. It consists of four
  11. independent ordering algorithms that can be applied at for different
  12. points in the ordering. By applying progressivly fewer ordering
  13. operations it is possible to give each consideration it's own
  14. priority and create an order that satisfies the lowest applicable
  15. consideration.
  16. The rules for unpacking ordering are:
  17. 1) Unpacking ignores Depends: on all packages
  18. 2) Unpacking requires Conflicts: on -ALL- packages to be satisfied
  19. 3) Unpacking requires PreDepends: on this package only to be satisfied
  20. 4) Removing requires that no packages depend on the package to be
  21. removed.
  22. And the rule for configuration ordering is:
  23. 1) Configuring requires that the Depends: of the package be satisfied
  24. Conflicts+PreDepends are ignored because unpacking says they are
  25. already correct [exageration, it does check but we need not be
  26. concerned]
  27. And some features that are valuable for unpacking ordering.
  28. f1) Unpacking a new package should advoid breaking dependencies of
  29. configured packages
  30. f2) Removal should not require a force, corrolory of f1
  31. f3) Unpacking should order by depends rather than fall back to random
  32. ordering.
  33. Each of the features can be enabled in the sorting routine at an
  34. arbitrary priority to give quite abit of control over the final unpacking
  35. order.
  36. The rules listed above may never be violated and are called Critical.
  37. When a critical rule is violated then a loop condition is recorded
  38. and will have to be delt with in the caller.
  39. The ordering keeps two lists, the main list and the 'After List'. The
  40. purpose of the after list is to allow packages to be delayed. This is done
  41. by setting the after flag on the package. Any package which requires this
  42. package to be ordered before will inherit the after flag and so on. This
  43. is used for CD swap ordering where all packages on a second CD have the
  44. after flag set. This forces them and all their dependents to be ordered
  45. toward the end.
  46. There are complications in this algorithm when presented with cycles.
  47. For all known practical cases it works, all cases where it doesn't work
  48. is fixable by tweaking the package descriptions. However, it should be
  49. possible to improve this further to make some better choices when
  50. presented with cycles.
  51. ##################################################################### */
  52. /*}}}*/
  53. // Include Files /*{{{*/
  54. #include <config.h>
  55. #include <apt-pkg/configuration.h>
  56. #include <apt-pkg/depcache.h>
  57. #include <apt-pkg/error.h>
  58. #include <apt-pkg/orderlist.h>
  59. #include <apt-pkg/pkgcache.h>
  60. #include <algorithm>
  61. #include <iostream>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. /*}}}*/
  65. using namespace std;
  66. // OrderList::pkgOrderList - Constructor /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* */
  69. pkgOrderList::pkgOrderList(pkgDepCache *pCache) : d(NULL), Cache(*pCache),
  70. Primary(NULL), Secondary(NULL),
  71. RevDepends(NULL), Remove(NULL),
  72. AfterEnd(NULL), FileList(NULL),
  73. LoopCount(-1), Depth(0)
  74. {
  75. Debug = _config->FindB("Debug::pkgOrderList",false);
  76. /* Construct the arrays, egcs 1.0.1 bug requires the package count
  77. hack */
  78. auto const Size = Cache.Head().PackageCount;
  79. Flags = new unsigned short[Size];
  80. End = List = new Package *[Size];
  81. memset(Flags,0,sizeof(*Flags)*Size);
  82. }
  83. /*}}}*/
  84. // OrderList::~pkgOrderList - Destructor /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* */
  87. pkgOrderList::~pkgOrderList()
  88. {
  89. delete [] List;
  90. delete [] Flags;
  91. }
  92. /*}}}*/
  93. // OrderList::IsMissing - Check if a file is missing /*{{{*/
  94. // ---------------------------------------------------------------------
  95. /* */
  96. bool pkgOrderList::IsMissing(PkgIterator Pkg)
  97. {
  98. // Skip packages to erase
  99. if (Cache[Pkg].Delete() == true)
  100. return false;
  101. // Skip Packages that need configure only.
  102. if ((Pkg.State() == pkgCache::PkgIterator::NeedsConfigure ||
  103. Pkg.State() == pkgCache::PkgIterator::NeedsNothing) &&
  104. Cache[Pkg].Keep() == true)
  105. return false;
  106. if (FileList == 0)
  107. return false;
  108. if (FileList[Pkg->ID].empty() == false)
  109. return false;
  110. return true;
  111. }
  112. /*}}}*/
  113. // OrderList::DoRun - Does an order run /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* The caller is expected to have setup the desired probe state */
  116. bool pkgOrderList::DoRun()
  117. {
  118. // Temp list
  119. unsigned long Size = Cache.Head().PackageCount;
  120. std::unique_ptr<Package *[]> NList(new Package *[Size]);
  121. std::unique_ptr<Package *[]> AfterList(new Package *[Size]);
  122. AfterEnd = AfterList.get();
  123. Depth = 0;
  124. WipeFlags(Added | AddPending | Loop | InList);
  125. for (iterator I = List; I != End; ++I)
  126. Flag(*I,InList);
  127. // Rebuild the main list into the temp list.
  128. iterator OldEnd = End;
  129. End = NList.get();
  130. for (iterator I = List; I != OldEnd; ++I)
  131. if (VisitNode(PkgIterator(Cache,*I), "DoRun") == false)
  132. {
  133. End = OldEnd;
  134. return false;
  135. }
  136. // Copy the after list to the end of the main list
  137. for (Package **I = AfterList.get(); I != AfterEnd; I++)
  138. *End++ = *I;
  139. // Swap the main list to the new list
  140. delete [] List;
  141. List = NList.release();
  142. return true;
  143. }
  144. /*}}}*/
  145. // OrderList::OrderCritical - Perform critical unpacking ordering /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* This performs predepends and immediate configuration ordering only.
  148. This is termed critical unpacking ordering. Any loops that form are
  149. fatal and indicate that the packages cannot be installed. */
  150. bool pkgOrderList::OrderCritical()
  151. {
  152. FileList = 0;
  153. Primary = &pkgOrderList::DepUnPackPreD;
  154. Secondary = 0;
  155. RevDepends = 0;
  156. Remove = 0;
  157. LoopCount = 0;
  158. // Sort
  159. std::sort(List,End, [this](Package *a, Package *b) { return OrderCompareB(a, b) < 0; } );
  160. if (DoRun() == false)
  161. return false;
  162. if (LoopCount != 0)
  163. return _error->Error("Fatal, predepends looping detected");
  164. if (Debug == true)
  165. {
  166. clog << "** Critical Unpack ordering done" << endl;
  167. for (iterator I = List; I != End; ++I)
  168. {
  169. PkgIterator P(Cache,*I);
  170. if (IsNow(P) == true)
  171. clog << " " << P.FullName() << ' ' << IsMissing(P) << ',' << IsFlag(P,After) << endl;
  172. }
  173. }
  174. return true;
  175. }
  176. /*}}}*/
  177. // OrderList::OrderUnpack - Perform complete unpacking ordering /*{{{*/
  178. // ---------------------------------------------------------------------
  179. /* This performs complete unpacking ordering and creates an order that is
  180. suitable for unpacking */
  181. bool pkgOrderList::OrderUnpack(string *FileList)
  182. {
  183. this->FileList = FileList;
  184. // Setup the after flags
  185. if (FileList != 0)
  186. {
  187. WipeFlags(After);
  188. // Set the inlist flag
  189. for (iterator I = List; I != End; ++I)
  190. {
  191. PkgIterator P(Cache,*I);
  192. if (IsMissing(P) == true && IsNow(P) == true)
  193. Flag(*I,After);
  194. }
  195. }
  196. Primary = &pkgOrderList::DepUnPackCrit;
  197. Secondary = &pkgOrderList::DepConfigure;
  198. RevDepends = &pkgOrderList::DepUnPackDep;
  199. Remove = &pkgOrderList::DepRemove;
  200. LoopCount = -1;
  201. // Sort
  202. std::sort(List,End, [this](Package *a, Package *b) { return OrderCompareA(a, b) < 0; });
  203. if (Debug == true)
  204. clog << "** Pass A" << endl;
  205. if (DoRun() == false)
  206. return false;
  207. if (Debug == true)
  208. clog << "** Pass B" << endl;
  209. Secondary = 0;
  210. if (DoRun() == false)
  211. return false;
  212. if (Debug == true)
  213. clog << "** Pass C" << endl;
  214. LoopCount = 0;
  215. RevDepends = 0;
  216. Remove = 0; // Otherwise the libreadline remove problem occurs
  217. if (DoRun() == false)
  218. return false;
  219. if (Debug == true)
  220. clog << "** Pass D" << endl;
  221. LoopCount = 0;
  222. Primary = &pkgOrderList::DepUnPackPre;
  223. if (DoRun() == false)
  224. return false;
  225. if (Debug == true)
  226. {
  227. clog << "** Unpack ordering done" << endl;
  228. for (iterator I = List; I != End; ++I)
  229. {
  230. PkgIterator P(Cache,*I);
  231. if (IsNow(P) == true)
  232. clog << " " << P.FullName() << ' ' << IsMissing(P) << ',' << IsFlag(P,After) << endl;
  233. }
  234. }
  235. return true;
  236. }
  237. /*}}}*/
  238. // OrderList::OrderConfigure - Perform configuration ordering /*{{{*/
  239. // ---------------------------------------------------------------------
  240. /* This orders by depends only and produces an order which is suitable
  241. for configuration */
  242. bool pkgOrderList::OrderConfigure()
  243. {
  244. FileList = 0;
  245. Primary = &pkgOrderList::DepConfigure;
  246. Secondary = 0;
  247. RevDepends = 0;
  248. Remove = 0;
  249. LoopCount = -1;
  250. return DoRun();
  251. }
  252. /*}}}*/
  253. // OrderList::Score - Score the package for sorting /*{{{*/
  254. // ---------------------------------------------------------------------
  255. /* Higher scores order earlier */
  256. int pkgOrderList::Score(PkgIterator Pkg)
  257. {
  258. // Removals should be done after we dealt with essentials
  259. static int const ScoreDelete = _config->FindI("OrderList::Score::Delete", 100);
  260. if (Cache[Pkg].Delete() == true)
  261. return ScoreDelete;
  262. // This should never happen..
  263. if (Cache[Pkg].InstVerIter(Cache).end() == true)
  264. return -1;
  265. static int const ScoreEssential = _config->FindI("OrderList::Score::Essential", 200);
  266. static int const ScoreImmediate = _config->FindI("OrderList::Score::Immediate", 10);
  267. static int const ScorePreDepends = _config->FindI("OrderList::Score::PreDepends", 50);
  268. int Score = 0;
  269. if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  270. Score += ScoreEssential;
  271. if (IsFlag(Pkg,Immediate) == true)
  272. Score += ScoreImmediate;
  273. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  274. D.end() == false; ++D)
  275. if (D->Type == pkgCache::Dep::PreDepends)
  276. {
  277. Score += ScorePreDepends;
  278. break;
  279. }
  280. // Required Important Standard Optional Extra
  281. if (Cache[Pkg].InstVerIter(Cache)->Priority <= 5)
  282. {
  283. signed short PrioMap[] = {0,5,4,3,1,0};
  284. Score += PrioMap[Cache[Pkg].InstVerIter(Cache)->Priority];
  285. }
  286. return Score;
  287. }
  288. /*}}}*/
  289. // OrderList::FileCmp - Compare by package file /*{{{*/
  290. // ---------------------------------------------------------------------
  291. /* This compares by the package file that the install version is in. */
  292. int pkgOrderList::FileCmp(PkgIterator A,PkgIterator B)
  293. {
  294. if (Cache[A].Delete() == true && Cache[B].Delete() == true)
  295. return 0;
  296. if (Cache[A].Delete() == true)
  297. return -1;
  298. if (Cache[B].Delete() == true)
  299. return 1;
  300. if (Cache[A].InstVerIter(Cache).FileList().end() == true)
  301. return -1;
  302. if (Cache[B].InstVerIter(Cache).FileList().end() == true)
  303. return 1;
  304. pkgCache::PackageFile *FA = Cache[A].InstVerIter(Cache).FileList().File();
  305. pkgCache::PackageFile *FB = Cache[B].InstVerIter(Cache).FileList().File();
  306. if (FA < FB)
  307. return -1;
  308. if (FA > FB)
  309. return 1;
  310. return 0;
  311. }
  312. /*}}}*/
  313. // BoolCompare - Comparison function for two booleans /*{{{*/
  314. // ---------------------------------------------------------------------
  315. /* */
  316. static int BoolCompare(bool A,bool B)
  317. {
  318. if (A == B)
  319. return 0;
  320. if (A == false)
  321. return -1;
  322. return 1;
  323. }
  324. /*}}}*/
  325. // OrderList::OrderCompareA - Order the installation by op /*{{{*/
  326. // ---------------------------------------------------------------------
  327. /* This provides a first-pass sort of the list and gives a decent starting
  328. point for further complete ordering. It is used by OrderUnpack only */
  329. int pkgOrderList::OrderCompareA(Package *a, Package *b)
  330. {
  331. PkgIterator A(Cache,a);
  332. PkgIterator B(Cache,b);
  333. // We order packages with a set state toward the front
  334. int Res;
  335. if ((Res = BoolCompare(IsNow(A),IsNow(B))) != 0)
  336. return -1*Res;
  337. // We order missing files to toward the end
  338. /* if (FileList != 0)
  339. {
  340. if ((Res = BoolCompare(IsMissing(A),
  341. IsMissing(B))) != 0)
  342. return Res;
  343. }*/
  344. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  345. B.State() == pkgCache::PkgIterator::NeedsNothing)
  346. return -1;
  347. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  348. B.State() != pkgCache::PkgIterator::NeedsNothing)
  349. return 1;
  350. int ScoreA = Score(A);
  351. int ScoreB = Score(B);
  352. if (ScoreA > ScoreB)
  353. return -1;
  354. if (ScoreA < ScoreB)
  355. return 1;
  356. return strcmp(A.Name(),B.Name());
  357. }
  358. /*}}}*/
  359. // OrderList::OrderCompareB - Order the installation by source /*{{{*/
  360. // ---------------------------------------------------------------------
  361. /* This orders by installation source. This is useful to handle
  362. inter-source breaks */
  363. int pkgOrderList::OrderCompareB(Package *a, Package *b)
  364. {
  365. PkgIterator A(Cache,a);
  366. PkgIterator B(Cache,b);
  367. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  368. B.State() == pkgCache::PkgIterator::NeedsNothing)
  369. return -1;
  370. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  371. B.State() != pkgCache::PkgIterator::NeedsNothing)
  372. return 1;
  373. int F = FileCmp(A,B);
  374. if (F != 0)
  375. {
  376. if (F > 0)
  377. return -1;
  378. return 1;
  379. }
  380. int ScoreA = Score(A);
  381. int ScoreB = Score(B);
  382. if (ScoreA > ScoreB)
  383. return -1;
  384. if (ScoreA < ScoreB)
  385. return 1;
  386. return strcmp(A.Name(),B.Name());
  387. }
  388. /*}}}*/
  389. // OrderList::VisitDeps - Visit forward install dependencies /*{{{*/
  390. // ---------------------------------------------------------------------
  391. /* This calls the dependency function for the normal forwards dependencies
  392. of the package */
  393. bool pkgOrderList::VisitDeps(DepFunc F,PkgIterator Pkg)
  394. {
  395. if (F == 0 || Pkg.end() == true || Cache[Pkg].InstallVer == 0)
  396. return true;
  397. return (this->*F)(Cache[Pkg].InstVerIter(Cache).DependsList());
  398. }
  399. /*}}}*/
  400. // OrderList::VisitRDeps - Visit reverse dependencies /*{{{*/
  401. // ---------------------------------------------------------------------
  402. /* This calls the dependency function for all of the normal reverse depends
  403. of the package */
  404. bool pkgOrderList::VisitRDeps(DepFunc F,PkgIterator Pkg)
  405. {
  406. if (F == 0 || Pkg.end() == true)
  407. return true;
  408. return (this->*F)(Pkg.RevDependsList());
  409. }
  410. /*}}}*/
  411. // OrderList::VisitRProvides - Visit provides reverse dependencies /*{{{*/
  412. // ---------------------------------------------------------------------
  413. /* This calls the dependency function for all reverse dependencies
  414. generated by the provides line on the package. */
  415. bool pkgOrderList::VisitRProvides(DepFunc F,VerIterator Ver)
  416. {
  417. if (F == 0 || Ver.end() == true)
  418. return true;
  419. bool Res = true;
  420. for (PrvIterator P = Ver.ProvidesList(); P.end() == false; ++P)
  421. Res &= (this->*F)(P.ParentPkg().RevDependsList());
  422. return Res;
  423. }
  424. /*}}}*/
  425. // OrderList::VisitProvides - Visit all of the providing packages /*{{{*/
  426. // ---------------------------------------------------------------------
  427. /* This routine calls visit on all providing packages.
  428. If the dependency is negative it first visits packages which are
  429. intended to be removed and after that all other packages.
  430. It does so to avoid situations in which this package is used to
  431. satisfy a (or-group/provides) dependency of another package which
  432. could have been satisfied also by upgrading another package -
  433. otherwise we have more broken packages dpkg needs to auto-
  434. deconfigure and in very complicated situations it even decides
  435. against it! */
  436. bool pkgOrderList::VisitProvides(DepIterator D,bool Critical)
  437. {
  438. std::unique_ptr<Version *[]> List(D.AllTargets());
  439. for (Version **I = List.get(); *I != 0; ++I)
  440. {
  441. VerIterator Ver(Cache,*I);
  442. PkgIterator Pkg = Ver.ParentPkg();
  443. if (D.IsNegative() == true && Cache[Pkg].Delete() == false)
  444. continue;
  445. if (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing)
  446. continue;
  447. if (D.IsNegative() == false &&
  448. Cache[Pkg].InstallVer != *I)
  449. continue;
  450. if (D.IsNegative() == true &&
  451. (Version *)Pkg.CurrentVer() != *I)
  452. continue;
  453. // Skip over missing files
  454. if (Critical == false && IsMissing(D.ParentPkg()) == true)
  455. continue;
  456. if (VisitNode(Pkg, "Provides-1") == false)
  457. return false;
  458. }
  459. if (D.IsNegative() == false)
  460. return true;
  461. for (Version **I = List.get(); *I != 0; ++I)
  462. {
  463. VerIterator Ver(Cache,*I);
  464. PkgIterator Pkg = Ver.ParentPkg();
  465. if (Cache[Pkg].Delete() == true)
  466. continue;
  467. if (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing)
  468. continue;
  469. if ((Version *)Pkg.CurrentVer() != *I)
  470. continue;
  471. // Skip over missing files
  472. if (Critical == false && IsMissing(D.ParentPkg()) == true)
  473. continue;
  474. if (VisitNode(Pkg, "Provides-2") == false)
  475. return false;
  476. }
  477. return true;
  478. }
  479. /*}}}*/
  480. // OrderList::VisitNode - Recursive ordering director /*{{{*/
  481. // ---------------------------------------------------------------------
  482. /* This is the core ordering routine. It calls the set dependency
  483. consideration functions which then potentially call this again. Finite
  484. depth is achieved through the colouring mechinism. */
  485. bool pkgOrderList::VisitNode(PkgIterator Pkg, char const* from)
  486. {
  487. // Looping or irrelevant.
  488. // This should probably trancend not installed packages
  489. if (Pkg.end() == true || IsFlag(Pkg,Added) == true ||
  490. IsFlag(Pkg,AddPending) == true || IsFlag(Pkg,InList) == false)
  491. return true;
  492. if (Debug == true)
  493. {
  494. for (int j = 0; j != Depth; j++) clog << ' ';
  495. clog << "Visit " << Pkg.FullName() << " from " << from << endl;
  496. }
  497. Depth++;
  498. // Color grey
  499. Flag(Pkg,AddPending);
  500. DepFunc Old = Primary;
  501. // Perform immediate configuration of the package if so flagged.
  502. if (IsFlag(Pkg,Immediate) == true && Primary != &pkgOrderList::DepUnPackPre)
  503. Primary = &pkgOrderList::DepUnPackPreD;
  504. if (IsNow(Pkg) == true)
  505. {
  506. bool Res = true;
  507. if (Cache[Pkg].Delete() == false)
  508. {
  509. // Primary
  510. Res &= Res && VisitDeps(Primary,Pkg);
  511. Res &= Res && VisitRDeps(Primary,Pkg);
  512. Res &= Res && VisitRProvides(Primary,Pkg.CurrentVer());
  513. Res &= Res && VisitRProvides(Primary,Cache[Pkg].InstVerIter(Cache));
  514. // RevDep
  515. Res &= Res && VisitRDeps(RevDepends,Pkg);
  516. Res &= Res && VisitRProvides(RevDepends,Pkg.CurrentVer());
  517. Res &= Res && VisitRProvides(RevDepends,Cache[Pkg].InstVerIter(Cache));
  518. // Secondary
  519. Res &= Res && VisitDeps(Secondary,Pkg);
  520. Res &= Res && VisitRDeps(Secondary,Pkg);
  521. Res &= Res && VisitRProvides(Secondary,Pkg.CurrentVer());
  522. Res &= Res && VisitRProvides(Secondary,Cache[Pkg].InstVerIter(Cache));
  523. }
  524. else
  525. {
  526. // RevDep
  527. Res &= Res && VisitRDeps(Remove,Pkg);
  528. Res &= Res && VisitRProvides(Remove,Pkg.CurrentVer());
  529. }
  530. }
  531. if (IsFlag(Pkg,Added) == false)
  532. {
  533. Flag(Pkg,Added,Added | AddPending);
  534. if (IsFlag(Pkg,After) == true)
  535. *AfterEnd++ = Pkg;
  536. else
  537. *End++ = Pkg;
  538. }
  539. Primary = Old;
  540. Depth--;
  541. if (Debug == true)
  542. {
  543. for (int j = 0; j != Depth; j++) clog << ' ';
  544. clog << "Leave " << Pkg.FullName() << ' ' << IsFlag(Pkg,Added) << ',' << IsFlag(Pkg,AddPending) << endl;
  545. }
  546. return true;
  547. }
  548. /*}}}*/
  549. // OrderList::DepUnPackCrit - Critical UnPacking ordering /*{{{*/
  550. // ---------------------------------------------------------------------
  551. /* Critical unpacking ordering strives to satisfy Conflicts: and
  552. PreDepends: only. When a prdepends is encountered the Primary
  553. DepFunc is changed to be DepUnPackPreD.
  554. Loops are preprocessed and logged. */
  555. bool pkgOrderList::DepUnPackCrit(DepIterator D)
  556. {
  557. for (; D.end() == false; ++D)
  558. {
  559. if (D.Reverse() == true)
  560. {
  561. /* Reverse depenanices are only interested in conflicts,
  562. predepend breakage is ignored here */
  563. if (D->Type != pkgCache::Dep::Conflicts &&
  564. D->Type != pkgCache::Dep::Obsoletes)
  565. continue;
  566. // Duplication elimination, consider only the current version
  567. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  568. continue;
  569. /* For reverse dependencies we wish to check if the
  570. dependency is satisfied in the install state. The
  571. target package (caller) is going to be in the installed
  572. state. */
  573. if (CheckDep(D) == true)
  574. continue;
  575. if (VisitNode(D.ParentPkg(), "UnPackCrit") == false)
  576. return false;
  577. }
  578. else
  579. {
  580. /* Forward critical dependencies MUST be correct before the
  581. package can be unpacked. */
  582. if (D.IsNegative() == false &&
  583. D->Type != pkgCache::Dep::PreDepends)
  584. continue;
  585. /* We wish to check if the dep is okay in the now state of the
  586. target package against the install state of this package. */
  587. if (CheckDep(D) == true)
  588. {
  589. /* We want to catch predepends loops with the code below.
  590. Conflicts loops that are Dep OK are ignored */
  591. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  592. D->Type != pkgCache::Dep::PreDepends)
  593. continue;
  594. }
  595. // This is the loop detection
  596. if (IsFlag(D.TargetPkg(),Added) == true ||
  597. IsFlag(D.TargetPkg(),AddPending) == true)
  598. {
  599. if (IsFlag(D.TargetPkg(),AddPending) == true)
  600. AddLoop(D);
  601. continue;
  602. }
  603. /* Predepends require a special ordering stage, they must have
  604. all dependents installed as well */
  605. DepFunc Old = Primary;
  606. bool Res = false;
  607. if (D->Type == pkgCache::Dep::PreDepends)
  608. Primary = &pkgOrderList::DepUnPackPreD;
  609. Res = VisitProvides(D,true);
  610. Primary = Old;
  611. if (Res == false)
  612. return false;
  613. }
  614. }
  615. return true;
  616. }
  617. /*}}}*/
  618. // OrderList::DepUnPackPreD - Critical UnPacking ordering with depends /*{{{*/
  619. // ---------------------------------------------------------------------
  620. /* Critical PreDepends (also configure immediate and essential) strives to
  621. ensure not only that all conflicts+predepends are met but that this
  622. package will be immediately configurable when it is unpacked.
  623. Loops are preprocessed and logged. */
  624. bool pkgOrderList::DepUnPackPreD(DepIterator D)
  625. {
  626. if (D.Reverse() == true)
  627. return DepUnPackCrit(D);
  628. for (; D.end() == false; ++D)
  629. {
  630. if (D.IsCritical() == false)
  631. continue;
  632. /* We wish to check if the dep is okay in the now state of the
  633. target package against the install state of this package. */
  634. if (CheckDep(D) == true)
  635. {
  636. /* We want to catch predepends loops with the code below.
  637. Conflicts loops that are Dep OK are ignored */
  638. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  639. D->Type != pkgCache::Dep::PreDepends)
  640. continue;
  641. }
  642. // This is the loop detection
  643. if (IsFlag(D.TargetPkg(),Added) == true ||
  644. IsFlag(D.TargetPkg(),AddPending) == true)
  645. {
  646. if (IsFlag(D.TargetPkg(),AddPending) == true)
  647. AddLoop(D);
  648. continue;
  649. }
  650. if (VisitProvides(D,true) == false)
  651. return false;
  652. }
  653. return true;
  654. }
  655. /*}}}*/
  656. // OrderList::DepUnPackPre - Critical Predepends ordering /*{{{*/
  657. // ---------------------------------------------------------------------
  658. /* Critical PreDepends (also configure immediate and essential) strives to
  659. ensure not only that all conflicts+predepends are met but that this
  660. package will be immediately configurable when it is unpacked.
  661. Loops are preprocessed and logged. All loops will be fatal. */
  662. bool pkgOrderList::DepUnPackPre(DepIterator D)
  663. {
  664. if (D.Reverse() == true)
  665. return true;
  666. for (; D.end() == false; ++D)
  667. {
  668. /* Only consider the PreDepends or Depends. Depends are only
  669. considered at the lowest depth or in the case of immediate
  670. configure */
  671. if (D->Type != pkgCache::Dep::PreDepends)
  672. {
  673. if (D->Type == pkgCache::Dep::Depends)
  674. {
  675. if (Depth == 1 && IsFlag(D.ParentPkg(),Immediate) == false)
  676. continue;
  677. }
  678. else
  679. continue;
  680. }
  681. /* We wish to check if the dep is okay in the now state of the
  682. target package against the install state of this package. */
  683. if (CheckDep(D) == true)
  684. {
  685. /* We want to catch predepends loops with the code below.
  686. Conflicts loops that are Dep OK are ignored */
  687. if (IsFlag(D.TargetPkg(),AddPending) == false)
  688. continue;
  689. }
  690. // This is the loop detection
  691. if (IsFlag(D.TargetPkg(),Added) == true ||
  692. IsFlag(D.TargetPkg(),AddPending) == true)
  693. {
  694. if (IsFlag(D.TargetPkg(),AddPending) == true)
  695. AddLoop(D);
  696. continue;
  697. }
  698. if (VisitProvides(D,true) == false)
  699. return false;
  700. }
  701. return true;
  702. }
  703. /*}}}*/
  704. // OrderList::DepUnPackDep - Reverse dependency considerations /*{{{*/
  705. // ---------------------------------------------------------------------
  706. /* Reverse dependencies are considered to determine if unpacking this
  707. package will break any existing dependencies. If so then those
  708. packages are ordered before this one so that they are in the
  709. UnPacked state.
  710. The forwards depends loop is designed to bring the packages dependents
  711. close to the package. This helps reduce deconfigure time.
  712. Loops are irrelevant to this. */
  713. bool pkgOrderList::DepUnPackDep(DepIterator D)
  714. {
  715. for (; D.end() == false; ++D)
  716. if (D.IsCritical() == true)
  717. {
  718. if (D.Reverse() == true)
  719. {
  720. /* Duplication prevention. We consider rev deps only on
  721. the current version, a not installed package
  722. cannot break */
  723. if (D.ParentPkg()->CurrentVer == 0 ||
  724. D.ParentPkg().CurrentVer() != D.ParentVer())
  725. continue;
  726. // The dep will not break so it is irrelevant.
  727. if (CheckDep(D) == true)
  728. continue;
  729. // Skip over missing files
  730. if (IsMissing(D.ParentPkg()) == true)
  731. continue;
  732. if (VisitNode(D.ParentPkg(), "UnPackDep-Parent") == false)
  733. return false;
  734. }
  735. else
  736. {
  737. if (D->Type == pkgCache::Dep::Depends)
  738. if (VisitProvides(D,false) == false)
  739. return false;
  740. if (D->Type == pkgCache::Dep::DpkgBreaks)
  741. {
  742. if (CheckDep(D) == true)
  743. continue;
  744. if (VisitNode(D.TargetPkg(), "UnPackDep-Target") == false)
  745. return false;
  746. }
  747. }
  748. }
  749. return true;
  750. }
  751. /*}}}*/
  752. // OrderList::DepConfigure - Configuration ordering /*{{{*/
  753. // ---------------------------------------------------------------------
  754. /* Configuration only ordering orders by the Depends: line only. It
  755. orders configuration so that when a package comes to be configured it's
  756. dependents are configured.
  757. Loops are ignored. Depends loop entry points are chaotic. */
  758. bool pkgOrderList::DepConfigure(DepIterator D)
  759. {
  760. // Never consider reverse configuration dependencies.
  761. if (D.Reverse() == true)
  762. return true;
  763. for (; D.end() == false; ++D)
  764. if (D->Type == pkgCache::Dep::Depends)
  765. if (VisitProvides(D,false) == false)
  766. return false;
  767. return true;
  768. }
  769. /*}}}*/
  770. // OrderList::DepRemove - Removal ordering /*{{{*/
  771. // ---------------------------------------------------------------------
  772. /* Checks all given dependencies if they are broken by the remove of a
  773. package and if so fix it by visiting another provider or or-group
  774. member to ensure that the dependee keeps working which is especially
  775. important for Immediate packages like e.g. those depending on an
  776. awk implementation. If the dependency can't be fixed with another
  777. package this means an upgrade of the package will solve the problem. */
  778. bool pkgOrderList::DepRemove(DepIterator Broken)
  779. {
  780. if (Broken.Reverse() == false)
  781. return true;
  782. for (; Broken.end() == false; ++Broken)
  783. {
  784. if (Broken->Type != pkgCache::Dep::Depends &&
  785. Broken->Type != pkgCache::Dep::PreDepends)
  786. continue;
  787. PkgIterator BrokenPkg = Broken.ParentPkg();
  788. // uninstalled packages can't break via a remove
  789. if (BrokenPkg->CurrentVer == 0)
  790. continue;
  791. // if its already added, we can't do anything useful
  792. if (IsFlag(BrokenPkg, AddPending) == true || IsFlag(BrokenPkg, Added) == true)
  793. continue;
  794. // if the dependee is going to be removed, visit it now
  795. if (Cache[BrokenPkg].Delete() == true)
  796. return VisitNode(BrokenPkg, "Remove-Dependee");
  797. // The package stays around, so find out how this is possible
  798. for (DepIterator D = BrokenPkg.CurrentVer().DependsList(); D.end() == false;)
  799. {
  800. // only important or-groups need fixing
  801. if (D->Type != pkgCache::Dep::Depends &&
  802. D->Type != pkgCache::Dep::PreDepends)
  803. {
  804. ++D;
  805. continue;
  806. }
  807. // Start is the beginning of the or-group, D is the first one after or
  808. DepIterator Start = D;
  809. bool foundBroken = false;
  810. for (bool LastOR = true; D.end() == false && LastOR == true; ++D)
  811. {
  812. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  813. if (D == Broken)
  814. foundBroken = true;
  815. }
  816. // this or-group isn't the broken one: keep searching
  817. if (foundBroken == false)
  818. continue;
  819. // iterate over all members of the or-group searching for a ready replacement
  820. bool readyReplacement = false;
  821. for (DepIterator OrMember = Start; OrMember != D && readyReplacement == false; ++OrMember)
  822. {
  823. Version ** Replacements = OrMember.AllTargets();
  824. for (Version **R = Replacements; *R != 0; ++R)
  825. {
  826. VerIterator Ver(Cache,*R);
  827. // only currently installed packages can be a replacement
  828. PkgIterator RPkg = Ver.ParentPkg();
  829. if (RPkg.CurrentVer() != Ver)
  830. continue;
  831. // packages going to be removed can't be a replacement
  832. if (Cache[RPkg].Delete() == true)
  833. continue;
  834. readyReplacement = true;
  835. break;
  836. }
  837. delete[] Replacements;
  838. }
  839. // something else is ready to take over, do nothing
  840. if (readyReplacement == true)
  841. continue;
  842. // see if we can visit a replacement
  843. bool visitReplacement = false;
  844. for (DepIterator OrMember = Start; OrMember != D && visitReplacement == false; ++OrMember)
  845. {
  846. Version ** Replacements = OrMember.AllTargets();
  847. for (Version **R = Replacements; *R != 0; ++R)
  848. {
  849. VerIterator Ver(Cache,*R);
  850. // consider only versions we plan to install
  851. PkgIterator RPkg = Ver.ParentPkg();
  852. if (Cache[RPkg].Install() == false || Cache[RPkg].InstallVer != Ver)
  853. continue;
  854. // loops are not going to help us, so don't create them
  855. if (IsFlag(RPkg, AddPending) == true)
  856. continue;
  857. if (IsMissing(RPkg) == true)
  858. continue;
  859. visitReplacement = true;
  860. if (IsFlag(BrokenPkg, Immediate) == false)
  861. {
  862. if (VisitNode(RPkg, "Remove-Rep") == true)
  863. break;
  864. }
  865. else
  866. {
  867. Flag(RPkg, Immediate);
  868. if (VisitNode(RPkg, "Remove-ImmRep") == true)
  869. break;
  870. }
  871. visitReplacement = false;
  872. }
  873. delete[] Replacements;
  874. }
  875. if (visitReplacement == true)
  876. continue;
  877. // the broken package in current version can't be fixed, so install new version
  878. if (IsMissing(BrokenPkg) == true)
  879. break;
  880. if (VisitNode(BrokenPkg, "Remove-Upgrade") == false)
  881. return false;
  882. }
  883. }
  884. return true;
  885. }
  886. /*}}}*/
  887. // OrderList::AddLoop - Add a loop to the loop list /*{{{*/
  888. // ---------------------------------------------------------------------
  889. /* We record the loops. This is a relic since loop breaking is done
  890. genericaly as part of the safety routines. */
  891. bool pkgOrderList::AddLoop(DepIterator D)
  892. {
  893. if (LoopCount < 0 || LoopCount >= 20)
  894. return false;
  895. // Skip dups
  896. if (LoopCount != 0)
  897. {
  898. if (Loops[LoopCount - 1].ParentPkg() == D.ParentPkg() ||
  899. Loops[LoopCount - 1].TargetPkg() == D.ParentPkg())
  900. return true;
  901. }
  902. Loops[LoopCount++] = D;
  903. // Mark the packages as being part of a loop.
  904. //Flag(D.TargetPkg(),Loop);
  905. //Flag(D.ParentPkg(),Loop);
  906. /* This is currently disabled because the Loop flag is being used for
  907. loop management in the package manager. Check the orderlist.h file for more info */
  908. return true;
  909. }
  910. /*}}}*/
  911. // OrderList::WipeFlags - Unset the given flags from all packages /*{{{*/
  912. // ---------------------------------------------------------------------
  913. /* */
  914. void pkgOrderList::WipeFlags(unsigned long F)
  915. {
  916. auto Size = Cache.Head().PackageCount;
  917. for (decltype(Size) I = 0; I != Size; ++I)
  918. Flags[I] &= ~F;
  919. }
  920. /*}}}*/
  921. // OrderList::CheckDep - Check a dependency for truth /*{{{*/
  922. // ---------------------------------------------------------------------
  923. /* This performs a complete analysis of the dependency wrt to the
  924. current add list. It returns true if after all events are
  925. performed it is still true. This sort of routine can be approximated
  926. by examining the DepCache, however in convoluted cases of provides
  927. this fails to produce a suitable result. */
  928. bool pkgOrderList::CheckDep(DepIterator D)
  929. {
  930. std::unique_ptr<Version *[]> List(D.AllTargets());
  931. bool Hit = false;
  932. for (Version **I = List.get(); *I != 0; I++)
  933. {
  934. VerIterator Ver(Cache,*I);
  935. PkgIterator Pkg = Ver.ParentPkg();
  936. /* The meaning of Added and AddPending is subtle. AddPending is
  937. an indication that the package is looping. Because of the
  938. way ordering works Added means the package will be unpacked
  939. before this one and AddPending means after. It is therefore
  940. correct to ignore AddPending in all cases, but that exposes
  941. reverse-ordering loops which should be ignored. */
  942. if (IsFlag(Pkg,Added) == true ||
  943. (IsFlag(Pkg,AddPending) == true && D.Reverse() == true))
  944. {
  945. if (Cache[Pkg].InstallVer != *I)
  946. continue;
  947. }
  948. else
  949. if ((Version *)Pkg.CurrentVer() != *I ||
  950. Pkg.State() != PkgIterator::NeedsNothing)
  951. continue;
  952. /* Conflicts requires that all versions are not present, depends
  953. just needs one */
  954. if (D.IsNegative() == false)
  955. {
  956. // ignore provides by older versions of this package
  957. if (((D.Reverse() == false && Pkg == D.ParentPkg()) ||
  958. (D.Reverse() == true && Pkg == D.TargetPkg())) &&
  959. Cache[Pkg].InstallVer != *I)
  960. continue;
  961. /* Try to find something that does not have the after flag set
  962. if at all possible */
  963. if (IsFlag(Pkg,After) == true)
  964. {
  965. Hit = true;
  966. continue;
  967. }
  968. return true;
  969. }
  970. else
  971. {
  972. if (IsFlag(Pkg,After) == true)
  973. Flag(D.ParentPkg(),After);
  974. return false;
  975. }
  976. }
  977. // We found a hit, but it had the after flag set
  978. if (Hit == true && D->Type == pkgCache::Dep::PreDepends)
  979. {
  980. Flag(D.ParentPkg(),After);
  981. return true;
  982. }
  983. /* Conflicts requires that all versions are not present, depends
  984. just needs one */
  985. if (D->Type == pkgCache::Dep::Conflicts ||
  986. D->Type == pkgCache::Dep::Obsoletes)
  987. return true;
  988. return false;
  989. }
  990. /*}}}*/