orderlist.cc 35 KB

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