orderlist.cc 35 KB

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