orderlist.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: orderlist.cc,v 1.2 1998/07/12 23:58:28 jgg Exp $
  4. /* ######################################################################
  5. Order List - Represents and Manipulates an ordered list of packages.
  6. A list of packages can be ordered by a number of conflicting criteria
  7. each given a specific priority. Each package also has a set of flags
  8. indicating some usefull things about it that are derived in the
  9. course of sorting. The pkgPackageManager class uses this class for
  10. all of it's installation ordering needs.
  11. This is a modified version of Manoj's Routine B. It consists of four
  12. independent ordering algorithms that can be applied at for different
  13. points in the ordering. By appling progressivly fewer ordering
  14. operations it is possible to give each consideration it's own
  15. priority and create an order that satisfies the lowest applicable
  16. consideration.
  17. The rules for unpacking ordering are:
  18. 1) Unpacking ignores Depends: on all packages
  19. 2) Unpacking requires Conflicts: on -ALL- packages to be satisfied
  20. 3) Unpacking requires PreDepends: on this package only to be satisfied
  21. 4) Removing requires that no packages depend on the package to be
  22. removed.
  23. And the rule for configuration ordering is:
  24. 1) Configuring requires that the Depends: of the package be satisfied
  25. Conflicts+PreDepends are ignored because unpacking says they are
  26. already correct [exageration, it does check but we need not be
  27. concerned]
  28. And some features that are valuable for unpacking ordering.
  29. f1) Unpacking a new package should advoid breaking dependencies of
  30. configured packages
  31. f2) Removal should not require a force, corrolory of f1
  32. f3) Unpacking should order by depends rather than fall back to random
  33. ordering.
  34. Each of the features can be enabled in the sorting routine at an
  35. arbitary priority to give quite abit of control over the final unpacking
  36. order.
  37. The rules listed above my never be violated and are called Critical.
  38. When a critical rule is violated then a loop condition is recorded
  39. and will have to be delt with in the caller.
  40. ##################################################################### */
  41. /*}}}*/
  42. // Include Files /*{{{*/
  43. #ifdef __GNUG__
  44. #pragma implementation "apt-pkg/orderlist.h"
  45. #endif
  46. #include <apt-pkg/orderlist.h>
  47. #include <apt-pkg/depcache.h>
  48. #include <apt-pkg/error.h>
  49. #include <apt-pkg/version.h>
  50. /*}}}*/
  51. pkgOrderList *pkgOrderList::Me = 0;
  52. // OrderList::pkgOrderList - Constructor /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* */
  55. pkgOrderList::pkgOrderList(pkgDepCache &Cache) : Cache(Cache)
  56. {
  57. Primary = 0;
  58. Secondary = 0;
  59. RevDepends = 0;
  60. Remove = 0;
  61. LoopCount = -1;
  62. /* Construct the arrays, egcs 1.0.1 bug requires the package count
  63. hack */
  64. unsigned long Size = Cache.HeaderP->PackageCount;
  65. Flags = new unsigned char[Size];
  66. End = List = new Package *[Size];
  67. memset(Flags,0,sizeof(*Flags)*Size);
  68. }
  69. /*}}}*/
  70. // OrderList::~pkgOrderList - Destructor /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* */
  73. pkgOrderList::~pkgOrderList()
  74. {
  75. delete [] List;
  76. delete [] Flags;
  77. }
  78. /*}}}*/
  79. // OrderList::DoRun - Does an order run /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* The caller is expeted to have setup the desired probe state */
  82. bool pkgOrderList::DoRun()
  83. {
  84. // Temp list
  85. unsigned long Size = Cache.HeaderP->PackageCount;
  86. Package **NList = new Package *[Size];
  87. Depth = 0;
  88. WipeFlags(Added | AddPending | Loop | InList);
  89. for (iterator I = List; I != End; I++)
  90. Flag(*I,InList);
  91. // Rebuild the main list into the temp list.
  92. iterator OldEnd = End;
  93. End = NList;
  94. for (iterator I = List; I != OldEnd; I++)
  95. if (VisitNode(PkgIterator(Cache,*I)) == false)
  96. {
  97. End = OldEnd;
  98. delete [] NList;
  99. return false;
  100. }
  101. // Swap the main list to the new list
  102. delete [] List;
  103. List = NList;
  104. return true;
  105. }
  106. /*}}}*/
  107. // OrderList::OrderCritical - Perform critical unpacking ordering /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* This performs predepends and immediate configuration ordering only.
  110. This is termed critical unpacking ordering. Any loops that form are
  111. fatal and indicate that the packages cannot be installed. */
  112. bool pkgOrderList::OrderCritical()
  113. {
  114. Primary = &DepUnPackPre;
  115. Secondary = 0;
  116. RevDepends = 0;
  117. Remove = 0;
  118. LoopCount = 0;
  119. // Sort
  120. Me = this;
  121. qsort(List,End - List,sizeof(*List),&OrderCompareB);
  122. if (DoRun() == false)
  123. return false;
  124. if (LoopCount != 0)
  125. return _error->Error("Fatal, predepends looping detected");
  126. return true;
  127. }
  128. /*}}}*/
  129. // OrderList::OrderUnpack - Perform complete unpacking ordering /*{{{*/
  130. // ---------------------------------------------------------------------
  131. /* This performs complete unpacking ordering and creates an order that is
  132. suitable for unpacking */
  133. bool pkgOrderList::OrderUnpack()
  134. {
  135. Primary = &DepUnPackCrit;
  136. Secondary = &DepConfigure;
  137. RevDepends = &DepUnPackDep;
  138. Remove = &DepRemove;
  139. LoopCount = -1;
  140. // Sort
  141. Me = this;
  142. qsort(List,End - List,sizeof(*List),&OrderCompareA);
  143. if (DoRun() == false)
  144. return false;
  145. Secondary = 0;
  146. if (DoRun() == false)
  147. return false;
  148. LoopCount = 0;
  149. RevDepends = 0;
  150. Remove = 0; // Otherwise the libreadline remove problem occures
  151. if (DoRun() == false)
  152. return false;
  153. LoopCount = 0;
  154. Primary = &DepUnPackPre;
  155. if (DoRun() == false)
  156. return false;
  157. /* cout << "----------END" << endl;
  158. for (iterator I = List; I != End; I++)
  159. {
  160. PkgIterator P(Cache,*I);
  161. cout << P.Name() << endl;
  162. }*/
  163. return true;
  164. }
  165. /*}}}*/
  166. // OrderList::OrderConfigure - Perform configuration ordering /*{{{*/
  167. // ---------------------------------------------------------------------
  168. /* This orders by depends only and produces an order which is suitable
  169. for configuration */
  170. bool pkgOrderList::OrderConfigure()
  171. {
  172. Primary = &DepConfigure;
  173. Secondary = 0;
  174. RevDepends = 0;
  175. Remove = 0;
  176. LoopCount = -1;
  177. return DoRun();
  178. }
  179. /*}}}*/
  180. // OrderList::Score - Score the package for sorting /*{{{*/
  181. // ---------------------------------------------------------------------
  182. /* Higher scores order earlier */
  183. int pkgOrderList::Score(PkgIterator Pkg)
  184. {
  185. // Removal is always done first
  186. if (Cache[Pkg].Delete() == true)
  187. return 200;
  188. int Score = 0;
  189. if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  190. Score += 100;
  191. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  192. D.end() == false; D++)
  193. if (D->Type == pkgCache::Dep::PreDepends)
  194. {
  195. Score += 50;
  196. break;
  197. }
  198. // Important Required Standard Optional Extra
  199. signed short PrioMap[] = {0,5,4,3,1,0};
  200. if (Cache[Pkg].InstVerIter(Cache)->Priority <= 5)
  201. Score += PrioMap[Cache[Pkg].InstVerIter(Cache)->Priority];
  202. return Score;
  203. }
  204. /*}}}*/
  205. // OrderList::FileCmp - Compare by package file /*{{{*/
  206. // ---------------------------------------------------------------------
  207. /* This compares by the package file that the install version is in. */
  208. int pkgOrderList::FileCmp(PkgIterator A,PkgIterator B)
  209. {
  210. if (Cache[A].Delete() == true && Cache[B].Delete() == true)
  211. return 0;
  212. if (Cache[A].Delete() == true)
  213. return -1;
  214. if (Cache[B].Delete() == true)
  215. return 1;
  216. if (Cache[A].InstVerIter(Cache).FileList().end() == true)
  217. return -1;
  218. if (Cache[A].InstVerIter(Cache).FileList().end() == true)
  219. return 1;
  220. pkgCache::PackageFile *FA = Cache[A].InstVerIter(Cache).FileList().File();
  221. pkgCache::PackageFile *FB = Cache[B].InstVerIter(Cache).FileList().File();
  222. if (FA < FB)
  223. return -1;
  224. if (FA > FB)
  225. return 1;
  226. return 0;
  227. }
  228. /*}}}*/
  229. // OrderList::OrderCompareA - Order the installation by op /*{{{*/
  230. // ---------------------------------------------------------------------
  231. /* This provides a first-pass sort of the list and gives a decent starting
  232. point for further complete ordering. It is used by OrderUnpack only */
  233. int pkgOrderList::OrderCompareA(const void *a, const void *b)
  234. {
  235. PkgIterator A(Me->Cache,*(Package **)a);
  236. PkgIterator B(Me->Cache,*(Package **)b);
  237. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  238. B.State() == pkgCache::PkgIterator::NeedsNothing)
  239. return -1;
  240. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  241. B.State() != pkgCache::PkgIterator::NeedsNothing)
  242. return 1;
  243. int ScoreA = Me->Score(A);
  244. int ScoreB = Me->Score(B);
  245. if (ScoreA > ScoreB)
  246. return -1;
  247. if (ScoreA < ScoreB)
  248. return 1;
  249. return strcmp(A.Name(),B.Name());
  250. }
  251. /*}}}*/
  252. // OrderList::OrderCompareB - Order the installation by source /*{{{*/
  253. // ---------------------------------------------------------------------
  254. /* This orders by installation source. This is usefull to handle
  255. inter-source breaks */
  256. int pkgOrderList::OrderCompareB(const void *a, const void *b)
  257. {
  258. PkgIterator A(Me->Cache,*(Package **)a);
  259. PkgIterator B(Me->Cache,*(Package **)b);
  260. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  261. B.State() == pkgCache::PkgIterator::NeedsNothing)
  262. return -1;
  263. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  264. B.State() != pkgCache::PkgIterator::NeedsNothing)
  265. return 1;
  266. int F = Me->FileCmp(A,B);
  267. if (F != 0)
  268. {
  269. if (F > 0)
  270. return -1;
  271. return 1;
  272. }
  273. int ScoreA = Me->Score(A);
  274. int ScoreB = Me->Score(B);
  275. if (ScoreA > ScoreB)
  276. return -1;
  277. if (ScoreA < ScoreB)
  278. return 1;
  279. return strcmp(A.Name(),B.Name());
  280. }
  281. /*}}}*/
  282. // OrderList::VisitDeps - Visit forward install dependencies /*{{{*/
  283. // ---------------------------------------------------------------------
  284. /* This calls the dependency function for the normal forwards dependencies
  285. of the package */
  286. bool pkgOrderList::VisitDeps(DepFunc F,PkgIterator Pkg)
  287. {
  288. if (F == 0 || Pkg.end() == true || Cache[Pkg].InstallVer == 0)
  289. return true;
  290. return (this->*F)(Cache[Pkg].InstVerIter(Cache).DependsList());
  291. }
  292. /*}}}*/
  293. // OrderList::VisitRDeps - Visit reverse dependencies /*{{{*/
  294. // ---------------------------------------------------------------------
  295. /* This calls the dependency function for all of the normal reverse depends
  296. of the package */
  297. bool pkgOrderList::VisitRDeps(DepFunc F,PkgIterator Pkg)
  298. {
  299. if (F == 0 || Pkg.end() == true)
  300. return true;
  301. return (this->*F)(Pkg.RevDependsList());
  302. }
  303. /*}}}*/
  304. // OrderList::VisitRProvides - Visit provides reverse dependencies /*{{{*/
  305. // ---------------------------------------------------------------------
  306. /* This calls the dependency function for all reverse dependencies
  307. generated by the provides line on the package. */
  308. bool pkgOrderList::VisitRProvides(DepFunc F,VerIterator Ver)
  309. {
  310. if (F == 0 || Ver.end() == true)
  311. return true;
  312. bool Res = true;
  313. for (PrvIterator P = Ver.ProvidesList(); P.end() == false; P++)
  314. Res &= (this->*F)(P.ParentPkg().RevDependsList());
  315. return true;
  316. }
  317. /*}}}*/
  318. // OrderList::VisitProvides - Visit all of the providing packages /*{{{*/
  319. // ---------------------------------------------------------------------
  320. /* This routine calls visit on all providing packages. */
  321. bool pkgOrderList::VisitProvides(DepIterator D)
  322. {
  323. Version **List = D.AllTargets();
  324. for (Version **I = List; *I != 0; I++)
  325. {
  326. VerIterator Ver(Cache,*I);
  327. PkgIterator Pkg = Ver.ParentPkg();
  328. if (Cache[Pkg].Keep() == true)
  329. continue;
  330. if (D->Type != pkgCache::Dep::Conflicts && Cache[Pkg].InstallVer != *I)
  331. continue;
  332. if (D->Type == pkgCache::Dep::Conflicts && (Version *)Pkg.CurrentVer() != *I)
  333. continue;
  334. if (VisitNode(Pkg) == false)
  335. {
  336. delete [] List;
  337. return false;
  338. }
  339. }
  340. delete [] List;
  341. return true;
  342. }
  343. /*}}}*/
  344. // OrderList::VisitNode - Recursive ordering director /*{{{*/
  345. // ---------------------------------------------------------------------
  346. /* This is the core ordering routine. It calls the set dependency
  347. consideration functions which then potentialy call this again. Finite
  348. depth is achived through the colouring mechinism. */
  349. bool pkgOrderList::VisitNode(PkgIterator Pkg)
  350. {
  351. // Looping or irrelevent.
  352. if (Pkg.end() == true || IsFlag(Pkg,Added) == true ||
  353. IsFlag(Pkg,AddPending) == true || IsFlag(Pkg,InList) == false)
  354. return true;
  355. /* for (int j = 0; j != Depth; j++) cout << ' ';
  356. cout << "Visit " << Pkg.Name() << endl;*/
  357. Depth++;
  358. // Color grey
  359. Flag(Pkg,AddPending);
  360. DepFunc Old = Primary;
  361. // Perform immedate configuration of the package if so flagged.
  362. if (IsFlag(Pkg,Immediate) == true && Primary != &DepUnPackPre)
  363. Primary = &DepUnPackPreD;
  364. bool Res = true;
  365. if (Cache[Pkg].Delete() == false)
  366. {
  367. // Primary
  368. Res &= Res && VisitDeps(Primary,Pkg);
  369. Res &= Res && VisitRDeps(Primary,Pkg);
  370. Res &= Res && VisitRProvides(Primary,Pkg.CurrentVer());
  371. Res &= Res && VisitRProvides(Primary,Cache[Pkg].InstVerIter(Cache));
  372. // RevDep
  373. Res &= Res && VisitRDeps(RevDepends,Pkg);
  374. Res &= Res && VisitRProvides(RevDepends,Pkg.CurrentVer());
  375. Res &= Res && VisitRProvides(RevDepends,Cache[Pkg].InstVerIter(Cache));
  376. // Secondary
  377. Res &= Res && VisitDeps(Secondary,Pkg);
  378. Res &= Res && VisitRDeps(Secondary,Pkg);
  379. Res &= Res && VisitRProvides(Secondary,Pkg.CurrentVer());
  380. Res &= Res && VisitRProvides(Secondary,Cache[Pkg].InstVerIter(Cache));
  381. }
  382. else
  383. {
  384. // RevDep
  385. Res &= Res && VisitRDeps(Remove,Pkg);
  386. Res &= Res && VisitRProvides(Remove,Pkg.CurrentVer());
  387. }
  388. if (IsFlag(Pkg,Added) == false)
  389. {
  390. Flag(Pkg,Added,Added | AddPending);
  391. *End = Pkg;
  392. End++;
  393. }
  394. Primary = Old;
  395. Depth--;
  396. /* for (int j = 0; j != Depth; j++) cout << ' ';
  397. cout << "Leave " << Pkg.Name() << ' ' << IsFlag(Pkg,Added) << ',' << IsFlag(Pkg,AddPending) << endl;*/
  398. return true;
  399. }
  400. /*}}}*/
  401. // OrderList::DepUnPackCrit - Critical UnPacking ordering /*{{{*/
  402. // ---------------------------------------------------------------------
  403. /* Critical unpacking ordering strives to satisfy Conflicts: and
  404. PreDepends: only. When a prdepends is encountered the Primary
  405. DepFunc is changed to be DepUnPackPreD.
  406. Loops are preprocessed and logged. */
  407. bool pkgOrderList::DepUnPackCrit(DepIterator D)
  408. {
  409. for (; D.end() == false; D++)
  410. {
  411. if (D.Reverse() == true)
  412. {
  413. /* Reverse depenanices are only interested in conflicts,
  414. predepend breakage is ignored here */
  415. if (D->Type != pkgCache::Dep::Conflicts)
  416. continue;
  417. // Duplication elimination, consider only the current version
  418. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  419. continue;
  420. /* For reverse dependencies we wish to check if the
  421. dependency is satisifed in the install state. The
  422. target package (caller) is going to be in the installed
  423. state. */
  424. if (CheckDep(D) == true)
  425. continue;
  426. if (VisitNode(D.ParentPkg()) == false)
  427. return false;
  428. }
  429. else
  430. {
  431. /* Forward critical dependencies MUST be correct before the
  432. package can be unpacked. */
  433. if (D->Type != pkgCache::Dep::Conflicts && D->Type != pkgCache::Dep::PreDepends)
  434. continue;
  435. /* We wish to check if the dep is okay in the now state of the
  436. target package against the install state of this package. */
  437. if (CheckDep(D) == true)
  438. {
  439. /* We want to catch predepends loops with the code below.
  440. Conflicts loops that are Dep OK are ignored */
  441. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  442. D->Type != pkgCache::Dep::PreDepends)
  443. continue;
  444. }
  445. // This is the loop detection
  446. if (IsFlag(D.TargetPkg(),Added) == true ||
  447. IsFlag(D.TargetPkg(),AddPending) == true)
  448. {
  449. if (IsFlag(D.TargetPkg(),AddPending) == true)
  450. AddLoop(D);
  451. continue;
  452. }
  453. /* Predepends require a special ordering stage, they must have
  454. all dependents installed as well */
  455. DepFunc Old = Primary;
  456. bool Res = false;
  457. if (D->Type == pkgCache::Dep::PreDepends)
  458. Primary = &DepUnPackPreD;
  459. Res = VisitProvides(D);
  460. Primary = Old;
  461. if (Res == false)
  462. return false;
  463. }
  464. }
  465. return true;
  466. }
  467. /*}}}*/
  468. // OrderList::DepUnPackPreD - Critical UnPacking ordering with depends /*{{{*/
  469. // ---------------------------------------------------------------------
  470. /* Critical PreDepends (also configure immediate and essential) strives to
  471. ensure not only that all conflicts+predepends are met but that this
  472. package will be immediately configurable when it is unpacked.
  473. Loops are preprocessed and logged. */
  474. bool pkgOrderList::DepUnPackPreD(DepIterator D)
  475. {
  476. if (D.Reverse() == true)
  477. return DepUnPackCrit(D);
  478. for (; D.end() == false; D++)
  479. {
  480. if (D.IsCritical() == false)
  481. continue;
  482. /* We wish to check if the dep is okay in the now state of the
  483. target package against the install state of this package. */
  484. if (CheckDep(D) == true)
  485. {
  486. /* We want to catch predepends loops with the code below.
  487. Conflicts loops that are Dep OK are ignored */
  488. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  489. D->Type != pkgCache::Dep::PreDepends)
  490. continue;
  491. }
  492. // This is the loop detection
  493. if (IsFlag(D.TargetPkg(),Added) == true ||
  494. IsFlag(D.TargetPkg(),AddPending) == true)
  495. {
  496. if (IsFlag(D.TargetPkg(),AddPending) == true)
  497. AddLoop(D);
  498. continue;
  499. }
  500. if (VisitProvides(D) == false)
  501. return false;
  502. }
  503. return true;
  504. }
  505. /*}}}*/
  506. // OrderList::DepUnPackPre - Critical Predepends ordering /*{{{*/
  507. // ---------------------------------------------------------------------
  508. /* Critical PreDepends (also configure immediate and essential) strives to
  509. ensure not only that all conflicts+predepends are met but that this
  510. package will be immediately configurable when it is unpacked.
  511. Loops are preprocessed and logged. All loops will be fatal. */
  512. bool pkgOrderList::DepUnPackPre(DepIterator D)
  513. {
  514. if (D.Reverse() == true)
  515. return true;
  516. for (; D.end() == false; D++)
  517. {
  518. /* Only consider the PreDepends or Depends. Depends are only
  519. considered at the lowest depth or in the case of immediate
  520. configure */
  521. if (D->Type != pkgCache::Dep::PreDepends)
  522. {
  523. if (D->Type == pkgCache::Dep::Depends)
  524. {
  525. if (Depth == 1 && IsFlag(D.ParentPkg(),Immediate) == false)
  526. continue;
  527. }
  528. else
  529. continue;
  530. }
  531. /* We wish to check if the dep is okay in the now state of the
  532. target package against the install state of this package. */
  533. if (CheckDep(D) == true)
  534. {
  535. /* We want to catch predepends loops with the code below.
  536. Conflicts loops that are Dep OK are ignored */
  537. if (IsFlag(D.TargetPkg(),AddPending) == false)
  538. continue;
  539. }
  540. // This is the loop detection
  541. if (IsFlag(D.TargetPkg(),Added) == true ||
  542. IsFlag(D.TargetPkg(),AddPending) == true)
  543. {
  544. if (IsFlag(D.TargetPkg(),AddPending) == true)
  545. AddLoop(D);
  546. continue;
  547. }
  548. if (VisitProvides(D) == false)
  549. return false;
  550. }
  551. return true;
  552. }
  553. /*}}}*/
  554. // OrderList::DepUnPackDep - Reverse dependency considerations /*{{{*/
  555. // ---------------------------------------------------------------------
  556. /* Reverse dependencies are considered to determine if unpacking this
  557. package will break any existing dependencies. If so then those
  558. packages are ordered before this one so that they are in the
  559. UnPacked state.
  560. The forwards depends loop is designed to bring the packages dependents
  561. close to the package. This helps reduce deconfigure time.
  562. Loops are irrelevent to this. */
  563. bool pkgOrderList::DepUnPackDep(DepIterator D)
  564. {
  565. for (; D.end() == false; D++)
  566. if (D.IsCritical() == true)
  567. {
  568. if (D.Reverse() == true)
  569. {
  570. /* Duplication prevention. We consider rev deps only on
  571. the current version, a not installed package
  572. cannot break */
  573. if (D.ParentPkg()->CurrentVer == 0 ||
  574. D.ParentPkg().CurrentVer() != D.ParentVer())
  575. continue;
  576. // The dep will not break so it is irrelevent.
  577. if (CheckDep(D) == true)
  578. continue;
  579. if (VisitNode(D.ParentPkg()) == false)
  580. return false;
  581. }
  582. else
  583. if (D->Type == pkgCache::Dep::Depends)
  584. if (VisitProvides(D) == false)
  585. return false;
  586. }
  587. return true;
  588. }
  589. /*}}}*/
  590. // OrderList::DepConfigure - Configuration ordering /*{{{*/
  591. // ---------------------------------------------------------------------
  592. /* Configuration only ordering orders by the Depends: line only. It
  593. orders configuration so that when a package comes to be configured it's
  594. dependents are configured.
  595. Loops are ingored. Depends loop entry points are chaotic. */
  596. bool pkgOrderList::DepConfigure(DepIterator D)
  597. {
  598. // Never consider reverse configuration dependencies.
  599. if (D.Reverse() == true)
  600. return true;
  601. for (; D.end() == false; D++)
  602. if (D->Type == pkgCache::Dep::Depends)
  603. if (VisitProvides(D) == false)
  604. return false;
  605. return true;
  606. }
  607. /*}}}*/
  608. // OrderList::DepRemove - Removal ordering /*{{{*/
  609. // ---------------------------------------------------------------------
  610. /* Removal visits all reverse depends. It considers if the dependency
  611. of the Now state version to see if it is okay with removing this
  612. package. This check should always fail, but is provided for symetery
  613. with the other critical handlers.
  614. Loops are preprocessed and logged. Removal loops can also be
  615. detected in the critical handler. They are characterized by an
  616. old version of A depending on B but the new version of A conflicting
  617. with B, thus either A or B must break to install. */
  618. bool pkgOrderList::DepRemove(DepIterator D)
  619. {
  620. if (D.Reverse() == false)
  621. return true;
  622. for (; D.end() == false; D++)
  623. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  624. {
  625. // Duplication elimination, consider the current version only
  626. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  627. continue;
  628. /* We wish to see if the dep on the parent package is okay
  629. in the removed (install) state of the target pkg. */
  630. if (CheckDep(D) == true)
  631. {
  632. // We want to catch loops with the code below.
  633. if (IsFlag(D.ParentPkg(),AddPending) == false)
  634. continue;
  635. }
  636. // This is the loop detection
  637. if (IsFlag(D.ParentPkg(),Added) == true ||
  638. IsFlag(D.ParentPkg(),AddPending) == true)
  639. {
  640. if (IsFlag(D.ParentPkg(),AddPending) == true)
  641. AddLoop(D);
  642. continue;
  643. }
  644. if (VisitNode(D.ParentPkg()) == false)
  645. return false;
  646. }
  647. return true;
  648. }
  649. /*}}}*/
  650. // OrderList::AddLoop - Add a loop to the loop list /*{{{*/
  651. // ---------------------------------------------------------------------
  652. /* We record the loops. This is a relic since loop breaking is done
  653. genericaly as part of the safety routines. */
  654. bool pkgOrderList::AddLoop(DepIterator D)
  655. {
  656. if (LoopCount < 0 || LoopCount >= 20)
  657. return false;
  658. // Skip dups
  659. if (LoopCount != 0)
  660. {
  661. if (Loops[LoopCount - 1].ParentPkg() == D.ParentPkg() ||
  662. Loops[LoopCount - 1].TargetPkg() == D.ParentPkg())
  663. return true;
  664. }
  665. Loops[LoopCount++] = D;
  666. // Mark the packages as being part of a loop.
  667. Flag(D.TargetPkg(),Loop);
  668. Flag(D.ParentPkg(),Loop);
  669. return true;
  670. }
  671. /*}}}*/
  672. // OrderList::WipeFlags - Unset the given flags from all packages /*{{{*/
  673. // ---------------------------------------------------------------------
  674. /* */
  675. void pkgOrderList::WipeFlags(unsigned long F)
  676. {
  677. unsigned long Size = Cache.HeaderP->PackageCount;
  678. for (unsigned long I = 0; I != Size; I++)
  679. Flags[I] &= ~F;
  680. }
  681. /*}}}*/
  682. // OrderList::CheckDep - Check a dependency for truth /*{{{*/
  683. // ---------------------------------------------------------------------
  684. /* This performs a complete analysis of the dependency wrt to the
  685. current add list. It returns true if after all events are
  686. performed it is still true. This sort of routine can be approximated
  687. by examining the DepCache, however in convoluted cases of provides
  688. this fails to produce a suitable result. */
  689. bool pkgOrderList::CheckDep(DepIterator D)
  690. {
  691. Version **List = D.AllTargets();
  692. for (Version **I = List; *I != 0; I++)
  693. {
  694. VerIterator Ver(Cache,*I);
  695. PkgIterator Pkg = Ver.ParentPkg();
  696. /* The meaning of Added and AddPending is subtle. AddPending is
  697. an indication that the package is looping. Because of the
  698. way ordering works Added means the package will be unpacked
  699. before this one and AddPending means after. It is therefore
  700. correct to ignore AddPending in all cases, but that exposes
  701. reverse-ordering loops which should be ignore. */
  702. if (IsFlag(Pkg,Added) == true ||
  703. (IsFlag(Pkg,AddPending) == true && D.Reverse() == true))
  704. {
  705. if (Cache[Pkg].InstallVer != *I)
  706. continue;
  707. }
  708. else
  709. if ((Version *)Pkg.CurrentVer() != *I ||
  710. Pkg.State() != PkgIterator::NeedsNothing)
  711. continue;
  712. delete [] List;
  713. /* Conflicts requires that all versions are not present, depends
  714. just needs one */
  715. if (D->Type != pkgCache::Dep::Conflicts)
  716. return true;
  717. else
  718. return false;
  719. }
  720. delete [] List;
  721. /* Conflicts requires that all versions are not present, depends
  722. just needs one */
  723. if (D->Type == pkgCache::Dep::Conflicts)
  724. return true;
  725. return false;
  726. }
  727. /*}}}*/