orderlist.cc 27 KB

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