orderlist.cc 26 KB

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