orderlist.cc 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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/version.h>
  60. #include <apt-pkg/sptr.h>
  61. #include <apt-pkg/configuration.h>
  62. #include <iostream>
  63. /*}}}*/
  64. using namespace std;
  65. pkgOrderList *pkgOrderList::Me = 0;
  66. // OrderList::pkgOrderList - Constructor /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* */
  69. pkgOrderList::pkgOrderList(pkgDepCache *pCache) : Cache(*pCache)
  70. {
  71. FileList = 0;
  72. Primary = 0;
  73. Secondary = 0;
  74. RevDepends = 0;
  75. Remove = 0;
  76. LoopCount = -1;
  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. SPtrArray<Package *> NList = new Package *[Size];
  123. SPtrArray<Package *> AfterList = new Package *[Size];
  124. AfterEnd = AfterList;
  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;
  132. for (iterator I = List; I != OldEnd; I++)
  133. if (VisitNode(PkgIterator(Cache,*I)) == 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; I != AfterEnd; I++)
  140. *End++ = *I;
  141. // Swap the main list to the new list
  142. delete [] List;
  143. List = NList.UnGuard();
  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. static int const ScoreDelete = _config->FindI("OrderList::Score::Delete", 500);
  263. // Removal is always done first
  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. signed short PrioMap[] = {0,5,4,3,1,0};
  286. if (Cache[Pkg].InstVerIter(Cache)->Priority <= 5)
  287. Score += PrioMap[Cache[Pkg].InstVerIter(Cache)->Priority];
  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(const void *a, const void *b)
  332. {
  333. PkgIterator A(Me->Cache,*(Package **)a);
  334. PkgIterator B(Me->Cache,*(Package **)b);
  335. // We order packages with a set state toward the front
  336. int Res;
  337. if ((Res = BoolCompare(Me->IsNow(A),Me->IsNow(B))) != 0)
  338. return -1*Res;
  339. // We order missing files to toward the end
  340. /* if (Me->FileList != 0)
  341. {
  342. if ((Res = BoolCompare(Me->IsMissing(A),
  343. Me->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 = Me->Score(A);
  353. int ScoreB = Me->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(const void *a, const void *b)
  366. {
  367. PkgIterator A(Me->Cache,*(Package **)a);
  368. PkgIterator B(Me->Cache,*(Package **)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 = Me->FileCmp(A,B);
  376. if (F != 0)
  377. {
  378. if (F > 0)
  379. return -1;
  380. return 1;
  381. }
  382. int ScoreA = Me->Score(A);
  383. int ScoreB = Me->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. bool pkgOrderList::VisitProvides(DepIterator D,bool Critical)
  431. {
  432. SPtrArray<Version *> List = D.AllTargets();
  433. for (Version **I = List; *I != 0; I++)
  434. {
  435. VerIterator Ver(Cache,*I);
  436. PkgIterator Pkg = Ver.ParentPkg();
  437. if (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing)
  438. continue;
  439. if (D.IsNegative() == false &&
  440. Cache[Pkg].InstallVer != *I)
  441. continue;
  442. if (D.IsNegative() == true &&
  443. (Version *)Pkg.CurrentVer() != *I)
  444. continue;
  445. // Skip over missing files
  446. if (Critical == false && IsMissing(D.ParentPkg()) == true)
  447. continue;
  448. if (VisitNode(Pkg) == false)
  449. return false;
  450. }
  451. return true;
  452. }
  453. /*}}}*/
  454. // OrderList::VisitNode - Recursive ordering director /*{{{*/
  455. // ---------------------------------------------------------------------
  456. /* This is the core ordering routine. It calls the set dependency
  457. consideration functions which then potentialy call this again. Finite
  458. depth is achived through the colouring mechinism. */
  459. bool pkgOrderList::VisitNode(PkgIterator Pkg)
  460. {
  461. // Looping or irrelevent.
  462. // This should probably trancend not installed packages
  463. if (Pkg.end() == true || IsFlag(Pkg,Added) == true ||
  464. IsFlag(Pkg,AddPending) == true || IsFlag(Pkg,InList) == false)
  465. return true;
  466. if (Debug == true)
  467. {
  468. for (int j = 0; j != Depth; j++) clog << ' ';
  469. clog << "Visit " << Pkg.FullName() << endl;
  470. }
  471. Depth++;
  472. // Color grey
  473. Flag(Pkg,AddPending);
  474. DepFunc Old = Primary;
  475. // Perform immedate configuration of the package if so flagged.
  476. if (IsFlag(Pkg,Immediate) == true && Primary != &pkgOrderList::DepUnPackPre)
  477. Primary = &pkgOrderList::DepUnPackPreD;
  478. if (IsNow(Pkg) == true)
  479. {
  480. bool Res = true;
  481. if (Cache[Pkg].Delete() == false)
  482. {
  483. // Primary
  484. Res &= Res && VisitDeps(Primary,Pkg);
  485. Res &= Res && VisitRDeps(Primary,Pkg);
  486. Res &= Res && VisitRProvides(Primary,Pkg.CurrentVer());
  487. Res &= Res && VisitRProvides(Primary,Cache[Pkg].InstVerIter(Cache));
  488. // RevDep
  489. Res &= Res && VisitRDeps(RevDepends,Pkg);
  490. Res &= Res && VisitRProvides(RevDepends,Pkg.CurrentVer());
  491. Res &= Res && VisitRProvides(RevDepends,Cache[Pkg].InstVerIter(Cache));
  492. // Secondary
  493. Res &= Res && VisitDeps(Secondary,Pkg);
  494. Res &= Res && VisitRDeps(Secondary,Pkg);
  495. Res &= Res && VisitRProvides(Secondary,Pkg.CurrentVer());
  496. Res &= Res && VisitRProvides(Secondary,Cache[Pkg].InstVerIter(Cache));
  497. }
  498. else
  499. {
  500. // RevDep
  501. Res &= Res && VisitRDeps(Remove,Pkg);
  502. Res &= Res && VisitRProvides(Remove,Pkg.CurrentVer());
  503. }
  504. }
  505. if (IsFlag(Pkg,Added) == false)
  506. {
  507. Flag(Pkg,Added,Added | AddPending);
  508. if (IsFlag(Pkg,After) == true)
  509. *AfterEnd++ = Pkg;
  510. else
  511. *End++ = Pkg;
  512. }
  513. Primary = Old;
  514. Depth--;
  515. if (Debug == true)
  516. {
  517. for (int j = 0; j != Depth; j++) clog << ' ';
  518. clog << "Leave " << Pkg.FullName() << ' ' << IsFlag(Pkg,Added) << ',' << IsFlag(Pkg,AddPending) << endl;
  519. }
  520. return true;
  521. }
  522. /*}}}*/
  523. // OrderList::DepUnPackCrit - Critical UnPacking ordering /*{{{*/
  524. // ---------------------------------------------------------------------
  525. /* Critical unpacking ordering strives to satisfy Conflicts: and
  526. PreDepends: only. When a prdepends is encountered the Primary
  527. DepFunc is changed to be DepUnPackPreD.
  528. Loops are preprocessed and logged. */
  529. bool pkgOrderList::DepUnPackCrit(DepIterator D)
  530. {
  531. for (; D.end() == false; D++)
  532. {
  533. if (D.Reverse() == true)
  534. {
  535. /* Reverse depenanices are only interested in conflicts,
  536. predepend breakage is ignored here */
  537. if (D->Type != pkgCache::Dep::Conflicts &&
  538. D->Type != pkgCache::Dep::Obsoletes)
  539. continue;
  540. // Duplication elimination, consider only the current version
  541. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  542. continue;
  543. /* For reverse dependencies we wish to check if the
  544. dependency is satisifed in the install state. The
  545. target package (caller) is going to be in the installed
  546. state. */
  547. if (CheckDep(D) == true)
  548. continue;
  549. if (VisitNode(D.ParentPkg()) == false)
  550. return false;
  551. }
  552. else
  553. {
  554. /* Forward critical dependencies MUST be correct before the
  555. package can be unpacked. */
  556. if (D.IsNegative() == false &&
  557. D->Type != pkgCache::Dep::PreDepends)
  558. continue;
  559. /* We wish to check if the dep is okay in the now state of the
  560. target package against the install state of this package. */
  561. if (CheckDep(D) == true)
  562. {
  563. /* We want to catch predepends loops with the code below.
  564. Conflicts loops that are Dep OK are ignored */
  565. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  566. D->Type != pkgCache::Dep::PreDepends)
  567. continue;
  568. }
  569. // This is the loop detection
  570. if (IsFlag(D.TargetPkg(),Added) == true ||
  571. IsFlag(D.TargetPkg(),AddPending) == true)
  572. {
  573. if (IsFlag(D.TargetPkg(),AddPending) == true)
  574. AddLoop(D);
  575. continue;
  576. }
  577. /* Predepends require a special ordering stage, they must have
  578. all dependents installed as well */
  579. DepFunc Old = Primary;
  580. bool Res = false;
  581. if (D->Type == pkgCache::Dep::PreDepends)
  582. Primary = &pkgOrderList::DepUnPackPreD;
  583. Res = VisitProvides(D,true);
  584. Primary = Old;
  585. if (Res == false)
  586. return false;
  587. }
  588. }
  589. return true;
  590. }
  591. /*}}}*/
  592. // OrderList::DepUnPackPreD - Critical UnPacking ordering with depends /*{{{*/
  593. // ---------------------------------------------------------------------
  594. /* Critical PreDepends (also configure immediate and essential) strives to
  595. ensure not only that all conflicts+predepends are met but that this
  596. package will be immediately configurable when it is unpacked.
  597. Loops are preprocessed and logged. */
  598. bool pkgOrderList::DepUnPackPreD(DepIterator D)
  599. {
  600. if (D.Reverse() == true)
  601. return DepUnPackCrit(D);
  602. for (; D.end() == false; D++)
  603. {
  604. if (D.IsCritical() == false)
  605. continue;
  606. /* We wish to check if the dep is okay in the now state of the
  607. target package against the install state of this package. */
  608. if (CheckDep(D) == true)
  609. {
  610. /* We want to catch predepends loops with the code below.
  611. Conflicts loops that are Dep OK are ignored */
  612. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  613. D->Type != pkgCache::Dep::PreDepends)
  614. continue;
  615. }
  616. // This is the loop detection
  617. if (IsFlag(D.TargetPkg(),Added) == true ||
  618. IsFlag(D.TargetPkg(),AddPending) == true)
  619. {
  620. if (IsFlag(D.TargetPkg(),AddPending) == true)
  621. AddLoop(D);
  622. continue;
  623. }
  624. if (VisitProvides(D,true) == false)
  625. return false;
  626. }
  627. return true;
  628. }
  629. /*}}}*/
  630. // OrderList::DepUnPackPre - Critical Predepends ordering /*{{{*/
  631. // ---------------------------------------------------------------------
  632. /* Critical PreDepends (also configure immediate and essential) strives to
  633. ensure not only that all conflicts+predepends are met but that this
  634. package will be immediately configurable when it is unpacked.
  635. Loops are preprocessed and logged. All loops will be fatal. */
  636. bool pkgOrderList::DepUnPackPre(DepIterator D)
  637. {
  638. if (D.Reverse() == true)
  639. return true;
  640. for (; D.end() == false; D++)
  641. {
  642. /* Only consider the PreDepends or Depends. Depends are only
  643. considered at the lowest depth or in the case of immediate
  644. configure */
  645. if (D->Type != pkgCache::Dep::PreDepends)
  646. {
  647. if (D->Type == pkgCache::Dep::Depends)
  648. {
  649. if (Depth == 1 && IsFlag(D.ParentPkg(),Immediate) == false)
  650. continue;
  651. }
  652. else
  653. continue;
  654. }
  655. /* We wish to check if the dep is okay in the now state of the
  656. target package against the install state of this package. */
  657. if (CheckDep(D) == true)
  658. {
  659. /* We want to catch predepends loops with the code below.
  660. Conflicts loops that are Dep OK are ignored */
  661. if (IsFlag(D.TargetPkg(),AddPending) == false)
  662. continue;
  663. }
  664. // This is the loop detection
  665. if (IsFlag(D.TargetPkg(),Added) == true ||
  666. IsFlag(D.TargetPkg(),AddPending) == true)
  667. {
  668. if (IsFlag(D.TargetPkg(),AddPending) == true)
  669. AddLoop(D);
  670. continue;
  671. }
  672. if (VisitProvides(D,true) == false)
  673. return false;
  674. }
  675. return true;
  676. }
  677. /*}}}*/
  678. // OrderList::DepUnPackDep - Reverse dependency considerations /*{{{*/
  679. // ---------------------------------------------------------------------
  680. /* Reverse dependencies are considered to determine if unpacking this
  681. package will break any existing dependencies. If so then those
  682. packages are ordered before this one so that they are in the
  683. UnPacked state.
  684. The forwards depends loop is designed to bring the packages dependents
  685. close to the package. This helps reduce deconfigure time.
  686. Loops are irrelevent to this. */
  687. bool pkgOrderList::DepUnPackDep(DepIterator D)
  688. {
  689. for (; D.end() == false; D++)
  690. if (D.IsCritical() == true)
  691. {
  692. if (D.Reverse() == true)
  693. {
  694. /* Duplication prevention. We consider rev deps only on
  695. the current version, a not installed package
  696. cannot break */
  697. if (D.ParentPkg()->CurrentVer == 0 ||
  698. D.ParentPkg().CurrentVer() != D.ParentVer())
  699. continue;
  700. // The dep will not break so it is irrelevent.
  701. if (CheckDep(D) == true)
  702. continue;
  703. // Skip over missing files
  704. if (IsMissing(D.ParentPkg()) == true)
  705. continue;
  706. if (VisitNode(D.ParentPkg()) == false)
  707. return false;
  708. }
  709. else
  710. {
  711. if (D->Type == pkgCache::Dep::Depends)
  712. if (VisitProvides(D,false) == false)
  713. return false;
  714. if (D->Type == pkgCache::Dep::DpkgBreaks)
  715. {
  716. if (CheckDep(D) == true)
  717. continue;
  718. if (VisitNode(D.TargetPkg()) == false)
  719. return false;
  720. }
  721. }
  722. }
  723. return true;
  724. }
  725. /*}}}*/
  726. // OrderList::DepConfigure - Configuration ordering /*{{{*/
  727. // ---------------------------------------------------------------------
  728. /* Configuration only ordering orders by the Depends: line only. It
  729. orders configuration so that when a package comes to be configured it's
  730. dependents are configured.
  731. Loops are ingored. Depends loop entry points are chaotic. */
  732. bool pkgOrderList::DepConfigure(DepIterator D)
  733. {
  734. // Never consider reverse configuration dependencies.
  735. if (D.Reverse() == true)
  736. return true;
  737. for (; D.end() == false; D++)
  738. if (D->Type == pkgCache::Dep::Depends)
  739. if (VisitProvides(D,false) == false)
  740. return false;
  741. return true;
  742. }
  743. /*}}}*/
  744. // OrderList::DepRemove - Removal ordering /*{{{*/
  745. // ---------------------------------------------------------------------
  746. /* Removal visits all reverse depends. It considers if the dependency
  747. of the Now state version to see if it is okay with removing this
  748. package. This check should always fail, but is provided for symetery
  749. with the other critical handlers.
  750. Loops are preprocessed and logged. Removal loops can also be
  751. detected in the critical handler. They are characterized by an
  752. old version of A depending on B but the new version of A conflicting
  753. with B, thus either A or B must break to install. */
  754. bool pkgOrderList::DepRemove(DepIterator D)
  755. {
  756. if (D.Reverse() == false)
  757. return true;
  758. for (; D.end() == false; D++)
  759. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  760. {
  761. // Duplication elimination, consider the current version only
  762. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  763. continue;
  764. /* We wish to see if the dep on the parent package is okay
  765. in the removed (install) state of the target pkg. */
  766. bool tryFixDeps = false;
  767. if (CheckDep(D) == true)
  768. {
  769. // We want to catch loops with the code below.
  770. if (IsFlag(D.ParentPkg(),AddPending) == false)
  771. continue;
  772. }
  773. else
  774. tryFixDeps = true;
  775. // This is the loop detection
  776. if (IsFlag(D.ParentPkg(),Added) == true ||
  777. IsFlag(D.ParentPkg(),AddPending) == true)
  778. {
  779. if (IsFlag(D.ParentPkg(),AddPending) == true)
  780. AddLoop(D);
  781. continue;
  782. }
  783. if (tryFixDeps == true)
  784. {
  785. for (pkgCache::DepIterator F = D.ParentPkg().CurrentVer().DependsList();
  786. F.end() == false; ++F)
  787. {
  788. if (F->Type != pkgCache::Dep::Depends && F->Type != pkgCache::Dep::PreDepends)
  789. continue;
  790. // Check the Providers
  791. if (F.TargetPkg()->ProvidesList != 0)
  792. {
  793. pkgCache::PrvIterator Prov = F.TargetPkg().ProvidesList();
  794. for (; Prov.end() == false; ++Prov)
  795. {
  796. pkgCache::PkgIterator const P = Prov.OwnerPkg();
  797. if (IsFlag(P, InList) == true &&
  798. IsFlag(P, AddPending) == true &&
  799. IsFlag(P, Added) == false &&
  800. Cache[P].InstallVer == 0)
  801. break;
  802. }
  803. if (Prov.end() == false)
  804. for (pkgCache::PrvIterator Prv = F.TargetPkg().ProvidesList();
  805. Prv.end() == false; ++Prv)
  806. {
  807. pkgCache::PkgIterator const P = Prv.OwnerPkg();
  808. if (IsFlag(P, InList) == true &&
  809. IsFlag(P, AddPending) == false &&
  810. Cache[P].InstallVer != 0 &&
  811. VisitNode(P) == true)
  812. {
  813. Flag(P, Immediate);
  814. tryFixDeps = false;
  815. break;
  816. }
  817. }
  818. if (tryFixDeps == false)
  819. break;
  820. }
  821. // Check for Or groups
  822. if ((F->CompareOp & pkgCache::Dep::Or) != pkgCache::Dep::Or)
  823. continue;
  824. // Lets see if the package is part of the Or group
  825. pkgCache::DepIterator S = F;
  826. for (; S.end() == false; ++S)
  827. {
  828. if (S.TargetPkg() == D.TargetPkg())
  829. break;
  830. if ((S->CompareOp & pkgCache::Dep::Or) != pkgCache::Dep::Or ||
  831. CheckDep(S)) // Or group is satisfied by another package
  832. for (;S.end() == false; ++S);
  833. }
  834. if (S.end() == true)
  835. continue;
  836. // skip to the end of the or group
  837. for (;S.end() == false && (S->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or; ++S);
  838. ++S;
  839. // The soon to be removed is part of the Or group
  840. // start again in the or group and find something which will serve as replacement
  841. for (; F.end() == false && F != S; ++F)
  842. {
  843. if (IsFlag(F.TargetPkg(), InList) == true &&
  844. IsFlag(F.TargetPkg(), AddPending) == false &&
  845. Cache[F.TargetPkg()].InstallVer != 0 &&
  846. VisitNode(F.TargetPkg()) == true)
  847. {
  848. Flag(F.TargetPkg(), Immediate);
  849. tryFixDeps = false;
  850. break;
  851. }
  852. else if (F.TargetPkg()->ProvidesList != 0)
  853. {
  854. pkgCache::PrvIterator Prv = F.TargetPkg().ProvidesList();
  855. for (; Prv.end() == false; ++Prv)
  856. {
  857. if (IsFlag(Prv.OwnerPkg(), InList) == true &&
  858. IsFlag(Prv.OwnerPkg(), AddPending) == false &&
  859. Cache[Prv.OwnerPkg()].InstallVer != 0 &&
  860. VisitNode(Prv.OwnerPkg()) == true)
  861. {
  862. Flag(Prv.OwnerPkg(), Immediate);
  863. tryFixDeps = false;
  864. break;
  865. }
  866. }
  867. if (Prv.end() == false)
  868. break;
  869. }
  870. }
  871. if (tryFixDeps == false)
  872. break;
  873. }
  874. }
  875. // Skip over missing files
  876. if (IsMissing(D.ParentPkg()) == true)
  877. continue;
  878. if (VisitNode(D.ParentPkg()) == false)
  879. return false;
  880. }
  881. return true;
  882. }
  883. /*}}}*/
  884. // OrderList::AddLoop - Add a loop to the loop list /*{{{*/
  885. // ---------------------------------------------------------------------
  886. /* We record the loops. This is a relic since loop breaking is done
  887. genericaly as part of the safety routines. */
  888. bool pkgOrderList::AddLoop(DepIterator D)
  889. {
  890. if (LoopCount < 0 || LoopCount >= 20)
  891. return false;
  892. // Skip dups
  893. if (LoopCount != 0)
  894. {
  895. if (Loops[LoopCount - 1].ParentPkg() == D.ParentPkg() ||
  896. Loops[LoopCount - 1].TargetPkg() == D.ParentPkg())
  897. return true;
  898. }
  899. Loops[LoopCount++] = D;
  900. // Mark the packages as being part of a loop.
  901. Flag(D.TargetPkg(),Loop);
  902. Flag(D.ParentPkg(),Loop);
  903. return true;
  904. }
  905. /*}}}*/
  906. // OrderList::WipeFlags - Unset the given flags from all packages /*{{{*/
  907. // ---------------------------------------------------------------------
  908. /* */
  909. void pkgOrderList::WipeFlags(unsigned long F)
  910. {
  911. unsigned long Size = Cache.Head().PackageCount;
  912. for (unsigned long I = 0; I != Size; I++)
  913. Flags[I] &= ~F;
  914. }
  915. /*}}}*/
  916. // OrderList::CheckDep - Check a dependency for truth /*{{{*/
  917. // ---------------------------------------------------------------------
  918. /* This performs a complete analysis of the dependency wrt to the
  919. current add list. It returns true if after all events are
  920. performed it is still true. This sort of routine can be approximated
  921. by examining the DepCache, however in convoluted cases of provides
  922. this fails to produce a suitable result. */
  923. bool pkgOrderList::CheckDep(DepIterator D)
  924. {
  925. SPtrArray<Version *> List = D.AllTargets();
  926. bool Hit = false;
  927. for (Version **I = List; *I != 0; I++)
  928. {
  929. VerIterator Ver(Cache,*I);
  930. PkgIterator Pkg = Ver.ParentPkg();
  931. /* The meaning of Added and AddPending is subtle. AddPending is
  932. an indication that the package is looping. Because of the
  933. way ordering works Added means the package will be unpacked
  934. before this one and AddPending means after. It is therefore
  935. correct to ignore AddPending in all cases, but that exposes
  936. reverse-ordering loops which should be ignored. */
  937. if (IsFlag(Pkg,Added) == true ||
  938. (IsFlag(Pkg,AddPending) == true && D.Reverse() == true))
  939. {
  940. if (Cache[Pkg].InstallVer != *I)
  941. continue;
  942. }
  943. else
  944. if ((Version *)Pkg.CurrentVer() != *I ||
  945. Pkg.State() != PkgIterator::NeedsNothing)
  946. continue;
  947. /* Conflicts requires that all versions are not present, depends
  948. just needs one */
  949. if (D.IsNegative() == false)
  950. {
  951. // ignore provides by older versions of this package
  952. if (((D.Reverse() == false && Pkg == D.ParentPkg()) ||
  953. (D.Reverse() == true && Pkg == D.TargetPkg())) &&
  954. Cache[Pkg].InstallVer != *I)
  955. continue;
  956. /* Try to find something that does not have the after flag set
  957. if at all possible */
  958. if (IsFlag(Pkg,After) == true)
  959. {
  960. Hit = true;
  961. continue;
  962. }
  963. return true;
  964. }
  965. else
  966. {
  967. if (IsFlag(Pkg,After) == true)
  968. Flag(D.ParentPkg(),After);
  969. return false;
  970. }
  971. }
  972. // We found a hit, but it had the after flag set
  973. if (Hit == true && D->Type == pkgCache::Dep::PreDepends)
  974. {
  975. Flag(D.ParentPkg(),After);
  976. return true;
  977. }
  978. /* Conflicts requires that all versions are not present, depends
  979. just needs one */
  980. if (D->Type == pkgCache::Dep::Conflicts ||
  981. D->Type == pkgCache::Dep::Obsoletes)
  982. return true;
  983. return false;
  984. }
  985. /*}}}*/